add theme option (#95)

closes #89
This commit is contained in:
Ryan Leung 2018-05-11 19:53:17 +08:00 committed by David Peter
parent f711fb5006
commit 22c8978fca
3 changed files with 29 additions and 6 deletions

View File

@ -85,6 +85,17 @@ impl App {
.long("list-languages")
.help("Displays supported languages"),
)
.arg(
Arg::with_name("theme")
.long("theme")
.takes_value(true)
.help("Set the theme for highlighting"),
)
.arg(
Arg::with_name("list-themes")
.long("list-themes")
.help("Displays supported themes"),
)
.subcommand(
SubCommand::with_name("cache")
.about("Modify the syntax-definition and theme cache")
@ -143,6 +154,7 @@ impl App {
},
term_width: Term::stdout().size().1 as usize,
files,
theme: self.matches.value_of("theme"),
})
}
@ -185,6 +197,7 @@ pub struct Config<'a> {
pub paging: bool,
pub term_width: usize,
pub files: Vec<Option<&'a str>>,
pub theme: Option<&'a str>,
}
fn is_truecolor_terminal() -> bool {

View File

@ -126,11 +126,13 @@ impl HighlightingAssets {
Ok(())
}
pub fn default_theme(&self) -> Result<&Theme> {
Ok(self.theme_set
.themes
.get("Default")
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not find 'Default' theme"))?)
pub fn get_theme(&self, theme: &str) -> Result<&Theme> {
Ok(self.theme_set.themes.get(theme).ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
format!("Could not find '{}' theme", theme),
)
})?)
}
}

View File

@ -223,7 +223,7 @@ fn run() -> Result<()> {
let config = app.config()?;
let assets = HighlightingAssets::new();
let theme = assets.default_theme()?;
let theme = assets.get_theme(config.theme.unwrap_or("Default"))?;
if app.matches.is_present("list-languages") {
let languages = assets.syntax_set.syntaxes();
@ -269,6 +269,14 @@ fn run() -> Result<()> {
return Ok(());
}
if app.matches.is_present("list-themes") {
let themes = &assets.theme_set.themes;
for (theme, _) in themes.iter() {
println!("{}", theme);
}
return Ok(());
}
let mut output_type = get_output_type(config.paging);
let handle = output_type.handle()?;
let mut printer = Printer::new(handle, &config);