1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-02-21 12:28:30 +00:00

Use stdout locks

This commit is contained in:
sharkdp 2018-08-31 21:48:26 +02:00
parent 56002267d2
commit 90c7d0c365

View File

@ -30,7 +30,6 @@ mod terminal;
use std::collections::HashSet; use std::collections::HashSet;
use std::io; use std::io;
use std::io::stdout;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::process; use std::process;
@ -82,7 +81,7 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
} else if matches.is_present("clear") { } else if matches.is_present("clear") {
clear_assets(); clear_assets();
} else if matches.is_present("config-dir") { } else if matches.is_present("config-dir") {
writeln!(stdout(), "{}", config_dir())?; writeln!(io::stdout(), "{}", config_dir())?;
} }
Ok(()) Ok(())
@ -108,14 +107,11 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
// Line-wrapping for the possible file extension overflow. // Line-wrapping for the possible file extension overflow.
let desired_width = term_width - longest - separator.len(); let desired_width = term_width - longest - separator.len();
let stdout = io::stdout();
let mut stdout = stdout.lock();
for lang in languages { for lang in languages {
write!( write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
stdout(),
"{:width$}{}",
lang.name,
separator,
width = longest
)?;
// Number of characters on this line so far, wrap before `desired_width` // Number of characters on this line so far, wrap before `desired_width`
let mut num_chars = 0; let mut num_chars = 0;
@ -126,16 +122,16 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
let new_chars = word.len() + comma_separator.len(); let new_chars = word.len() + comma_separator.len();
if num_chars + new_chars >= desired_width { if num_chars + new_chars >= desired_width {
num_chars = 0; num_chars = 0;
write!(stdout(), "\n{:width$}{}", "", separator, width = longest)?; write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
} }
num_chars += new_chars; num_chars += new_chars;
write!(stdout(), "{}", Green.paint(&word[..]))?; write!(stdout, "{}", Green.paint(&word[..]))?;
if extension.peek().is_some() { if extension.peek().is_some() {
write!(stdout(), "{}", comma_separator)?; write!(stdout, "{}", comma_separator)?;
} }
} }
writeln!(stdout())?; writeln!(stdout)?;
} }
Ok(()) Ok(())
@ -148,15 +144,19 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
style.insert(OutputComponent::Plain); style.insert(OutputComponent::Plain);
config.files = vec![InputFile::ThemePreviewFile]; config.files = vec![InputFile::ThemePreviewFile];
config.output_components = OutputComponents(style); config.output_components = OutputComponents(style);
let stdout = io::stdout();
let mut stdout = stdout.lock();
for (theme, _) in themes.iter() { for (theme, _) in themes.iter() {
writeln!( writeln!(
stdout(), stdout,
"Theme: {}\n", "Theme: {}\n",
Style::new().bold().paint(theme.to_string()) Style::new().bold().paint(theme.to_string())
)?; )?;
config.theme = theme.to_string(); config.theme = theme.to_string();
let _controller = Controller::new(&config, &assets).run(); let _controller = Controller::new(&config, &assets).run();
writeln!(stdout())?; writeln!(stdout)?;
} }
Ok(()) Ok(())