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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::io::Read;
use std::path::Path;
use console::Term;
use crate::{
assets::HighlightingAssets,
config::{Config, VisibleLines},
controller::Controller,
error::Result,
input,
line_range::{HighlightedLineRanges, LineRange, LineRanges},
style::{StyleComponent, StyleComponents},
SyntaxMapping, WrappingMode,
};
#[cfg(feature = "paging")]
use crate::paging::PagingMode;
#[derive(Default)]
struct ActiveStyleComponents {
header: bool,
vcs_modification_markers: bool,
grid: bool,
rule: bool,
line_numbers: bool,
snip: bool,
}
#[non_exhaustive]
pub struct Syntax {
pub name: String,
pub file_extensions: Vec<String>,
}
pub struct PrettyPrinter<'a> {
inputs: Vec<Input<'a>>,
config: Config<'a>,
assets: HighlightingAssets,
highlighted_lines: Vec<LineRange>,
term_width: Option<usize>,
active_style_components: ActiveStyleComponents,
}
impl<'a> PrettyPrinter<'a> {
pub fn new() -> Self {
let config = Config {
colored_output: true,
true_color: true,
..Default::default()
};
PrettyPrinter {
inputs: vec![],
config,
assets: HighlightingAssets::from_binary(),
highlighted_lines: vec![],
term_width: None,
active_style_components: ActiveStyleComponents::default(),
}
}
pub fn input(&mut self, input: Input<'a>) -> &mut Self {
self.inputs.push(input);
self
}
pub fn inputs(&mut self, inputs: impl IntoIterator<Item = Input<'a>>) -> &mut Self {
for input in inputs {
self.inputs.push(input);
}
self
}
pub fn input_file(&mut self, path: impl AsRef<Path>) -> &mut Self {
self.input(Input::from_file(path).kind("File"))
}
pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
where
I: IntoIterator<Item = P>,
P: AsRef<Path>,
{
self.inputs(paths.into_iter().map(Input::from_file))
}
pub fn input_stdin(&mut self) -> &mut Self {
self.inputs.push(Input::from_stdin());
self
}
pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
self.input_from_reader(content)
}
pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
self.inputs.push(Input::from_reader(reader));
self
}
pub fn language(&mut self, language: &'a str) -> &mut Self {
self.config.language = Some(language);
self
}
pub fn term_width(&mut self, width: usize) -> &mut Self {
self.term_width = Some(width);
self
}
pub fn tab_width(&mut self, tab_width: Option<usize>) -> &mut Self {
self.config.tab_width = tab_width.unwrap_or(0);
self
}
pub fn colored_output(&mut self, yes: bool) -> &mut Self {
self.config.colored_output = yes;
self
}
pub fn true_color(&mut self, yes: bool) -> &mut Self {
self.config.true_color = yes;
self
}
pub fn header(&mut self, yes: bool) -> &mut Self {
self.active_style_components.header = yes;
self
}
pub fn line_numbers(&mut self, yes: bool) -> &mut Self {
self.active_style_components.line_numbers = yes;
self
}
pub fn grid(&mut self, yes: bool) -> &mut Self {
self.active_style_components.grid = yes;
self
}
pub fn rule(&mut self, yes: bool) -> &mut Self {
self.active_style_components.rule = yes;
self
}
#[cfg(feature = "git")]
pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self {
self.active_style_components.vcs_modification_markers = yes;
self
}
pub fn show_nonprintable(&mut self, yes: bool) -> &mut Self {
self.config.show_nonprintable = yes;
self
}
pub fn snip(&mut self, yes: bool) -> &mut Self {
self.active_style_components.snip = yes;
self
}
pub fn wrapping_mode(&mut self, mode: WrappingMode) -> &mut Self {
self.config.wrapping_mode = mode;
self
}
pub fn use_italics(&mut self, yes: bool) -> &mut Self {
self.config.use_italic_text = yes;
self
}
#[cfg(feature = "paging")]
pub fn paging_mode(&mut self, mode: PagingMode) -> &mut Self {
self.config.paging_mode = mode;
self
}
#[cfg(feature = "paging")]
pub fn pager(&mut self, cmd: &'a str) -> &mut Self {
self.config.pager = Some(cmd);
self
}
pub fn line_ranges(&mut self, ranges: LineRanges) -> &mut Self {
self.config.visible_lines = VisibleLines::Ranges(ranges);
self
}
pub fn highlight(&mut self, line: usize) -> &mut Self {
self.highlighted_lines.push(LineRange::new(line, line));
self
}
pub fn highlight_range(&mut self, from: usize, to: usize) -> &mut Self {
self.highlighted_lines.push(LineRange::new(from, to));
self
}
pub fn theme(&mut self, theme: impl AsRef<str>) -> &mut Self {
self.config.theme = theme.as_ref().to_owned();
self
}
pub fn syntax_mapping(&mut self, mapping: SyntaxMapping<'a>) -> &mut Self {
self.config.syntax_mapping = mapping;
self
}
pub fn themes(&self) -> impl Iterator<Item = &str> {
self.assets.themes()
}
pub fn syntaxes(&self) -> impl Iterator<Item = Syntax> + '_ {
self.assets
.get_syntaxes()
.unwrap()
.iter()
.filter(|s| !s.hidden)
.map(|s| Syntax {
name: s.name.clone(),
file_extensions: s.file_extensions.clone(),
})
}
pub fn print(&mut self) -> Result<bool> {
let highlight_lines = std::mem::take(&mut self.highlighted_lines);
self.config.highlighted_lines = HighlightedLineRanges(LineRanges::from(highlight_lines));
self.config.term_width = self
.term_width
.unwrap_or_else(|| Term::stdout().size().1 as usize);
let mut style_components = vec![];
if self.active_style_components.grid {
style_components.push(StyleComponent::Grid);
}
if self.active_style_components.rule {
style_components.push(StyleComponent::Rule);
}
if self.active_style_components.header {
style_components.push(StyleComponent::Header);
}
if self.active_style_components.line_numbers {
style_components.push(StyleComponent::LineNumbers);
}
if self.active_style_components.snip {
style_components.push(StyleComponent::Snip);
}
if self.active_style_components.vcs_modification_markers {
#[cfg(feature = "git")]
style_components.push(StyleComponent::Changes);
}
self.config.style_components = StyleComponents::new(&style_components);
let mut inputs: Vec<Input> = vec![];
std::mem::swap(&mut inputs, &mut self.inputs);
let controller = Controller::new(&self.config, &self.assets);
controller.run(inputs.into_iter().map(|i| i.into()).collect())
}
}
impl Default for PrettyPrinter<'_> {
fn default() -> Self {
Self::new()
}
}
pub struct Input<'a> {
input: input::Input<'a>,
}
impl<'a> Input<'a> {
pub fn from_reader<R: Read + 'a>(reader: R) -> Self {
input::Input::from_reader(Box::new(reader)).into()
}
pub fn from_file(path: impl AsRef<Path>) -> Self {
input::Input::ordinary_file(path).into()
}
pub fn from_bytes(bytes: &'a [u8]) -> Self {
Input::from_reader(bytes)
}
pub fn from_stdin() -> Self {
input::Input::stdin().into()
}
pub fn name(mut self, name: impl AsRef<Path>) -> Self {
self.input = self.input.with_name(Some(name));
self
}
pub fn kind(mut self, kind: impl Into<String>) -> Self {
let kind = kind.into();
self.input
.description_mut()
.set_kind(if kind.is_empty() { None } else { Some(kind) });
self
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.input.description_mut().set_title(Some(title.into()));
self
}
}
impl<'a> From<input::Input<'a>> for Input<'a> {
fn from(input: input::Input<'a>) -> Self {
Self { input }
}
}
impl<'a> From<Input<'a>> for input::Input<'a> {
fn from(Input { input }: Input<'a>) -> Self {
input
}
}