on master help sign in
Use error-chain
commit36bbc770ebb33883d21e55e3d44605fdd00518fc authorsharkdp <davidpeter@web.de> dateMon 30 Apr 2018, 11:09 (9 years ago) parentf43409f
3 files changed+86−22
Cargo.lock+45−6 Cargo.toml+1 src/main.rs+40−16
Cargo.lock+45−6
@@ -29,6 +29,27 @@ dependencies = [
2929 "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
3030]
3131
32+[[package]]
33+name = "backtrace"
34+version = "0.3.6"
35+source = "registry+https://github.com/rust-lang/crates.io-index"
36+dependencies = [
37+ "backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
38+ "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
39+ "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
40+ "rustc-demangle 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
41+ "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
42+]
43+
44+[[package]]
45+name = "backtrace-sys"
46+version = "0.1.16"
47+source = "registry+https://github.com/rust-lang/crates.io-index"
48+dependencies = [
49+ "cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
50+ "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
51+]
52+
3253[[package]]
3354name = "base64"
3455version = "0.8.0"
@@ -46,13 +67,14 @@ dependencies = [
4667 "atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
4768 "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
4869 "console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
70+ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
4971 "git2 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
50− "syntect 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
72+ "syntect 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
5173]
5274
5375[[package]]
5476name = "bincode"
55−version = "0.9.2"
77+version = "1.0.0"
5678source = "registry+https://github.com/rust-lang/crates.io-index"
5779dependencies = [
5880 "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -172,6 +194,14 @@ dependencies = [
172194 "shared_child 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
173195]
174196
197+[[package]]
198+name = "error-chain"
199+version = "0.11.0"
200+source = "registry+https://github.com/rust-lang/crates.io-index"
201+dependencies = [
202+ "backtrace 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
203+]
204+
175205[[package]]
176206name = "flate2"
177207version = "1.0.1"
@@ -508,6 +538,11 @@ dependencies = [
508538 "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
509539]
510540
541+[[package]]
542+name = "rustc-demangle"
543+version = "0.1.7"
544+source = "registry+https://github.com/rust-lang/crates.io-index"
545+
511546[[package]]
512547name = "safemem"
513548version = "0.2.0"
@@ -594,10 +629,10 @@ dependencies = [
594629
595630[[package]]
596631name = "syntect"
597−version = "2.0.0"
632+version = "2.0.1"
598633source = "registry+https://github.com/rust-lang/crates.io-index"
599634dependencies = [
600− "bincode 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
635+ "bincode 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
601636 "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
602637 "flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
603638 "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -788,8 +823,10 @@ dependencies = [
788823"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
789824"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
790825"checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4"
826+"checksum backtrace 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ebbe525f66f42d207968308ee86bc2dd60aa5fab535b22e616323a173d097d8e"
827+"checksum backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "44585761d6161b0f57afc49482ab6bd067e4edef48c12a152c237eb0203f7661"
791828"checksum base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c4a342b450b268e1be8036311e2c613d7f8a7ed31214dff1cc3b60852a3168d"
792−"checksum bincode 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9a6301db0b49fb63551bc15b5ae348147101cdf323242b93ec7546d5002ff1af"
829+"checksum bincode 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bda13183df33055cbb84b847becce220d392df502ebe7a4a78d7021771ed94d0"
793830"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"
794831"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf"
795832"checksum byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "73b5bdfe7ee3ad0b99c9801d58807a9dbc9e09196365b0203853b99889ab3c87"
@@ -803,6 +840,7 @@ dependencies = [
803840"checksum curl-sys 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f3f7738d877ec81040305d5bb91976ac594f564f5e455dc02a29a23c1d00fe6f"
804841"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
805842"checksum duct 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "166298c17c5b4fe5997b962c2f22e887c7c5adc44308eb9103ce5b66af45a423"
843+"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
806844"checksum flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fac2277e84e5e858483756647a9d0aa8d9a2b7cba517fd84325a0aaa69a0909"
807845"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
808846"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
@@ -844,6 +882,7 @@ dependencies = [
844882"checksum regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "aec3f58d903a7d2a9dc2bf0e41a746f4530e0cab6b615494e058f67a3ef947fb"
845883"checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e"
846884"checksum regex-syntax 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "bd90079345f4a4c3409214734ae220fd773c6f2e8a543d07370c6c1c369cfbfb"
885+"checksum rustc-demangle 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11fb43a206a04116ffd7cfcf9bcb941f8eb6cc7ff667272246b0a1c74259a3cb"
847886"checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f"
848887"checksum same-file 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cfb6eded0b06a0b512c8ddbcf04089138c9b4362c2f696f3c3d76039d68f3637"
849888"checksum serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "d3bcee660dcde8f52c3765dd9ca5ee36b4bf35470a738eb0bd5a8752b0389645"
@@ -855,7 +894,7 @@ dependencies = [
855894"checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b"
856895"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
857896"checksum syn 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "91b52877572087400e83d24b9178488541e3d535259e04ff17a63df1e5ceff59"
858−"checksum syntect 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "db9fffcb25a761118df53811bd1cfcd54cf57fcbc51e1ea3167ae263477129ad"
897+"checksum syntect 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eceb461f684c9e8e00f7a50055698af62c76d5b86e4ad50b4311acd5f655d606"
859898"checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327"
860899"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
861900"checksum termios 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a"
Cargo.toml+1
@@ -15,6 +15,7 @@ syntect = "2"
1515ansi_term = "0.9"
1616console = "0.6"
1717git2 = "0.6"
18+error-chain = "0.11"
1819
1920[dependencies.clap]
2021version = "2"
src/main.rs+40−16
@@ -1,17 +1,23 @@
1+// `error_chain!` can recurse deeply
2+#![recursion_limit = "1024"]
3+
4+#[macro_use]
5+extern crate error_chain;
6+
7+#[macro_use]
8+extern crate clap;
9+
110extern crate ansi_term;
211extern crate atty;
312extern crate console;
413extern crate git2;
514extern crate syntect;
615
7−#[macro_use]
8−extern crate clap;
9−
1016mod terminal;
1117
1218use std::collections::HashMap;
1319use std::env;
14−use std::io::{self, BufRead, ErrorKind, Result, StdoutLock, Write};
20+use std::io::{self, BufRead, StdoutLock, Write};
1521use std::path::Path;
1622use std::process;
1723
@@ -28,6 +34,16 @@ use syntect::parsing::SyntaxSet;
2834
2935use terminal::as_terminal_escaped;
3036
37+mod errors {
38+ error_chain!{
39+ foreign_links {
40+ Io(::std::io::Error);
41+ }
42+ }
43+}
44+
45+use errors::*;
46+
3147struct Options {
3248 true_color: bool,
3349}
@@ -49,7 +65,7 @@ fn print_horizontal_line(
4965 handle: &mut StdoutLock,
5066 grid_char: char,
5167 term_width: usize,
52−) -> io::Result<()> {
68+) -> Result<()> {
5369 let hline = "─".repeat(term_width - (PANEL_WIDTH + 1));
5470 let hline = format!("{}{}{}", "─".repeat(PANEL_WIDTH), grid_char, hline);
5571
@@ -64,7 +80,7 @@ fn print_file<P: AsRef<Path>>(
6480 syntax_set: &SyntaxSet,
6581 filename: P,
6682 line_changes: &Option<LineChanges>,
67−) -> io::Result<()> {
83+) -> Result<()> {
6884 let mut highlighter = HighlightFile::new(filename.as_ref(), syntax_set, theme)?;
6985
7086 let stdout = io::stdout();
@@ -182,8 +198,7 @@ fn get_git_diff(filename: &str) -> Option<LineChanges> {
182198}
183199
184200fn run(matches: &ArgMatches) -> Result<()> {
185− let home_dir = env::home_dir()
186− .ok_or_else(|| io::Error::new(ErrorKind::Other, "Could not get home directory"))?;
201+ let home_dir = env::home_dir().chain_err(|| "Could not get home directory")?;
187202
188203 let colorterm = env::var("COLORTERM").unwrap_or_else(|_| "".into());
189204
@@ -192,11 +207,15 @@ fn run(matches: &ArgMatches) -> Result<()> {
192207 };
193208
194209 let theme_dir = home_dir.join(".config").join("bat").join("themes");
195− let theme_set = ThemeSet::load_from_folder(theme_dir)
196− .map_err(|_| io::Error::new(ErrorKind::Other, "Could not load themes"))?;
210+ let theme_set = ThemeSet::load_from_folder(theme_dir).map_err(|_| {
211+ io::Error::new(
212+ io::ErrorKind::Other,
213+ "Could not load themes from ~/.config/bat/themes",
214+ )
215+ })?;
197216 let theme = &theme_set.themes.get("Default").ok_or_else(|| {
198217 io::Error::new(
199− ErrorKind::Other,
218+ io::ErrorKind::Other,
200219 "Could not load default theme (~/.config/bat/themes/Default.tmTheme)",
201220 )
202221 })?;
@@ -246,10 +265,15 @@ fn main() {
246265
247266 let result = run(&matches);
248267
249− if let Err(e) = result {
250− if e.kind() != ErrorKind::BrokenPipe {
251− eprintln!("{}: {}", Red.paint("[bat error]"), e);
252− process::exit(1);
253− }
268+ if let Err(error) = result {
269+ match error {
270+ Error(ErrorKind::Io(ref io_error), _)
271+ if io_error.kind() == io::ErrorKind::BrokenPipe => {}
272+ _ => {
273+ eprintln!("{}: {}", Red.paint("[bat error]"), error);
274+
275+ process::exit(1);
276+ }
277+ };
254278 }
255279}