1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-03-13 14:18:35 +00:00
This commit is contained in:
Nakul Chaudhari 2018-05-03 18:21:44 +02:00
commit 8e7c9341cb
5 changed files with 48 additions and 14 deletions

View File

@ -6,16 +6,26 @@ matrix:
- os: linux
rust: stable
env: TARGET=x86_64-unknown-linux-gnu
- os: linux
rust: stable
env:
- TARGET=x86_64-unknown-linux-musl
- CC_x86_64_unknown_linux_musl=/usr/bin/musl-gcc
- os: osx
rust: stable
env: TARGET=x86_64-apple-darwin
# Minimum Rust supported channel.
- os: linux
rust: 1.22.1
rust: 1.24.0
env: TARGET=x86_64-unknown-linux-gnu
- os: linux
rust: 1.24.0
env:
- TARGET=x86_64-unknown-linux-musl
- CC_x86_64_unknown_linux_musl=/usr/bin/musl-gcc
- os: osx
rust: 1.22.1
rust: 1.24.0
env: TARGET=x86_64-apple-darwin
addons:
@ -23,6 +33,8 @@ addons:
packages:
# needed for i686-unknown-linux-gnu target
- gcc-multilib
# needed for musl targets
- musl-tools
# needed to build deb packages
- fakeroot

2
Cargo.lock generated
View File

@ -61,7 +61,7 @@ dependencies = [
[[package]]
name = "bat"
version = "0.2.1"
version = "0.2.3"
dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -7,7 +7,7 @@ license = "MIT/Apache-2.0"
name = "bat"
readme = "README.md"
repository = "https://github.com/sharkdp/bat"
version = "0.2.1"
version = "0.2.3"
[dependencies]
atty = "0.2.2"

View File

@ -37,7 +37,7 @@ makepkg -si
### From source
If you want to build to compile `bat` from source, you need Rust 1.22 or higher.
If you want to build to compile `bat` from source, you need Rust 1.24 or higher.
You can then use `cargo` to build everything:
``` bash
@ -46,11 +46,12 @@ cargo install bat
## Customization
`bat` uses the excellent [`syntect`](https://github.com/trishume/syntect/) library for syntax highlighting. `syntect` can read any [Sublime Text `.sublime-syntax` files](https://www.sublimetext.com/docs/3/syntax.html) and themes.
`bat` uses the excellent [`syntect`](https://github.com/trishume/syntect/) library for syntax highlighting. `syntect` can read any [Sublime Text `.sublime-syntax` file](https://www.sublimetext.com/docs/3/syntax.html) and theme.
To build your own language-set and theme, follow these steps:
Create a folder with a syntax highlighting theme:
``` bash
mkdir -p ~/.config/bat/themes
cd ~/.config/bat/themes
@ -63,6 +64,7 @@ ln -s "sublime-monokai-extended/Monokai Extended.tmTheme" Default.tmTheme
```
Create a folder with language definition files:
``` bash
mkdir -p ~/.config/bat/syntax
cd ~/.config/bat/syntax
@ -75,6 +77,7 @@ git clone https://github.com/jonschlinkert/sublime-markdown-extended
Finally, use the following command to parse all these files into a binary
cache:
``` bash
bat init-cache
```

View File

@ -22,7 +22,7 @@ mod terminal;
use std::collections::HashMap;
use std::env;
use std::fs::{self, File};
use std::io::{self, BufRead, StdoutLock, Write};
use std::io::{self, BufRead, BufReader, StdoutLock, Write};
use std::path::Path;
use std::process;
@ -35,7 +35,7 @@ use directories::ProjectDirs;
use git2::{DiffOptions, IntoCString, Repository};
use syntect::dumps::{dump_to_file, from_binary, from_reader};
use syntect::easy::HighlightFile;
use syntect::easy::HighlightLines;
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::SyntaxSet;
@ -61,9 +61,10 @@ enum OptionsStyle {
Full,
}
struct Options {
struct Options<'a> {
true_color: bool,
style: OptionsStyle,
language: Option<&'a str>,
}
#[derive(Copy, Clone, Debug)]
@ -99,7 +100,16 @@ fn print_file<P: AsRef<Path>>(
filename: P,
line_changes: &Option<LineChanges>,
) -> Result<()> {
let mut highlighter = HighlightFile::new(filename.as_ref(), syntax_set, theme)?;
let reader = BufReader::new(File::open(filename.as_ref())?);
let syntax = match options.language {
Some(language) => syntax_set
.syntaxes()
.iter()
.find(|syntax| syntax.name.eq_ignore_ascii_case(language)),
None => syntax_set.find_syntax_for_file(filename.as_ref())?,
};
let syntax = syntax.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
let mut highlighter = HighlightLines::new(syntax, theme);
let stdout = io::stdout();
let mut handle = stdout.lock();
@ -120,10 +130,10 @@ fn print_file<P: AsRef<Path>>(
print_horizontal_line(&mut handle, '┼', term_width)?;
for (idx, maybe_line) in highlighter.reader.lines().enumerate() {
for (idx, maybe_line) in reader.lines().enumerate() {
let line_nr = idx + 1;
let line = maybe_line.unwrap_or_else(|_| "<INVALID UTF-8>".into());
let regions = highlighter.highlight_lines.highlight(&line);
let regions = highlighter.highlight(&line);
let line_change = if let Some(ref changes) = *line_changes {
match changes.get(&(line_nr as u32)) {
@ -357,6 +367,13 @@ fn run() -> Result<()> {
.setting(AppSettings::DisableVersion)
.max_term_width(90)
.about(crate_description!())
.arg(
Arg::with_name("language")
.short("l")
.long("language")
.help("Language of the file(s)")
.takes_value(true),
)
.arg(
Arg::with_name("FILE")
.help("File(s) to print")
@ -369,7 +386,8 @@ fn run() -> Result<()> {
.long("style")
.possible_values(&["plain", "line-numbers", "full"])
.default_value("full")
.help("Additional info to display alongwith content"))
.help("Additional info to display alongwith content"),
)
.subcommand(
SubCommand::with_name("init-cache")
.about("Load syntax definitions and themes into cache"),
@ -389,8 +407,9 @@ fn run() -> Result<()> {
style: match app_matches.value_of("style").unwrap() {
"plain" => OptionsStyle::Plain,
"line-numbers" => OptionsStyle::LineNumbers,
_ => OptionsStyle::Full
_ => OptionsStyle::Full,
},
language: app_matches.value_of("language"),
};
let assets =