Skip to content

Commit 41b14b6

Browse files
authored
Merge pull request #2353 from davidalber/merge-print-diff
Consolidating the logic for printing output
2 parents 1abbd74 + 9f5f9d2 commit 41b14b6

File tree

2 files changed

+44
-65
lines changed

2 files changed

+44
-65
lines changed

src/rustfmt_diff.rs

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@ impl Mismatch {
3737
}
3838
}
3939

40+
// This struct handles writing output to stdout and abstracts away the logic
41+
// of printing in color, if it's possible in the executing environment.
42+
pub struct OutputWriter {
43+
terminal: Option<Box<term::Terminal<Output = io::Stdout>>>,
44+
}
45+
46+
impl OutputWriter {
47+
// Create a new OutputWriter instance based on the caller's preference
48+
// for colorized output and the capabilities of the terminal.
49+
pub fn new(color: Color) -> Self {
50+
if let Some(t) = term::stdout() {
51+
if use_colored_tty(color) && t.supports_color() {
52+
return OutputWriter { terminal: Some(t) };
53+
}
54+
}
55+
OutputWriter { terminal: None }
56+
}
57+
58+
// Write output in the optionally specified color. The output is written
59+
// in the specified color if this OutputWriter instance contains a
60+
// Terminal in its `terminal` field.
61+
pub fn writeln(&mut self, msg: &str, color: Option<term::color::Color>) {
62+
match &mut self.terminal {
63+
Some(ref mut t) => {
64+
if let Some(color) = color {
65+
t.fg(color).unwrap();
66+
}
67+
writeln!(t, "{}", msg).unwrap();
68+
if color.is_some() {
69+
t.reset().unwrap();
70+
}
71+
}
72+
None => println!("{}", msg),
73+
}
74+
}
75+
}
76+
4077
// Produces a diff between the expected output and actual output of rustfmt.
4178
pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
4279
let mut line_number = 1;
@@ -97,80 +134,24 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
97134
results
98135
}
99136

100-
// A representation of how to write output.
101-
pub enum PrintType {
102-
Fancy, // want to output color and the terminal supports it
103-
Basic, // do not want to output color or the terminal does not support color
104-
}
105-
106-
impl PrintType {
107-
pub fn get(color: Color) -> Self {
108-
match term::stdout() {
109-
Some(ref t) if use_colored_tty(color) && t.supports_color() => PrintType::Fancy,
110-
_ => PrintType::Basic,
111-
}
112-
}
113-
}
114-
115137
pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, color: Color)
116138
where
117139
F: Fn(u32) -> String,
118140
{
119-
match PrintType::get(color) {
120-
PrintType::Fancy => print_diff_fancy(diff, get_section_title, term::stdout().unwrap()),
121-
PrintType::Basic => print_diff_basic(diff, get_section_title),
122-
}
123-
}
124-
125-
fn print_diff_fancy<F>(
126-
diff: Vec<Mismatch>,
127-
get_section_title: F,
128-
mut t: Box<term::Terminal<Output = io::Stdout>>,
129-
) where
130-
F: Fn(u32) -> String,
131-
{
132-
for mismatch in diff {
133-
let title = get_section_title(mismatch.line_number);
134-
writeln!(t, "{}", title).unwrap();
135-
136-
for line in mismatch.lines {
137-
match line {
138-
DiffLine::Context(ref str) => {
139-
t.reset().unwrap();
140-
writeln!(t, " {}⏎", str).unwrap();
141-
}
142-
DiffLine::Expected(ref str) => {
143-
t.fg(term::color::GREEN).unwrap();
144-
writeln!(t, "+{}⏎", str).unwrap();
145-
}
146-
DiffLine::Resulting(ref str) => {
147-
t.fg(term::color::RED).unwrap();
148-
writeln!(t, "-{}⏎", str).unwrap();
149-
}
150-
}
151-
}
152-
t.reset().unwrap();
153-
}
154-
}
141+
let mut writer = OutputWriter::new(color);
155142

156-
pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
157-
where
158-
F: Fn(u32) -> String,
159-
{
160143
for mismatch in diff {
161144
let title = get_section_title(mismatch.line_number);
162-
println!("{}", title);
145+
writer.writeln(&format!("{}", title), None);
163146

164147
for line in mismatch.lines {
165148
match line {
166-
DiffLine::Context(ref str) => {
167-
println!(" {}⏎", str);
168-
}
149+
DiffLine::Context(ref str) => writer.writeln(&format!(" {}⏎", str), None),
169150
DiffLine::Expected(ref str) => {
170-
println!("+{}⏎", str);
151+
writer.writeln(&format!("+{}⏎", str), Some(term::color::GREEN))
171152
}
172153
DiffLine::Resulting(ref str) => {
173-
println!("-{}⏎", str);
154+
writer.writeln(&format!("-{}⏎", str), Some(term::color::RED))
174155
}
175156
}
176157
}

tests/system.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ fn verify_config_test_names() {
106106
// using only one or the other will cause the output order to differ when
107107
// `print_diff` selects the approach not used.
108108
fn write_message(msg: String) {
109-
match PrintType::get(Color::Auto) {
110-
PrintType::Fancy => writeln!(term::stdout().unwrap(), "{}", msg).unwrap(),
111-
PrintType::Basic => println!("{}", msg),
112-
}
109+
let mut writer = OutputWriter::new(Color::Auto);
110+
writer.writeln(&format!("{}", msg), None);
113111
}
114112

115113
// Integration tests. The files in the tests/source are formatted and compared

0 commit comments

Comments
 (0)