Skip to content

Commit c3fffdd

Browse files
epagedjc
authored andcommitted
refactor: Directly apply styling
1 parent 39c236e commit c3fffdd

File tree

5 files changed

+47
-75
lines changed

5 files changed

+47
-75
lines changed

src/cli/common.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ fn show_channel_updates(
193193
Ok((pkg, banner, width, color, version, previous_version))
194194
});
195195

196-
let mut t = cfg.process.stdout();
196+
let t = cfg.process.stdout();
197+
let mut t = t.lock();
197198

198199
let data: Vec<_> = data.collect::<Result<_>>()?;
199200
let max_width = data
@@ -208,17 +209,13 @@ fn show_channel_updates(
208209
None => Style::new(),
209210
}
210211
.bold();
211-
let _ = write!(t.lock(), " {padding}");
212-
let _ = t.style(&style);
213-
let _ = write!(t.lock(), "{pkg} {banner}");
214-
let _ = t.reset();
215-
let _ = write!(t.lock(), " - {version}");
212+
let _ = write!(t, " {padding}{style}{pkg} {banner}{style:#} - {version}");
216213
if let Some(previous_version) = previous_version {
217-
let _ = write!(t.lock(), " (from {previous_version})");
214+
let _ = write!(t, " (from {previous_version})");
218215
}
219-
let _ = writeln!(t.lock());
216+
let _ = writeln!(t);
220217
}
221-
let _ = writeln!(t.lock());
218+
let _ = writeln!(t);
222219

223220
Ok(())
224221
}
@@ -258,15 +255,14 @@ pub(super) fn list_items(
258255
quiet: bool,
259256
process: &Process,
260257
) -> Result<utils::ExitCode> {
261-
let mut t = process.stdout();
258+
let t = process.stdout();
259+
let mut t = t.lock();
262260
let bold = Style::new().bold();
263261
for (name, installed) in items {
264262
if installed && !installed_only && !quiet {
265-
t.style(&bold)?;
266-
writeln!(t.lock(), "{name} (installed)")?;
267-
t.reset()?;
263+
writeln!(t, "{bold}{name} (installed){bold:#}")?;
268264
} else if installed || !installed_only {
269-
writeln!(t.lock(), "{name}")?;
265+
writeln!(t, "{name}")?;
270266
}
271267
}
272268
Ok(utils::ExitCode(0))

src/cli/markdown.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Write Markdown to the terminal
22
use std::io::Write;
33

4-
use anstyle::{AnsiColor, Style};
4+
use anstyle::{AnsiColor, Reset, Style};
55
use pulldown_cmark::{Event, Tag, TagEnd};
66

77
use crate::process::ColorableTerminal;
@@ -111,15 +111,15 @@ impl<'a> LineFormatter<'a> {
111111
fn push_attr(&mut self, attr: Attr) {
112112
self.attrs.push(attr);
113113
attr.apply_to(&mut self.style);
114-
let _ = self.wrapper.w.style(&self.style);
114+
let _ = write!(self.wrapper.w.lock(), "{Reset}{}", self.style);
115115
}
116116
fn pop_attr(&mut self) {
117117
self.attrs.pop();
118118
self.style = Style::new();
119119
for attr in &self.attrs {
120120
attr.apply_to(&mut self.style);
121121
}
122-
let _ = self.wrapper.w.style(&self.style);
122+
let _ = write!(self.wrapper.w.lock(), "{Reset}{}", self.style);
123123
}
124124

125125
fn start_tag(&mut self, tag: Tag<'a>) {

src/cli/rustup_mode.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,21 +1077,25 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
10771077

10781078
// Print host triple
10791079
{
1080-
let mut t = cfg.process.stdout();
1081-
t.style(&bold)?;
1082-
write!(t.lock(), "Default host: ")?;
1083-
t.reset()?;
1084-
writeln!(t.lock(), "{}", cfg.get_default_host_triple()?)?;
1080+
let t = cfg.process.stdout();
1081+
let mut t = t.lock();
1082+
writeln!(
1083+
t,
1084+
"{bold}Default host: {bold:#}{}",
1085+
cfg.get_default_host_triple()?
1086+
)?;
10851087
}
10861088

10871089
// Print rustup home directory
10881090
{
1089-
let mut t = cfg.process.stdout();
1090-
t.style(&bold)?;
1091-
write!(t.lock(), "rustup home: ")?;
1092-
t.reset()?;
1093-
writeln!(t.lock(), "{}", cfg.rustup_dir.display())?;
1094-
writeln!(t.lock())?;
1091+
let t = cfg.process.stdout();
1092+
let mut t = t.lock();
1093+
writeln!(
1094+
t,
1095+
"{bold}rustup home: {bold:#}{}",
1096+
cfg.rustup_dir.display()
1097+
)?;
1098+
writeln!(t)?;
10951099
}
10961100

10971101
let installed_toolchains = cfg.list_toolchains()?;
@@ -1195,15 +1199,12 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
11951199
}
11961200
}
11971201

1198-
fn print_header(t: &mut ColorableTerminal, s: &str) -> Result<(), Error> {
1202+
fn print_header(t: &mut ColorableTerminal, text: &str) -> Result<(), Error> {
11991203
let bold = Style::new().bold();
1200-
t.style(&bold)?;
1201-
{
1202-
let mut term_lock = t.lock();
1203-
writeln!(term_lock, "{s}")?;
1204-
writeln!(term_lock, "{}", "-".repeat(s.len()))?;
1205-
} // drop the term_lock
1206-
t.reset()?;
1204+
let divider = "-".repeat(text.len());
1205+
let mut term_lock = t.lock();
1206+
writeln!(term_lock, "{bold}{text}{bold:#}")?;
1207+
writeln!(term_lock, "{bold}{divider}{bold:#}")?;
12071208
Ok(())
12081209
}
12091210

src/cli/self_update.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,8 @@ impl fmt::Display for SchemaVersion {
13571357

13581358
/// Returns whether an update was available
13591359
pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg<'_>) -> anyhow::Result<bool> {
1360-
let mut t = dl_cfg.process.stdout();
1360+
let t = dl_cfg.process.stdout();
1361+
let mut t = t.lock();
13611362
// Get current rustup version
13621363
let current_version = env!("CARGO_PKG_VERSION");
13631364

@@ -1368,20 +1369,16 @@ pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg<'_>) -> anyhow::Res
13681369
let yellow = AnsiColor::Yellow.on_default().bold();
13691370
let green = AnsiColor::Green.on_default().bold();
13701371

1371-
let _ = t.style(&bold);
1372-
write!(t.lock(), "rustup - ")?;
1372+
write!(t, "{bold}rustup - {bold:#}")?;
13731373

13741374
Ok(if current_version != available_version {
1375-
let _ = t.style(&yellow);
1376-
write!(t.lock(), "Update available")?;
1377-
let _ = t.reset();
1378-
writeln!(t.lock(), " : {current_version} -> {available_version}")?;
1375+
writeln!(
1376+
t,
1377+
"{yellow}Update available{yellow:#} : {current_version} -> {available_version}"
1378+
)?;
13791379
true
13801380
} else {
1381-
let _ = t.style(&green);
1382-
write!(t.lock(), "Up to date")?;
1383-
let _ = t.reset();
1384-
writeln!(t.lock(), " : {current_version}")?;
1381+
writeln!(t, "{green}Up to date{green:#} : {current_version}")?;
13851382
false
13861383
})
13871384
}

src/process/terminal_source.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use std::{
99
sync::{Arc, Mutex, MutexGuard},
1010
};
1111

12+
#[cfg(feature = "test")]
13+
use anstream::StripStream;
1214
use anstream::{AutoStream, ColorChoice};
13-
use anstyle::{Reset, Style};
1415

1516
use super::Process;
1617
#[cfg(feature = "test")]
@@ -102,38 +103,15 @@ impl ColorableTerminal {
102103
TerminalInnerLocked::Stderr(AutoStream::new(locked, self.color_choice))
103104
}
104105
#[cfg(feature = "test")]
105-
TerminalInner::TestWriter(w, _) => TerminalInnerLocked::TestWriter(w.clone()),
106+
TerminalInner::TestWriter(w, _) => {
107+
TerminalInnerLocked::TestWriter(StripStream::new(Box::new(w.clone())))
108+
}
106109
});
107110
// ColorableTerminalLocked { inner, guard, locked }
108111
uninit.assume_init()
109112
}
110113
}
111114

112-
pub fn style(&mut self, new: &Style) -> io::Result<()> {
113-
match self.inner.lock().unwrap().deref_mut() {
114-
TerminalInner::Stdout(s) => {
115-
write!(s, "{Reset}{new}")
116-
}
117-
TerminalInner::Stderr(s) => {
118-
write!(s, "{Reset}{new}")
119-
}
120-
#[cfg(feature = "test")]
121-
TerminalInner::TestWriter(_, _) => Ok(()),
122-
}
123-
}
124-
pub fn reset(&mut self) -> io::Result<()> {
125-
match self.inner.lock().unwrap().deref_mut() {
126-
TerminalInner::Stdout(s) => {
127-
write!(s, "{Reset}")
128-
}
129-
TerminalInner::Stderr(s) => {
130-
write!(s, "{Reset}")
131-
}
132-
#[cfg(feature = "test")]
133-
TerminalInner::TestWriter(_, _) => Ok(()),
134-
}
135-
}
136-
137115
pub fn is_a_tty(&self) -> bool {
138116
self.is_a_tty
139117
}
@@ -282,7 +260,7 @@ enum TerminalInnerLocked {
282260
Stdout(AutoStream<io::StdoutLock<'static>>),
283261
Stderr(AutoStream<io::StderrLock<'static>>),
284262
#[cfg(feature = "test")]
285-
TestWriter(TestWriter),
263+
TestWriter(StripStream<Box<dyn Write>>),
286264
}
287265

288266
impl TerminalInnerLocked {

0 commit comments

Comments
 (0)