mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-14 06:38:24 +00:00
Added line wrapping.
This commit is contained in:
parent
22c8978fca
commit
4e4110bf50
@ -4,7 +4,7 @@ use console::Term;
|
|||||||
use errors::*;
|
use errors::*;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::env;
|
use std::env;
|
||||||
use style::{OutputComponent, OutputComponents};
|
use style::{OutputComponent, OutputComponents, OutputWrap};
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub matches: ArgMatches<'static>,
|
pub matches: ArgMatches<'static>,
|
||||||
@ -135,6 +135,7 @@ impl App {
|
|||||||
true_color: is_truecolor_terminal(),
|
true_color: is_truecolor_terminal(),
|
||||||
output_components: self.output_components()?,
|
output_components: self.output_components()?,
|
||||||
language: self.matches.value_of("language"),
|
language: self.matches.value_of("language"),
|
||||||
|
output_wrap: OutputWrap::Character,
|
||||||
colored_output: match self.matches.value_of("color") {
|
colored_output: match self.matches.value_of("color") {
|
||||||
Some("always") => true,
|
Some("always") => true,
|
||||||
Some("never") => false,
|
Some("never") => false,
|
||||||
@ -191,6 +192,7 @@ impl App {
|
|||||||
|
|
||||||
pub struct Config<'a> {
|
pub struct Config<'a> {
|
||||||
pub true_color: bool,
|
pub true_color: bool,
|
||||||
|
pub output_wrap: OutputWrap,
|
||||||
pub output_components: OutputComponents,
|
pub output_components: OutputComponents,
|
||||||
pub language: Option<&'a str>,
|
pub language: Option<&'a str>,
|
||||||
pub colored_output: bool,
|
pub colored_output: bool,
|
||||||
|
240
src/printer.rs
240
src/printer.rs
@ -5,14 +5,21 @@ use errors::*;
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use syntect::highlighting;
|
use syntect::highlighting;
|
||||||
use terminal::as_terminal_escaped;
|
use terminal::as_terminal_escaped;
|
||||||
|
use style::OutputWrap;
|
||||||
use Colors;
|
use Colors;
|
||||||
|
|
||||||
const PANEL_WIDTH: usize = 7;
|
const LINE_NUMBER_WIDTH: usize = 4;
|
||||||
|
|
||||||
|
struct PrintSegment {
|
||||||
|
size: usize,
|
||||||
|
text: String
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Printer<'a> {
|
pub struct Printer<'a> {
|
||||||
handle: &'a mut Write,
|
handle: &'a mut Write,
|
||||||
colors: Colors,
|
colors: Colors,
|
||||||
config: &'a Config<'a>,
|
config: &'a Config<'a>,
|
||||||
|
panel_width: usize,
|
||||||
pub line_changes: Option<LineChanges>,
|
pub line_changes: Option<LineChanges>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,12 +31,27 @@ impl<'a> Printer<'a> {
|
|||||||
Colors::plain()
|
Colors::plain()
|
||||||
};
|
};
|
||||||
|
|
||||||
Printer {
|
// Create the instance.
|
||||||
|
let mut instance = Printer {
|
||||||
handle,
|
handle,
|
||||||
colors,
|
colors,
|
||||||
config,
|
config,
|
||||||
|
panel_width: 0,
|
||||||
line_changes: None,
|
line_changes: None,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
// Generate the panel (gutter) width.
|
||||||
|
let decorations = instance.gen_decorations(0);
|
||||||
|
instance.panel_width = decorations.len()
|
||||||
|
+ decorations.iter().fold(0, |a, x| a + x.size)
|
||||||
|
+ if config.output_components.grid() {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Return the instance.
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
|
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
|
||||||
@ -43,8 +65,12 @@ impl<'a> Printer<'a> {
|
|||||||
write!(
|
write!(
|
||||||
self.handle,
|
self.handle,
|
||||||
"{}{} ",
|
"{}{} ",
|
||||||
" ".repeat(PANEL_WIDTH),
|
" ".repeat(self.panel_width),
|
||||||
self.colors.grid.paint("│"),
|
self.colors.grid.paint(if self.panel_width > 0 {
|
||||||
|
"│"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,86 +101,162 @@ impl<'a> Printer<'a> {
|
|||||||
line_number: usize,
|
line_number: usize,
|
||||||
regions: &[(highlighting::Style, &str)],
|
regions: &[(highlighting::Style, &str)],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let decorations = vec![
|
let mut cursor:usize = 0;
|
||||||
self.print_line_number(line_number),
|
let mut cursor_max:usize = self.config.term_width - 2;
|
||||||
self.print_git_marker(line_number),
|
|
||||||
self.print_line_border(),
|
|
||||||
Some(as_terminal_escaped(
|
|
||||||
®ions,
|
|
||||||
self.config.true_color,
|
|
||||||
self.config.colored_output,
|
|
||||||
)),
|
|
||||||
];
|
|
||||||
|
|
||||||
let grid_requested = self.config.output_components.grid();
|
// Line decoration.
|
||||||
write!(
|
let decorations = self.gen_decorations(line_number);
|
||||||
self.handle,
|
let gutter_width = decorations.len() + decorations.iter().fold(0, |a, x| a + x.size);
|
||||||
"{}",
|
|
||||||
decorations
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|dec| if grid_requested {
|
|
||||||
Some(dec.unwrap_or_else(|| " ".to_owned()))
|
|
||||||
} else {
|
|
||||||
dec
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(" ")
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
if gutter_width > 0 {
|
||||||
|
cursor_max -= gutter_width;
|
||||||
|
write!(self.handle, "{} ", decorations
|
||||||
|
.iter()
|
||||||
|
.map(|seg| seg.text.to_owned())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" "))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grid border.
|
||||||
|
let border = if gutter_width > 0 && self.config.output_components.grid() {
|
||||||
|
self.gen_border()
|
||||||
|
} else {
|
||||||
|
PrintSegment {
|
||||||
|
size: 0,
|
||||||
|
text: "".to_owned()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
cursor_max -= border.size;
|
||||||
|
write!(self.handle, "{} ", border.text)?;
|
||||||
|
|
||||||
|
// Line contents.
|
||||||
|
for &(style, text) in regions.iter() {
|
||||||
|
let mut chars = text.chars().filter(|c| *c != '\n');
|
||||||
|
let mut remaining = chars.clone().count();
|
||||||
|
|
||||||
|
while remaining > 0 {
|
||||||
|
let available = cursor_max - cursor;
|
||||||
|
|
||||||
|
// It fits.
|
||||||
|
if remaining <= available {
|
||||||
|
let text = chars.by_ref().take(remaining).collect::<String>();
|
||||||
|
cursor += remaining;
|
||||||
|
|
||||||
|
write!(self.handle, "{}", as_terminal_escaped(
|
||||||
|
style,
|
||||||
|
&*text,
|
||||||
|
self.config.true_color,
|
||||||
|
self.config.colored_output
|
||||||
|
))?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// It wraps.
|
||||||
|
if self.config.output_wrap == OutputWrap::Character {
|
||||||
|
let text = chars.by_ref().take(available).collect::<String>();
|
||||||
|
cursor = 0;
|
||||||
|
remaining -= available;
|
||||||
|
|
||||||
|
write!(self.handle, "{}\n{}{} ", as_terminal_escaped(
|
||||||
|
style,
|
||||||
|
&*text,
|
||||||
|
self.config.true_color,
|
||||||
|
self.config.colored_output
|
||||||
|
), " ".repeat(gutter_width), border.text.to_owned())?;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finished.
|
||||||
|
write!(self.handle, "\n")?;
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_line_number(&self, line_number: usize) -> Option<String> {
|
|
||||||
|
|
||||||
|
#[doc = "
|
||||||
|
Generates all the line decorations.
|
||||||
|
|
||||||
|
# Arguments
|
||||||
|
* `line_number` - The line number.
|
||||||
|
"]
|
||||||
|
fn gen_decorations(&self, line_number: usize) -> Vec<PrintSegment> {
|
||||||
|
let mut decorations = Vec::new();
|
||||||
|
|
||||||
if self.config.output_components.numbers() {
|
if self.config.output_components.numbers() {
|
||||||
Some(
|
decorations.push(self.gen_deco_line_number(line_number));
|
||||||
self.colors
|
|
||||||
.line_number
|
|
||||||
.paint(format!("{:4}", line_number))
|
|
||||||
.to_string(),
|
|
||||||
)
|
|
||||||
} else if self.config.output_components.grid() {
|
|
||||||
Some(" ".to_owned())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn print_git_marker(&self, line_number: usize) -> Option<String> {
|
|
||||||
if self.config.output_components.changes() {
|
if self.config.output_components.changes() {
|
||||||
Some(
|
decorations.push(self.gen_deco_line_changes(line_number));
|
||||||
if let Some(ref changes) = self.line_changes {
|
}
|
||||||
match changes.get(&(line_number as u32)) {
|
|
||||||
Some(&LineChange::Added) => self.colors.git_added.paint("+"),
|
return decorations;
|
||||||
Some(&LineChange::RemovedAbove) => self.colors.git_removed.paint("‾"),
|
}
|
||||||
Some(&LineChange::RemovedBelow) => self.colors.git_removed.paint("_"),
|
|
||||||
Some(&LineChange::Modified) => self.colors.git_modified.paint("~"),
|
#[doc = "
|
||||||
_ => Style::default().paint(" "),
|
Generates the decoration for displaying the line number.
|
||||||
}
|
|
||||||
} else {
|
# Arguments
|
||||||
Style::default().paint(" ")
|
* `line_number` - The line number.
|
||||||
}.to_string(),
|
"]
|
||||||
)
|
fn gen_deco_line_number(&self, line_number: usize) -> PrintSegment {
|
||||||
} else if self.config.output_components.grid() {
|
let plain:String = format!("{:width$}", line_number, width = LINE_NUMBER_WIDTH);
|
||||||
Some(" ".to_owned())
|
let color = self.colors.line_number.paint(plain.to_owned());
|
||||||
} else {
|
|
||||||
None
|
return PrintSegment {
|
||||||
|
text: color.to_string(),
|
||||||
|
size: plain.len()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_line_border(&self) -> Option<String> {
|
#[doc = "
|
||||||
if self.config.output_components.grid() {
|
Generates the decoration for displaying the git changes.
|
||||||
Some(self.colors.grid.paint("│").to_string())
|
|
||||||
|
# Arguments
|
||||||
|
* `line_number` - The line number.
|
||||||
|
"]
|
||||||
|
fn gen_deco_line_changes(&self, line_number: usize) -> PrintSegment {
|
||||||
|
let color = if let Some(ref changes) = self.line_changes {
|
||||||
|
match changes.get(&(line_number as u32)) {
|
||||||
|
Some(&LineChange::Added) => self.colors.git_added.paint("+"),
|
||||||
|
Some(&LineChange::RemovedAbove) => self.colors.git_removed.paint("‾"),
|
||||||
|
Some(&LineChange::RemovedBelow) => self.colors.git_removed.paint("_"),
|
||||||
|
Some(&LineChange::Modified) => self.colors.git_modified.paint("~"),
|
||||||
|
_ => Style::default().paint(" "),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
Style::default().paint(" ")
|
||||||
|
};
|
||||||
|
|
||||||
|
return PrintSegment {
|
||||||
|
text: color.to_string(),
|
||||||
|
size: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc = "
|
||||||
|
Generates the vertical grid border.
|
||||||
|
"]
|
||||||
|
fn gen_border(&self) -> PrintSegment {
|
||||||
|
return PrintSegment {
|
||||||
|
text: self.colors.grid.paint("│").to_string(),
|
||||||
|
size: 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
|
fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
|
||||||
let hline = "─".repeat(self.config.term_width - (PANEL_WIDTH + 1));
|
if self.panel_width == 0 {
|
||||||
let hline = format!("{}{}{}", "─".repeat(PANEL_WIDTH), grid_char, hline);
|
writeln!(self.handle, "{}", "─".repeat(self.config.term_width))?;
|
||||||
|
} else {
|
||||||
|
let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
|
||||||
|
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(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,11 @@ pub enum OutputComponent {
|
|||||||
Plain,
|
Plain,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
|
||||||
|
pub enum OutputWrap {
|
||||||
|
Character
|
||||||
|
}
|
||||||
|
|
||||||
impl OutputComponent {
|
impl OutputComponent {
|
||||||
pub fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] {
|
pub fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -26,26 +26,46 @@ 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(
|
||||||
v: &[(highlighting::Style, &str)],
|
color:highlighting::Style,
|
||||||
|
text: &str,
|
||||||
true_color: bool,
|
true_color: bool,
|
||||||
colored: bool,
|
colored: bool,
|
||||||
) -> String {
|
) -> String {
|
||||||
|
let style = if !colored {
|
||||||
|
Style::default()
|
||||||
|
} else if true_color {
|
||||||
|
RGB(color.foreground.r, color.foreground.g, color.foreground.b).normal()
|
||||||
|
} else {
|
||||||
|
let ansi = rgb2ansi(color.foreground.r, color.foreground.g, color.foreground.b);
|
||||||
|
Fixed(ansi).normal()
|
||||||
|
};
|
||||||
|
|
||||||
let mut s: String = String::new();
|
let mut s: String = String::new();
|
||||||
for &(ref style, text) in v.iter() {
|
write!(s, "{}", style.paint(text)).unwrap();
|
||||||
let style = if !colored {
|
return s;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user