mirror of
https://github.com/sharkdp/bat.git
synced 2025-04-14 06:40:39 +01:00
Easier configuration of style components
This commit is contained in:
parent
7a87315b94
commit
6a124591df
@ -19,7 +19,8 @@ use bat::{
|
|||||||
errors::*,
|
errors::*,
|
||||||
input::Input,
|
input::Input,
|
||||||
line_range::{HighlightedLineRanges, LineRange, LineRanges},
|
line_range::{HighlightedLineRanges, LineRange, LineRanges},
|
||||||
MappingTarget, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
|
style::{StyleComponent, StyleComponents},
|
||||||
|
MappingTarget, SyntaxMapping, WrappingMode,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn is_truecolor_terminal() -> bool {
|
fn is_truecolor_terminal() -> bool {
|
||||||
|
@ -26,8 +26,12 @@ use clap::crate_version;
|
|||||||
use directories::PROJECT_DIRS;
|
use directories::PROJECT_DIRS;
|
||||||
|
|
||||||
use bat::{
|
use bat::{
|
||||||
assets::HighlightingAssets, config::Config, controller::Controller, errors::*, input::Input,
|
assets::HighlightingAssets,
|
||||||
StyleComponent, StyleComponents,
|
config::Config,
|
||||||
|
controller::Controller,
|
||||||
|
errors::*,
|
||||||
|
input::Input,
|
||||||
|
style::{StyleComponent, StyleComponents},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
|
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
|
||||||
|
@ -32,13 +32,12 @@ mod output;
|
|||||||
mod preprocessor;
|
mod preprocessor;
|
||||||
mod pretty_printer;
|
mod pretty_printer;
|
||||||
pub(crate) mod printer;
|
pub(crate) mod printer;
|
||||||
pub(crate) mod style;
|
pub mod style;
|
||||||
pub(crate) mod syntax_mapping;
|
pub(crate) mod syntax_mapping;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
pub(crate) mod wrapping;
|
pub(crate) mod wrapping;
|
||||||
|
|
||||||
pub use line_range::LineRange;
|
pub use line_range::LineRange;
|
||||||
pub use pretty_printer::PrettyPrinter;
|
pub use pretty_printer::PrettyPrinter;
|
||||||
pub use style::{StyleComponent, StyleComponents};
|
|
||||||
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
|
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
|
||||||
pub use wrapping::WrappingMode;
|
pub use wrapping::WrappingMode;
|
||||||
|
@ -10,12 +10,22 @@ use crate::{
|
|||||||
errors::Result,
|
errors::Result,
|
||||||
input::Input,
|
input::Input,
|
||||||
line_range::{HighlightedLineRanges, LineRanges},
|
line_range::{HighlightedLineRanges, LineRanges},
|
||||||
LineRange, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
|
style::{StyleComponent, StyleComponents},
|
||||||
|
LineRange, SyntaxMapping, WrappingMode,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "paging")]
|
#[cfg(feature = "paging")]
|
||||||
use crate::config::PagingMode;
|
use crate::config::PagingMode;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ActiveStyleComponents {
|
||||||
|
header: bool,
|
||||||
|
vcs_modification_markers: bool,
|
||||||
|
grid: bool,
|
||||||
|
line_numbers: bool,
|
||||||
|
snip: bool,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct PrettyPrinter<'a> {
|
pub struct PrettyPrinter<'a> {
|
||||||
inputs: Vec<Input<'a>>,
|
inputs: Vec<Input<'a>>,
|
||||||
config: Config<'a>,
|
config: Config<'a>,
|
||||||
@ -23,6 +33,7 @@ pub struct PrettyPrinter<'a> {
|
|||||||
|
|
||||||
highlighted_lines: Vec<LineRange>,
|
highlighted_lines: Vec<LineRange>,
|
||||||
term_width: Option<usize>,
|
term_width: Option<usize>,
|
||||||
|
active_style_components: ActiveStyleComponents,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PrettyPrinter<'a> {
|
impl<'a> PrettyPrinter<'a> {
|
||||||
@ -39,6 +50,7 @@ impl<'a> PrettyPrinter<'a> {
|
|||||||
|
|
||||||
highlighted_lines: vec![],
|
highlighted_lines: vec![],
|
||||||
term_width: None,
|
term_width: None,
|
||||||
|
active_style_components: ActiveStyleComponents::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,9 +119,33 @@ impl<'a> PrettyPrinter<'a> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configure style elements like grid or line numbers (default: "full" style)
|
/// Whether to show a header with the file name
|
||||||
pub fn style_components(&mut self, components: &[StyleComponent]) -> &mut Self {
|
pub fn header(&mut self, yes: bool) -> &mut Self {
|
||||||
self.config.style_components = StyleComponents::new(components);
|
self.active_style_components.header = yes;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether to show line numbers
|
||||||
|
pub fn line_numbers(&mut self, yes: bool) -> &mut Self {
|
||||||
|
self.active_style_components.line_numbers = yes;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether to paint a grid, separating line numbers, git changes and the code
|
||||||
|
pub fn grid(&mut self, yes: bool) -> &mut Self {
|
||||||
|
self.active_style_components.grid = yes;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether to show modification markers for VCS changes
|
||||||
|
pub fn vcs_modification_markers(&mut self, yes: bool) -> &mut Self {
|
||||||
|
self.active_style_components.vcs_modification_markers = yes;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether to show "snip" markers between visible line ranges (default: no)
|
||||||
|
pub fn snip(&mut self, yes: bool) -> &mut Self {
|
||||||
|
self.active_style_components.snip = yes;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,6 +211,24 @@ impl<'a> PrettyPrinter<'a> {
|
|||||||
.term_width
|
.term_width
|
||||||
.unwrap_or_else(|| Term::stdout().size().1 as usize);
|
.unwrap_or_else(|| Term::stdout().size().1 as usize);
|
||||||
|
|
||||||
|
let mut style_components = vec![];
|
||||||
|
if self.active_style_components.grid {
|
||||||
|
style_components.push(StyleComponent::Grid);
|
||||||
|
}
|
||||||
|
if self.active_style_components.header {
|
||||||
|
style_components.push(StyleComponent::Header);
|
||||||
|
}
|
||||||
|
if self.active_style_components.line_numbers {
|
||||||
|
style_components.push(StyleComponent::LineNumbers);
|
||||||
|
}
|
||||||
|
if self.active_style_components.snip {
|
||||||
|
style_components.push(StyleComponent::Snip);
|
||||||
|
}
|
||||||
|
if self.active_style_components.vcs_modification_markers {
|
||||||
|
style_components.push(StyleComponent::Changes);
|
||||||
|
}
|
||||||
|
self.config.style_components = StyleComponents::new(&style_components);
|
||||||
|
|
||||||
let mut inputs: Vec<Input> = vec![];
|
let mut inputs: Vec<Input> = vec![];
|
||||||
std::mem::swap(&mut inputs, &mut self.inputs);
|
std::mem::swap(&mut inputs, &mut self.inputs);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user