2use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
9use syntect::highlighting;
10use terminal::as_terminal_escaped;
13pub struct Printer<'a> {
14 handle: &'a mut Write,
16 config: &'a Config<'a>,
17 decorations: Vec<Box<Decoration>>,
19 pub line_changes: Option<LineChanges>,
23 pub fn new(handle: &'a mut Write, config: &'a Config) -> Self {
24 let colors = if config.colored_output {
30 // Create decorations.
31 let mut decorations: Vec<Box<Decoration>> = Vec::new();
33 if config.output_components.numbers() {
34 decorations.push(Box::new(LineNumberDecoration::new(&colors)));
37 if config.output_components.changes() {
38 decorations.push(Box::new(LineChangesDecoration::new(&colors)));
41 let mut panel_width: usize =
42 decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
44 // The grid border decoration isn't added until after the panel_width calculation, since the
45 // print_horizontal_line, print_header, and print_footer functions all assume the panel
46 // width is without the grid border.
47 if config.output_components.grid() && decorations.len() > 0 {
48 decorations.push(Box::new(GridBorderDecoration::new(&colors)));
51 // Disable the panel if the terminal is too small (i.e. can't fit 5 characters with the
54 < (decorations.len() + decorations.iter().fold(0, |a, x| a + x.width())) + 5
71 pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
72 if !self.config.output_components.header() {
76 if self.config.output_components.grid() {
77 self.print_horizontal_line('┬')?;
82 " ".repeat(self.panel_width),
85 .paint(if self.panel_width > 0 { "│" } else { "" }),
92 filename.map_or("", |_| "File: "),
93 self.colors.filename.paint(filename.unwrap_or("STDIN"))
96 if self.config.output_components.grid() {
97 self.print_horizontal_line('┼')?;
103 pub fn print_footer(&mut self) -> Result<()> {
104 if self.config.output_components.grid() {
105 self.print_horizontal_line('┴')
114 regions: &[(highlighting::Style, &str)],
116 let mut cursor: usize = 0;
117 let mut cursor_max: usize = self.config.term_width;
118 let mut panel_wrap: Option<String> = None;
121 if self.panel_width > 0 {
122 let decorations = self
125 .map(|ref d| d.generate(line_number, false, self))
126 .collect::<Vec<_>>();
128 for deco in decorations {
129 write!(self.handle, "{} ", deco.text)?;
130 cursor_max -= deco.width + 1;
135 if self.config.output_wrap == OutputWrap::None {
136 let true_color = self.config.true_color;
137 let colored_output = self.config.colored_output;
144 .map(|&(style, text)| as_terminal_escaped(
154 for &(style, text) in regions.iter() {
155 let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
156 let mut chars = text.chars();
157 let mut remaining = text.chars().count();
159 while remaining > 0 {
160 let available = cursor_max - cursor;
163 if remaining <= available {
164 let text = chars.by_ref().take(remaining).collect::<String>();
173 self.config.true_color,
174 self.config.colored_output,
180 // Generate wrap padding if not already generated.
181 if panel_wrap.is_none() {
182 panel_wrap = if self.panel_width > 0 {
187 .map(|ref d| d.generate(line_number, true, self).text)
188 .collect::<Vec<String>>()
197 let text = chars.by_ref().take(available).collect::<String>();
199 remaining -= available;
207 self.config.true_color,
208 self.config.colored_output,
210 panel_wrap.clone().unwrap()
215 write!(self.handle, "\n")?;
221 fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
222 if self.panel_width == 0 {
226 self.colors.grid.paint("─".repeat(self.config.term_width))
229 let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
230 let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
231 writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
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