3use diff::{LineChange, LineChanges};
6use syntect::highlighting;
7use terminal::as_terminal_escaped;
11const LINE_NUMBER_WIDTH: usize = 4;
18pub struct Printer<'a> {
19 handle: &'a mut Write,
21 config: &'a Config<'a>,
23 pub line_changes: Option<LineChanges>,
27 pub fn new(handle: &'a mut Write, config: &'a Config) -> Self {
28 let colors = if config.colored_output {
34 // Create the instance.
35 let mut instance = Printer {
43 // Generate the panel (gutter) width.
44 let decorations = instance.gen_decorations(0);
45 instance.panel_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
47 // Return the instance.
51 pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
52 if !self.config.output_components.header() {
56 if self.config.output_components.grid() {
57 self.print_horizontal_line('┬')?;
62 " ".repeat(self.panel_width),
65 .paint(if self.panel_width > 0 { "│" } else { "" }),
72 filename.map_or("", |_| "File: "),
73 self.colors.filename.paint(filename.unwrap_or("STDIN"))
76 if self.config.output_components.grid() {
77 self.print_horizontal_line('┼')?;
83 pub fn print_footer(&mut self) -> Result<()> {
84 if self.config.output_components.grid() {
85 self.print_horizontal_line('┴')
94 regions: &[(highlighting::Style, &str)],
96 let mut cursor: usize = 0;
97 let mut cursor_max: usize = self.config.term_width;
100 let decorations = self.gen_decorations(line_number);
101 let gutter_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
103 if gutter_width > 0 {
104 cursor_max -= gutter_width;
110 .map(|seg| seg.text.to_owned())
111 .collect::<Vec<String>>()
117 let border = if gutter_width > 0 && self.config.output_components.grid() {
126 cursor_max -= border.size;
127 write!(self.handle, "{}", border.text)?;
130 if self.config.output_wrap == OutputWrap::None {
131 let true_color = self.config.true_color;
132 let colored_output = self.config.colored_output;
134 write!(self.handle, "{}",
136 .map(|&(style, text)| as_terminal_escaped(style, text, true_color, colored_output))
141 for &(style, text) in regions.iter() {
142 let mut chars = text.chars().filter(|c| *c != '\n');
143 let mut remaining = text.chars().filter(|c| *c != '\n').count();
145 while remaining > 0 {
146 let available = cursor_max - cursor;
149 if remaining <= available {
150 let text = chars.by_ref().take(remaining).collect::<String>();
159 self.config.true_color,
160 self.config.colored_output,
167 if self.config.output_wrap == OutputWrap::Character {
168 let text = chars.by_ref().take(available).collect::<String>();
170 remaining -= available;
178 self.config.true_color,
179 self.config.colored_output,
181 " ".repeat(gutter_width),
182 border.text.to_owned()
190 write!(self.handle, "\n")?;
197 /// Generates all the line decorations.
198 fn gen_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
199 let mut decorations = Vec::new();
201 if self.config.output_components.numbers() {
202 decorations.push(self.gen_deco_line_number(line_number));
205 if self.config.output_components.changes() {
206 decorations.push(self.gen_deco_line_changes(line_number));
212 /// Generates the decoration for displaying the line number.
213 fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment {
214 let plain: String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH);
215 let color = self.colors.line_number.paint(plain.to_owned());
217 return PrintSegment {
218 text: color.to_string(),
223 /// Generates the decoration for displaying the git changes.
224 fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment {
225 let color = if let Some(ref changes) = self.line_changes {
226 match changes.get(&(line_number as u32)) {
227 Some(&LineChange::Added) => self.colors.git_added.paint("+"),
228 Some(&LineChange::RemovedAbove) => self.colors.git_removed.paint("‾"),
229 Some(&LineChange::RemovedBelow) => self.colors.git_removed.paint("_"),
230 Some(&LineChange::Modified) => self.colors.git_modified.paint("~"),
231 _ => Style::default().paint(" "),
234 Style::default().paint(" ")
237 return PrintSegment {
238 text: color.to_string(),
243 /// Generates the vertical grid border.
244 fn gen_border(&self) -> PrintSegment {
245 return PrintSegment {
246 text: self.colors.grid.paint("│ ").to_string(),
251 fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
252 if self.panel_width == 0 {
256 self.colors.grid.paint("─".repeat(self.config.term_width))
259 let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
260 let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
261 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