-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathprinter.rs
160 lines (140 loc) · 4.5 KB
/
printer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use ansi_term::Style;
use app::Config;
use diff::{LineChange, LineChanges};
use errors::*;
use std::io::Write;
use syntect::highlighting;
use terminal::as_terminal_escaped;
use Colors;
const PANEL_WIDTH: usize = 7;
pub struct Printer<'a> {
handle: &'a mut Write,
colors: Colors,
config: &'a Config<'a>,
pub line_changes: Option<LineChanges>,
}
impl<'a> Printer<'a> {
pub fn new(handle: &'a mut Write, config: &'a Config) -> Self {
let colors = if config.colored_output {
Colors::colored()
} else {
Colors::plain()
};
Printer {
handle,
colors,
config,
line_changes: None,
}
}
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
if !self.config.output_components.header() {
return Ok(());
}
if self.config.output_components.grid() {
self.print_horizontal_line('┬')?;
write!(
self.handle,
"{}{} ",
" ".repeat(PANEL_WIDTH),
self.colors.grid.paint("│"),
)?;
}
writeln!(
self.handle,
"{}{}",
filename.map_or("", |_| "File: "),
self.colors.filename.paint(filename.unwrap_or("STDIN"))
)?;
if self.config.output_components.grid() {
self.print_horizontal_line('┼')?;
}
Ok(())
}
pub fn print_footer(&mut self) -> Result<()> {
if self.config.output_components.grid() {
self.print_horizontal_line('┴')
} else {
Ok(())
}
}
pub fn print_line(
&mut self,
line_number: usize,
regions: &[(highlighting::Style, &str)],
) -> Result<()> {
let decorations = vec![
self.print_line_number(line_number),
self.print_git_marker(line_number),
self.print_line_border(),
Some(as_terminal_escaped(
®ions,
self.config.true_color,
self.config.colored_output,
)),
];
let grid_requested = self.config.output_components.grid();
write!(
self.handle,
"{}",
decorations
.into_iter()
.filter_map(|dec| if grid_requested {
Some(dec.unwrap_or_else(|| " ".to_owned()))
} else {
dec
})
.collect::<Vec<_>>()
.join(" ")
)?;
Ok(())
}
fn print_line_number(&self, line_number: usize) -> Option<String> {
if self.config.output_components.numbers() {
Some(
self.colors
.line_number
.paint(format!("{:4}", line_number))
.to_string(),
)
} else if self.config.output_components.grid() {
Some(" ".to_owned())
} else {
None
}
}
fn print_git_marker(&self, line_number: usize) -> Option<String> {
if self.config.output_components.changes() {
Some(
if let Some(ref changes) = self.line_changes {
match changes.get(&(line_number as u32)) {
Some(&LineChange::Added) => self.colors.git_added.paint("+"),
Some(&LineChange::RemovedAbove) => self.colors.git_removed.paint("‾"),
Some(&LineChange::RemovedBelow) => self.colors.git_removed.paint("_"),
Some(&LineChange::Modified) => self.colors.git_modified.paint("~"),
_ => Style::default().paint(" "),
}
} else {
Style::default().paint(" ")
}.to_string(),
)
} else if self.config.output_components.grid() {
Some(" ".to_owned())
} else {
None
}
}
fn print_line_border(&self) -> Option<String> {
if self.config.output_components.grid() {
Some(self.colors.grid.paint("│").to_string())
} else {
None
}
}
fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
let hline = "─".repeat(self.config.term_width - (PANEL_WIDTH + 1));
let hline = format!("{}{}{}", "─".repeat(PANEL_WIDTH), grid_char, hline);
writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
Ok(())
}
}