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;
19use preprocessor::expand;
21use terminal::{as_terminal_escaped, to_ansi_color};
24 fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()>;
25 fn print_footer(&mut self, handle: &mut Write) -> Result<()>;
35pub struct SimplePrinter;
38 pub fn new() -> Self {
43impl Printer for SimplePrinter {
44 fn print_header(&mut self, _handle: &mut Write, _file: InputFile) -> Result<()> {
48 fn print_footer(&mut self, _handle: &mut Write) -> Result<()> {
60 handle.write(line_buffer)?;
66pub struct InteractivePrinter<'a> {
68 config: &'a Config<'a>,
69 decorations: Vec<Box<Decoration>>,
71 ansi_prefix_sgr: String,
72 pub line_changes: Option<LineChanges>,
73 highlighter: HighlightLines<'a>,
76impl<'a> InteractivePrinter<'a> {
77 pub fn new(config: &'a Config, assets: &'a HighlightingAssets, file: InputFile) -> Self {
78 let theme = assets.get_theme(&config.theme);
80 let colors = if config.colored_output {
81 Colors::colored(theme, config.true_color)
86 // Create decorations.
87 let mut decorations: Vec<Box<Decoration>> = Vec::new();
89 if config.output_components.numbers() {
90 decorations.push(Box::new(LineNumberDecoration::new(&colors)));
93 if config.output_components.changes() {
94 decorations.push(Box::new(LineChangesDecoration::new(&colors)));
97 let mut panel_width: usize =
98 decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
100 // The grid border decoration isn't added until after the panel_width calculation, since the
101 // print_horizontal_line, print_header, and print_footer functions all assume the panel
102 // width is without the grid border.
103 if config.output_components.grid() && !decorations.is_empty() {
104 decorations.push(Box::new(GridBorderDecoration::new(&colors)));
107 // Disable the panel if the terminal is too small (i.e. can't fit 5 characters with the
110 < (decorations.len() + decorations.iter().fold(0, |a, x| a + x.width())) + 5
116 // Get the Git modifications
117 let line_changes = if config.output_components.changes() {
119 InputFile::Ordinary(filename) => get_git_diff(filename),
126 // Determine the type of syntax for highlighting
127 let syntax = assets.get_syntax(config.language, file);
128 let highlighter = HighlightLines::new(syntax, theme);
135 ansi_prefix_sgr: String::new(),
141 fn print_horizontal_line(&mut self, handle: &mut Write, grid_char: char) -> Result<()> {
142 if self.panel_width == 0 {
146 self.colors.grid.paint("─".repeat(self.config.term_width))
149 let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
150 let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
151 writeln!(handle, "{}", self.colors.grid.paint(hline))?;
157 fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
158 if self.config.tab_width > 0 {
159 expand(text, self.config.tab_width, cursor)
166impl<'a> Printer for InteractivePrinter<'a> {
167 fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()> {
168 if !self.config.output_components.header() {
172 if self.config.output_components.grid() {
173 self.print_horizontal_line(handle, '┬')?;
178 " ".repeat(self.panel_width),
181 .paint(if self.panel_width > 0 { "│ " } else { "" }),
184 write!(handle, "{}", " ".repeat(self.panel_width))?;
187 let (prefix, name) = match file {
188 InputFile::Ordinary(filename) => ("File: ", filename),
192 writeln!(handle, "{}{}", prefix, self.colors.filename.paint(name))?;
194 if self.config.output_components.grid() {
195 self.print_horizontal_line(handle, '┼')?;
201 fn print_footer(&mut self, handle: &mut Write) -> Result<()> {
202 if self.config.output_components.grid() {
203 self.print_horizontal_line(handle, '┴')
216 let line = String::from_utf8_lossy(&line_buffer).to_string();
217 let regions = self.highlighter.highlight(line.as_ref());
223 let mut cursor: usize = 0;
224 let mut cursor_max: usize = self.config.term_width;
225 let mut cursor_total: usize = 0;
226 let mut panel_wrap: Option<String> = None;
229 if self.panel_width > 0 {
230 let decorations = self
233 .map(|ref d| d.generate(line_number, false, self))
234 .collect::<Vec<_>>();
236 for deco in decorations {
237 write!(handle, "{} ", deco.text)?;
238 cursor_max -= deco.width + 1;
243 if self.config.output_wrap == OutputWrap::None {
244 let true_color = self.config.true_color;
245 let colored_output = self.config.colored_output;
247 for &(style, region) in regions.iter() {
248 let text = &*self.preprocess(region, &mut cursor_total);
252 as_terminal_escaped(style, &*text, true_color, colored_output,)
256 if line.bytes().next_back() != Some(b'\n') {
257 write!(handle, "\n")?;
260 for &(style, region) in regions.iter() {
261 let mut ansi_iterator = AnsiCodeIterator::new(region);
262 let mut ansi_prefix: String = String::new();
263 for chunk in ansi_iterator {
265 // ANSI escape passthrough.
267 if text.chars().last().map_or(false, |c| c == 'm') {
268 ansi_prefix.push_str(text);
269 if text == "\x1B[0m" {
270 self.ansi_prefix_sgr = "\x1B[0m".to_owned();
272 self.ansi_prefix_sgr.push_str(text);
275 ansi_prefix.push_str(text);
281 let text = self.preprocess(
282 text.trim_right_matches(|c| c == '\r' || c == '\n'),
286 let mut chars = text.chars();
287 let mut remaining = text.chars().count();
289 while remaining > 0 {
290 let available = cursor_max - cursor;
293 if remaining <= available {
294 let text = chars.by_ref().take(remaining).collect::<String>();
304 self.ansi_prefix_sgr, ansi_prefix, text
306 self.config.true_color,
307 self.config.colored_output,
313 // Generate wrap padding if not already generated.
314 if panel_wrap.is_none() {
315 panel_wrap = if self.panel_width > 0 {
321 .generate(line_number, true, self)
322 .text).collect::<Vec<String>>()
331 let text = chars.by_ref().take(available).collect::<String>();
333 remaining -= available;
342 self.ansi_prefix_sgr, ansi_prefix, text
344 self.config.true_color,
345 self.config.colored_output,
347 panel_wrap.clone().unwrap()
351 // Clear the ANSI prefix buffer.
358 write!(handle, "\n")?;
365const DEFAULT_GUTTER_COLOR: u8 = 238;
371 pub git_added: Style,
372 pub git_removed: Style,
373 pub git_modified: Style,
374 pub line_number: Style,
382 fn colored(theme: &Theme, true_color: bool) -> Self {
383 let gutter_color = theme
386 .map(|c| to_ansi_color(c, true_color))
387 .unwrap_or(Fixed(DEFAULT_GUTTER_COLOR));
390 grid: gutter_color.normal(),
391 filename: Style::new().bold(),
392 git_added: Green.normal(),
393 git_removed: Red.normal(),
394 git_modified: Yellow.normal(),
395 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