1// `error_chain!` can recurse deeply
2#![recursion_limit = "1024"]
5extern crate error_chain;
11extern crate lazy_static;
13extern crate ansi_term;
16extern crate content_inspector;
17extern crate directories;
37use std::collections::HashSet;
43use ansi_term::Colour::Green;
46use app::{App, Config};
47use assets::{clear_assets, config_dir, HighlightingAssets};
48use controller::Controller;
49use inputfile::InputFile;
50use style::{OutputComponent, OutputComponents};
57 SyntectError(::syntect::LoadingError);
58 ParseIntError(::std::num::ParseIntError);
62 pub fn handle_error(error: &Error) {
64 &Error(ErrorKind::Io(ref io_error), _)
65 if io_error.kind() == super::io::ErrorKind::BrokenPipe =>
67 super::process::exit(0);
70 use ansi_term::Colour::Red;
71 eprintln!("{}: {}", Red.paint("[bat error]"), error);
79fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
80 if matches.is_present("init") {
81 let source_dir = matches.value_of("source").map(Path::new);
82 let target_dir = matches.value_of("target").map(Path::new);
84 let blank = matches.is_present("blank");
86 let assets = HighlightingAssets::from_files(source_dir, blank)?;
87 assets.save(target_dir)?;
88 } else if matches.is_present("clear") {
90 } else if matches.is_present("config-dir") {
91 writeln!(io::stdout(), "{}", config_dir())?;
97pub fn list_languages(config: &Config) -> Result<()> {
98 let assets = HighlightingAssets::new();
99 let mut languages = assets
103 .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
104 .collect::<Vec<_>>();
105 languages.sort_by_key(|lang| lang.name.to_uppercase());
107 let longest = languages
109 .map(|syntax| syntax.name.len())
111 .unwrap_or(32); // Fallback width if they have no language definitions.
113 let comma_separator = ", ";
115 // Line-wrapping for the possible file extension overflow.
116 let desired_width = config.term_width - longest - separator.len();
118 let stdout = io::stdout();
119 let mut stdout = stdout.lock();
121 let style = if config.colored_output {
127 for lang in languages {
128 write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
130 // Number of characters on this line so far, wrap before `desired_width`
131 let mut num_chars = 0;
133 let mut extension = lang.file_extensions.iter().peekable();
134 while let Some(word) = extension.next() {
135 // If we can't fit this word in, then create a line break and align it in.
136 let new_chars = word.len() + comma_separator.len();
137 if num_chars + new_chars >= desired_width {
139 write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
142 num_chars += new_chars;
143 write!(stdout, "{}", style.paint(&word[..]))?;
144 if extension.peek().is_some() {
145 write!(stdout, "{}", comma_separator)?;
154pub fn list_themes(cfg: &Config) -> Result<()> {
155 let assets = HighlightingAssets::new();
156 let themes = &assets.theme_set.themes;
157 let mut config = cfg.clone();
158 let mut style = HashSet::new();
159 style.insert(OutputComponent::Plain);
160 config.files = vec![InputFile::ThemePreviewFile];
161 config.output_components = OutputComponents(style);
163 let stdout = io::stdout();
164 let mut stdout = stdout.lock();
166 if config.colored_output {
167 for (theme, _) in themes.iter() {
171 Style::new().bold().paint(theme.to_string())
173 config.theme = theme.to_string();
174 let _controller = Controller::new(&config, &assets).run();
178 for (theme, _) in themes.iter() {
179 writeln!(stdout, "{}", theme)?;
186fn run_controller(config: &Config) -> Result<bool> {
187 let assets = HighlightingAssets::new();
188 let controller = Controller::new(&config, &assets);
192/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
193/// `Some(false)` if any intermediate errors occurred (were printed).
194fn run() -> Result<bool> {
195 let app = App::new();
197 match app.matches.subcommand() {
198 ("cache", Some(cache_matches)) => {
199 // If there is a file named 'cache' in the current working directory,
200 // arguments for subcommand 'cache' are not mandatory.
201 // If there are non-zero arguments, execute the subcommand cache, else, open the file cache.
202 if !cache_matches.args.is_empty() {
203 run_cache_subcommand(cache_matches)?;
206 let mut config = app.config()?;
207 config.files = vec![InputFile::Ordinary(&"cache")];
209 run_controller(&config)
213 let config = app.config()?;
215 if app.matches.is_present("list-languages") {
216 list_languages(&config)?;
219 } else if app.matches.is_present("list-themes") {
220 list_themes(&config)?;
224 run_controller(&config)
235 handle_error(&error);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list