mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-15 15:18:45 +00:00
Create OutputComponents
struct, use HashSet
This commit is contained in:
parent
71684ead20
commit
dd98a89430
32
src/main.rs
32
src/main.rs
@ -20,7 +20,7 @@ extern crate syntect;
|
|||||||
mod printer;
|
mod printer;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{self, BufRead, BufReader, Write};
|
use std::io::{self, BufRead, BufReader, Write};
|
||||||
@ -71,7 +71,7 @@ mod errors {
|
|||||||
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
|
||||||
pub enum OutputComponent {
|
pub enum OutputComponent {
|
||||||
Changes,
|
Changes,
|
||||||
Grid,
|
Grid,
|
||||||
@ -115,9 +115,29 @@ impl FromStr for OutputComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct OutputComponents(HashSet<OutputComponent>);
|
||||||
|
|
||||||
|
impl OutputComponents {
|
||||||
|
fn changes(&self) -> bool {
|
||||||
|
self.0.contains(&OutputComponent::Changes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn grid(&self) -> bool {
|
||||||
|
self.0.contains(&OutputComponent::Grid)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn header(&self) -> bool {
|
||||||
|
self.0.contains(&OutputComponent::Header)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn numbers(&self) -> bool {
|
||||||
|
self.0.contains(&OutputComponent::Numbers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Options<'a> {
|
pub struct Options<'a> {
|
||||||
pub true_color: bool,
|
pub true_color: bool,
|
||||||
pub output_components: &'a [OutputComponent],
|
pub output_components: OutputComponents,
|
||||||
pub language: Option<&'a str>,
|
pub language: Option<&'a str>,
|
||||||
pub colored_output: bool,
|
pub colored_output: bool,
|
||||||
pub paging: bool,
|
pub paging: bool,
|
||||||
@ -601,14 +621,14 @@ fn run() -> Result<()> {
|
|||||||
let output_components = values_t!(app_matches.values_of("style"), OutputComponent)?
|
let output_components = values_t!(app_matches.values_of("style"), OutputComponent)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|style| style.components())
|
.map(|style| style.components())
|
||||||
.fold(vec![], |mut acc, x| {
|
.fold(HashSet::new(), |mut acc, components| {
|
||||||
acc.extend_from_slice(x);
|
acc.extend(components.iter().cloned());
|
||||||
acc
|
acc
|
||||||
});
|
});
|
||||||
|
|
||||||
let options = Options {
|
let options = Options {
|
||||||
true_color: is_truecolor_terminal(),
|
true_color: is_truecolor_terminal(),
|
||||||
output_components: &output_components,
|
output_components: OutputComponents(output_components),
|
||||||
language: app_matches.value_of("language"),
|
language: app_matches.value_of("language"),
|
||||||
colored_output: match app_matches.value_of("color") {
|
colored_output: match app_matches.value_of("color") {
|
||||||
Some("always") => true,
|
Some("always") => true,
|
||||||
|
@ -4,7 +4,7 @@ use std::borrow::Cow;
|
|||||||
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 {Colors, LineChange, LineChanges, Options, OutputComponent};
|
use {Colors, LineChange, LineChanges, Options};
|
||||||
|
|
||||||
const PANEL_WIDTH: usize = 7;
|
const PANEL_WIDTH: usize = 7;
|
||||||
|
|
||||||
@ -32,17 +32,11 @@ impl<'a> Printer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
|
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
|
||||||
if !self.options
|
if !self.options.output_components.header() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Header)
|
|
||||||
{
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.options
|
if self.options.output_components.grid() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Grid)
|
|
||||||
{
|
|
||||||
self.print_horizontal_line('┬')?;
|
self.print_horizontal_line('┬')?;
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
@ -70,10 +64,7 @@ impl<'a> Printer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_footer(&mut self) -> Result<()> {
|
pub fn print_footer(&mut self) -> Result<()> {
|
||||||
if self.options
|
if self.options.output_components.grid() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Grid)
|
|
||||||
{
|
|
||||||
self.print_horizontal_line('┴')
|
self.print_horizontal_line('┴')
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -98,9 +89,7 @@ impl<'a> Printer<'a> {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
let grid_requested = self.options
|
let grid_requested = self.options.output_components.grid();
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Grid);
|
|
||||||
write!(
|
write!(
|
||||||
self.handle,
|
self.handle,
|
||||||
"{}",
|
"{}",
|
||||||
@ -119,10 +108,7 @@ impl<'a> Printer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_line_number<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
|
fn print_line_number<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
|
||||||
if self.options
|
if self.options.output_components.numbers() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Numbers)
|
|
||||||
{
|
|
||||||
Some(
|
Some(
|
||||||
self.colors
|
self.colors
|
||||||
.line_number
|
.line_number
|
||||||
@ -130,10 +116,7 @@ impl<'a> Printer<'a> {
|
|||||||
.to_string()
|
.to_string()
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
} else if self.options
|
} else if self.options.output_components.grid() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Grid)
|
|
||||||
{
|
|
||||||
Some(" ".into())
|
Some(" ".into())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -141,10 +124,7 @@ impl<'a> Printer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_git_marker<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
|
fn print_git_marker<'s>(&self, line_number: usize) -> Option<Cow<'s, str>> {
|
||||||
if self.options
|
if self.options.output_components.changes() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Changes)
|
|
||||||
{
|
|
||||||
Some(
|
Some(
|
||||||
if let Some(ref changes) = self.line_changes {
|
if let Some(ref changes) = self.line_changes {
|
||||||
match changes.get(&(line_number as u32)) {
|
match changes.get(&(line_number as u32)) {
|
||||||
@ -159,10 +139,7 @@ impl<'a> Printer<'a> {
|
|||||||
}.to_string()
|
}.to_string()
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
} else if self.options
|
} else if self.options.output_components.grid() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Grid)
|
|
||||||
{
|
|
||||||
Some(" ".into())
|
Some(" ".into())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -170,10 +147,7 @@ impl<'a> Printer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_line_border<'s>(&self) -> Option<Cow<'s, str>> {
|
fn print_line_border<'s>(&self) -> Option<Cow<'s, str>> {
|
||||||
if self.options
|
if self.options.output_components.grid() {
|
||||||
.output_components
|
|
||||||
.contains(&OutputComponent::Grid)
|
|
||||||
{
|
|
||||||
Some(self.colors.grid.paint("│").to_string().into())
|
Some(self.colors.grid.paint("│").to_string().into())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user