mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-18 12:05:52 +00:00
Rename output component to 'style compinent', move wrap to separate module
This commit is contained in:
parent
a8851e1307
commit
84ba323b1c
@ -3,7 +3,7 @@ use bat::{
|
||||
config::Config,
|
||||
controller::Controller,
|
||||
inputfile::InputFile,
|
||||
style::{OutputComponent, OutputComponents},
|
||||
style::{StyleComponent, StyleComponents},
|
||||
};
|
||||
use console::Term;
|
||||
use std::process;
|
||||
@ -20,10 +20,10 @@ fn main() {
|
||||
term_width: Term::stdout().size().1 as usize,
|
||||
colored_output: true,
|
||||
true_color: true,
|
||||
output_components: OutputComponents::new(&[
|
||||
OutputComponent::Header,
|
||||
OutputComponent::Grid,
|
||||
OutputComponent::Numbers,
|
||||
style_components: StyleComponents::new(&[
|
||||
StyleComponent::Header,
|
||||
StyleComponent::Grid,
|
||||
StyleComponent::Numbers,
|
||||
]),
|
||||
files: files.iter().map(|file| InputFile::Ordinary(file)).collect(),
|
||||
theme: "1337".into(),
|
||||
|
@ -22,8 +22,9 @@ use bat::{
|
||||
errors::*,
|
||||
inputfile::InputFile,
|
||||
line_range::{HighlightedLineRanges, LineRange, LineRanges},
|
||||
style::{OutputComponent, OutputComponents, OutputWrap},
|
||||
style::{StyleComponent, StyleComponents},
|
||||
syntax_mapping::SyntaxMapping,
|
||||
wrap::OutputWrap,
|
||||
};
|
||||
|
||||
fn is_truecolor_terminal() -> bool {
|
||||
@ -79,7 +80,7 @@ impl App {
|
||||
|
||||
pub fn config(&self) -> Result<Config> {
|
||||
let files = self.files();
|
||||
let output_components = self.output_components()?;
|
||||
let style_components = self.style_components()?;
|
||||
|
||||
let paging_mode = match self.matches.value_of("paging") {
|
||||
Some("always") => PagingMode::Always,
|
||||
@ -152,7 +153,7 @@ impl App {
|
||||
Some("character") => OutputWrap::Character,
|
||||
Some("never") => OutputWrap::None,
|
||||
Some("auto") | _ => {
|
||||
if output_components.plain() {
|
||||
if style_components.plain() {
|
||||
OutputWrap::None
|
||||
} else {
|
||||
OutputWrap::Character
|
||||
@ -182,7 +183,7 @@ impl App {
|
||||
.or_else(|| env::var("BAT_TABS").ok())
|
||||
.and_then(|t| t.parse().ok())
|
||||
.unwrap_or(
|
||||
if output_components.plain() && paging_mode == PagingMode::Never {
|
||||
if style_components.plain() && paging_mode == PagingMode::Never {
|
||||
0
|
||||
} else {
|
||||
4
|
||||
@ -208,7 +209,7 @@ impl App {
|
||||
.transpose()?
|
||||
.map(LineRanges::from)
|
||||
.unwrap_or_default(),
|
||||
output_components,
|
||||
style_components,
|
||||
syntax_mapping,
|
||||
pager: self.matches.value_of("pager"),
|
||||
use_italic_text: match self.matches.value_of("italic-text") {
|
||||
@ -243,23 +244,23 @@ impl App {
|
||||
.unwrap_or_else(|| vec![InputFile::StdIn])
|
||||
}
|
||||
|
||||
fn output_components(&self) -> Result<OutputComponents> {
|
||||
fn style_components(&self) -> Result<StyleComponents> {
|
||||
let matches = &self.matches;
|
||||
Ok(OutputComponents(
|
||||
Ok(StyleComponents(
|
||||
if matches.value_of("decorations") == Some("never") {
|
||||
HashSet::new()
|
||||
} else if matches.is_present("number") {
|
||||
[OutputComponent::Numbers].iter().cloned().collect()
|
||||
[StyleComponent::Numbers].iter().cloned().collect()
|
||||
} else if matches.is_present("plain") {
|
||||
[OutputComponent::Plain].iter().cloned().collect()
|
||||
[StyleComponent::Plain].iter().cloned().collect()
|
||||
} else {
|
||||
let env_style_components: Option<Vec<OutputComponent>> = env::var("BAT_STYLE")
|
||||
let env_style_components: Option<Vec<StyleComponent>> = env::var("BAT_STYLE")
|
||||
.ok()
|
||||
.map(|style_str| {
|
||||
style_str
|
||||
.split(',')
|
||||
.map(|x| OutputComponent::from_str(&x))
|
||||
.collect::<Result<Vec<OutputComponent>>>()
|
||||
.map(|x| StyleComponent::from_str(&x))
|
||||
.collect::<Result<Vec<StyleComponent>>>()
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
@ -268,12 +269,12 @@ impl App {
|
||||
.map(|styles| {
|
||||
styles
|
||||
.split(',')
|
||||
.map(|style| style.parse::<OutputComponent>())
|
||||
.map(|style| style.parse::<StyleComponent>())
|
||||
.filter_map(|style| style.ok())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.or(env_style_components)
|
||||
.unwrap_or_else(|| vec![OutputComponent::Full])
|
||||
.unwrap_or_else(|| vec![StyleComponent::Full])
|
||||
.into_iter()
|
||||
.map(|style| style.components(self.interactive_output))
|
||||
.fold(HashSet::new(), |mut acc, components| {
|
||||
|
@ -32,7 +32,7 @@ use bat::{
|
||||
config::Config,
|
||||
errors::*,
|
||||
inputfile::InputFile,
|
||||
style::{OutputComponent, OutputComponents},
|
||||
style::{StyleComponent, StyleComponents},
|
||||
};
|
||||
|
||||
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
|
||||
@ -123,9 +123,9 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
|
||||
let assets = assets_from_cache_or_binary();
|
||||
let mut config = cfg.clone();
|
||||
let mut style = HashSet::new();
|
||||
style.insert(OutputComponent::Plain);
|
||||
style.insert(StyleComponent::Plain);
|
||||
config.files = vec![InputFile::ThemePreviewFile];
|
||||
config.output_components = OutputComponents(style);
|
||||
config.style_components = StyleComponents(style);
|
||||
|
||||
let stdout = io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
|
@ -1,7 +1,8 @@
|
||||
use crate::inputfile::InputFile;
|
||||
use crate::line_range::{HighlightedLineRanges, LineRanges};
|
||||
use crate::style::{OutputComponents, OutputWrap};
|
||||
use crate::style::StyleComponents;
|
||||
use crate::syntax_mapping::SyntaxMapping;
|
||||
use crate::wrap::OutputWrap;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum PagingMode {
|
||||
@ -44,7 +45,7 @@ pub struct Config<'a> {
|
||||
pub true_color: bool,
|
||||
|
||||
/// Style elements (grid, line numbers, ...)
|
||||
pub output_components: OutputComponents,
|
||||
pub style_components: StyleComponents,
|
||||
|
||||
/// Text wrapping mode
|
||||
pub output_wrap: OutputWrap,
|
||||
|
@ -83,7 +83,7 @@ impl<'b> Controller<'b> {
|
||||
writer: &mut dyn Write,
|
||||
input_file: InputFile<'a>,
|
||||
) -> Result<()> {
|
||||
if !reader.first_line.is_empty() || self.config.output_components.header() {
|
||||
if !reader.first_line.is_empty() || self.config.style_components.header() {
|
||||
printer.print_header(writer, input_file)?;
|
||||
}
|
||||
|
||||
@ -118,7 +118,7 @@ impl<'b> Controller<'b> {
|
||||
}
|
||||
|
||||
RangeCheckResult::InRange => {
|
||||
if self.config.output_components.snip() {
|
||||
if self.config.style_components.snip() {
|
||||
if first_range {
|
||||
first_range = false;
|
||||
mid_range = true;
|
||||
|
@ -27,3 +27,4 @@ mod printer;
|
||||
pub mod style;
|
||||
pub mod syntax_mapping;
|
||||
mod terminal;
|
||||
pub mod wrap;
|
||||
|
@ -30,8 +30,8 @@ use crate::errors::*;
|
||||
use crate::inputfile::{InputFile, InputFileReader};
|
||||
use crate::line_range::RangeCheckResult;
|
||||
use crate::preprocessor::{expand_tabs, replace_nonprintable};
|
||||
use crate::style::OutputWrap;
|
||||
use crate::terminal::{as_terminal_escaped, to_ansi_color};
|
||||
use crate::wrap::OutputWrap;
|
||||
|
||||
pub trait Printer {
|
||||
fn print_header(&mut self, handle: &mut dyn Write, file: InputFile) -> Result<()>;
|
||||
@ -116,11 +116,11 @@ impl<'a> InteractivePrinter<'a> {
|
||||
// Create decorations.
|
||||
let mut decorations: Vec<Box<dyn Decoration>> = Vec::new();
|
||||
|
||||
if config.output_components.numbers() {
|
||||
if config.style_components.numbers() {
|
||||
decorations.push(Box::new(LineNumberDecoration::new(&colors)));
|
||||
}
|
||||
|
||||
if config.output_components.changes() {
|
||||
if config.style_components.changes() {
|
||||
decorations.push(Box::new(LineChangesDecoration::new(&colors)));
|
||||
}
|
||||
|
||||
@ -130,7 +130,7 @@ impl<'a> InteractivePrinter<'a> {
|
||||
// The grid border decoration isn't added until after the panel_width calculation, since the
|
||||
// print_horizontal_line, print_header, and print_footer functions all assume the panel
|
||||
// width is without the grid border.
|
||||
if config.output_components.grid() && !decorations.is_empty() {
|
||||
if config.style_components.grid() && !decorations.is_empty() {
|
||||
decorations.push(Box::new(GridBorderDecoration::new(&colors)));
|
||||
}
|
||||
|
||||
@ -152,7 +152,7 @@ impl<'a> InteractivePrinter<'a> {
|
||||
None
|
||||
} else {
|
||||
// Get the Git modifications
|
||||
line_changes = if config.output_components.changes() {
|
||||
line_changes = if config.style_components.changes() {
|
||||
match file {
|
||||
InputFile::Ordinary(filename) => get_git_diff(filename),
|
||||
_ => None,
|
||||
@ -206,7 +206,7 @@ impl<'a> InteractivePrinter<'a> {
|
||||
text_truncated,
|
||||
" ".repeat(self.panel_width - 1 - text_truncated.len())
|
||||
);
|
||||
if self.config.output_components.grid() {
|
||||
if self.config.style_components.grid() {
|
||||
format!("{} │ ", text_filled)
|
||||
} else {
|
||||
format!("{}", text_filled)
|
||||
@ -225,7 +225,7 @@ impl<'a> InteractivePrinter<'a> {
|
||||
|
||||
impl<'a> Printer for InteractivePrinter<'a> {
|
||||
fn print_header(&mut self, handle: &mut dyn Write, file: InputFile) -> Result<()> {
|
||||
if !self.config.output_components.header() {
|
||||
if !self.config.style_components.header() {
|
||||
if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
|
||||
let input = match file {
|
||||
InputFile::Ordinary(filename) => {
|
||||
@ -243,14 +243,14 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
input
|
||||
)?;
|
||||
} else {
|
||||
if self.config.output_components.grid() {
|
||||
if self.config.style_components.grid() {
|
||||
self.print_horizontal_line(handle, '┬')?;
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if self.config.output_components.grid() {
|
||||
if self.config.style_components.grid() {
|
||||
self.print_horizontal_line(handle, '┬')?;
|
||||
|
||||
write!(
|
||||
@ -286,7 +286,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
mode
|
||||
)?;
|
||||
|
||||
if self.config.output_components.grid() {
|
||||
if self.config.style_components.grid() {
|
||||
if self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable {
|
||||
self.print_horizontal_line(handle, '┼')?;
|
||||
} else {
|
||||
@ -298,7 +298,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
}
|
||||
|
||||
fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()> {
|
||||
if self.config.output_components.grid()
|
||||
if self.config.style_components.grid()
|
||||
&& (self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable)
|
||||
{
|
||||
self.print_horizontal_line(handle, '┴')
|
||||
|
86
src/style.rs
86
src/style.rs
@ -4,7 +4,7 @@ use std::str::FromStr;
|
||||
use crate::errors::*;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
|
||||
pub enum OutputComponent {
|
||||
pub enum StyleComponent {
|
||||
Auto,
|
||||
Changes,
|
||||
Grid,
|
||||
@ -15,92 +15,80 @@ pub enum OutputComponent {
|
||||
Plain,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
|
||||
pub enum OutputWrap {
|
||||
Character,
|
||||
None,
|
||||
}
|
||||
|
||||
impl Default for OutputWrap {
|
||||
fn default() -> Self {
|
||||
OutputWrap::None
|
||||
}
|
||||
}
|
||||
|
||||
impl OutputComponent {
|
||||
pub fn components(self, interactive_terminal: bool) -> &'static [OutputComponent] {
|
||||
impl StyleComponent {
|
||||
pub fn components(self, interactive_terminal: bool) -> &'static [StyleComponent] {
|
||||
match self {
|
||||
OutputComponent::Auto => {
|
||||
StyleComponent::Auto => {
|
||||
if interactive_terminal {
|
||||
OutputComponent::Full.components(interactive_terminal)
|
||||
StyleComponent::Full.components(interactive_terminal)
|
||||
} else {
|
||||
OutputComponent::Plain.components(interactive_terminal)
|
||||
StyleComponent::Plain.components(interactive_terminal)
|
||||
}
|
||||
}
|
||||
OutputComponent::Changes => &[OutputComponent::Changes],
|
||||
OutputComponent::Grid => &[OutputComponent::Grid],
|
||||
OutputComponent::Header => &[OutputComponent::Header],
|
||||
OutputComponent::Numbers => &[OutputComponent::Numbers],
|
||||
OutputComponent::Snip => &[OutputComponent::Snip],
|
||||
OutputComponent::Full => &[
|
||||
OutputComponent::Changes,
|
||||
OutputComponent::Grid,
|
||||
OutputComponent::Header,
|
||||
OutputComponent::Numbers,
|
||||
OutputComponent::Snip,
|
||||
StyleComponent::Changes => &[StyleComponent::Changes],
|
||||
StyleComponent::Grid => &[StyleComponent::Grid],
|
||||
StyleComponent::Header => &[StyleComponent::Header],
|
||||
StyleComponent::Numbers => &[StyleComponent::Numbers],
|
||||
StyleComponent::Snip => &[StyleComponent::Snip],
|
||||
StyleComponent::Full => &[
|
||||
StyleComponent::Changes,
|
||||
StyleComponent::Grid,
|
||||
StyleComponent::Header,
|
||||
StyleComponent::Numbers,
|
||||
StyleComponent::Snip,
|
||||
],
|
||||
OutputComponent::Plain => &[],
|
||||
StyleComponent::Plain => &[],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for OutputComponent {
|
||||
impl FromStr for StyleComponent {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
match s {
|
||||
"auto" => Ok(OutputComponent::Auto),
|
||||
"changes" => Ok(OutputComponent::Changes),
|
||||
"grid" => Ok(OutputComponent::Grid),
|
||||
"header" => Ok(OutputComponent::Header),
|
||||
"numbers" => Ok(OutputComponent::Numbers),
|
||||
"snip" => Ok(OutputComponent::Snip),
|
||||
"full" => Ok(OutputComponent::Full),
|
||||
"plain" => Ok(OutputComponent::Plain),
|
||||
"auto" => Ok(StyleComponent::Auto),
|
||||
"changes" => Ok(StyleComponent::Changes),
|
||||
"grid" => Ok(StyleComponent::Grid),
|
||||
"header" => Ok(StyleComponent::Header),
|
||||
"numbers" => Ok(StyleComponent::Numbers),
|
||||
"snip" => Ok(StyleComponent::Snip),
|
||||
"full" => Ok(StyleComponent::Full),
|
||||
"plain" => Ok(StyleComponent::Plain),
|
||||
_ => Err(format!("Unknown style '{}'", s).into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct OutputComponents(pub HashSet<OutputComponent>);
|
||||
pub struct StyleComponents(pub HashSet<StyleComponent>);
|
||||
|
||||
impl OutputComponents {
|
||||
pub fn new(components: &[OutputComponent]) -> OutputComponents {
|
||||
OutputComponents(components.iter().cloned().collect())
|
||||
impl StyleComponents {
|
||||
pub fn new(components: &[StyleComponent]) -> StyleComponents {
|
||||
StyleComponents(components.iter().cloned().collect())
|
||||
}
|
||||
|
||||
pub fn changes(&self) -> bool {
|
||||
self.0.contains(&OutputComponent::Changes)
|
||||
self.0.contains(&StyleComponent::Changes)
|
||||
}
|
||||
|
||||
pub fn grid(&self) -> bool {
|
||||
self.0.contains(&OutputComponent::Grid)
|
||||
self.0.contains(&StyleComponent::Grid)
|
||||
}
|
||||
|
||||
pub fn header(&self) -> bool {
|
||||
self.0.contains(&OutputComponent::Header)
|
||||
self.0.contains(&StyleComponent::Header)
|
||||
}
|
||||
|
||||
pub fn numbers(&self) -> bool {
|
||||
self.0.contains(&OutputComponent::Numbers)
|
||||
self.0.contains(&StyleComponent::Numbers)
|
||||
}
|
||||
|
||||
pub fn snip(&self) -> bool {
|
||||
self.0.contains(&OutputComponent::Snip)
|
||||
self.0.contains(&StyleComponent::Snip)
|
||||
}
|
||||
|
||||
pub fn plain(&self) -> bool {
|
||||
self.0.iter().all(|c| c == &OutputComponent::Plain)
|
||||
self.0.iter().all(|c| c == &StyleComponent::Plain)
|
||||
}
|
||||
}
|
||||
|
11
src/wrap.rs
Normal file
11
src/wrap.rs
Normal file
@ -0,0 +1,11 @@
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
|
||||
pub enum OutputWrap {
|
||||
Character,
|
||||
None,
|
||||
}
|
||||
|
||||
impl Default for OutputWrap {
|
||||
fn default() -> Self {
|
||||
OutputWrap::None
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user