Code restructuring

This commit is contained in:
sharkdp 2018-04-22 14:05:43 +02:00
parent fcfc7c465b
commit de11558ad3

View file

@ -13,6 +13,7 @@ use std::path::Path;
use std::process; use std::process;
use ansi_term::Colour::{Fixed, Green, Red, White, Yellow}; use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
use ansi_term::Style;
use atty::Stream; use atty::Stream;
use clap::{App, AppSettings, Arg, ArgMatches}; use clap::{App, AppSettings, Arg, ArgMatches};
use console::Term; use console::Term;
@ -33,22 +34,23 @@ enum LineChange {
type LineChanges = HashMap<u32, LineChange>; type LineChanges = HashMap<u32, LineChange>;
const PANEL_WIDTH: usize = 7;
const GRID_COLOR: u8 = 238; const GRID_COLOR: u8 = 238;
fn print_horizontal_line(grid_char: char, term_width: usize) { fn print_horizontal_line(grid_char: char, term_width: usize) {
let prefix = format!("{}{}", "".repeat(7), grid_char); let bar = "".repeat(term_width - (PANEL_WIDTH + 1));
let line = "".repeat(term_width - prefix.len()); let line = format!("{}{}{}", "".repeat(PANEL_WIDTH), grid_char, bar);
println!("{}{}", Fixed(GRID_COLOR).paint(prefix), Fixed(GRID_COLOR).paint(line));
println!("{}", Fixed(GRID_COLOR).paint(line));
} }
fn print_file<P: AsRef<Path>>( fn print_file<P: AsRef<Path>>(
theme: &Theme, theme: &Theme,
syntax_set: &SyntaxSet,
filename: P, filename: P,
line_changes: Option<LineChanges>, line_changes: Option<LineChanges>,
) -> io::Result<()> { ) -> io::Result<()> {
let ss = SyntaxSet::load_defaults_nonewlines(); let mut highlighter = HighlightFile::new(filename.as_ref().clone(), syntax_set, theme)?;
let mut highlighter = HighlightFile::new(filename.as_ref().clone(), &ss, &theme)?;
let term = Term::stdout(); let term = Term::stdout();
let (_, term_width) = term.size(); let (_, term_width) = term.size();
@ -57,7 +59,8 @@ fn print_file<P: AsRef<Path>>(
print_horizontal_line('┬', term_width); print_horizontal_line('┬', term_width);
println!( println!(
" {} {}", "{}{} {}",
" ".repeat(PANEL_WIDTH),
Fixed(GRID_COLOR).paint(""), Fixed(GRID_COLOR).paint(""),
White.bold().paint(filename.as_ref().to_string_lossy()) White.bold().paint(filename.as_ref().to_string_lossy())
); );
@ -75,10 +78,10 @@ fn print_file<P: AsRef<Path>>(
Some(&LineChange::RemovedAbove) => Red.paint(""), Some(&LineChange::RemovedAbove) => Red.paint(""),
Some(&LineChange::RemovedBelow) => Red.paint("_"), Some(&LineChange::RemovedBelow) => Red.paint("_"),
Some(&LineChange::Modified) => Yellow.paint("~"), Some(&LineChange::Modified) => Yellow.paint("~"),
_ => Fixed(1).paint(" "), // TODO _ => Style::default().paint(" "),
} }
} else { } else {
Fixed(1).paint(" ") // TODO Style::default().paint(" ")
}; };
println!( println!(
@ -152,10 +155,12 @@ fn run(matches: &ArgMatches) -> Result<()> {
let theme_set = ThemeSet::load_from_folder("/home/shark/Informatik/rust/bat/themes").unwrap(); let theme_set = ThemeSet::load_from_folder("/home/shark/Informatik/rust/bat/themes").unwrap();
let theme = &theme_set.themes["Monokai"]; let theme = &theme_set.themes["Monokai"];
let syntax_set = SyntaxSet::load_defaults_nonewlines();
if let Some(files) = matches.values_of("FILE") { if let Some(files) = matches.values_of("FILE") {
for file in files { for file in files {
let line_changes = get_line_changes(file.to_string()); let line_changes = get_line_changes(file.to_string());
print_file(theme, file, line_changes)?; print_file(theme, &syntax_set, file, line_changes)?;
} }
} }