mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-07 13:41:14 +00:00
PR #102 Review Changes
- Reformatted code. - Removed leftover code. - Removed leftover comments. - Fixed compiling on Rust 1.24.0
This commit is contained in:
parent
fcc36b1f79
commit
fc160b0dcd
@ -59,7 +59,7 @@ impl App {
|
|||||||
.use_delimiter(true)
|
.use_delimiter(true)
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.possible_values(&[
|
.possible_values(&[
|
||||||
"auto", "full", "plain", "changes", "header", "grid", "numbers",
|
"auto", "full", "plain", "changes", "header", "grid", "numbers"
|
||||||
])
|
])
|
||||||
.default_value("auto")
|
.default_value("auto")
|
||||||
.help("Additional info to display along with content"),
|
.help("Additional info to display along with content"),
|
||||||
|
125
src/printer.rs
125
src/printer.rs
@ -12,7 +12,7 @@ const LINE_NUMBER_WIDTH: usize = 4;
|
|||||||
|
|
||||||
struct PrintSegment {
|
struct PrintSegment {
|
||||||
size: usize,
|
size: usize,
|
||||||
text: String
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Printer<'a> {
|
pub struct Printer<'a> {
|
||||||
@ -42,13 +42,7 @@ impl<'a> Printer<'a> {
|
|||||||
|
|
||||||
// Generate the panel (gutter) width.
|
// Generate the panel (gutter) width.
|
||||||
let decorations = instance.gen_decorations(0);
|
let decorations = instance.gen_decorations(0);
|
||||||
instance.panel_width = decorations.len()
|
instance.panel_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
|
||||||
+ decorations.iter().fold(0, |a, x| a + x.size)
|
|
||||||
+ if config.output_components.grid() {
|
|
||||||
0
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
};
|
|
||||||
|
|
||||||
// Return the instance.
|
// Return the instance.
|
||||||
return instance;
|
return instance;
|
||||||
@ -66,11 +60,9 @@ impl<'a> Printer<'a> {
|
|||||||
self.handle,
|
self.handle,
|
||||||
"{}{} ",
|
"{}{} ",
|
||||||
" ".repeat(self.panel_width),
|
" ".repeat(self.panel_width),
|
||||||
self.colors.grid.paint(if self.panel_width > 0 {
|
self.colors
|
||||||
"│"
|
.grid
|
||||||
} else {
|
.paint(if self.panel_width > 0 { "│" } else { "" }),
|
||||||
""
|
|
||||||
}),
|
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,8 +93,8 @@ impl<'a> Printer<'a> {
|
|||||||
line_number: usize,
|
line_number: usize,
|
||||||
regions: &[(highlighting::Style, &str)],
|
regions: &[(highlighting::Style, &str)],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut cursor:usize = 0;
|
let mut cursor: usize = 0;
|
||||||
let mut cursor_max:usize = self.config.term_width - 2;
|
let mut cursor_max: usize = self.config.term_width - 2;
|
||||||
|
|
||||||
// Line decoration.
|
// Line decoration.
|
||||||
let decorations = self.gen_decorations(line_number);
|
let decorations = self.gen_decorations(line_number);
|
||||||
@ -110,11 +102,15 @@ impl<'a> Printer<'a> {
|
|||||||
|
|
||||||
if gutter_width > 0 {
|
if gutter_width > 0 {
|
||||||
cursor_max -= gutter_width;
|
cursor_max -= gutter_width;
|
||||||
write!(self.handle, "{} ", decorations
|
write!(
|
||||||
.iter()
|
self.handle,
|
||||||
.map(|seg| seg.text.to_owned())
|
"{} ",
|
||||||
.collect::<Vec<String>>()
|
decorations
|
||||||
.join(" "))?;
|
.iter()
|
||||||
|
.map(|seg| seg.text.to_owned())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" ")
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grid border.
|
// Grid border.
|
||||||
@ -123,7 +119,7 @@ impl<'a> Printer<'a> {
|
|||||||
} else {
|
} else {
|
||||||
PrintSegment {
|
PrintSegment {
|
||||||
size: 0,
|
size: 0,
|
||||||
text: "".to_owned()
|
text: "".to_owned(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -133,7 +129,7 @@ impl<'a> Printer<'a> {
|
|||||||
// Line contents.
|
// Line contents.
|
||||||
for &(style, text) in regions.iter() {
|
for &(style, text) in regions.iter() {
|
||||||
let mut chars = text.chars().filter(|c| *c != '\n');
|
let mut chars = text.chars().filter(|c| *c != '\n');
|
||||||
let mut remaining = chars.clone().count();
|
let mut remaining = text.chars().filter(|c| *c != '\n').count();
|
||||||
|
|
||||||
while remaining > 0 {
|
while remaining > 0 {
|
||||||
let available = cursor_max - cursor;
|
let available = cursor_max - cursor;
|
||||||
@ -143,12 +139,16 @@ impl<'a> Printer<'a> {
|
|||||||
let text = chars.by_ref().take(remaining).collect::<String>();
|
let text = chars.by_ref().take(remaining).collect::<String>();
|
||||||
cursor += remaining;
|
cursor += remaining;
|
||||||
|
|
||||||
write!(self.handle, "{}", as_terminal_escaped(
|
write!(
|
||||||
style,
|
self.handle,
|
||||||
&*text,
|
"{}",
|
||||||
self.config.true_color,
|
as_terminal_escaped(
|
||||||
self.config.colored_output
|
style,
|
||||||
))?;
|
&*text,
|
||||||
|
self.config.true_color,
|
||||||
|
self.config.colored_output
|
||||||
|
)
|
||||||
|
)?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,12 +158,18 @@ impl<'a> Printer<'a> {
|
|||||||
cursor = 0;
|
cursor = 0;
|
||||||
remaining -= available;
|
remaining -= available;
|
||||||
|
|
||||||
write!(self.handle, "{}\n{}{} ", as_terminal_escaped(
|
write!(
|
||||||
style,
|
self.handle,
|
||||||
&*text,
|
"{}\n{}{} ",
|
||||||
self.config.true_color,
|
as_terminal_escaped(
|
||||||
self.config.colored_output
|
style,
|
||||||
), " ".repeat(gutter_width), border.text.to_owned())?;
|
&*text,
|
||||||
|
self.config.true_color,
|
||||||
|
self.config.colored_output
|
||||||
|
),
|
||||||
|
" ".repeat(gutter_width),
|
||||||
|
border.text.to_owned()
|
||||||
|
)?;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -172,17 +178,10 @@ impl<'a> Printer<'a> {
|
|||||||
|
|
||||||
// Finished.
|
// Finished.
|
||||||
write!(self.handle, "\n")?;
|
write!(self.handle, "\n")?;
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generates all the line decorations.
|
||||||
|
|
||||||
#[doc = "
|
|
||||||
Generates all the line decorations.
|
|
||||||
|
|
||||||
# Arguments
|
|
||||||
* `line_number` - The line number.
|
|
||||||
"]
|
|
||||||
fn gen_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
|
fn gen_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
|
||||||
let mut decorations = Vec::new();
|
let mut decorations = Vec::new();
|
||||||
|
|
||||||
@ -197,28 +196,18 @@ impl<'a> Printer<'a> {
|
|||||||
return decorations;
|
return decorations;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "
|
/// Generates the decoration for displaying the line number.
|
||||||
Generates the decoration for displaying the line number.
|
|
||||||
|
|
||||||
# Arguments
|
|
||||||
* `line_number` - The line number.
|
|
||||||
"]
|
|
||||||
fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment {
|
fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment {
|
||||||
let plain:String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH);
|
let plain: String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH);
|
||||||
let color = self.colors.line_number.paint(plain.to_owned());
|
let color = self.colors.line_number.paint(plain.to_owned());
|
||||||
|
|
||||||
return PrintSegment {
|
return PrintSegment {
|
||||||
text: color.to_string(),
|
text: color.to_string(),
|
||||||
size: plain.len()
|
size: plain.len(),
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "
|
/// Generates the decoration for displaying the git changes.
|
||||||
Generates the decoration for displaying the git changes.
|
|
||||||
|
|
||||||
# Arguments
|
|
||||||
* `line_number` - The line number.
|
|
||||||
"]
|
|
||||||
fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment {
|
fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment {
|
||||||
let color = if let Some(ref changes) = self.line_changes {
|
let color = if let Some(ref changes) = self.line_changes {
|
||||||
match changes.get(&(line_number as u32)) {
|
match changes.get(&(line_number as u32)) {
|
||||||
@ -234,29 +223,31 @@ impl<'a> Printer<'a> {
|
|||||||
|
|
||||||
return PrintSegment {
|
return PrintSegment {
|
||||||
text: color.to_string(),
|
text: color.to_string(),
|
||||||
size: 1
|
size: 1,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "
|
/// Generates the vertical grid border.
|
||||||
Generates the vertical grid border.
|
|
||||||
"]
|
|
||||||
fn gen_border(&self) -> PrintSegment {
|
fn gen_border(&self) -> PrintSegment {
|
||||||
return PrintSegment {
|
return PrintSegment {
|
||||||
text: self.colors.grid.paint("│").to_string(),
|
text: self.colors.grid.paint("│").to_string(),
|
||||||
size: 2
|
size: 2,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
|
fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
|
||||||
if self.panel_width == 0 {
|
if self.panel_width == 0 {
|
||||||
writeln!(self.handle, "{}", self.colors.grid.paint("─".repeat(self.config.term_width)))?;
|
writeln!(
|
||||||
|
self.handle,
|
||||||
|
"{}",
|
||||||
|
self.colors.grid.paint("─".repeat(self.config.term_width))
|
||||||
|
)?;
|
||||||
} else {
|
} else {
|
||||||
let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
|
let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
|
||||||
let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
|
let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
|
||||||
writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
|
writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ pub enum OutputComponent {
|
|||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
|
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
|
||||||
pub enum OutputWrap {
|
pub enum OutputWrap {
|
||||||
Character
|
Character,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutputComponent {
|
impl OutputComponent {
|
||||||
|
@ -26,30 +26,8 @@ fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//pub fn as_terminal_escaped(
|
|
||||||
// v: &[(highlighting::Style, &str)],
|
|
||||||
// true_color: bool,
|
|
||||||
// colored: bool,
|
|
||||||
//) -> String {
|
|
||||||
// let mut s: String = String::new();
|
|
||||||
// for &(ref style, text) in v.iter() {
|
|
||||||
// let style = if !colored {
|
|
||||||
// Style::default()
|
|
||||||
// } else if true_color {
|
|
||||||
// RGB(style.foreground.r, style.foreground.g, style.foreground.b).normal()
|
|
||||||
// } else {
|
|
||||||
// let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
|
|
||||||
// Fixed(ansi).normal()
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// write!(s, "{}", style.paint(text)).unwrap();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// s
|
|
||||||
//}
|
|
||||||
|
|
||||||
pub fn as_terminal_escaped(
|
pub fn as_terminal_escaped(
|
||||||
color:highlighting::Style,
|
color: highlighting::Style,
|
||||||
text: &str,
|
text: &str,
|
||||||
true_color: bool,
|
true_color: bool,
|
||||||
colored: bool,
|
colored: bool,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user