mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-15 15:18:45 +00:00
Added non-interactive mode for --list-languages
This makes scripting it a lot easier and less hacky.
This commit is contained in:
parent
27e0ca98d9
commit
493a4e719e
82
src/main.rs
82
src/main.rs
@ -108,48 +108,54 @@ pub fn list_languages(config: &Config) -> Result<()> {
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
languages.sort_by_key(|lang| lang.name.to_uppercase());
|
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 stdout = io::stdout();
|
||||||
let mut stdout = stdout.lock();
|
let mut stdout = stdout.lock();
|
||||||
|
|
||||||
let style = if config.colored_output {
|
if config.loop_through {
|
||||||
Green.normal()
|
for lang in languages {
|
||||||
} else {
|
write!(stdout, "{}:{}\n", lang.name, lang.file_extensions.join(","));
|
||||||
Style::default()
|
}
|
||||||
};
|
} else {
|
||||||
|
let longest = languages
|
||||||
for lang in languages {
|
.iter()
|
||||||
write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
|
.map(|syntax| syntax.name.len())
|
||||||
|
.max()
|
||||||
// Number of characters on this line so far, wrap before `desired_width`
|
.unwrap_or(32); // Fallback width if they have no language definitions.
|
||||||
let mut num_chars = 0;
|
|
||||||
|
let comma_separator = ", ";
|
||||||
let mut extension = lang.file_extensions.iter().peekable();
|
let separator = " ";
|
||||||
while let Some(word) = extension.next() {
|
// Line-wrapping for the possible file extension overflow.
|
||||||
// If we can't fit this word in, then create a line break and align it in.
|
let desired_width = config.term_width - longest - separator.len();
|
||||||
let new_chars = word.len() + comma_separator.len();
|
|
||||||
if num_chars + new_chars >= desired_width {
|
let style = if config.colored_output {
|
||||||
num_chars = 0;
|
Green.normal()
|
||||||
write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
|
} else {
|
||||||
}
|
Style::default()
|
||||||
|
};
|
||||||
num_chars += new_chars;
|
|
||||||
write!(stdout, "{}", style.paint(&word[..]))?;
|
for lang in languages {
|
||||||
if extension.peek().is_some() {
|
write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
|
||||||
write!(stdout, "{}", comma_separator)?;
|
|
||||||
}
|
// 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(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user