on master help sign in
Respect color settings for list-themes and list-languages
commite3c990f11e48b7b4df889c4706d54e7f04c9fdfe authorsharkdp <davidpeter@web.de> dateFri 31 Aug 2018, 21:57 (8 years ago) parent90c7d0c
1 file changed+25−13
src/main.rs+25−13
src/main.rs+25−13
@@ -87,7 +87,7 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
8787 Ok(())
8888}
8989
90−pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<()> {
90+pub fn list_languages(assets: &HighlightingAssets, config: &Config) -> Result<()> {
9191 let mut languages = assets
9292 .syntax_set
9393 .syntaxes()
@@ -105,11 +105,17 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
105105 let comma_separator = ", ";
106106 let separator = " ";
107107 // Line-wrapping for the possible file extension overflow.
108− let desired_width = term_width - longest - separator.len();
108+ let desired_width = config.term_width - longest - separator.len();
109109
110110 let stdout = io::stdout();
111111 let mut stdout = stdout.lock();
112112
113+ let style = if config.colored_output {
114+ Green.normal()
115+ } else {
116+ Style::default()
117+ };
118+
113119 for lang in languages {
114120 write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
115121
@@ -126,7 +132,7 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
126132 }
127133
128134 num_chars += new_chars;
129− write!(stdout, "{}", Green.paint(&word[..]))?;
135+ write!(stdout, "{}", style.paint(&word[..]))?;
130136 if extension.peek().is_some() {
131137 write!(stdout, "{}", comma_separator)?;
132138 }
@@ -148,15 +154,21 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
148154 let stdout = io::stdout();
149155 let mut stdout = stdout.lock();
150156
151− for (theme, _) in themes.iter() {
152− writeln!(
153− stdout,
154− "Theme: {}\n",
155− Style::new().bold().paint(theme.to_string())
156− )?;
157− config.theme = theme.to_string();
158− let _controller = Controller::new(&config, &assets).run();
159− writeln!(stdout)?;
157+ if config.colored_output {
158+ for (theme, _) in themes.iter() {
159+ writeln!(
160+ stdout,
161+ "Theme: {}\n",
162+ Style::new().bold().paint(theme.to_string())
163+ )?;
164+ config.theme = theme.to_string();
165+ let _controller = Controller::new(&config, &assets).run();
166+ writeln!(stdout)?;
167+ }
168+ } else {
169+ for (theme, _) in themes.iter() {
170+ writeln!(stdout, "{}", theme)?;
171+ }
160172 }
161173
162174 Ok(())
@@ -177,7 +189,7 @@ fn run() -> Result<bool> {
177189 let assets = HighlightingAssets::new();
178190
179191 if app.matches.is_present("list-languages") {
180− list_languages(&assets, config.term_width)?;
192+ list_languages(&assets, &config)?;
181193
182194 Ok(true)
183195 } else if app.matches.is_present("list-themes") {