1// `error_chain!` can recurse deeply
2#![recursion_limit = "1024"]
5extern crate error_chain;
11extern crate lazy_static;
13extern crate ansi_term;
16extern crate directories;
31use std::collections::HashSet;
37use ansi_term::Colour::Green;
40use app::{App, Config, InputFile};
41use assets::{clear_assets, config_dir, HighlightingAssets};
42use controller::Controller;
43use style::{OutputComponent, OutputComponents};
50 SyntectError(::syntect::LoadingError);
51 ParseIntError(::std::num::ParseIntError);
55 pub fn handle_error(error: &Error) {
57 &Error(ErrorKind::Io(ref io_error), _)
58 if io_error.kind() == super::io::ErrorKind::BrokenPipe =>
60 super::process::exit(0);
63 use ansi_term::Colour::Red;
64 eprintln!("{}: {}", Red.paint("[bat error]"), error);
72fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
73 if matches.is_present("init") {
74 let source_dir = matches.value_of("source").map(Path::new);
75 let target_dir = matches.value_of("target").map(Path::new);
77 let blank = matches.is_present("blank");
79 let assets = HighlightingAssets::from_files(source_dir, blank)?;
80 assets.save(target_dir)?;
81 } else if matches.is_present("clear") {
83 } else if matches.is_present("config-dir") {
84 writeln!(io::stdout(), "{}", config_dir())?;
90pub fn list_languages(assets: &HighlightingAssets, config: &Config) -> Result<()> {
91 let mut languages = assets
95 .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
97 languages.sort_by_key(|lang| lang.name.to_uppercase());
99 let longest = languages
101 .map(|syntax| syntax.name.len())
103 .unwrap_or(32); // Fallback width if they have no language definitions.
105 let comma_separator = ", ";
107 // Line-wrapping for the possible file extension overflow.
108 let desired_width = config.term_width - longest - separator.len();
110 let stdout = io::stdout();
111 let mut stdout = stdout.lock();
113 let style = if config.colored_output {
119 for lang in languages {
120 write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
122 // Number of characters on this line so far, wrap before `desired_width`
123 let mut num_chars = 0;
125 let mut extension = lang.file_extensions.iter().peekable();
126 while let Some(word) = extension.next() {
127 // If we can't fit this word in, then create a line break and align it in.
128 let new_chars = word.len() + comma_separator.len();
129 if num_chars + new_chars >= desired_width {
131 write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
134 num_chars += new_chars;
135 write!(stdout, "{}", style.paint(&word[..]))?;
136 if extension.peek().is_some() {
137 write!(stdout, "{}", comma_separator)?;
146pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
147 let themes = &assets.theme_set.themes;
148 let mut config = cfg.clone();
149 let mut style = HashSet::new();
150 style.insert(OutputComponent::Plain);
151 config.files = vec![InputFile::ThemePreviewFile];
152 config.output_components = OutputComponents(style);
154 let stdout = io::stdout();
155 let mut stdout = stdout.lock();
157 if config.colored_output {
158 for (theme, _) in themes.iter() {
162 Style::new().bold().paint(theme.to_string())
164 config.theme = theme.to_string();
165 let _controller = Controller::new(&config, &assets).run();
169 for (theme, _) in themes.iter() {
170 writeln!(stdout, "{}", theme)?;
177/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
178/// `Some(false)` if any intermediate errors occurred (were printed).
179fn run() -> Result<bool> {
180 let app = App::new();
182 match app.matches.subcommand() {
183 ("cache", Some(cache_matches)) => {
184 run_cache_subcommand(cache_matches)?;
188 let config = app.config()?;
189 let assets = HighlightingAssets::new();
191 if app.matches.is_present("list-languages") {
192 list_languages(&assets, &config)?;
195 } else if app.matches.is_present("list-themes") {
196 list_themes(&assets, &config)?;
200 let controller = Controller::new(&config, &assets);
212 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