diff --git a/Cargo.lock b/Cargo.lock index 0d84c26..103bfbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,15 @@ version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" +[[package]] +name = "devicons" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "878cb77b811b29aab82d4246bc9c65df0b4dd9c5509657556cfc7970e2ae5ecc" +dependencies = [ + "lazy_static", +] + [[package]] name = "encoding_rs" version = "0.8.34" @@ -270,6 +279,7 @@ version = "0.4.1" dependencies = [ "anyhow", "clap", + "devicons", "grep", "ignore", "serde", @@ -311,6 +321,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.155" diff --git a/Cargo.toml b/Cargo.toml index 7cc9c25..1c50d30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ categories = [ [dependencies] anyhow = "1.0.86" clap = { version = "4.5.9", features = ["derive"] } +devicons = "0.6.7" grep = "0.3.1" ignore = "0.4.22" serde = { version = "1.0.204", features = ["derive"] } diff --git a/README.md b/README.md index 28b85c0..14005d3 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ Summary ``` ```plaintext -A somewhat faster, more lightweight, ripgrep-inspired alternative. +A faster, more lightweight ripgrep alternative for day to day usecases. Usage: gg [OPTIONS] [PATTERN] [PATHS]... [COMMAND] @@ -200,6 +200,8 @@ Options: filter on filetype (defaults to all filetypes) -H, --disable-hyperlinks disable hyperlinks in output (defaults to false) + -D, --disable-devicons + disable devicons in output (defaults to false) -h, --help Print help -V, --version diff --git a/src/cli.rs b/src/cli.rs index 99a0cf6..7a4a197 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -64,6 +64,10 @@ pub struct Cli { #[clap(short = 'H', long, default_value_t = false)] pub disable_hyperlinks: bool, + /// disable devicons in output (defaults to false) + #[clap(short = 'D', long, default_value_t = false)] + pub disable_devicons: bool, + /// Subcommands #[clap(subcommand)] pub sub_command: Option, @@ -120,6 +124,7 @@ pub struct PostProcessedCli { pub colored_output: bool, pub filter_filetypes: Vec, pub disable_hyperlinks: bool, + pub disable_devicons: bool, pub sub_command: Option, } @@ -137,6 +142,7 @@ impl Default for PostProcessedCli { colored_output: true, filter_filetypes: Vec::new(), disable_hyperlinks: false, + disable_devicons: false, sub_command: None, } } @@ -177,6 +183,7 @@ pub fn process_cli_args(mut cli: Cli) -> anyhow::Result { colored_output: !cli.disable_colored_output, filter_filetypes: cli.filter_filetypes, disable_hyperlinks: cli.disable_hyperlinks, + disable_devicons: cli.disable_devicons, sub_command: cli.sub_command, }) } diff --git a/src/main.rs b/src/main.rs index 8eacb6c..b6da6d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,6 +111,7 @@ pub fn main() -> anyhow::Result<()> { absolute_paths: cli_args.absolute_paths, colored_output: cli_args.colored_output, disable_hyperlinks: cli_args.disable_hyperlinks, + disable_devicons: cli_args.disable_devicons, ..Default::default() }; let mut printer = ResultsPrinter::new(printer_config); diff --git a/src/printer.rs b/src/printer.rs index bf2984b..0620e98 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -1,3 +1,4 @@ +use devicons::FileIcon; use std::{ env::current_dir, fmt, @@ -29,6 +30,7 @@ pub struct PrinterConfig { pub color_specs: ColorSpecs, pub absolute_paths: bool, pub disable_hyperlinks: bool, + pub disable_devicons: bool, } impl Default for PrinterConfig { @@ -39,6 +41,7 @@ impl Default for PrinterConfig { color_specs: ColorSpecs::default(), absolute_paths: false, disable_hyperlinks: false, + disable_devicons: false, } } } @@ -118,6 +121,14 @@ impl ResultsPrinter { } fn write_colored_path(&mut self, path: &Path) -> Result<()> { + if !self.config.disable_devicons { + let icon = FileIcon::from(path); + self.buffer.set_color(ColorSpec::new().set_fg(Some( + devicons_to_termcolor_color(&icon.color).unwrap_or(Color::White), + )))?; + write!(&mut self.buffer, "{} ", icon.icon)?; + } + self.buffer.set_color(&self.config.color_specs.paths)?; let display_path = if self.config.absolute_paths { path.to_string_lossy() @@ -127,14 +138,14 @@ impl ResultsPrinter { path.to_string_lossy() }; if self.config.disable_hyperlinks { - return writeln!(&mut self.buffer, "{}", display_path); + return write!(&mut self.buffer, "{}\n", display_path); } let path_str = path.to_string_lossy(); let link = Hyperlink { uri: &format!("file://{}", path_str), id: None, }; - writeln!(&mut self.buffer, "{link}{}{link:#}", display_path) + write!(&mut self.buffer, "{link}{}{link:#}\n", display_path) } fn write_colored_search_results(&mut self, results: Vec) -> Result<()> { @@ -196,6 +207,14 @@ impl ResultsPrinter { } } +fn devicons_to_termcolor_color(d_color: &str) -> Option { + d_color.strip_prefix("#").and_then(|hex| { + u32::from_str_radix(hex, 16) + .ok() + .map(|c| Color::Rgb((c >> 16) as u8, (c >> 8) as u8, c as u8)) + }) +} + #[derive(Default, Debug, PartialEq, Clone)] pub struct Hyperlink<'a> { // maybe this should use u8 to support non-utf encodings?