on master help sign in
PR #102 Review Changes

- Reformatted code. - Removed leftover code. - Removed leftover comments. - Fixed compiling on Rust 1.24.0

commitfc160b0dcdcd47708cd152b3a7c6848de61c1bef authoreth-p <32112321+eth-p@users.noreply.github.com> dateSat 12 May 2018, 06:32 (8 years ago) parentfcc36b1
4 files changed+61−92
src/app.rs+1−1 src/printer.rs+58−67 src/style.rs+1−1 src/terminal.rs+1−23
src/app.rs+1−1
@@ -59,7 +59,7 @@ impl App {
5959 .use_delimiter(true)
6060 .takes_value(true)
6161 .possible_values(&[
62− "auto", "full", "plain", "changes", "header", "grid", "numbers",
62+ "auto", "full", "plain", "changes", "header", "grid", "numbers"
6363 ])
6464 .default_value("auto")
6565 .help("Additional info to display along with content"),
src/printer.rs+58−67
@@ -12,7 +12,7 @@ const LINE_NUMBER_WIDTH: usize = 4;
1212
1313struct PrintSegment {
1414 size: usize,
15− text: String
15+ text: String,
1616}
1717
1818pub struct Printer<'a> {
@@ -42,13 +42,7 @@ impl<'a> Printer<'a> {
4242
4343 // Generate the panel (gutter) width.
4444 let decorations = instance.gen_decorations(0);
45− instance.panel_width = decorations.len()
46− + decorations.iter().fold(0, |a, x| a + x.size)
47− + if config.output_components.grid() {
48− 0
49− } else {
50− 0
51− };
45+ instance.panel_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
5246
5347 // Return the instance.
5448 return instance;
@@ -66,11 +60,9 @@ impl<'a> Printer<'a> {
6660 self.handle,
6761 "{}{} ",
6862 " ".repeat(self.panel_width),
69− self.colors.grid.paint(if self.panel_width > 0 {
70− "│"
71− } else {
72− ""
73− }),
63+ self.colors
64+ .grid
65+ .paint(if self.panel_width > 0 { "│" } else { "" }),
7466 )?;
7567 }
7668
@@ -101,8 +93,8 @@ impl<'a> Printer<'a> {
10193 line_number: usize,
10294 regions: &[(highlighting::Style, &str)],
10395 ) -> Result<()> {
104− let mut cursor:usize = 0;
105− let mut cursor_max:usize = self.config.term_width - 2;
96+ let mut cursor: usize = 0;
97+ let mut cursor_max: usize = self.config.term_width - 2;
10698
10799 // Line decoration.
108100 let decorations = self.gen_decorations(line_number);
@@ -110,11 +102,15 @@ impl<'a> Printer<'a> {
110102
111103 if gutter_width > 0 {
112104 cursor_max -= gutter_width;
113− write!(self.handle, "{} ", decorations
114− .iter()
115− .map(|seg| seg.text.to_owned())
116− .collect::<Vec<String>>()
117− .join(" "))?;
105+ write!(
106+ self.handle,
107+ "{} ",
108+ decorations
109+ .iter()
110+ .map(|seg| seg.text.to_owned())
111+ .collect::<Vec<String>>()
112+ .join(" ")
113+ )?;
118114 }
119115
120116 // Grid border.
@@ -123,7 +119,7 @@ impl<'a> Printer<'a> {
123119 } else {
124120 PrintSegment {
125121 size: 0,
126− text: "".to_owned()
122+ text: "".to_owned(),
127123 }
128124 };
129125
@@ -133,7 +129,7 @@ impl<'a> Printer<'a> {
133129 // Line contents.
134130 for &(style, text) in regions.iter() {
135131 let mut chars = text.chars().filter(|c| *c != '\n');
136− let mut remaining = chars.clone().count();
132+ let mut remaining = text.chars().filter(|c| *c != '\n').count();
137133
138134 while remaining > 0 {
139135 let available = cursor_max - cursor;
@@ -143,12 +139,16 @@ impl<'a> Printer<'a> {
143139 let text = chars.by_ref().take(remaining).collect::<String>();
144140 cursor += remaining;
145141
146− write!(self.handle, "{}", as_terminal_escaped(
147− style,
148− &*text,
149− self.config.true_color,
150− self.config.colored_output
151− ))?;
142+ write!(
143+ self.handle,
144+ "{}",
145+ as_terminal_escaped(
146+ style,
147+ &*text,
148+ self.config.true_color,
149+ self.config.colored_output
150+ )
151+ )?;
152152 break;
153153 }
154154
@@ -158,12 +158,18 @@ impl<'a> Printer<'a> {
158158 cursor = 0;
159159 remaining -= available;
160160
161− write!(self.handle, "{}\n{}{} ", as_terminal_escaped(
162− style,
163− &*text,
164− self.config.true_color,
165− self.config.colored_output
166− ), " ".repeat(gutter_width), border.text.to_owned())?;
161+ write!(
162+ self.handle,
163+ "{}\n{}{} ",
164+ as_terminal_escaped(
165+ style,
166+ &*text,
167+ self.config.true_color,
168+ self.config.colored_output
169+ ),
170+ " ".repeat(gutter_width),
171+ border.text.to_owned()
172+ )?;
167173
168174 continue;
169175 }
@@ -172,17 +178,10 @@ impl<'a> Printer<'a> {
172178
173179 // Finished.
174180 write!(self.handle, "\n")?;
175− return Ok(());
181+ Ok(())
176182 }
177183
178−
179−
180− #[doc = "
181− Generates all the line decorations.
182−
183− # Arguments
184− * `line_number` - The line number.
185− "]
184+ /// Generates all the line decorations.
186185 fn gen_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
187186 let mut decorations = Vec::new();
188187
@@ -197,28 +196,18 @@ impl<'a> Printer<'a> {
197196 return decorations;
198197 }
199198
200− #[doc = "
201− Generates the decoration for displaying the line number.
202−
203− # Arguments
204− * `line_number` - The line number.
205− "]
199+ /// Generates the decoration for displaying the line number.
206200 fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment {
207− let plain:String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH);
201+ let plain: String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH);
208202 let color = self.colors.line_number.paint(plain.to_owned());
209203
210204 return PrintSegment {
211205 text: color.to_string(),
212− size: plain.len()
213− }
206+ size: plain.len(),
207+ };
214208 }
215209
216− #[doc = "
217− Generates the decoration for displaying the git changes.
218−
219− # Arguments
220− * `line_number` - The line number.
221− "]
210+ /// Generates the decoration for displaying the git changes.
222211 fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment {
223212 let color = if let Some(ref changes) = self.line_changes {
224213 match changes.get(&(line_number as u32)) {
@@ -234,29 +223,31 @@ impl<'a> Printer<'a> {
234223
235224 return PrintSegment {
236225 text: color.to_string(),
237− size: 1
238− }
226+ size: 1,
227+ };
239228 }
240229
241− #[doc = "
242− Generates the vertical grid border.
243− "]
230+ /// Generates the vertical grid border.
244231 fn gen_border(&self) -> PrintSegment {
245232 return PrintSegment {
246233 text: self.colors.grid.paint("│").to_string(),
247− size: 2
248− }
234+ size: 2,
235+ };
249236 }
250237
251238 fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
252239 if self.panel_width == 0 {
253− writeln!(self.handle, "{}", self.colors.grid.paint("─".repeat(self.config.term_width)))?;
240+ writeln!(
241+ self.handle,
242+ "{}",
243+ self.colors.grid.paint("─".repeat(self.config.term_width))
244+ )?;
254245 } else {
255246 let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
256247 let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
257248 writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
258249 }
259250
260− return Ok(());
251+ Ok(())
261252 }
262253}
src/style.rs+1−1
@@ -15,7 +15,7 @@ pub enum OutputComponent {
1515
1616#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
1717pub enum OutputWrap {
18− Character
18+ Character,
1919}
2020
2121impl OutputComponent {
src/terminal.rs+1−23
@@ -26,30 +26,8 @@ fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
2626 }
2727}
2828
29−//pub fn as_terminal_escaped(
30−// v: &[(highlighting::Style, &str)],
31−// true_color: bool,
32−// colored: bool,
33−//) -> String {
34−// let mut s: String = String::new();
35−// for &(ref style, text) in v.iter() {
36−// let style = if !colored {
37−// Style::default()
38−// } else if true_color {
39−// RGB(style.foreground.r, style.foreground.g, style.foreground.b).normal()
40−// } else {
41−// let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
42−// Fixed(ansi).normal()
43−// };
44−//
45−// write!(s, "{}", style.paint(text)).unwrap();
46−// }
47−//
48−// s
49−//}
50−
5129pub fn as_terminal_escaped(
52− color:highlighting::Style,
30+ color: highlighting::Style,
5331 text: &str,
5432 true_color: bool,
5533 colored: bool,