mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-02 03:12:25 +01:00
Add Git support, closes #8
This commit is contained in:
104
src/main.rs
104
src/main.rs
@@ -1,59 +1,143 @@
|
||||
extern crate ansi_term;
|
||||
extern crate atty;
|
||||
extern crate console;
|
||||
extern crate git2;
|
||||
extern crate syntect;
|
||||
|
||||
#[macro_use]
|
||||
extern crate clap;
|
||||
|
||||
use std::io::{BufRead, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::io::{self, BufRead, Result};
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
|
||||
use ansi_term::Colour::Fixed;
|
||||
use ansi_term::Colour::{Fixed, Green, Red, Yellow};
|
||||
use atty::Stream;
|
||||
use clap::{App, AppSettings, Arg, ArgMatches};
|
||||
use console::Term;
|
||||
use git2::{DiffOptions, IntoCString, Repository};
|
||||
|
||||
use syntect::easy::HighlightFile;
|
||||
use syntect::highlighting::ThemeSet;
|
||||
use syntect::parsing::SyntaxSet;
|
||||
use syntect::util::as_24_bit_terminal_escaped;
|
||||
|
||||
fn print_file<P: AsRef<Path>>(filename: P) -> Result<()> {
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
enum LineChange {
|
||||
Added,
|
||||
RemovedAbove,
|
||||
RemovedBelow,
|
||||
Modified,
|
||||
}
|
||||
|
||||
type LineChanges = HashMap<u32, LineChange>;
|
||||
|
||||
fn print_file<P: AsRef<Path>>(filename: P, line_changes: Option<LineChanges>) -> io::Result<()> {
|
||||
let ss = SyntaxSet::load_defaults_nonewlines();
|
||||
let ts = ThemeSet::load_from_folder("/home/shark/Informatik/rust/bat/themes").unwrap();
|
||||
let theme = &ts.themes["Monokai"];
|
||||
|
||||
let mut highlighter = HighlightFile::new(filename, &ss, theme).unwrap();
|
||||
let mut highlighter = HighlightFile::new(filename, &ss, theme)?;
|
||||
|
||||
let term = Term::stdout();
|
||||
let (_height, width) = term.size();
|
||||
|
||||
println!("{}", width);
|
||||
let prefix = " ┌";
|
||||
let prefix = "───────┬";
|
||||
let line = "─".repeat(width as usize - prefix.len());
|
||||
println!("{}{}", Fixed(238).paint(prefix), Fixed(238).paint(line));
|
||||
|
||||
for (line_nr, maybe_line) in highlighter.reader.lines().enumerate() {
|
||||
for (idx, maybe_line) in highlighter.reader.lines().enumerate() {
|
||||
let line_nr = idx + 1;
|
||||
let line = maybe_line.unwrap_or("<INVALID UTF-8>".into());
|
||||
let regions = highlighter.highlight_lines.highlight(&line);
|
||||
|
||||
let line_change = if let Some(ref changes) = line_changes {
|
||||
match changes.get(&(line_nr as u32)) {
|
||||
Some(&LineChange::Added) => Green.paint("+"),
|
||||
Some(&LineChange::RemovedAbove) => Red.paint("‾"),
|
||||
Some(&LineChange::RemovedBelow) => Red.paint("_"),
|
||||
Some(&LineChange::Modified) => Yellow.paint("~"),
|
||||
_ => Fixed(1).paint(" "), // TODO
|
||||
}
|
||||
} else {
|
||||
Fixed(1).paint(" ") // TODO
|
||||
};
|
||||
|
||||
println!(
|
||||
"{} {} {}",
|
||||
"{} {} {} {}",
|
||||
Fixed(244).paint(format!("{:4}", line_nr)),
|
||||
line_change,
|
||||
Fixed(238).paint("│"),
|
||||
as_24_bit_terminal_escaped(®ions, false)
|
||||
);
|
||||
}
|
||||
|
||||
let prefix = "───────┴";
|
||||
let line = "─".repeat(width as usize - prefix.len());
|
||||
println!("{}{}", Fixed(238).paint(prefix), Fixed(238).paint(line));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_line_changes(filename: String) -> Option<LineChanges> {
|
||||
let repo = Repository::open_from_env().ok()?;
|
||||
|
||||
let mut diff_options = DiffOptions::new();
|
||||
diff_options.pathspec(filename.into_c_string().unwrap());
|
||||
diff_options.context_lines(0);
|
||||
|
||||
let diff = repo.diff_index_to_workdir(None, Some(&mut diff_options))
|
||||
.unwrap();
|
||||
|
||||
let mut line_changes: LineChanges = HashMap::new();
|
||||
|
||||
let mark_section =
|
||||
|line_changes: &mut LineChanges, start: u32, end: i32, change: LineChange| {
|
||||
for line in start..(end + 1) as u32 {
|
||||
line_changes.insert(line, change);
|
||||
}
|
||||
};
|
||||
|
||||
let _ = diff.foreach(
|
||||
&mut |_, _| true,
|
||||
None,
|
||||
Some(&mut |_, hunk| {
|
||||
let old_lines = hunk.old_lines();
|
||||
let new_start = hunk.new_start();
|
||||
let new_lines = hunk.new_lines();
|
||||
let new_end = (new_start + new_lines) as i32 - 1;
|
||||
|
||||
if old_lines == 0 && new_lines > 0 {
|
||||
mark_section(&mut line_changes, new_start, new_end, LineChange::Added);
|
||||
} else if new_lines == 0 && old_lines > 0 {
|
||||
if new_start <= 0 {
|
||||
mark_section(&mut line_changes, 1, 1, LineChange::RemovedAbove);
|
||||
} else {
|
||||
mark_section(
|
||||
&mut line_changes,
|
||||
new_start,
|
||||
new_start as i32,
|
||||
LineChange::RemovedBelow,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
mark_section(&mut line_changes, new_start, new_end, LineChange::Modified);
|
||||
}
|
||||
|
||||
true
|
||||
}),
|
||||
None,
|
||||
);
|
||||
|
||||
Some(line_changes)
|
||||
}
|
||||
|
||||
fn run(matches: &ArgMatches) -> Result<()> {
|
||||
if let Some(files) = matches.values_of("file") {
|
||||
for file in files {
|
||||
print_file(file)?;
|
||||
let line_changes = get_line_changes(file.to_string());
|
||||
print_file(file, line_changes)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +172,7 @@ fn main() {
|
||||
let result = run(&matches);
|
||||
|
||||
if let Err(e) = result {
|
||||
eprintln!("Error: {}", e);
|
||||
eprintln!("{}: {}", Red.paint("bat error"), e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user