5use syntect::parsing::SyntaxReference;
8 assets::HighlightingAssets,
9 config::{Config, VisibleLines},
10 controller::Controller,
13 line_range::{HighlightedLineRanges, LineRange, LineRanges},
14 style::{StyleComponent, StyleComponents},
15 SyntaxMapping, WrappingMode,
18#[cfg(feature = "paging")]
19use crate::paging::PagingMode;
22struct ActiveStyleComponents {
24 vcs_modification_markers: bool,
31pub struct PrettyPrinter<'a> {
32 inputs: Vec<Input<'a>>,
34 assets: HighlightingAssets,
36 highlighted_lines: Vec<LineRange>,
37 term_width: Option<usize>,
38 active_style_components: ActiveStyleComponents,
41impl<'a> PrettyPrinter<'a> {
42 pub fn new() -> Self {
43 let mut config = Config::default();
45 config.colored_output = true;
46 config.true_color = true;
51 assets: HighlightingAssets::from_binary(),
53 highlighted_lines: vec![],
55 active_style_components: ActiveStyleComponents::default(),
59 /// Add an input which should be pretty-printed
60 pub fn input(&mut self, input: Input<'a>) -> &mut Self {
61 self.inputs.push(input);
65 /// Adds multiple inputs which should be pretty-printed
66 pub fn inputs(&mut self, inputs: impl IntoIterator<Item = Input<'a>>) -> &mut Self {
68 self.inputs.push(input);
73 /// Add a file which should be pretty-printed
74 pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
75 self.input(Input::from_file(path).kind("File"))
78 /// Add multiple files which should be pretty-printed
79 pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
81 I: IntoIterator<Item = P>,
84 self.inputs(paths.into_iter().map(Input::from_file))
87 /// Add STDIN as an input
88 pub fn input_stdin(&mut self) -> &mut Self {
89 self.inputs.push(Input::from_stdin());
93 /// Add STDIN as an input (with customized name)
95 pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
97 .push(Input::from_stdin().name(name).kind("File"));
101 /// Add a byte string as an input
102 pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
103 self.input_from_reader(content)
106 /// Add a byte string as an input (with customized name)
109 pub fn input_from_bytes_with_name(
112 name: impl AsRef<OsStr>,
114 self.input_from_reader_with_name(content, name)
117 /// Add a custom reader as an input
118 pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
119 self.inputs.push(Input::from_reader(reader));
123 /// Add a custom reader as an input (with customized name)
125 pub fn input_from_reader_with_name<R: Read + 'a>(
128 name: impl AsRef<OsStr>,
131 .push(Input::from_reader(reader).name(name).kind("File"));
135 /// Specify the syntax file which should be used (default: auto-detect)
136 pub fn language(&mut self, language: &'a str) -> &mut Self {
137 self.config.language = Some(language);
141 /// The character width of the terminal (default: autodetect)
142 pub fn term_width(&mut self, width: usize) -> &mut Self {
143 self.term_width = Some(width);
147 /// The width of tab characters (default: None - do not turn tabs to spaces)
148 pub fn tab_width(&mut self, tab_width: Option<usize>) -> &mut Self {
149 self.config.tab_width = tab_width.unwrap_or(0);
153 /// Whether or not the output should be colorized (default: true)
154 pub fn colored_output(&mut self, yes: bool) -> &mut Self {
155 self.config.colored_output = yes;
159 /// Whether or not to output 24bit colors (default: true)
160 pub fn true_color(&mut self, yes: bool) -> &mut Self {
161 self.config.true_color = yes;
165 /// Whether to show a header with the file name
166 pub fn header(&mut self, yes: bool) -> &mut Self {
167 self.active_style_components.header = yes;
171 /// Whether to show line numbers
172 pub fn line_numbers(&mut self, yes: bool) -> &mut Self {
173 self.active_style_components.line_numbers = yes;
177 /// Whether to paint a grid, separating line numbers, git changes and the code
178 pub fn grid(&mut self, yes: bool) -> &mut Self {
179 self.active_style_components.grid = yes;
183 /// Whether to paint a horizontal rule to delimit files
184 pub fn rule(&mut self, yes: bool) -> &mut Self {
185 self.active_style_components.rule = yes;
189 /// Whether to show modification markers for VCS changes. This has no effect if
190 /// the `git` feature is not activated.
192 not(feature = "git"),
194 note = "Using vcs_modification_markers without the 'git' feature has no effect. \
195 The function will be removed (for non-'git' use cases) in the future."
198 #[allow(unused_variables)]
199 pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self {
200 #[cfg(feature = "git")]
202 self.active_style_components.vcs_modification_markers = yes;
207 /// Whether to show "snip" markers between visible line ranges (default: no)
208 pub fn snip(&mut self, yes: bool) -> &mut Self {
209 self.active_style_components.snip = yes;
213 /// Text wrapping mode (default: do not wrap)
214 pub fn wrapping_mode(&mut self, mode: WrappingMode) -> &mut Self {
215 self.config.wrapping_mode = mode;
219 /// Whether or not to use ANSI italics (default: off)
220 pub fn use_italics(&mut self, yes: bool) -> &mut Self {
221 self.config.use_italic_text = yes;
225 /// If and how to use a pager (default: no paging)
226 #[cfg(feature = "paging")]
227 pub fn paging_mode(&mut self, mode: PagingMode) -> &mut Self {
228 self.config.paging_mode = mode;
232 /// Specify the command to start the pager (default: use "less")
233 #[cfg(feature = "paging")]
234 pub fn pager(&mut self, cmd: &'a str) -> &mut Self {
235 self.config.pager = Some(cmd);
239 /// Specify the lines that should be printed (default: all)
240 pub fn line_ranges(&mut self, ranges: LineRanges) -> &mut Self {
241 self.config.visible_lines = VisibleLines::Ranges(ranges);
245 /// Specify a line that should be highlighted (default: none).
246 /// This can be called multiple times to highlight more than one
247 /// line. See also: highlight_range.
248 pub fn highlight(&mut self, line: usize) -> &mut Self {
249 self.highlighted_lines.push(LineRange::new(line, line));
253 /// Specify a range of lines that should be highlighted (default: none).
254 /// This can be called multiple times to highlight more than one range
256 pub fn highlight_range(&mut self, from: usize, to: usize) -> &mut Self {
257 self.highlighted_lines.push(LineRange::new(from, to));
261 /// Specify the highlighting theme
262 pub fn theme(&mut self, theme: impl AsRef<str>) -> &mut Self {
263 self.config.theme = theme.as_ref().to_owned();
267 /// Specify custom file extension / file name to syntax mappings
268 pub fn syntax_mapping(&mut self, mapping: SyntaxMapping<'a>) -> &mut Self {
269 self.config.syntax_mapping = mapping;
273 pub fn themes(&self) -> impl Iterator<Item = &str> {
277 pub fn syntaxes(&self) -> impl Iterator<Item = &SyntaxReference> {
278 self.assets.syntaxes().iter()
281 /// Pretty-print all specified inputs. This method will "use" all stored inputs.
282 /// If you want to call 'print' multiple times, you have to call the appropriate
283 /// input_* methods again.
284 pub fn print(&mut self) -> Result<bool> {
285 self.config.highlighted_lines =
286 HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
287 self.config.term_width = self
289 .unwrap_or_else(|| Term::stdout().size().1 as usize);
291 let mut style_components = vec![];
292 if self.active_style_components.grid {
293 style_components.push(StyleComponent::Grid);
295 if self.active_style_components.rule {
296 style_components.push(StyleComponent::Rule);
298 if self.active_style_components.header {
299 style_components.push(StyleComponent::Header);
301 if self.active_style_components.line_numbers {
302 style_components.push(StyleComponent::LineNumbers);
304 if self.active_style_components.snip {
305 style_components.push(StyleComponent::Snip);
307 if self.active_style_components.vcs_modification_markers {
308 #[cfg(feature = "git")]
309 style_components.push(StyleComponent::Changes);
311 self.config.style_components = StyleComponents::new(&style_components);
313 // Collect the inputs to print
314 let mut inputs: Vec<Input> = vec![];
315 std::mem::swap(&mut inputs, &mut self.inputs);
317 // Run the controller
318 let controller = Controller::new(&self.config, &self.assets);
319 controller.run(inputs.into_iter().map(|i| i.into()).collect())
323/// An input source for the pretty printer.
324pub struct Input<'a> {
325 input: input::Input<'a>,
329 /// A new input from a reader.
330 pub fn from_reader<R: Read + 'a>(reader: R) -> Self {
331 input::Input::from_reader(Box::new(reader)).into()
334 /// A new input from a file.
335 pub fn from_file(path: impl AsRef<OsStr>) -> Self {
336 input::Input::ordinary_file(path.as_ref()).into()
339 /// A new input from bytes.
340 pub fn from_bytes(bytes: &'a [u8]) -> Self {
341 Input::from_reader(bytes).into()
344 /// A new input from STDIN.
345 pub fn from_stdin() -> Self {
346 input::Input::stdin().into()
349 /// The filename of the input.
350 /// This affects syntax detection and changes the default header title.
351 pub fn name(mut self, name: impl AsRef<OsStr>) -> Self {
352 self.input = self.input.with_name(Some(name.as_ref()));
356 /// The description for the type of input (e.g. "File")
357 pub fn kind(mut self, kind: impl Into<String>) -> Self {
358 let kind = kind.into();
361 .set_kind(if kind.is_empty() { None } else { Some(kind) });
365 /// The title for the input (e.g. "http://example.com/example.txt")
366 /// This defaults to the file name.
367 pub fn title(mut self, title: impl Into<String>) -> Self {
368 self.input.description_mut().set_title(Some(title.into()));
373impl<'a> Into<Input<'a>> for input::Input<'a> {
374 fn into(self) -> Input<'a> {
375 Input { input: self }
379impl<'a> Into<input::Input<'a>> for Input<'a> {
380 fn into(self) -> input::Input<'a> {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list