Skip to content

Commit

Permalink
feat: devicons
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 4, 2024
1 parent 67c0e93 commit df4d56a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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>,
}

Expand All @@ -137,6 +142,7 @@ impl Default for PostProcessedCli {
colored_output: true,
filter_filetypes: Vec::new(),
disable_hyperlinks: false,
disable_devicons: false,
sub_command: None,
}
}
Expand Down Expand Up @@ -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,
})
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 21 additions & 2 deletions src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use devicons::FileIcon;
use std::{
env::current_dir,
fmt,
Expand Down Expand Up @@ -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 {
Expand All @@ -39,6 +41,7 @@ impl Default for PrinterConfig {
color_specs: ColorSpecs::default(),
absolute_paths: false,
disable_hyperlinks: false,
disable_devicons: false,
}
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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<()> {
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit df4d56a

Please sign in to comment.