mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-15 15:18:45 +00:00
Add support for reading from stdin, closes #2
This commit is contained in:
parent
53d67e2b6e
commit
3d47816bd5
55
src/main.rs
55
src/main.rs
@ -146,23 +146,30 @@ impl Colors {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_file<P: AsRef<Path>>(
|
fn print_file(
|
||||||
options: &Options,
|
options: &Options,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
syntax_set: &SyntaxSet,
|
syntax_set: &SyntaxSet,
|
||||||
printer: &mut Printer,
|
printer: &mut Printer,
|
||||||
filename: P,
|
filename: Option<&str>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut reader = BufReader::new(File::open(filename.as_ref())?);
|
let stdin = io::stdin(); // TODO: this is not always needed
|
||||||
let syntax = match options.language {
|
|
||||||
Some(language) => syntax_set.find_syntax_by_token(language),
|
let mut reader: Box<BufRead> = match filename {
|
||||||
None => syntax_set.find_syntax_for_file(filename.as_ref())?,
|
None => Box::new(stdin.lock()),
|
||||||
|
Some(filename) => Box::new(BufReader::new(File::open(filename)?)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let syntax = match (options.language, filename) {
|
||||||
|
(Some(language), _) => syntax_set.find_syntax_by_token(language),
|
||||||
|
(None, Some(filename)) => syntax_set.find_syntax_for_file(filename)?,
|
||||||
|
(None, None) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let syntax = syntax.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
|
let syntax = syntax.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
|
||||||
let mut highlighter = HighlightLines::new(syntax, theme);
|
let mut highlighter = HighlightLines::new(syntax, theme);
|
||||||
|
|
||||||
printer.print_header(filename.as_ref().to_string_lossy().as_ref())?;
|
printer.print_header(filename)?;
|
||||||
|
|
||||||
let mut line_nr = 1;
|
let mut line_nr = 1;
|
||||||
let mut line_buffer = String::new();
|
let mut line_buffer = String::new();
|
||||||
@ -486,16 +493,30 @@ fn run() -> Result<()> {
|
|||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if let Some(files) = app_matches.values_of("FILE") {
|
let files: Vec<Option<&str>> = app_matches
|
||||||
let stdout = io::stdout();
|
.values_of("FILE")
|
||||||
let mut output_type = get_output_type(&stdout, options.paging);
|
.map(|values| {
|
||||||
let handle = output_type.handle()?;
|
values
|
||||||
let mut printer = Printer::new(handle, &options);
|
.map(|filename| {
|
||||||
for file in files {
|
if filename == "-" {
|
||||||
let line_changes = get_git_diff(&file.to_string());
|
None
|
||||||
printer.line_changes = line_changes;
|
} else {
|
||||||
print_file(&options, theme, &assets.syntax_set, &mut printer, file)?;
|
Some(filename)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| vec![None]); // read from stdin (None) if no args are given
|
||||||
|
|
||||||
|
let stdout = io::stdout();
|
||||||
|
let mut output_type = get_output_type(&stdout, options.paging);
|
||||||
|
let handle = output_type.handle()?;
|
||||||
|
let mut printer = Printer::new(handle, &options);
|
||||||
|
|
||||||
|
for file in files {
|
||||||
|
printer.line_changes = file.and_then(|filename| get_git_diff(filename));
|
||||||
|
|
||||||
|
print_file(&options, theme, &assets.syntax_set, &mut printer, file)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ impl<'a> Printer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_header(&mut self, filename: &str) -> Result<()> {
|
pub fn print_header(&mut self, filename: Option<&str>) -> Result<()> {
|
||||||
match self.options.style {
|
match self.options.style {
|
||||||
OptionsStyle::Full => {}
|
OptionsStyle::Full => {}
|
||||||
_ => return Ok(()),
|
_ => return Ok(()),
|
||||||
@ -44,14 +44,22 @@ impl<'a> Printer<'a> {
|
|||||||
|
|
||||||
self.print_horizontal_line('┬')?;
|
self.print_horizontal_line('┬')?;
|
||||||
|
|
||||||
writeln!(
|
write!(
|
||||||
self.handle,
|
self.handle,
|
||||||
"{}{} File {}",
|
"{}{} ",
|
||||||
" ".repeat(PANEL_WIDTH),
|
" ".repeat(PANEL_WIDTH),
|
||||||
self.colors.grid.paint("│"),
|
self.colors.grid.paint("│"),
|
||||||
self.colors.filename.paint(filename)
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
match filename {
|
||||||
|
None => {
|
||||||
|
writeln!(self.handle, "STDIN",)?;
|
||||||
|
}
|
||||||
|
Some(filename) => {
|
||||||
|
writeln!(self.handle, "File {}", self.colors.filename.paint(filename))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.print_horizontal_line('┼')
|
self.print_horizontal_line('┼')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,6 +97,8 @@ impl<'a> Printer<'a> {
|
|||||||
.join(" ")
|
.join(" ")
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
self.handle.flush()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user