Skip to content

Commit

Permalink
feat: --list-themes support
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 11, 2022
1 parent 60bdbce commit a47ff5d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
5 changes: 5 additions & 0 deletions assets/theme_preview.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Output the square of a number.
fn print_square(num: f64) {
let result = f64::powf(num, 2.0);
println!("The square of {:.2} is {:.2}.", num, result);
}
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct Args {
#[arg(long, group = "mode")]
pub list_languages: bool,

/// Display all supported highlighting themes
#[arg(long, group = "mode")]
pub list_themes: bool,

/// Run as if git was started in `PATH` instead of the current working directory.
///
/// When multiple -C options are given, each subsequent
Expand Down
6 changes: 3 additions & 3 deletions src/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,15 @@ pub struct Highlighter<'a> {
}

impl<'a> Highlighter<'a> {
fn enabled(
pub fn enabled(
syntax: &'a syntect::parsing::SyntaxReference,
theme: &'a syntect::highlighting::Theme,
) -> Self {
let highlighter = Some(syntect::easy::HighlightLines::new(syntax, theme));
Self { highlighter, theme }
}

fn disabled() -> Self {
pub fn disabled() -> Self {
let highlighter = None;
static THEME: syntect::highlighting::Theme = syntect::highlighting::Theme {
name: None,
Expand Down Expand Up @@ -392,7 +392,7 @@ impl<'a> Highlighter<'a> {
self.theme
}

fn highlight_line(
pub fn highlight_line(
&mut self,
line: &str,
syntax_set: &syntect::parsing::SyntaxSet,
Expand Down
44 changes: 44 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ fn run() -> proc_exit::ExitResult {
config::dump_config(output_path, &mut config)?;
} else if args.list_languages {
list_languages(&mut config, colored_stdout)?;
} else if args.list_themes {
list_themes(&mut config, colored_stdout)?;
} else if let Some(file_path) = args.file.as_deref() {
blame::blame(
file_path,
Expand Down Expand Up @@ -121,3 +123,45 @@ fn list_languages(config: &mut Config, colored_stdout: bool) -> proc_exit::ExitR

Ok(())
}

fn list_themes(config: &mut Config, colored_stdout: bool) -> proc_exit::ExitResult {
let pager = config.get(&crate::git2_config::PAGER);
let mut pager = Pager::stdout(&pager);
let mut pager = pager.start();
let pager = pager.as_writer().with_code(proc_exit::Code::FAILURE)?;

let theme_set = syntect::highlighting::ThemeSet::load_defaults();
if colored_stdout {
let syntax_set = syntect::parsing::SyntaxSet::load_defaults_newlines();
let syntax = syntax_set
.find_syntax_by_name("Rust")
.expect("always included");
for (name, theme) in theme_set.themes.iter() {
let mut highlighter = blame::Highlighter::enabled(syntax, theme);
let _ = writeln!(
pager,
"Theme: {}{}{}",
anstyle::Effects::BOLD.render(),
name,
anstyle::Reset.render()
);
let _ = writeln!(pager);
for line in THEME_PREVIEW_DATA.lines() {
let _ = writeln!(
pager,
"{}{}",
highlighter.highlight_line(line, &syntax_set).unwrap(),
anstyle::Reset.render()
);
}
let _ = writeln!(pager);
}
} else {
for name in theme_set.themes.keys() {
let _ = writeln!(pager, "{}", name);
}
}
Ok(())
}

const THEME_PREVIEW_DATA: &str = include_str!("../assets/theme_preview.rs");

0 comments on commit a47ff5d

Please sign in to comment.