feat: devicons

This commit is contained in:
alexpasmantier 2024-10-04 14:15:15 +02:00
parent 67c0e93a5e
commit df4d56a067
6 changed files with 49 additions and 3 deletions

16
Cargo.lock generated
View File

@ -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"

View File

@ -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"] }

View File

@ -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

View File

@ -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<Commands>,
@ -120,6 +124,7 @@ pub struct PostProcessedCli {
pub colored_output: bool,
pub filter_filetypes: Vec<String>,
pub disable_hyperlinks: bool,
pub disable_devicons: bool,
pub sub_command: Option<Commands>,
}
@ -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<PostProcessedCli> {
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,
})
}

View File

@ -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);

View File

@ -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<SearchResult>) -> Result<()> {
@ -196,6 +207,14 @@ impl ResultsPrinter {
}
}
fn devicons_to_termcolor_color(d_color: &str) -> Option<Color> {
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?