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: [],
        },
    }
This commit is contained in:
zjp 2023-05-12 17:01:49 +08:00 committed by Denis Isidoro
parent eb9ad7c09f
commit 196872b109
12 changed files with 54 additions and 15 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target /target
**/*.rs.bk **/*.rs.bk
navi.log

22
Cargo.lock generated
View file

@ -260,6 +260,16 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" 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]] [[package]]
name = "errno" name = "errno"
version = "0.3.0" version = "0.3.0"
@ -344,6 +354,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.9.3" version = "1.9.3"
@ -412,9 +428,9 @@ dependencies = [
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.14" version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
] ]
@ -456,8 +472,10 @@ dependencies = [
"dns_common", "dns_common",
"dns_common_derive", "dns_common_derive",
"edit", "edit",
"env_logger",
"etcetera", "etcetera",
"lazy_static", "lazy_static",
"log",
"regex", "regex",
"remove_dir_all 0.8.2", "remove_dir_all 0.8.2",
"serde", "serde",

View file

@ -36,6 +36,8 @@ serde_yaml = "0.9.21"
dns_common_derive = { version = "0.2.1" } dns_common_derive = { version = "0.2.1" }
dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] } dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] }
unicode-width = "0.1.10" unicode-width = "0.1.10"
log = "0.4"
env_logger = { version = "0.10", default_features = false, features = ["humantime"] }
[lib] [lib]
name = "navi" name = "navi"

View file

@ -1,6 +1,5 @@
extern crate navi; extern crate navi;
use std::fmt::Debug;
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -25,5 +24,18 @@ impl FileAnIssue {
} }
fn main() -> Result<(), anyhow::Error> { 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(())
} }

View file

@ -44,7 +44,9 @@ pub fn init(fetcher: Box<dyn Fetcher>) -> Result<()> {
} }
pub fn get_fetcher() -> Result<Box<dyn Fetcher>> { pub fn get_fetcher() -> Result<Box<dyn Fetcher>> {
match CONFIG.source() { let source = CONFIG.source();
log::debug!("{source:#?}");
match source {
Source::Cheats(query) => { Source::Cheats(query) => {
let lines = cheatsh::call(&query)?; let lines = cheatsh::call(&query)?;
let fetcher = Box::new(StaticFetcher::new(lines)); let fetcher = Box::new(StaticFetcher::new(lines));

View file

@ -12,6 +12,7 @@ use crate::prelude::*;
pub fn handle() -> Result<()> { pub fn handle() -> Result<()> {
use crate::config::Command::*; use crate::config::Command::*;
log::debug!("CONFIG = {:#?}", &*CONFIG);
match CONFIG.cmd() { match CONFIG.cmd() {
None => commands::core::main(), None => commands::core::main(),

View file

@ -117,6 +117,7 @@ pub enum Command {
Info(commands::info::Input), Info(commands::info::Input),
} }
#[derive(Debug)]
pub enum Source { pub enum Source {
Filesystem(Option<String>), Filesystem(Option<String>),
Tldr(String), Tldr(String),

View file

@ -2,6 +2,7 @@ use crate::env_var;
use crate::finder::FinderChoice; use crate::finder::FinderChoice;
use crate::prelude::*; use crate::prelude::*;
#[derive(Debug)]
pub struct EnvConfig { pub struct EnvConfig {
pub config_yaml: Option<String>, pub config_yaml: Option<String>,
pub config_path: Option<String>, pub config_path: Option<String>,

View file

@ -12,6 +12,7 @@ use yaml::YamlConfig;
lazy_static! { lazy_static! {
pub static ref CONFIG: Config = Config::new(); pub static ref CONFIG: Config = Config::new();
} }
#[derive(Debug)]
pub struct Config { pub struct Config {
yaml: YamlConfig, yaml: YamlConfig,
clap: ClapConfig, clap: ClapConfig,

View file

@ -6,7 +6,7 @@ use crate::prelude::*;
use crossterm::style::Color as TerminalColor; use crossterm::style::Color as TerminalColor;
use serde::de; use serde::de;
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor); pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor);
impl Color { impl Color {
@ -24,7 +24,7 @@ where
.map_err(|_| de::Error::custom(format!("Failed to deserialize color: {s}"))) .map_err(|_| de::Error::custom(format!("Failed to deserialize color: {s}")))
} }
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
#[serde(default)] #[serde(default)]
pub struct ColorWidth { pub struct ColorWidth {
pub color: Color, pub color: Color,
@ -32,7 +32,7 @@ pub struct ColorWidth {
pub min_width: u16, pub min_width: u16,
} }
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
#[serde(default)] #[serde(default)]
pub struct Style { pub struct Style {
pub tag: ColorWidth, pub tag: ColorWidth,
@ -40,7 +40,7 @@ pub struct Style {
pub snippet: ColorWidth, pub snippet: ColorWidth,
} }
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
#[serde(default)] #[serde(default)]
pub struct Finder { pub struct Finder {
#[serde(deserialize_with = "finder_deserialize")] #[serde(deserialize_with = "finder_deserialize")]
@ -58,27 +58,27 @@ where
.map_err(|_| de::Error::custom(format!("Failed to deserialize finder: {s}"))) .map_err(|_| de::Error::custom(format!("Failed to deserialize finder: {s}")))
} }
#[derive(Deserialize, Default)] #[derive(Deserialize, Default, Debug)]
#[serde(default)] #[serde(default)]
pub struct Cheats { pub struct Cheats {
pub path: Option<String>, pub path: Option<String>,
pub paths: Vec<String>, pub paths: Vec<String>,
} }
#[derive(Deserialize, Default)] #[derive(Deserialize, Default, Debug)]
#[serde(default)] #[serde(default)]
pub struct Search { pub struct Search {
pub tags: Option<String>, pub tags: Option<String>,
} }
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
#[serde(default)] #[serde(default)]
pub struct Shell { pub struct Shell {
pub command: String, pub command: String,
pub finder_command: Option<String>, pub finder_command: Option<String>,
} }
#[derive(Deserialize, Default)] #[derive(Deserialize, Default, Debug)]
#[serde(default)] #[serde(default)]
pub struct YamlConfig { pub struct YamlConfig {
pub style: Style, pub style: Style,

View file

@ -125,6 +125,7 @@ fn interpolate_paths(paths: String) -> String {
newtext newtext
} }
#[derive(Debug)]
pub struct Fetcher { pub struct Fetcher {
path: Option<String>, path: Option<String>,
files: RefCell<Vec<String>>, files: RefCell<Vec<String>>,
@ -180,6 +181,7 @@ impl fetcher::Fetcher for Fetcher {
} }
} }
log::debug!("filesystem::Fetcher = {self:#?}");
Ok(found_something) Ok(found_something)
} }

View file

@ -1,7 +1,5 @@
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
// #[macro_use]
// extern crate anyhow;
mod clients; mod clients;
mod commands; mod commands;