From 493a4e719e4014706adcc75f17ee76e2cf27fb96 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Fri, 10 May 2019 14:38:48 -0700 Subject: [PATCH] Added non-interactive mode for `--list-languages` This makes scripting it a lot easier and less hacky. --- src/main.rs | 82 ++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e9c4ccc..0f09f6e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,48 +108,54 @@ pub fn list_languages(config: &Config) -> Result<()> { .collect::>(); languages.sort_by_key(|lang| lang.name.to_uppercase()); - let longest = languages - .iter() - .map(|syntax| syntax.name.len()) - .max() - .unwrap_or(32); // Fallback width if they have no language definitions. - - let comma_separator = ", "; - let separator = " "; - // Line-wrapping for the possible file extension overflow. - let desired_width = config.term_width - longest - separator.len(); - let stdout = io::stdout(); let mut stdout = stdout.lock(); - let style = if config.colored_output { - Green.normal() - } else { - Style::default() - }; - - for lang in languages { - write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?; - - // Number of characters on this line so far, wrap before `desired_width` - let mut num_chars = 0; - - let mut extension = lang.file_extensions.iter().peekable(); - while let Some(word) = extension.next() { - // If we can't fit this word in, then create a line break and align it in. - let new_chars = word.len() + comma_separator.len(); - if num_chars + new_chars >= desired_width { - num_chars = 0; - write!(stdout, "\n{:width$}{}", "", separator, width = longest)?; - } - - num_chars += new_chars; - write!(stdout, "{}", style.paint(&word[..]))?; - if extension.peek().is_some() { - write!(stdout, "{}", comma_separator)?; - } + if config.loop_through { + for lang in languages { + write!(stdout, "{}:{}\n", lang.name, lang.file_extensions.join(",")); + } + } else { + let longest = languages + .iter() + .map(|syntax| syntax.name.len()) + .max() + .unwrap_or(32); // Fallback width if they have no language definitions. + + let comma_separator = ", "; + let separator = " "; + // Line-wrapping for the possible file extension overflow. + let desired_width = config.term_width - longest - separator.len(); + + let style = if config.colored_output { + Green.normal() + } else { + Style::default() + }; + + for lang in languages { + write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?; + + // Number of characters on this line so far, wrap before `desired_width` + let mut num_chars = 0; + + let mut extension = lang.file_extensions.iter().peekable(); + while let Some(word) = extension.next() { + // If we can't fit this word in, then create a line break and align it in. + let new_chars = word.len() + comma_separator.len(); + if num_chars + new_chars >= desired_width { + num_chars = 0; + write!(stdout, "\n{:width$}{}", "", separator, width = longest)?; + } + + num_chars += new_chars; + write!(stdout, "{}", style.paint(&word[..]))?; + if extension.peek().is_some() { + write!(stdout, "{}", comma_separator)?; + } + } + writeln!(stdout)?; } - writeln!(stdout)?; } Ok(())