mirror of
https://github.com/sharkdp/bat
synced 2024-11-26 22:00:30 +00:00
Code restructuring
This commit is contained in:
parent
fcfc7c465b
commit
de11558ad3
1 changed files with 16 additions and 11 deletions
25
src/main.rs
25
src/main.rs
|
@ -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)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue