5use ansi_term::Colour::{Fixed, Green, Red, Yellow};
8use console::AnsiCodeIterator;
10use syntect::easy::HighlightLines;
11use syntect::highlighting::Theme;
13use app::{Config, InputFile};
14use assets::HighlightingAssets;
15use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
16use diff::get_git_diff;
20use terminal::{as_terminal_escaped, to_ansi_color};
23 fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()>;
24 fn print_footer(&mut self, handle: &mut Write) -> Result<()>;
34pub struct SimplePrinter;
37 pub fn new() -> Self {
42impl Printer for SimplePrinter {
43 fn print_header(&mut self, _handle: &mut Write, _file: InputFile) -> Result<()> {
47 fn print_footer(&mut self, _handle: &mut Write) -> Result<()> {
59 handle.write(line_buffer)?;
65pub struct InteractivePrinter<'a> {
67 config: &'a Config<'a>,
68 decorations: Vec<Box<Decoration>>,
70 ansi_prefix_sgr: String,
71 pub line_changes: Option<LineChanges>,
72 highlighter: HighlightLines<'a>,
75impl<'a> InteractivePrinter<'a> {
76 pub fn new(config: &'a Config, assets: &'a HighlightingAssets, file: InputFile) -> Self {
77 let theme = assets.get_theme(&config.theme);
79 let colors = if config.colored_output {
80 Colors::colored(theme, config.true_color)
85 // Create decorations.
86 let mut decorations: Vec<Box<Decoration>> = Vec::new();
88 if config.output_components.numbers() {
89 decorations.push(Box::new(LineNumberDecoration::new(&colors)));
92 if config.output_components.changes() {
93 decorations.push(Box::new(LineChangesDecoration::new(&colors)));
96 let mut panel_width: usize =
97 decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
99 // The grid border decoration isn't added until after the panel_width calculation, since the
100 // print_horizontal_line, print_header, and print_footer functions all assume the panel
101 // width is without the grid border.
102 if config.output_components.grid() && !decorations.is_empty() {
103 decorations.push(Box::new(GridBorderDecoration::new(&colors)));
106 // Disable the panel if the terminal is too small (i.e. can't fit 5 characters with the
109 < (decorations.len() + decorations.iter().fold(0, |a, x| a + x.width())) + 5
115 // Get the Git modifications
116 let line_changes = match file {
117 InputFile::Ordinary(filename) => get_git_diff(filename),
121 // Determine the type of syntax for highlighting
122 let syntax = assets.get_syntax(config.language, file);
123 let highlighter = HighlightLines::new(syntax, theme);
130 ansi_prefix_sgr: String::new(),
136 fn print_horizontal_line(&mut self, handle: &mut Write, grid_char: char) -> Result<()> {
137 if self.panel_width == 0 {
141 self.colors.grid.paint("─".repeat(self.config.term_width))
144 let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
145 let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
146 writeln!(handle, "{}", self.colors.grid.paint(hline))?;
153impl<'a> Printer for InteractivePrinter<'a> {
154 fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()> {
155 if !self.config.output_components.header() {
159 if self.config.output_components.grid() {
160 self.print_horizontal_line(handle, '┬')?;
165 " ".repeat(self.panel_width),
168 .paint(if self.panel_width > 0 { "│ " } else { "" }),
171 write!(handle, "{}", " ".repeat(self.panel_width))?;
174 let (prefix, name) = match file {
175 InputFile::Ordinary(filename) => ("File: ", filename),
179 writeln!(handle, "{}{}", prefix, self.colors.filename.paint(name))?;
181 if self.config.output_components.grid() {
182 self.print_horizontal_line(handle, '┼')?;
188 fn print_footer(&mut self, handle: &mut Write) -> Result<()> {
189 if self.config.output_components.grid() {
190 self.print_horizontal_line(handle, '┴')
203 let line = String::from_utf8_lossy(&line_buffer);
204 let regions = self.highlighter.highlight(line.as_ref());
210 let mut cursor: usize = 0;
211 let mut cursor_max: usize = self.config.term_width;
212 let mut panel_wrap: Option<String> = None;
215 if self.panel_width > 0 {
216 let decorations = self
219 .map(|ref d| d.generate(line_number, false, self))
220 .collect::<Vec<_>>();
222 for deco in decorations {
223 write!(handle, "{} ", deco.text)?;
224 cursor_max -= deco.width + 1;
229 if self.config.output_wrap == OutputWrap::None {
230 let true_color = self.config.true_color;
231 let colored_output = self.config.colored_output;
238 .map(|&(style, text)| as_terminal_escaped(
243 )).collect::<Vec<_>>()
247 for &(style, region) in regions.iter() {
248 let mut ansi_iterator = AnsiCodeIterator::new(region);
249 let mut ansi_prefix: String = String::new();
250 for chunk in ansi_iterator {
252 // ANSI escape passthrough.
254 if text.chars().last().map_or(false, |c| c == 'm') {
255 ansi_prefix.push_str(text);
256 if text == "\x1B[0m" {
257 self.ansi_prefix_sgr = "\x1B[0m".to_owned();
259 self.ansi_prefix_sgr.push_str(text);
262 ansi_prefix.push_str(text);
268 let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
269 let mut chars = text.chars();
270 let mut remaining = text.chars().count();
272 while remaining > 0 {
273 let available = cursor_max - cursor;
276 if remaining <= available {
277 let text = chars.by_ref().take(remaining).collect::<String>();
287 self.ansi_prefix_sgr, ansi_prefix, text
289 self.config.true_color,
290 self.config.colored_output,
296 // Generate wrap padding if not already generated.
297 if panel_wrap.is_none() {
298 panel_wrap = if self.panel_width > 0 {
304 .generate(line_number, true, self)
305 .text).collect::<Vec<String>>()
314 let text = chars.by_ref().take(available).collect::<String>();
316 remaining -= available;
325 self.ansi_prefix_sgr, ansi_prefix, text
327 self.config.true_color,
328 self.config.colored_output,
330 panel_wrap.clone().unwrap()
334 // Clear the ANSI prefix buffer.
341 write!(handle, "\n")?;
348const DEFAULT_GUTTER_COLOR: u8 = 238;
354 pub git_added: Style,
355 pub git_removed: Style,
356 pub git_modified: Style,
357 pub line_number: Style,
365 fn colored(theme: &Theme, true_color: bool) -> Self {
366 let gutter_color = theme
369 .map(|c| to_ansi_color(c, true_color))
370 .unwrap_or(Fixed(DEFAULT_GUTTER_COLOR));
373 grid: gutter_color.normal(),
374 filename: Style::new().bold(),
375 git_added: Green.normal(),
376 git_removed: Red.normal(),
377 git_modified: Yellow.normal(),
378 line_number: gutter_color.normal(),
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