From d1699dfaf1eadcc4a650de979764a6815c1f44d2 Mon Sep 17 00:00:00 2001 From: zjp Date: Fri, 12 May 2023 17:01:49 +0800 Subject: [PATCH 01/15] introduce use env_logger related to https://github.com/denisidoro/navi/issues/576 For the following config cheats: paths: - C:\\Users\\Administrator\\AppData\\Roaming\\navi\\cheat - C:\\Users\\Administrator\\AppData\\Roaming\\navi\\cheat navi now gets incorrect paths on Windows, since the seperator `:` for path join is a valid component. [2023-05-12T08:58:26Z DEBUG navi::commands::core] Filesystem( Some( "C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat:C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat", ), ) [2023-05-12T08:58:28Z DEBUG navi::filesystem] filesystem::Fetcher = Fetcher { path: Some( "C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat:C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat", ), files: RefCell { value: [], }, } --- .gitignore | 1 + Cargo.lock | 22 ++++++++++++++++++++-- Cargo.toml | 2 ++ src/bin/main.rs | 16 ++++++++++++++-- src/commands/core/mod.rs | 4 +++- src/commands/mod.rs | 1 + src/config/cli.rs | 1 + src/config/env.rs | 1 + src/config/mod.rs | 1 + src/config/yaml.rs | 16 ++++++++-------- src/filesystem.rs | 2 ++ src/lib.rs | 2 -- 12 files changed, 54 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 53eaa21..3f2d71b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target **/*.rs.bk +navi.log diff --git a/Cargo.lock b/Cargo.lock index bbdffee..58753ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -260,6 +260,16 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "env_logger" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +dependencies = [ + "humantime", + "log", +] + [[package]] name = "errno" version = "0.3.0" @@ -344,6 +354,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "indexmap" version = "1.9.3" @@ -412,9 +428,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.14" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", ] @@ -456,8 +472,10 @@ dependencies = [ "dns_common", "dns_common_derive", "edit", + "env_logger", "etcetera", "lazy_static", + "log", "regex", "remove_dir_all 0.8.2", "serde", diff --git a/Cargo.toml b/Cargo.toml index a9dc3d1..0115aca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,8 @@ serde_yaml = "0.9.21" dns_common_derive = { version = "0.2.1" } dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] } unicode-width = "0.1.10" +log = "0.4" +env_logger = { version = "0.10", default_features = false, features = ["humantime"] } [lib] name = "navi" diff --git a/src/bin/main.rs b/src/bin/main.rs index ad67e71..48c2fa0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,6 +1,5 @@ extern crate navi; -use std::fmt::Debug; use thiserror::Error; #[derive(Error, Debug)] @@ -25,5 +24,18 @@ impl FileAnIssue { } fn main() -> Result<(), anyhow::Error> { - navi::handle().map_err(|e| FileAnIssue::new(e).into()) + init_logger()?; + navi::handle().map_err(|e| { + log::error!("{e:?}"); + FileAnIssue::new(e).into() + }) +} + +fn init_logger() -> anyhow::Result<()> { + let file = std::fs::File::create("navi.log")?; + env_logger::builder() + .target(env_logger::Target::Pipe(Box::new(file))) + .init(); + + Ok(()) } diff --git a/src/commands/core/mod.rs b/src/commands/core/mod.rs index ded24f1..2a3d334 100644 --- a/src/commands/core/mod.rs +++ b/src/commands/core/mod.rs @@ -44,7 +44,9 @@ pub fn init(fetcher: Box) -> Result<()> { } pub fn get_fetcher() -> Result> { - match CONFIG.source() { + let source = CONFIG.source(); + log::debug!("{source:#?}"); + match source { Source::Cheats(query) => { let lines = cheatsh::call(&query)?; let fetcher = Box::new(StaticFetcher::new(lines)); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 783cbad..257df1c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -12,6 +12,7 @@ use crate::prelude::*; pub fn handle() -> Result<()> { use crate::config::Command::*; + log::debug!("CONFIG = {:#?}", &*CONFIG); match CONFIG.cmd() { None => commands::core::main(), diff --git a/src/config/cli.rs b/src/config/cli.rs index 7e2ec20..c6f86f9 100644 --- a/src/config/cli.rs +++ b/src/config/cli.rs @@ -117,6 +117,7 @@ pub enum Command { Info(commands::info::Input), } +#[derive(Debug)] pub enum Source { Filesystem(Option), Tldr(String), diff --git a/src/config/env.rs b/src/config/env.rs index 7afd27e..ba428fc 100644 --- a/src/config/env.rs +++ b/src/config/env.rs @@ -2,6 +2,7 @@ use crate::env_var; use crate::finder::FinderChoice; use crate::prelude::*; +#[derive(Debug)] pub struct EnvConfig { pub config_yaml: Option, pub config_path: Option, diff --git a/src/config/mod.rs b/src/config/mod.rs index 774f930..68638cf 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -12,6 +12,7 @@ use yaml::YamlConfig; lazy_static! { pub static ref CONFIG: Config = Config::new(); } +#[derive(Debug)] pub struct Config { yaml: YamlConfig, clap: ClapConfig, diff --git a/src/config/yaml.rs b/src/config/yaml.rs index 8c4a693..70a5a17 100644 --- a/src/config/yaml.rs +++ b/src/config/yaml.rs @@ -6,7 +6,7 @@ use crate::prelude::*; use crossterm::style::Color as TerminalColor; use serde::de; -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor); impl Color { @@ -24,7 +24,7 @@ where .map_err(|_| de::Error::custom(format!("Failed to deserialize color: {s}"))) } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct ColorWidth { pub color: Color, @@ -32,7 +32,7 @@ pub struct ColorWidth { pub min_width: u16, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct Style { pub tag: ColorWidth, @@ -40,7 +40,7 @@ pub struct Style { pub snippet: ColorWidth, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct Finder { #[serde(deserialize_with = "finder_deserialize")] @@ -58,27 +58,27 @@ where .map_err(|_| de::Error::custom(format!("Failed to deserialize finder: {s}"))) } -#[derive(Deserialize, Default)] +#[derive(Deserialize, Default, Debug)] #[serde(default)] pub struct Cheats { pub path: Option, pub paths: Vec, } -#[derive(Deserialize, Default)] +#[derive(Deserialize, Default, Debug)] #[serde(default)] pub struct Search { pub tags: Option, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct Shell { pub command: String, pub finder_command: Option, } -#[derive(Deserialize, Default)] +#[derive(Deserialize, Default, Debug)] #[serde(default)] pub struct YamlConfig { pub style: Style, diff --git a/src/filesystem.rs b/src/filesystem.rs index 6340f82..ed0f32e 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -125,6 +125,7 @@ fn interpolate_paths(paths: String) -> String { newtext } +#[derive(Debug)] pub struct Fetcher { path: Option, files: RefCell>, @@ -180,6 +181,7 @@ impl fetcher::Fetcher for Fetcher { } } + log::debug!("filesystem::Fetcher = {self:#?}"); Ok(found_something) } diff --git a/src/lib.rs b/src/lib.rs index cbae52d..f4a434f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,5 @@ #[macro_use] extern crate lazy_static; -// #[macro_use] -// extern crate anyhow; mod clients; mod commands; From 818d5cdc0e3de89a464107378649f92a0c215ed1 Mon Sep 17 00:00:00 2001 From: zjp Date: Fri, 12 May 2023 19:13:50 +0800 Subject: [PATCH 02/15] rm env_logger; use tracing instead --- Cargo.lock | 18 ------------------ Cargo.toml | 2 -- src/bin/main.rs | 17 +++++++++++------ src/commands/core/mod.rs | 2 +- src/commands/mod.rs | 2 +- src/filesystem.rs | 2 +- 6 files changed, 14 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 58753ad..ac4c0ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -260,16 +260,6 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" -[[package]] -name = "env_logger" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" -dependencies = [ - "humantime", - "log", -] - [[package]] name = "errno" version = "0.3.0" @@ -354,12 +344,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "indexmap" version = "1.9.3" @@ -472,10 +456,8 @@ dependencies = [ "dns_common", "dns_common_derive", "edit", - "env_logger", "etcetera", "lazy_static", - "log", "regex", "remove_dir_all 0.8.2", "serde", diff --git a/Cargo.toml b/Cargo.toml index 0115aca..a9dc3d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,8 +36,6 @@ serde_yaml = "0.9.21" dns_common_derive = { version = "0.2.1" } dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] } unicode-width = "0.1.10" -log = "0.4" -env_logger = { version = "0.10", default_features = false, features = ["humantime"] } [lib] name = "navi" diff --git a/src/bin/main.rs b/src/bin/main.rs index 48c2fa0..a9b90b0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,5 +1,6 @@ extern crate navi; +use dns_common::prelude::{debug, error, tracing, tracing_subscriber}; use thiserror::Error; #[derive(Error, Debug)] @@ -26,16 +27,20 @@ impl FileAnIssue { fn main() -> Result<(), anyhow::Error> { init_logger()?; navi::handle().map_err(|e| { - log::error!("{e:?}"); + error!("{e:?}"); FileAnIssue::new(e).into() }) } fn init_logger() -> anyhow::Result<()> { - let file = std::fs::File::create("navi.log")?; - env_logger::builder() - .target(env_logger::Target::Pipe(Box::new(file))) - .init(); - + tracing::subscriber::set_global_default( + tracing_subscriber::fmt() + .with_ansi(false) + // TODO: config_path/navi.log + .with_writer(std::fs::File::create("navi.log")?) + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .finish(), + )?; + debug!("tracing initialized"); Ok(()) } diff --git a/src/commands/core/mod.rs b/src/commands/core/mod.rs index 2a3d334..8055125 100644 --- a/src/commands/core/mod.rs +++ b/src/commands/core/mod.rs @@ -45,7 +45,7 @@ pub fn init(fetcher: Box) -> Result<()> { pub fn get_fetcher() -> Result> { let source = CONFIG.source(); - log::debug!("{source:#?}"); + debug!("{source:#?}"); match source { Source::Cheats(query) => { let lines = cheatsh::call(&query)?; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 257df1c..4537177 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -12,7 +12,7 @@ use crate::prelude::*; pub fn handle() -> Result<()> { use crate::config::Command::*; - log::debug!("CONFIG = {:#?}", &*CONFIG); + debug!("CONFIG = {:#?}", &*CONFIG); match CONFIG.cmd() { None => commands::core::main(), diff --git a/src/filesystem.rs b/src/filesystem.rs index ed0f32e..fe4df97 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -181,7 +181,7 @@ impl fetcher::Fetcher for Fetcher { } } - log::debug!("filesystem::Fetcher = {self:#?}"); + debug!("filesystem::Fetcher = {self:#?}"); Ok(found_something) } From 2755d51b38a9badf28fa2b041b533aa7f0e8998d Mon Sep 17 00:00:00 2001 From: zjp Date: Fri, 12 May 2023 23:32:00 +0800 Subject: [PATCH 03/15] mv navi.log under config dir; debug folders on Windows 2023-05-12T15:51:54.280707Z DEBUG navi::filesystem: read cheat files in `"C"`: [] 2023-05-12T15:51:54.281083Z DEBUG navi::filesystem: read cheat files in `"\\Users\\Administrator\\AppData\\Roaming\\navi\\cheats"`: [] --- src/bin/main.rs | 10 +++++++--- src/filesystem.rs | 6 ++++-- src/lib.rs | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index a9b90b0..01f9ffb 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,6 +1,6 @@ extern crate navi; -use dns_common::prelude::{debug, error, tracing, tracing_subscriber}; +use dns_common::prelude::*; use thiserror::Error; #[derive(Error, Debug)] @@ -33,14 +33,18 @@ fn main() -> Result<(), anyhow::Error> { } fn init_logger() -> anyhow::Result<()> { + const FILE_NAME: &str = "navi.log"; + let mut file = navi::default_config_pathbuf()?; + file.set_file_name(FILE_NAME); + tracing::subscriber::set_global_default( tracing_subscriber::fmt() .with_ansi(false) - // TODO: config_path/navi.log - .with_writer(std::fs::File::create("navi.log")?) + .with_writer(std::fs::File::create(file)?) .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .finish(), )?; debug!("tracing initialized"); + Ok(()) } diff --git a/src/filesystem.rs b/src/filesystem.rs index fe4df97..bebe1d8 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -166,7 +166,9 @@ impl fetcher::Fetcher for Fetcher { None => folder.to_string(), }; let folder_pathbuf = PathBuf::from(interpolated_folder); - for file in all_cheat_files(&folder_pathbuf) { + let cheat_files = all_cheat_files(&folder_pathbuf); + debug!("read cheat files in `{folder_pathbuf:?}`: {cheat_files:#?}"); + for file in cheat_files { self.files.borrow_mut().push(file.clone()); let index = self.files.borrow().len() - 1; let read_file_result = { @@ -181,7 +183,7 @@ impl fetcher::Fetcher for Fetcher { } } - debug!("filesystem::Fetcher = {self:#?}"); + debug!("{self:#?}"); Ok(found_something) } diff --git a/src/lib.rs b/src/lib.rs index f4a434f..bc2bdf6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,4 +14,4 @@ mod prelude; mod structures; mod welcome; -pub use commands::handle; +pub use {commands::handle, filesystem::default_config_pathbuf}; From 8a2bf7d1eb1c82ed227f44955849082b4093b124 Mon Sep 17 00:00:00 2001 From: zjp Date: Sat, 13 May 2023 11:27:27 +0800 Subject: [PATCH 04/15] Fix multiple paths: define the platform-specific join separator --- src/config/mod.rs | 2 +- src/filesystem.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 68638cf..b652608 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -70,7 +70,7 @@ impl Config { if p.is_empty() { None } else { - Some(p.join(":")) + Some(p.join(crate::filesystem::JOIN_SEPARATOR)) } }) .or_else(|| self.yaml.cheats.path.clone()) diff --git a/src/filesystem.rs b/src/filesystem.rs index bebe1d8..a53f246 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -12,6 +12,13 @@ use std::path::MAIN_SEPARATOR; use walkdir::WalkDir; +/// Multiple paths are joint by a platform-specific separator. +/// FIXME: it's actually incorrect to assume a path doesn't containing this separator +#[cfg(target_family = "windows")] +pub const JOIN_SEPARATOR: &str = ";"; +#[cfg(not(target_family = "windows"))] +pub const JOIN_SEPARATOR: &str = ":"; + pub fn all_cheat_files(path: &Path) -> Vec { WalkDir::new(path) .follow_links(true) @@ -23,7 +30,7 @@ pub fn all_cheat_files(path: &Path) -> Vec { } fn paths_from_path_param(env_var: &str) -> impl Iterator { - env_var.split(':').filter(|folder| folder != &"") + env_var.split(JOIN_SEPARATOR).filter(|folder| folder != &"") } fn compiled_default_path(path: Option<&str>) -> Option { @@ -284,4 +291,12 @@ mod tests { assert_eq!(expected, cheats.to_string_lossy().to_string()) } + + #[test] + #[cfg(target_family = "windows")] + fn multiple_paths() { + let p = r#"C:\Users\Administrator\AppData\Roaming\navi\config.yaml"#; + let paths = &[p; 2].join(JOIN_SEPARATOR); + assert_eq!(paths_from_path_param(paths).collect::>(), [p; 2]); + } } From 0fe4bc24472de7f22cf088c273f5a7b6b9cb114e Mon Sep 17 00:00:00 2001 From: zjp Date: Sat, 13 May 2023 13:47:21 +0800 Subject: [PATCH 05/15] eprintln init_logger failure (may need redir if it's flushed away) and add a description on logging --- docs/config_file.md | 6 ++++++ src/bin/main.rs | 7 +++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/config_file.md b/docs/config_file.md index d7db573..25498c0 100644 --- a/docs/config_file.md +++ b/docs/config_file.md @@ -29,3 +29,9 @@ Run the following command to generate a config file with the default parameters: ```sh navi info config-example > "$(navi info config-path)" ``` + +### Logging + +The log file will be created under the same directory where the config locates. + +And you can use the `RUST_LOG` env to set the log level, e.g. `RUST_LOG=debug navi`. diff --git a/src/bin/main.rs b/src/bin/main.rs index 01f9ffb..fa12d64 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -24,8 +24,11 @@ impl FileAnIssue { } } -fn main() -> Result<(), anyhow::Error> { - init_logger()?; +fn main() -> anyhow::Result<()> { + if let Err(err) = init_logger() { + // may need redir stderr to a file to show this log initialization error + eprintln!("failed to initialize logging: {err:?}"); + } navi::handle().map_err(|e| { error!("{e:?}"); FileAnIssue::new(e).into() From e8489cd0d571290475054ac6fe8d1b9d187c3f87 Mon Sep 17 00:00:00 2001 From: zjp Date: Sat, 13 May 2023 13:57:32 +0800 Subject: [PATCH 06/15] update config_file_example: add a cheats path example on Windows --- docs/config_file_example.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/config_file_example.yaml b/docs/config_file_example.yaml index 2344dec..ce14365 100644 --- a/docs/config_file_example.yaml +++ b/docs/config_file_example.yaml @@ -19,9 +19,9 @@ finder: # overrides_var: --tac # equivalent to the --fzf-overrides-var option # cheats: -# paths: -# - /path/to/some/dir -# - /path/to/another/dir +# paths: +# - /path/to/some/dir # on unix-like os +# - F:\\path\\to\\dir # on Windows # path: /path/to/some/dir # (DEPRECATED) equivalent to the --path option # search: From eaadec172c30fa2819c20c0753b33dc91a81a617 Mon Sep 17 00:00:00 2001 From: zjp Date: Sat, 13 May 2023 14:14:36 +0800 Subject: [PATCH 07/15] If config path doesn't exist, navi won't log. --- src/bin/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index fa12d64..8c4856a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -40,10 +40,16 @@ fn init_logger() -> anyhow::Result<()> { let mut file = navi::default_config_pathbuf()?; file.set_file_name(FILE_NAME); + // If config path doesn't exist, navi won't log. + if file.parent().map(|p| !p.exists()).unwrap_or(true) { + return Ok(()); + } + + let writer = std::fs::File::create(&file).with_context(|| format!("{file:?} is not created"))?; tracing::subscriber::set_global_default( tracing_subscriber::fmt() .with_ansi(false) - .with_writer(std::fs::File::create(file)?) + .with_writer(writer) .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .finish(), )?; From 25792db877637e2d01ade1785115e5ea7f6445ad Mon Sep 17 00:00:00 2001 From: zjp Date: Sat, 13 May 2023 15:51:39 +0800 Subject: [PATCH 08/15] config_file_example: shell command should be `cmd.exe` on Windows and add shell command debug in logging --- docs/config_file_example.yaml | 5 ++++- src/common/shell.rs | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/config_file_example.yaml b/docs/config_file_example.yaml index ce14365..1eb7ab7 100644 --- a/docs/config_file_example.yaml +++ b/docs/config_file_example.yaml @@ -28,5 +28,8 @@ finder: # tags: git,!checkout # equivalent to the --tag-rules option shell: - command: bash # shell used for shell out. possible values: bash, zsh, dash, ... + # Shell used for shell out. Possible values: bash, zsh, dash, ... + # For Windows, use `cmd.exe` instead. + command: bash + # finder_command: bash # similar, but for fzf's internals diff --git a/src/common/shell.rs b/src/common/shell.rs index 331eb32..976128b 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -42,5 +42,6 @@ pub fn out() -> Command { cmd.args(words); let dash_c = if words_str.contains("cmd.exe") { "/c" } else { "-c" }; cmd.arg(dash_c); + debug!("shell cmd = `{cmd:#?}`"); cmd } From b9283f6718585cf3f1439f1476155d844c92c111 Mon Sep 17 00:00:00 2001 From: zjp Date: Sat, 13 May 2023 22:41:02 +0800 Subject: [PATCH 09/15] debug log --- src/commands/core/actor.rs | 14 ++++++++------ src/commands/core/mod.rs | 4 +++- src/commands/preview/var_stdin.rs | 9 ++++----- src/common/shell.rs | 1 - src/filesystem.rs | 2 +- src/finder/mod.rs | 8 +++++--- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/commands/core/actor.rs b/src/commands/core/actor.rs index 9d49717..91a1846 100644 --- a/src/commands/core/actor.rs +++ b/src/commands/core/actor.rs @@ -41,9 +41,10 @@ fn prompt_finder( } } - let child = shell::out() - .stdout(Stdio::piped()) - .arg(suggestion_command) + let mut cmd = shell::out(); + cmd.stdout(Stdio::piped()).arg(suggestion_command); + debug!(cmd = ?cmd); + let child = cmd .spawn() .map_err(|e| ShellSpawnError::new(suggestion_command, e))?; @@ -236,9 +237,10 @@ pub fn act( clipboard::copy(interpolated_snippet)?; } _ => { - shell::out() - .arg(&interpolated_snippet[..]) - .spawn() + let mut cmd = shell::out(); + cmd.arg(&interpolated_snippet[..]); + debug!(cmd = ?cmd); + cmd.spawn() .map_err(|e| ShellSpawnError::new(&interpolated_snippet[..], e))? .wait() .context("bash was not running")?; diff --git a/src/commands/core/mod.rs b/src/commands/core/mod.rs index 8055125..00e3a35 100644 --- a/src/commands/core/mod.rs +++ b/src/commands/core/mod.rs @@ -13,6 +13,7 @@ use crate::welcome; pub fn init(fetcher: Box) -> Result<()> { let config = &CONFIG; let opts = FinderOpts::snippet_default(); + debug!("opts = {opts:#?}"); // let fetcher = config.fetcher(); let (raw_selection, (variables, files)) = config @@ -32,6 +33,7 @@ pub fn init(fetcher: Box) -> Result<()> { }) .context("Failed getting selection and variables from finder")?; + debug!(raw_selection = ?raw_selection); let extractions = deser::terminal::read(&raw_selection, config.best_match()); if extractions.is_err() { @@ -45,7 +47,7 @@ pub fn init(fetcher: Box) -> Result<()> { pub fn get_fetcher() -> Result> { let source = CONFIG.source(); - debug!("{source:#?}"); + debug!(source = ?source); match source { Source::Cheats(query) => { let lines = cheatsh::call(&query)?; diff --git a/src/commands/preview/var_stdin.rs b/src/commands/preview/var_stdin.rs index 4b8ddfb..78cc8ef 100755 --- a/src/commands/preview/var_stdin.rs +++ b/src/commands/preview/var_stdin.rs @@ -30,11 +30,10 @@ impl Runnable for Input { if !extra.is_empty() { print!(""); - shell::out() - .arg(extra) - .spawn() - .map_err(|e| ShellSpawnError::new(extra, e))? - .wait()?; + let mut cmd = shell::out(); + cmd.arg(extra); + debug!(?cmd); + cmd.spawn().map_err(|e| ShellSpawnError::new(extra, e))?.wait()?; } } diff --git a/src/common/shell.rs b/src/common/shell.rs index 976128b..331eb32 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -42,6 +42,5 @@ pub fn out() -> Command { cmd.args(words); let dash_c = if words_str.contains("cmd.exe") { "/c" } else { "-c" }; cmd.arg(dash_c); - debug!("shell cmd = `{cmd:#?}`"); cmd } diff --git a/src/filesystem.rs b/src/filesystem.rs index a53f246..b8c4a03 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -190,7 +190,7 @@ impl fetcher::Fetcher for Fetcher { } } - debug!("{self:#?}"); + debug!("FilesystemFetcher = {self:#?}"); Ok(found_something) } diff --git a/src/finder/mod.rs b/src/finder/mod.rs index b18888f..9b4ccfd 100644 --- a/src/finder/mod.rs +++ b/src/finder/mod.rs @@ -152,11 +152,13 @@ impl FinderChoice { }); } - let child = command + command .env("SHELL", CONFIG.finder_shell()) .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn(); + .stdout(Stdio::piped()); + debug!(cmd = ?command); + + let child = command.spawn(); let mut child = match child { Ok(x) => x, From ea95dfec483d4d5f1ff5e474198eae62e7488b4d Mon Sep 17 00:00:00 2001 From: zjp Date: Sat, 13 May 2023 22:44:13 +0800 Subject: [PATCH 10/15] Fix preview: handle Windows NT UNC paths (`\\?\C:\foo`) --- Cargo.lock | 7 +++++++ Cargo.toml | 3 +++ src/common/fs.rs | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index ac4c0ef..a379f1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -244,6 +244,12 @@ dependencies = [ "synstructure", ] +[[package]] +name = "dunce" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" + [[package]] name = "edit" version = "0.1.4" @@ -455,6 +461,7 @@ dependencies = [ "crossterm", "dns_common", "dns_common_derive", + "dunce", "edit", "etcetera", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index a9dc3d1..fa054cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,9 @@ dns_common_derive = { version = "0.2.1" } dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] } unicode-width = "0.1.10" +[target.'cfg(windows)'.dependencies] +dunce = "1" + [lib] name = "navi" path = "src/lib.rs" diff --git a/src/common/fs.rs b/src/common/fs.rs index 32c8b1e..cf8a737 100644 --- a/src/common/fs.rs +++ b/src/common/fs.rs @@ -78,6 +78,11 @@ fn follow_symlink(pathbuf: PathBuf) -> Result { fn exe_pathbuf() -> Result { let pathbuf = std::env::current_exe().context("Unable to acquire executable's path")?; + + #[cfg(target_family = "windows")] + let pathbuf = dunce::canonicalize(pathbuf)?; + + debug!(current_exe = ?pathbuf); follow_symlink(pathbuf) } From 75e9306c0d490a179b9a583730c74d6e734731e2 Mon Sep 17 00:00:00 2001 From: tjex Date: Sat, 4 Nov 2023 20:53:33 +0100 Subject: [PATCH 11/15] documentation on paths and environment vars --- README.md | 1 + docs/paths_and_environment_variables.md | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 docs/paths_and_environment_variables.md diff --git a/README.md b/README.md index dd04376..a06284b 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ The full syntax and examples can be found [here](docs/cheatsheet_syntax.md). You can: - [setup your own config file](docs/config_file.md) +- [set custom paths for your config file and cheat sheets](docs/paths_and_environment_variables.md) - [change colors](docs/customization.md#changing-colors) - [resize columns](docs/customization.md#resizing-columns) - [change how search is performed](docs/customization.md#overriding-fzf-options) diff --git a/docs/paths_and_environment_variables.md b/docs/paths_and_environment_variables.md new file mode 100644 index 0000000..c53e828 --- /dev/null +++ b/docs/paths_and_environment_variables.md @@ -0,0 +1,21 @@ +# Paths and Environment Variables + +Navi uses the [`directories-next`](https://crates.io/crates/directories-next) package, which +defines platform-specific standard locations of directories for config, cache and other data. + +Mac users, this is why your files are being stored in `~/Library/Application Support/navi`. + +To set custom paths for your config and cheat sheets, you can set the following +environment variables: + +```zsh +export NAVI_CONFIG="~/.config/navi/config.yaml" +export NAVI_PATH="~/.local/share/navi" +``` +Note! Even when set, `$NAVI_PATH` will not be used when installing cheat +sheets directly via navi's own commands. + +For example when running `navi add repo `~/Library/...` will still be used. + +Instead, you may clone repos directly into `$NAVI_PATH`, or write your own +manually. From 8d19e8aa3c88d0b876577bc90d84ecbedcde1be8 Mon Sep 17 00:00:00 2001 From: tjex Date: Sat, 4 Nov 2023 20:54:49 +0100 Subject: [PATCH 12/15] doc: typo --- docs/paths_and_environment_variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/paths_and_environment_variables.md b/docs/paths_and_environment_variables.md index c53e828..6266266 100644 --- a/docs/paths_and_environment_variables.md +++ b/docs/paths_and_environment_variables.md @@ -15,7 +15,7 @@ export NAVI_PATH="~/.local/share/navi" Note! Even when set, `$NAVI_PATH` will not be used when installing cheat sheets directly via navi's own commands. -For example when running `navi add repo `~/Library/...` will still be used. +For example when running `navi add repo `, `~/Library/...` will still be used. Instead, you may clone repos directly into `$NAVI_PATH`, or write your own manually. From 3b35956a47d506654873ecb2a8cb7755a084e2a8 Mon Sep 17 00:00:00 2001 From: tjex Date: Sat, 4 Nov 2023 20:56:48 +0100 Subject: [PATCH 13/15] docs: clarity --- docs/paths_and_environment_variables.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/paths_and_environment_variables.md b/docs/paths_and_environment_variables.md index 6266266..99eb490 100644 --- a/docs/paths_and_environment_variables.md +++ b/docs/paths_and_environment_variables.md @@ -15,7 +15,8 @@ export NAVI_PATH="~/.local/share/navi" Note! Even when set, `$NAVI_PATH` will not be used when installing cheat sheets directly via navi's own commands. -For example when running `navi add repo `, `~/Library/...` will still be used. +For example when running `navi add repo `, the default paths as per the `directories-next` +package will still be used. -Instead, you may clone repos directly into `$NAVI_PATH`, or write your own -manually. +To avoid this, you may simply clone repos via a regular `git clone` command, +directly into `$NAVI_PATH`. From 0dd7b4c9f2d64ccb95707066445a85e4f1ee501e Mon Sep 17 00:00:00 2001 From: tjex Date: Sat, 4 Nov 2023 21:03:10 +0100 Subject: [PATCH 14/15] docs: extra clarity --- docs/paths_and_environment_variables.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/paths_and_environment_variables.md b/docs/paths_and_environment_variables.md index 99eb490..f0cdbb9 100644 --- a/docs/paths_and_environment_variables.md +++ b/docs/paths_and_environment_variables.md @@ -12,7 +12,7 @@ environment variables: export NAVI_CONFIG="~/.config/navi/config.yaml" export NAVI_PATH="~/.local/share/navi" ``` -Note! Even when set, `$NAVI_PATH` will not be used when installing cheat +Despite `$NAVI_PATH` being set, it will not be used when installing cheat sheets directly via navi's own commands. For example when running `navi add repo `, the default paths as per the `directories-next` @@ -20,3 +20,6 @@ package will still be used. To avoid this, you may simply clone repos via a regular `git clone` command, directly into `$NAVI_PATH`. + +Note! `navi info cheats-path` and `navi info config-path` display the *default* path, not +the path set by the user. [It is known that this is a little misleading!](https://github.com/denisidoro/navi/issues/664#issuecomment-1004721178). From d036487fbca9524b1849f09db1b4061cb3519080 Mon Sep 17 00:00:00 2001 From: tjex Date: Sat, 4 Nov 2023 21:04:15 +0100 Subject: [PATCH 15/15] doc: file rename --- README.md | 2 +- ...paths_and_environment_variables.md => paths_and_env_vars.md} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename docs/{paths_and_environment_variables.md => paths_and_env_vars.md} (100%) diff --git a/README.md b/README.md index a06284b..1011b9d 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ The full syntax and examples can be found [here](docs/cheatsheet_syntax.md). You can: - [setup your own config file](docs/config_file.md) -- [set custom paths for your config file and cheat sheets](docs/paths_and_environment_variables.md) +- [set custom paths for your config file and cheat sheets](docs/paths_and_env_vars.md) - [change colors](docs/customization.md#changing-colors) - [resize columns](docs/customization.md#resizing-columns) - [change how search is performed](docs/customization.md#overriding-fzf-options) diff --git a/docs/paths_and_environment_variables.md b/docs/paths_and_env_vars.md similarity index 100% rename from docs/paths_and_environment_variables.md rename to docs/paths_and_env_vars.md