2018-04-21 12:51:43 +02:00
|
|
|
extern crate ansi_term;
|
|
|
|
extern crate atty;
|
|
|
|
extern crate console;
|
2018-04-21 17:12:25 +02:00
|
|
|
extern crate git2;
|
2018-04-21 12:51:43 +02:00
|
|
|
extern crate syntect;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
|
|
|
|
2018-04-21 17:12:25 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{self, BufRead, Result};
|
2018-04-21 12:51:43 +02:00
|
|
|
use std::path::Path;
|
|
|
|
use std::process;
|
|
|
|
|
2018-04-21 17:36:57 +02:00
|
|
|
use ansi_term::Colour::{Fixed, Green, Red, Yellow, White};
|
2018-04-21 12:51:43 +02:00
|
|
|
use atty::Stream;
|
|
|
|
use clap::{App, AppSettings, Arg, ArgMatches};
|
|
|
|
use console::Term;
|
2018-04-21 17:12:25 +02:00
|
|
|
use git2::{DiffOptions, IntoCString, Repository};
|
2018-04-21 12:51:43 +02:00
|
|
|
|
|
|
|
use syntect::easy::HighlightFile;
|
|
|
|
use syntect::highlighting::ThemeSet;
|
|
|
|
use syntect::parsing::SyntaxSet;
|
|
|
|
use syntect::util::as_24_bit_terminal_escaped;
|
|
|
|
|
2018-04-21 17:12:25 +02:00
|
|
|
#[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<()> {
|
2018-04-21 12:51:43 +02:00
|
|
|
let ss = SyntaxSet::load_defaults_nonewlines();
|
2018-04-21 17:16:53 +02:00
|
|
|
let ts = ThemeSet::load_defaults();
|
|
|
|
let theme = &ts.themes["base16-eighties.dark"];
|
2018-04-21 12:51:43 +02:00
|
|
|
|
2018-04-21 17:36:57 +02:00
|
|
|
let mut highlighter = HighlightFile::new(filename.as_ref().clone(), &ss, theme)?;
|
2018-04-21 12:51:43 +02:00
|
|
|
|
|
|
|
let term = Term::stdout();
|
|
|
|
let (_height, width) = term.size();
|
|
|
|
|
2018-04-21 17:12:25 +02:00
|
|
|
let prefix = "───────┬";
|
2018-04-21 12:51:43 +02:00
|
|
|
let line = "─".repeat(width as usize - prefix.len());
|
|
|
|
println!("{}{}", Fixed(238).paint(prefix), Fixed(238).paint(line));
|
|
|
|
|
2018-04-21 17:36:57 +02:00
|
|
|
println!(
|
|
|
|
" {} {}",
|
|
|
|
Fixed(238).paint("│"),
|
|
|
|
White.bold().paint(filename.as_ref().to_string_lossy())
|
|
|
|
);
|
|
|
|
|
|
|
|
let prefix = "───────┼";
|
|
|
|
let line = "─".repeat(width as usize - prefix.len());
|
|
|
|
println!("{}{}", Fixed(238).paint(prefix), Fixed(238).paint(line));
|
|
|
|
|
2018-04-21 17:12:25 +02:00
|
|
|
for (idx, maybe_line) in highlighter.reader.lines().enumerate() {
|
|
|
|
let line_nr = idx + 1;
|
2018-04-21 12:51:43 +02:00
|
|
|
let line = maybe_line.unwrap_or("<INVALID UTF-8>".into());
|
|
|
|
let regions = highlighter.highlight_lines.highlight(&line);
|
|
|
|
|
2018-04-21 17:12:25 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-04-21 12:51:43 +02:00
|
|
|
println!(
|
2018-04-21 17:12:25 +02:00
|
|
|
"{} {} {} {}",
|
2018-04-21 12:51:43 +02:00
|
|
|
Fixed(244).paint(format!("{:4}", line_nr)),
|
2018-04-21 17:12:25 +02:00
|
|
|
line_change,
|
2018-04-21 12:51:43 +02:00
|
|
|
Fixed(238).paint("│"),
|
|
|
|
as_24_bit_terminal_escaped(®ions, false)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-21 17:12:25 +02:00
|
|
|
let prefix = "───────┴";
|
|
|
|
let line = "─".repeat(width as usize - prefix.len());
|
|
|
|
println!("{}{}", Fixed(238).paint(prefix), Fixed(238).paint(line));
|
|
|
|
|
2018-04-21 12:51:43 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-04-21 17:12:25 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-04-21 12:51:43 +02:00
|
|
|
fn run(matches: &ArgMatches) -> Result<()> {
|
|
|
|
if let Some(files) = matches.values_of("file") {
|
|
|
|
for file in files {
|
2018-04-21 17:12:25 +02:00
|
|
|
let line_changes = get_line_changes(file.to_string());
|
|
|
|
print_file(file, line_changes)?;
|
2018-04-21 12:51:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let clap_color_setting = if atty::is(Stream::Stdout) {
|
|
|
|
AppSettings::ColoredHelp
|
|
|
|
} else {
|
|
|
|
AppSettings::ColorNever
|
|
|
|
};
|
|
|
|
|
|
|
|
let matches = App::new("bat")
|
|
|
|
.version(crate_version!())
|
|
|
|
.setting(clap_color_setting)
|
|
|
|
.setting(AppSettings::DeriveDisplayOrder)
|
|
|
|
.setting(AppSettings::UnifiedHelpMessage)
|
|
|
|
.setting(AppSettings::NextLineHelp)
|
|
|
|
.max_term_width(90)
|
|
|
|
.about("A cat(1) clone with wings.")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("file")
|
|
|
|
.help("Files to print")
|
|
|
|
.multiple(true)
|
|
|
|
.empty_values(false),
|
|
|
|
)
|
|
|
|
.help_message("Print this help message.")
|
|
|
|
.version_message("Show version information.")
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let result = run(&matches);
|
|
|
|
|
|
|
|
if let Err(e) = result {
|
2018-04-21 17:12:25 +02:00
|
|
|
eprintln!("{}: {}", Red.paint("bat error"), e);
|
2018-04-21 12:51:43 +02:00
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|