1use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
4use console::AnsiCodeIterator;
5use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
12use syntect::highlighting;
13use terminal::as_terminal_escaped;
15pub struct Printer<'a> {
16 handle: &'a mut Write,
18 config: &'a Config<'a>,
19 decorations: Vec<Box<Decoration>>,
22 pub line_changes: Option<LineChanges>,
26 pub fn new(handle: &'a mut Write, config: &'a Config) -> Self {
27 let colors = if config.colored_output {
33 // Create decorations.
34 let mut decorations: Vec<Box<Decoration>> = Vec::new();
36 if config.output_components.numbers() {
37 decorations.push(Box::new(LineNumberDecoration::new(&colors)));
40 if config.output_components.changes() {
41 decorations.push(Box::new(LineChangesDecoration::new(&colors)));
44 let mut panel_width: usize =
45 decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
47 // The grid border decoration isn't added until after the panel_width calculation, since the
48 // print_horizontal_line, print_header, and print_footer functions all assume the panel
49 // width is without the grid border.
50 if config.output_components.grid() && !decorations.is_empty() {
51 decorations.push(Box::new(GridBorderDecoration::new(&colors)));
54 // Disable the panel if the terminal is too small (i.e. can't fit 5 characters with the
57 < (decorations.len() + decorations.iter().fold(0, |a, x| a + x.width())) + 5
75 pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
78 if !self.config.output_components.header() {
82 if self.config.output_components.grid() {
83 self.print_horizontal_line('┬')?;
88 " ".repeat(self.panel_width),
91 .paint(if self.panel_width > 0 { "│ " } else { "" }),
94 write!(self.handle, "{}", " ".repeat(self.panel_width))?;
100 filename.map_or("", |_| "File: "),
101 self.colors.filename.paint(filename.unwrap_or("STDIN"))
104 if self.config.output_components.grid() {
105 self.print_horizontal_line('┼')?;
111 pub fn print_footer(&mut self) -> Result<()> {
112 if self.config.output_components.grid() {
113 self.print_horizontal_line('┴')
119 pub fn print_line(&mut self, regions: &[(highlighting::Style, &str)]) -> Result<()> {
120 self.line_number += 1;
121 let mut cursor: usize = 0;
122 let mut cursor_max: usize = self.config.term_width;
123 let mut panel_wrap: Option<String> = None;
126 if self.panel_width > 0 {
127 let decorations = self
130 .map(|ref d| d.generate(self.line_number, false, self))
131 .collect::<Vec<_>>();
133 for deco in decorations {
134 write!(self.handle, "{} ", deco.text)?;
135 cursor_max -= deco.width + 1;
140 if self.config.output_wrap == OutputWrap::None {
141 let true_color = self.config.true_color;
142 let colored_output = self.config.colored_output;
149 .map(|&(style, text)| as_terminal_escaped(
159 for &(style, region) in regions.iter() {
160 let mut ansi_iterator = AnsiCodeIterator::new(region);
161 let mut ansi_prefix = "";
162 for chunk in ansi_iterator {
164 // ANSI escape passthrough.
171 let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
172 let mut chars = text.chars();
173 let mut remaining = text.chars().count();
175 while remaining > 0 {
176 let available = cursor_max - cursor;
179 if remaining <= available {
180 let text = chars.by_ref().take(remaining).collect::<String>();
188 &*format!("{}{}", ansi_prefix, text),
189 self.config.true_color,
190 self.config.colored_output,
196 // Generate wrap padding if not already generated.
197 if panel_wrap.is_none() {
198 panel_wrap = if self.panel_width > 0 {
204 .generate(self.line_number, true, self)
206 .collect::<Vec<String>>()
215 let text = chars.by_ref().take(available).collect::<String>();
217 remaining -= available;
224 &*format!("{}{}", ansi_prefix, text),
225 self.config.true_color,
226 self.config.colored_output,
228 panel_wrap.clone().unwrap()
236 write!(self.handle, "\n")?;
242 fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
243 if self.panel_width == 0 {
247 self.colors.grid.paint("─".repeat(self.config.term_width))
250 let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
251 let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
252 writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
259const GRID_COLOR: u8 = 238;
260const LINE_NUMBER_COLOR: u8 = 244;
266 pub git_added: Style,
267 pub git_removed: Style,
268 pub git_modified: Style,
269 pub line_number: Style,
277 fn colored() -> Self {
279 grid: Fixed(GRID_COLOR).normal(),
280 filename: White.bold(),
281 git_added: Green.normal(),
282 git_removed: Red.normal(),
283 git_modified: Yellow.normal(),
284 line_number: Fixed(LINE_NUMBER_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