on 90c7d0c help sign in
bat/src/main.rs
210 lines 5.7 kB history raw
1// `error_chain!` can recurse deeply
2#![recursion_limit = "1024"]
4#[macro_use]
5extern crate error_chain;
7#[macro_use]
8extern crate clap;
10#[macro_use]
11extern crate lazy_static;
13extern crate ansi_term;
14extern crate atty;
15extern crate console;
16extern crate directories;
17extern crate git2;
18extern crate syntect;
20mod app;
21mod assets;
22mod controller;
23mod decorations;
24mod diff;
25mod line_range;
26mod output;
27mod printer;
28mod style;
29mod terminal;
31use std::collections::HashSet;
32use std::io;
33use std::io::Write;
34use std::path::Path;
35use std::process;
37use ansi_term::Colour::Green;
38use ansi_term::Style;
40use app::{App, Config, InputFile};
41use assets::{clear_assets, config_dir, HighlightingAssets};
42use controller::Controller;
43use style::{OutputComponent, OutputComponents};
45mod errors {
46 error_chain! {
47 foreign_links {
48 Clap(::clap::Error);
49 Io(::std::io::Error);
50 SyntectError(::syntect::LoadingError);
51 ParseIntError(::std::num::ParseIntError);
52 }
53 }
55 pub fn handle_error(error: &Error) {
56 match error {
57 &Error(ErrorKind::Io(ref io_error), _)
58 if io_error.kind() == super::io::ErrorKind::BrokenPipe =>
59 {
60 super::process::exit(0);
61 }
62 _ => {
63 use ansi_term::Colour::Red;
64 eprintln!("{}: {}", Red.paint("[bat error]"), error);
65 }
66 };
67 }
70use errors::*;
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") {
82 clear_assets();
83 } else if matches.is_present("config-dir") {
84 writeln!(io::stdout(), "{}", config_dir())?;
85 }
87 Ok(())
90pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<()> {
91 let mut languages = assets
92 .syntax_set
93 .syntaxes()
94 .iter()
95 .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
96 .collect::<Vec<_>>();
97 languages.sort_by_key(|lang| lang.name.to_uppercase());
99 let longest = languages
100 .iter()
101 .map(|syntax| syntax.name.len())
102 .max()
103 .unwrap_or(32); // Fallback width if they have no language definitions.
105 let comma_separator = ", ";
106 let separator = " ";
107 // Line-wrapping for the possible file extension overflow.
108 let desired_width = term_width - longest - separator.len();
110 let stdout = io::stdout();
111 let mut stdout = stdout.lock();
113 for lang in languages {
114 write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
116 // Number of characters on this line so far, wrap before `desired_width`
117 let mut num_chars = 0;
119 let mut extension = lang.file_extensions.iter().peekable();
120 while let Some(word) = extension.next() {
121 // If we can't fit this word in, then create a line break and align it in.
122 let new_chars = word.len() + comma_separator.len();
123 if num_chars + new_chars >= desired_width {
124 num_chars = 0;
125 write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
126 }
128 num_chars += new_chars;
129 write!(stdout, "{}", Green.paint(&word[..]))?;
130 if extension.peek().is_some() {
131 write!(stdout, "{}", comma_separator)?;
132 }
133 }
134 writeln!(stdout)?;
135 }
137 Ok(())
140pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
141 let themes = &assets.theme_set.themes;
142 let mut config = cfg.clone();
143 let mut style = HashSet::new();
144 style.insert(OutputComponent::Plain);
145 config.files = vec![InputFile::ThemePreviewFile];
146 config.output_components = OutputComponents(style);
148 let stdout = io::stdout();
149 let mut stdout = stdout.lock();
151 for (theme, _) in themes.iter() {
152 writeln!(
153 stdout,
154 "Theme: {}\n",
155 Style::new().bold().paint(theme.to_string())
156 )?;
157 config.theme = theme.to_string();
158 let _controller = Controller::new(&config, &assets).run();
159 writeln!(stdout)?;
160 }
162 Ok(())
165/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
166/// `Some(false)` if any intermediate errors occurred (were printed).
167fn run() -> Result<bool> {
168 let app = App::new();
170 match app.matches.subcommand() {
171 ("cache", Some(cache_matches)) => {
172 run_cache_subcommand(cache_matches)?;
173 Ok(true)
174 }
175 _ => {
176 let config = app.config()?;
177 let assets = HighlightingAssets::new();
179 if app.matches.is_present("list-languages") {
180 list_languages(&assets, config.term_width)?;
182 Ok(true)
183 } else if app.matches.is_present("list-themes") {
184 list_themes(&assets, &config)?;
186 Ok(true)
187 } else {
188 let controller = Controller::new(&config, &assets);
189 controller.run()
190 }
191 }
192 }
195fn main() {
196 let result = run();
198 match result {
199 Err(error) => {
200 handle_error(&error);
201 process::exit(1);
202 }
203 Ok(false) => {
204 process::exit(1);
205 }
206 Ok(true) => {
207 process::exit(0);
208 }
209 }