Skip to content

Commit 56eef88

Browse files
committed
refactor(cli): Simplify palette handling
This did include a couple of tweaks to make it closer to rustc
1 parent 00220c3 commit 56eef88

File tree

3 files changed

+41
-117
lines changed

3 files changed

+41
-117
lines changed

crates/typos-cli/src/bin/typos-cli/args.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,11 @@ pub enum Format {
1313
}
1414

1515
impl Format {
16-
pub(crate) fn reporter(
17-
self,
18-
stdout_palette: crate::report::Palette,
19-
stderr_palette: crate::report::Palette,
20-
) -> Box<dyn typos_cli::report::Report> {
16+
pub(crate) fn reporter(self) -> Box<dyn typos_cli::report::Report> {
2117
match self {
2218
Format::Silent => Box::new(crate::report::PrintSilent),
23-
Format::Brief => Box::new(crate::report::PrintBrief {
24-
stdout_palette,
25-
stderr_palette,
26-
}),
27-
Format::Long => Box::new(crate::report::PrintLong {
28-
stdout_palette,
29-
stderr_palette,
30-
}),
19+
Format::Brief => Box::new(crate::report::PrintBrief),
20+
Format::Long => Box::new(crate::report::PrintLong),
3121
Format::Json => Box::new(crate::report::PrintJson),
3222
}
3323
}

crates/typos-cli/src/bin/typos-cli/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
255255
let output_reporter = if args.diff {
256256
Box::new(crate::report::PrintSilent)
257257
} else {
258-
let stdout_palette = report::Palette::colored();
259-
let stderr_palette = report::Palette::colored();
260-
args.format.reporter(stdout_palette, stderr_palette)
258+
args.format.reporter()
261259
};
262260
let status_reporter = report::MessageStatus::new(output_reporter.as_ref());
263261
let reporter: &dyn typos_cli::report::Report = &status_reporter;

crates/typos-cli/src/bin/typos-cli/report.rs

Lines changed: 37 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,9 @@ use unicode_width::UnicodeWidthStr;
88

99
use typos_cli::report::{Context, Message, Report, Typo};
1010

11-
#[derive(Copy, Clone, Debug, Default)]
12-
pub struct Palette {
13-
error: anstyle::Style,
14-
info: anstyle::Style,
15-
strong: anstyle::Style,
16-
}
17-
18-
impl Palette {
19-
pub fn colored() -> Self {
20-
Self {
21-
error: anstyle::AnsiColor::Red.on_default(),
22-
info: anstyle::AnsiColor::Blue.on_default(),
23-
strong: anstyle::Effects::BOLD.into(),
24-
}
25-
}
26-
27-
pub(crate) fn error<D: std::fmt::Display>(self, display: D) -> Styled<D> {
28-
Styled::new(display, self.error)
29-
}
30-
31-
pub(crate) fn info<D: std::fmt::Display>(self, display: D) -> Styled<D> {
32-
Styled::new(display, self.info)
33-
}
34-
35-
pub(crate) fn strong<D: std::fmt::Display>(self, display: D) -> Styled<D> {
36-
Styled::new(display, self.strong)
37-
}
38-
}
39-
40-
#[derive(Debug)]
41-
pub(crate) struct Styled<D> {
42-
display: D,
43-
style: anstyle::Style,
44-
}
45-
46-
impl<D: std::fmt::Display> Styled<D> {
47-
pub(crate) fn new(display: D, style: anstyle::Style) -> Self {
48-
Self { display, style }
49-
}
50-
}
51-
52-
impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
53-
#[inline]
54-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55-
if f.alternate() {
56-
write!(f, "{}", self.style.render())?;
57-
self.display.fmt(f)?;
58-
write!(f, "{}", self.style.render_reset())?;
59-
Ok(())
60-
} else {
61-
self.display.fmt(f)
62-
}
63-
}
64-
}
11+
const ERROR: anstyle::Style = anstyle::AnsiColor::Red.on_default();
12+
const INFO: anstyle::Style = anstyle::AnsiColor::Blue.on_default();
13+
const STRONG: anstyle::Style = anstyle::Style::new().effects(anstyle::Effects::BOLD);
6514

6615
pub struct MessageStatus<'r> {
6716
typos_found: atomic::AtomicBool,
@@ -108,18 +57,15 @@ impl Report for PrintSilent {
10857
}
10958
}
11059

111-
pub struct PrintBrief {
112-
pub stdout_palette: Palette,
113-
pub stderr_palette: Palette,
114-
}
60+
pub struct PrintBrief;
11561

11662
impl Report for PrintBrief {
11763
fn report(&self, msg: Message) -> Result<(), std::io::Error> {
11864
match &msg {
11965
Message::BinaryFile(msg) => {
12066
log::info!("{}", msg);
12167
}
122-
Message::Typo(msg) => print_brief_correction(msg, self.stdout_palette)?,
68+
Message::Typo(msg) => print_brief_correction(msg)?,
12369
Message::FileType(msg) => {
12470
writeln!(
12571
stdout().lock(),
@@ -143,18 +89,15 @@ impl Report for PrintBrief {
14389
}
14490
}
14591

146-
pub struct PrintLong {
147-
pub stdout_palette: Palette,
148-
pub stderr_palette: Palette,
149-
}
92+
pub struct PrintLong;
15093

15194
impl Report for PrintLong {
15295
fn report(&self, msg: Message) -> Result<(), std::io::Error> {
15396
match &msg {
15497
Message::BinaryFile(msg) => {
15598
log::info!("{}", msg);
15699
}
157-
Message::Typo(msg) => print_long_correction(msg, self.stdout_palette)?,
100+
Message::Typo(msg) => print_long_correction(msg)?,
158101
Message::FileType(msg) => {
159102
writeln!(
160103
stdout().lock(),
@@ -178,7 +121,11 @@ impl Report for PrintLong {
178121
}
179122
}
180123

181-
fn print_brief_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::Error> {
124+
fn print_brief_correction(msg: &Typo) -> Result<(), std::io::Error> {
125+
let info = INFO.render();
126+
let strong = STRONG.render();
127+
let reset = anstyle::Reset.render();
128+
182129
let start = String::from_utf8_lossy(&msg.buffer[0..msg.byte_offset]);
183130
let column_number =
184131
unicode_segmentation::UnicodeSegmentation::graphemes(start.as_ref(), true).count() + 1;
@@ -188,34 +135,32 @@ fn print_brief_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::E
188135
let divider = ":";
189136
writeln!(
190137
stdout().lock(),
191-
"{:#}{:#}{:#}: {:#}",
192-
palette.info(context_display(&msg.context)),
193-
palette.info(divider),
194-
palette.info(column_number),
195-
palette.strong(format_args!("`{}` is disallowed:", msg.typo)),
138+
"{info}{}{divider}{column_number}{reset}: {strong}`{}` is disallowed{reset}",
139+
context_display(&msg.context),
140+
msg.typo,
196141
)?;
197142
}
198143
typos::Status::Corrections(corrections) => {
199144
let divider = ":";
200145
writeln!(
201146
stdout().lock(),
202-
"{:#}{:#}{:#}: {:#}",
203-
palette.info(context_display(&msg.context)),
204-
palette.info(divider),
205-
palette.info(column_number),
206-
palette.strong(format_args!(
207-
"`{}` -> {}",
208-
msg.typo,
209-
itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ")
210-
)),
147+
"{info}{}{divider}{column_number}{reset}: {strong}`{}` -> {}{reset}",
148+
context_display(&msg.context),
149+
msg.typo,
150+
itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ")
211151
)?;
212152
}
213153
}
214154

215155
Ok(())
216156
}
217157

218-
fn print_long_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::Error> {
158+
fn print_long_correction(msg: &Typo) -> Result<(), std::io::Error> {
159+
let error = ERROR.render();
160+
let info = INFO.render();
161+
let strong = STRONG.render();
162+
let reset = anstyle::Reset.render();
163+
219164
let stdout = stdout();
220165
let mut handle = stdout.lock();
221166

@@ -229,53 +174,44 @@ fn print_long_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::Er
229174
typos::Status::Invalid => {
230175
writeln!(
231176
handle,
232-
"{:#}: {:#}",
233-
palette.error("error"),
234-
palette.strong(format_args!("`{}` is disallowed", msg.typo))
177+
"{error}error{reset}: {strong}`{}` is disallowed{reset}",
178+
msg.typo,
235179
)?;
236180
}
237181
typos::Status::Corrections(corrections) => {
238182
writeln!(
239183
handle,
240-
"{:#}: {:#}",
241-
palette.error("error"),
242-
palette.strong(format_args!(
243-
"`{}` should be {}",
244-
msg.typo,
245-
itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ")
246-
))
184+
"{error}error{reset}: {strong}`{}` should be {}{reset}",
185+
msg.typo,
186+
itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ")
247187
)?;
248188
}
249189
}
250190
let divider = ":";
251191
writeln!(
252192
handle,
253-
" --> {:#}{:#}{:#}",
254-
palette.info(context_display(&msg.context)),
255-
palette.info(divider),
256-
palette.info(column_number)
193+
"{info} --> {reset}{}{divider}{column_number}",
194+
context_display(&msg.context),
257195
)?;
258196

259197
if let Some(Context::File(context)) = &msg.context {
260198
let line_num = context.line_num.to_string();
261199
let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect();
200+
let line = line.trim_end();
262201

263202
let visible_column = calculate_visible_column_width(start.as_ref());
264203
let visible_len = calculate_visible_column_width(msg.typo);
265204

266205
let hl_indent: String = itertools::repeat_n(" ", visible_column).collect();
267206
let hl: String = itertools::repeat_n("^", visible_len).collect();
268207

269-
writeln!(handle, "{} |", line_indent)?;
270-
writeln!(handle, "{:#} | {}", palette.info(line_num), line.trim_end())?;
208+
writeln!(handle, "{info}{line_indent} |{reset}")?;
209+
writeln!(handle, "{info}{line_num} |{reset} {line}")?;
271210
writeln!(
272211
handle,
273-
"{} | {}{:#}",
274-
line_indent,
275-
hl_indent,
276-
palette.error(hl)
212+
"{info}{line_indent} |{reset} {hl_indent}{error}{hl}{reset}",
277213
)?;
278-
writeln!(handle, "{} |", line_indent)?;
214+
writeln!(handle, "{info}{line_indent} |{reset}")?;
279215
}
280216

281217
Ok(())

0 commit comments

Comments
 (0)