on master help sign in
Use stdout locks
commit90c7d0c3659ea08898f341792107a0716188cb20 authorsharkdp <davidpeter@web.de> dateFri 31 Aug 2018, 21:48 (8 years ago) parent5600226
1 file changed+15−15
src/main.rs+15−15
src/main.rs+15−15
@@ -30,7 +30,6 @@ mod terminal;
3030
3131use std::collections::HashSet;
3232use std::io;
33−use std::io::stdout;
3433use std::io::Write;
3534use std::path::Path;
3635use std::process;
@@ -82,7 +81,7 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
8281 } else if matches.is_present("clear") {
8382 clear_assets();
8483 } else if matches.is_present("config-dir") {
85− writeln!(stdout(), "{}", config_dir())?;
84+ writeln!(io::stdout(), "{}", config_dir())?;
8685 }
8786
8887 Ok(())
@@ -108,14 +107,11 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
108107 // Line-wrapping for the possible file extension overflow.
109108 let desired_width = term_width - longest - separator.len();
110109
110+ let stdout = io::stdout();
111+ let mut stdout = stdout.lock();
112+
111113 for lang in languages {
112− write!(
113− stdout(),
114− "{:width$}{}",
115− lang.name,
116− separator,
117− width = longest
118− )?;
114+ write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
119115
120116 // Number of characters on this line so far, wrap before `desired_width`
121117 let mut num_chars = 0;
@@ -126,16 +122,16 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<
126122 let new_chars = word.len() + comma_separator.len();
127123 if num_chars + new_chars >= desired_width {
128124 num_chars = 0;
129− write!(stdout(), "\n{:width$}{}", "", separator, width = longest)?;
125+ write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
130126 }
131127
132128 num_chars += new_chars;
133− write!(stdout(), "{}", Green.paint(&word[..]))?;
129+ write!(stdout, "{}", Green.paint(&word[..]))?;
134130 if extension.peek().is_some() {
135− write!(stdout(), "{}", comma_separator)?;
131+ write!(stdout, "{}", comma_separator)?;
136132 }
137133 }
138− writeln!(stdout())?;
134+ writeln!(stdout)?;
139135 }
140136
141137 Ok(())
@@ -148,15 +144,19 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
148144 style.insert(OutputComponent::Plain);
149145 config.files = vec![InputFile::ThemePreviewFile];
150146 config.output_components = OutputComponents(style);
147+
148+ let stdout = io::stdout();
149+ let mut stdout = stdout.lock();
150+
151151 for (theme, _) in themes.iter() {
152152 writeln!(
153− stdout(),
153+ stdout,
154154 "Theme: {}\n",
155155 Style::new().bold().paint(theme.to_string())
156156 )?;
157157 config.theme = theme.to_string();
158158 let _controller = Controller::new(&config, &assets).run();
159− writeln!(stdout())?;
159+ writeln!(stdout)?;
160160 }
161161
162162 Ok(())