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,
30pub struct PrettyPrinter<'a> {
31 inputs: Vec<Input<'a>>,
33 assets: HighlightingAssets,
35 highlighted_lines: Vec<LineRange>,
36 term_width: Option<usize>,
37 active_style_components: ActiveStyleComponents,
40impl<'a> PrettyPrinter<'a> {
41 pub fn new() -> Self {
42 let mut config = Config::default();
44 config.colored_output = true;
45 config.true_color = true;
50 assets: HighlightingAssets::from_binary(),
52 highlighted_lines: vec![],
54 active_style_components: ActiveStyleComponents::default(),
58 /// Add a file which should be pretty-printed
59 pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
60 self.inputs.push(Input::ordinary_file(path.as_ref()));
64 /// Add multiple files which should be pretty-printed
65 pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
67 I: IntoIterator<Item = P>,
71 self.inputs.push(Input::ordinary_file(path.as_ref()));
76 /// Add STDIN as an input
77 pub fn input_stdin(&mut self) -> &mut Self {
78 self.inputs.push(Input::stdin());
82 /// Add STDIN as an input (with customized name)
83 pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
85 .push(Input::stdin().with_name(Some(name.as_ref())));
89 /// Add a byte string as an input
90 pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
91 self.input_from_reader(content)
94 /// Add a byte string as an input (with customized name)
95 pub fn input_from_bytes_with_name(
98 name: impl AsRef<OsStr>,
100 self.input_from_reader_with_name(content, name)
103 /// Add a custom reader as an input
104 pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
105 self.inputs.push(Input::from_reader(Box::new(reader)));
109 /// Add a custom reader as an input (with customized name)
110 pub fn input_from_reader_with_name<R: Read + 'a>(
113 name: impl AsRef<OsStr>,
116 .push(Input::from_reader(Box::new(reader)).with_name(Some(name.as_ref())));
120 /// Specify the syntax file which should be used (default: auto-detect)
121 pub fn language(&mut self, language: &'a str) -> &mut Self {
122 self.config.language = Some(language);
126 /// The character width of the terminal (default: autodetect)
127 pub fn term_width(&mut self, width: usize) -> &mut Self {
128 self.term_width = Some(width);
132 /// The width of tab characters (default: None - do not turn tabs to spaces)
133 pub fn tab_width(&mut self, tab_width: Option<usize>) -> &mut Self {
134 self.config.tab_width = tab_width.unwrap_or(0);
138 /// Whether or not the output should be colorized (default: true)
139 pub fn colored_output(&mut self, yes: bool) -> &mut Self {
140 self.config.colored_output = yes;
144 /// Whether or not to output 24bit colors (default: true)
145 pub fn true_color(&mut self, yes: bool) -> &mut Self {
146 self.config.true_color = yes;
150 /// Whether to show a header with the file name
151 pub fn header(&mut self, yes: bool) -> &mut Self {
152 self.active_style_components.header = yes;
156 /// Whether to show line numbers
157 pub fn line_numbers(&mut self, yes: bool) -> &mut Self {
158 self.active_style_components.line_numbers = yes;
162 /// Whether to paint a grid, separating line numbers, git changes and the code
163 pub fn grid(&mut self, yes: bool) -> &mut Self {
164 self.active_style_components.grid = yes;
168 /// Whether to show modification markers for VCS changes
169 pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self {
170 self.active_style_components.vcs_modification_markers = yes;
174 /// Whether to show "snip" markers between visible line ranges (default: no)
175 pub fn snip(&mut self, yes: bool) -> &mut Self {
176 self.active_style_components.snip = yes;
180 /// Text wrapping mode (default: do not wrap)
181 pub fn wrapping_mode(&mut self, mode: WrappingMode) -> &mut Self {
182 self.config.wrapping_mode = mode;
186 /// Whether or not to use ANSI italics (default: off)
187 pub fn use_italics(&mut self, yes: bool) -> &mut Self {
188 self.config.use_italic_text = yes;
192 /// If and how to use a pager (default: no paging)
193 #[cfg(feature = "paging")]
194 pub fn paging_mode(&mut self, mode: PagingMode) -> &mut Self {
195 self.config.paging_mode = mode;
199 /// Specify the command to start the pager (default: use "less")
200 #[cfg(feature = "paging")]
201 pub fn pager(&mut self, cmd: &'a str) -> &mut Self {
202 self.config.pager = Some(cmd);
206 /// Specify the lines that should be printed (default: all)
207 pub fn line_ranges(&mut self, ranges: LineRanges) -> &mut Self {
208 self.config.visible_lines = VisibleLines::Ranges(ranges);
212 /// Specify a line that should be highlighted (default: none).
213 /// This can be called multiple times to highlight more than one
214 /// line. See also: highlight_range.
215 pub fn highlight(&mut self, line: usize) -> &mut Self {
216 self.highlighted_lines.push(LineRange::new(line, line));
220 /// Specify a range of lines that should be highlighted (default: none).
221 /// This can be called multiple times to highlight more than one range
223 pub fn highlight_range(&mut self, from: usize, to: usize) -> &mut Self {
224 self.highlighted_lines.push(LineRange::new(from, to));
228 /// Specify the highlighting theme
229 pub fn theme(&mut self, theme: impl AsRef<str>) -> &mut Self {
230 self.config.theme = theme.as_ref().to_owned();
234 /// Specify custom file extension / file name to syntax mappings
235 pub fn syntax_mapping(&mut self, mapping: SyntaxMapping<'a>) -> &mut Self {
236 self.config.syntax_mapping = mapping;
240 pub fn themes(&self) -> impl Iterator<Item = &str> {
244 pub fn syntaxes(&self) -> impl Iterator<Item = &SyntaxReference> {
245 self.assets.syntaxes().iter()
248 /// Pretty-print all specified inputs. This method will "use" all stored inputs.
249 /// If you want to call 'print' multiple times, you have to call the appropriate
250 /// input_* methods again.
251 pub fn print(&mut self) -> Result<bool> {
252 self.config.highlighted_lines =
253 HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
254 self.config.term_width = self
256 .unwrap_or_else(|| Term::stdout().size().1 as usize);
258 let mut style_components = vec![];
259 if self.active_style_components.grid {
260 style_components.push(StyleComponent::Grid);
262 if self.active_style_components.header {
263 style_components.push(StyleComponent::Header);
265 if self.active_style_components.line_numbers {
266 style_components.push(StyleComponent::LineNumbers);
268 if self.active_style_components.snip {
269 style_components.push(StyleComponent::Snip);
271 if self.active_style_components.vcs_modification_markers {
272 style_components.push(StyleComponent::Changes);
274 self.config.style_components = StyleComponents::new(&style_components);
276 let mut inputs: Vec<Input> = vec![];
277 std::mem::swap(&mut inputs, &mut self.inputs);
279 let controller = Controller::new(&self.config, &self.assets);
280 controller.run(inputs)
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