1use ansi_term::Colour::{Fixed, Green, Red, Yellow};
4use console::AnsiCodeIterator;
5use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
12use syntect::highlighting::{self, Theme};
13use terminal::{as_terminal_escaped, to_ansi_color};
15pub struct Printer<'a> {
16 handle: &'a mut Write,
18 pub config: &'a Config<'a>,
19 decorations: Vec<Box<Decoration>>,
21 pub ansi_prefix_sgr: String,
22 pub line_number: usize,
23 pub line_changes: Option<LineChanges>,
27 pub fn new(handle: &'a mut Write, config: &'a Config, theme: &Theme) -> Self {
28 let colors = if config.colored_output {
29 Colors::colored(theme, config.true_color)
34 // Create decorations.
35 let mut decorations: Vec<Box<Decoration>> = Vec::new();
37 if config.output_components.numbers() {
38 decorations.push(Box::new(LineNumberDecoration::new(&colors)));
41 if config.output_components.changes() {
42 decorations.push(Box::new(LineChangesDecoration::new(&colors)));
45 let mut panel_width: usize =
46 decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
48 // The grid border decoration isn't added until after the panel_width calculation, since the
49 // print_horizontal_line, print_header, and print_footer functions all assume the panel
50 // width is without the grid border.
51 if config.output_components.grid() && !decorations.is_empty() {
52 decorations.push(Box::new(GridBorderDecoration::new(&colors)));
55 // Disable the panel if the terminal is too small (i.e. can't fit 5 characters with the
58 < (decorations.len() + decorations.iter().fold(0, |a, x| a + x.width())) + 5
71 ansi_prefix_sgr: String::new(),
77 pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
80 if !self.config.output_components.header() {
84 if self.config.output_components.grid() {
85 self.print_horizontal_line('┬')?;
90 " ".repeat(self.panel_width),
93 .paint(if self.panel_width > 0 { "│ " } else { "" }),
96 write!(self.handle, "{}", " ".repeat(self.panel_width))?;
102 filename.map_or("", |_| "File: "),
103 self.colors.filename.paint(filename.unwrap_or("STDIN"))
106 if self.config.output_components.grid() {
107 self.print_horizontal_line('┼')?;
113 pub fn print_footer(&mut self) -> Result<()> {
114 if self.config.output_components.grid() {
115 self.print_horizontal_line('┴')
121 pub fn print_line(&mut self, regions: &[(highlighting::Style, &str)]) -> Result<()> {
122 self.line_number += 1;
123 let mut cursor: usize = 0;
124 let mut cursor_max: usize = self.config.term_width;
125 let mut panel_wrap: Option<String> = None;
128 if self.panel_width > 0 {
129 let decorations = self
132 .map(|ref d| d.generate(self.line_number, false, self))
133 .collect::<Vec<_>>();
135 for deco in decorations {
136 write!(self.handle, "{} ", deco.text)?;
137 cursor_max -= deco.width + 1;
142 if self.config.output_wrap == OutputWrap::None {
143 let true_color = self.config.true_color;
144 let colored_output = self.config.colored_output;
151 .map(|&(style, text)| as_terminal_escaped(
156 )).collect::<Vec<_>>()
160 for &(style, region) in regions.iter() {
161 let mut ansi_iterator = AnsiCodeIterator::new(region);
162 let mut ansi_prefix: String = String::new();
163 for chunk in ansi_iterator {
165 // ANSI escape passthrough.
167 if text.chars().last().map_or(false, |c| c == 'm') {
168 ansi_prefix.push_str(text);
169 if text == "\x1B[0m" {
170 self.ansi_prefix_sgr = "\x1B[0m".to_owned();
172 self.ansi_prefix_sgr.push_str(text);
175 ansi_prefix.push_str(text);
181 let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
182 let mut chars = text.chars();
183 let mut remaining = text.chars().count();
185 while remaining > 0 {
186 let available = cursor_max - cursor;
189 if remaining <= available {
190 let text = chars.by_ref().take(remaining).collect::<String>();
200 self.ansi_prefix_sgr, ansi_prefix, text
202 self.config.true_color,
203 self.config.colored_output,
209 // Generate wrap padding if not already generated.
210 if panel_wrap.is_none() {
211 panel_wrap = if self.panel_width > 0 {
217 .generate(self.line_number, true, self)
218 .text).collect::<Vec<String>>()
227 let text = chars.by_ref().take(available).collect::<String>();
229 remaining -= available;
238 self.ansi_prefix_sgr, ansi_prefix, text
240 self.config.true_color,
241 self.config.colored_output,
243 panel_wrap.clone().unwrap()
247 // Clear the ANSI prefix buffer.
254 write!(self.handle, "\n")?;
260 fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
261 if self.panel_width == 0 {
265 self.colors.grid.paint("─".repeat(self.config.term_width))
268 let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
269 let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
270 writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
277const DEFAULT_GUTTER_COLOR: u8 = 238;
283 pub git_added: Style,
284 pub git_removed: Style,
285 pub git_modified: Style,
286 pub line_number: Style,
294 fn colored(theme: &Theme, true_color: bool) -> Self {
295 let gutter_color = theme
298 .map(|c| to_ansi_color(c, true_color))
299 .unwrap_or(Fixed(DEFAULT_GUTTER_COLOR));
302 grid: gutter_color.normal(),
303 filename: Style::new().bold(),
304 git_added: Green.normal(),
305 git_removed: Red.normal(),
306 git_modified: Yellow.normal(),
307 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