Add --style option to disable line numbers and git markers (#43)

closes #5
pull/53/head
nakulcg 6 years ago committed by David Peter
parent 4bba08062c
commit 7df9a5fe82

@ -46,7 +46,7 @@ lazy_static! {
} }
mod errors { mod errors {
error_chain!{ error_chain! {
foreign_links { foreign_links {
Io(::std::io::Error); Io(::std::io::Error);
} }
@ -55,8 +55,15 @@ mod errors {
use errors::*; use errors::*;
enum OptionsStyle {
Plain,
LineNumbers,
Full,
}
struct Options<'a> { struct Options<'a> {
true_color: bool, true_color: bool,
style: OptionsStyle,
language: Option<&'a str>, language: Option<&'a str>,
} }
@ -95,7 +102,9 @@ fn print_file<P: AsRef<Path>>(
) -> Result<()> { ) -> Result<()> {
let reader = BufReader::new(File::open(filename.as_ref())?); let reader = BufReader::new(File::open(filename.as_ref())?);
let syntax = match options.language { let syntax = match options.language {
Some(language) => syntax_set.syntaxes().iter() Some(language) => syntax_set
.syntaxes()
.iter()
.find(|syntax| syntax.name.eq_ignore_ascii_case(language)), .find(|syntax| syntax.name.eq_ignore_ascii_case(language)),
None => syntax_set.find_syntax_for_file(filename.as_ref())?, None => syntax_set.find_syntax_for_file(filename.as_ref())?,
}; };
@ -141,8 +150,14 @@ fn print_file<P: AsRef<Path>>(
writeln!( writeln!(
handle, handle,
"{} {} {} {}", "{} {} {} {}",
Fixed(244).paint(format!("{:4}", line_nr)), Fixed(244).paint(match options.style {
line_change, OptionsStyle::Plain => " ".to_owned(),
_ => format!("{:4}", line_nr),
}),
match options.style {
OptionsStyle::Full => line_change,
_ => Style::default().paint(" "),
},
Fixed(GRID_COLOR).paint("│"), Fixed(GRID_COLOR).paint("│"),
as_terminal_escaped(&regions, options.true_color) as_terminal_escaped(&regions, options.true_color)
)?; )?;
@ -352,11 +367,12 @@ fn run() -> Result<()> {
.setting(AppSettings::DisableVersion) .setting(AppSettings::DisableVersion)
.max_term_width(90) .max_term_width(90)
.about(crate_description!()) .about(crate_description!())
.arg(Arg::with_name("language") .arg(
.short("l") Arg::with_name("language")
.long("language") .short("l")
.help("Language of the file(s)") .long("language")
.takes_value(true) .help("Language of the file(s)")
.takes_value(true),
) )
.arg( .arg(
Arg::with_name("FILE") Arg::with_name("FILE")
@ -364,6 +380,14 @@ fn run() -> Result<()> {
.multiple(true) .multiple(true)
.empty_values(false), .empty_values(false),
) )
.arg(
Arg::with_name("style")
.short("s")
.long("style")
.possible_values(&["plain", "line-numbers", "full"])
.default_value("full")
.help("Additional info to display alongwith content"),
)
.subcommand( .subcommand(
SubCommand::with_name("init-cache") SubCommand::with_name("init-cache")
.about("Load syntax definitions and themes into cache"), .about("Load syntax definitions and themes into cache"),
@ -380,6 +404,11 @@ fn run() -> Result<()> {
_ => { _ => {
let options = Options { let options = Options {
true_color: is_truecolor_terminal(), true_color: is_truecolor_terminal(),
style: match app_matches.value_of("style").unwrap() {
"plain" => OptionsStyle::Plain,
"line-numbers" => OptionsStyle::LineNumbers,
_ => OptionsStyle::Full,
},
language: app_matches.value_of("language"), language: app_matches.value_of("language"),
}; };
@ -411,7 +440,7 @@ fn main() {
if let Err(error) = result { if let Err(error) = result {
match error { match error {
Error(ErrorKind::Io(ref io_error), _) Error(ErrorKind::Io(ref io_error), _)
if io_error.kind() == io::ErrorKind::BrokenPipe => {} if io_error.kind() == io::ErrorKind::BrokenPipe => {}
_ => { _ => {
eprintln!("{}: {}", Red.paint("[bat error]"), error); eprintln!("{}: {}", Red.paint("[bat error]"), error);

Loading…
Cancel
Save