on master help sign in
Added non-interactive mode for `--list-languages`
493a4e7Ethan P
src/main.rs+41−35
@@ -108,48 +108,54 @@ pub fn list_languages(config: &Config) -> Result<()> {
108 .collect::<Vec<_>>();
108 .collect::<Vec<_>>();
109 languages.sort_by_key(|lang| lang.name.to_uppercase());
109 languages.sort_by_key(|lang| lang.name.to_uppercase());
110
110
111− let longest = languages
112− .iter()
113− .map(|syntax| syntax.name.len())
114− .max()
115− .unwrap_or(32); // Fallback width if they have no language definitions.
116−
117− let comma_separator = ", ";
118− let separator = " ";
119− // Line-wrapping for the possible file extension overflow.
120− let desired_width = config.term_width - longest - separator.len();
121−
122 let stdout = io::stdout();
111 let stdout = io::stdout();
123 let mut stdout = stdout.lock();
112 let mut stdout = stdout.lock();
124
113
125− let style = if config.colored_output {
114+ if config.loop_through {
126− Green.normal()
115+ for lang in languages {
116+ write!(stdout, "{}:{}\n", lang.name, lang.file_extensions.join(","));
117+ }
127 } else {
118 } else {
119+ let longest = languages
120+ .iter()
121+ .map(|syntax| syntax.name.len())
122+ .max()
123+ .unwrap_or(32); // Fallback width if they have no language definitions.
124+
125+ let comma_separator = ", ";
126+ let separator = " ";
127+ // Line-wrapping for the possible file extension overflow.
128+ let desired_width = config.term_width - longest - separator.len();
129+
130+ let style = if config.colored_output {
131+ Green.normal()
132+ } else {
128− Style::default()
133+ Style::default()
129− };
134+ };
130−
131− for lang in languages {
132− write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
133−
134− // Number of characters on this line so far, wrap before `desired_width`
135− let mut num_chars = 0;
136−
137− let mut extension = lang.file_extensions.iter().peekable();
138− while let Some(word) = extension.next() {
139− // If we can't fit this word in, then create a line break and align it in.
140− let new_chars = word.len() + comma_separator.len();
141− if num_chars + new_chars >= desired_width {
142− num_chars = 0;
143− write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
144− }
145
135
136+ for lang in languages {
137+ write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
138+
139+ // Number of characters on this line so far, wrap before `desired_width`
140+ let mut num_chars = 0;
141+
142+ let mut extension = lang.file_extensions.iter().peekable();
143+ while let Some(word) = extension.next() {
144+ // If we can't fit this word in, then create a line break and align it in.
145+ let new_chars = word.len() + comma_separator.len();
146+ if num_chars + new_chars >= desired_width {
147+ num_chars = 0;
148+ write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
149+ }
150+
146− num_chars += new_chars;
151+ num_chars += new_chars;
147− write!(stdout, "{}", style.paint(&word[..]))?;
152+ write!(stdout, "{}", style.paint(&word[..]))?;
148− if extension.peek().is_some() {
153+ if extension.peek().is_some() {
149− write!(stdout, "{}", comma_separator)?;
154+ write!(stdout, "{}", comma_separator)?;
155+ }
150 }
156 }
157+ writeln!(stdout)?;
151 }
158 }
152− writeln!(stdout)?;
153 }
159 }
154
160
155 Ok(())
161 Ok(())