on master help sign in
Respect color settings for list-themes and list-languages
e3c990fsharkdp
src/main.rs+25−13
@@ -87,7 +87,7 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
87 Ok(())
87 Ok(())
88}
88}
89
89
90−pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<()> {
90+pub fn list_languages(assets: &HighlightingAssets, config: &Config) -> Result<()> {
91 let mut languages = assets
91 let mut languages = assets
92 .syntax_set
92 .syntax_set
93 .syntaxes()
93 .syntaxes()
@@ -105,11 +105,17 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
105 let comma_separator = ", ";
105 let comma_separator = ", ";
106 let separator = " ";
106 let separator = " ";
107 // Line-wrapping for the possible file extension overflow.
107 // 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();
109
109
110 let stdout = io::stdout();
110 let stdout = io::stdout();
111 let mut stdout = stdout.lock();
111 let mut stdout = stdout.lock();
112
112
113+ let style = if config.colored_output {
114+ Green.normal()
115+ } else {
116+ Style::default()
117+ };
118+
113 for lang in languages {
119 for lang in languages {
114 write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
120 write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
115
121
@@ -126,7 +132,7 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
126 }
132 }
127
133
128 num_chars += new_chars;
134 num_chars += new_chars;
129− write!(stdout, "{}", Green.paint(&word[..]))?;
135+ write!(stdout, "{}", style.paint(&word[..]))?;
130 if extension.peek().is_some() {
136 if extension.peek().is_some() {
131 write!(stdout, "{}", comma_separator)?;
137 write!(stdout, "{}", comma_separator)?;
132 }
138 }
@@ -148,15 +154,21 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
148 let stdout = io::stdout();
154 let stdout = io::stdout();
149 let mut stdout = stdout.lock();
155 let mut stdout = stdout.lock();
150
156
157+ if config.colored_output {
151− for (theme, _) in themes.iter() {
158+ for (theme, _) in themes.iter() {
152− writeln!(
159+ writeln!(
153− stdout,
160+ stdout,
154− "Theme: {}\n",
161+ "Theme: {}\n",
155− Style::new().bold().paint(theme.to_string())
162+ Style::new().bold().paint(theme.to_string())
156− )?;
163+ )?;
157− config.theme = theme.to_string();
164+ config.theme = theme.to_string();
158− let _controller = Controller::new(&config, &assets).run();
165+ let _controller = Controller::new(&config, &assets).run();
166+ writeln!(stdout)?;
167+ }
168+ } else {
169+ for (theme, _) in themes.iter() {
159− writeln!(stdout)?;
170+ writeln!(stdout, "{}", theme)?;
171+ }
160 }
172 }
161
173
162 Ok(())
174 Ok(())
@@ -177,7 +189,7 @@ fn run() -> Result<bool> {
177 let assets = HighlightingAssets::new();
189 let assets = HighlightingAssets::new();
178
190
179 if app.matches.is_present("list-languages") {
191 if app.matches.is_present("list-languages") {
180− list_languages(&assets, config.term_width)?;
192+ list_languages(&assets, &config)?;
181
193
182 Ok(true)
194 Ok(true)
183 } else if app.matches.is_present("list-themes") {
195 } else if app.matches.is_present("list-themes") {