-
Notifications
You must be signed in to change notification settings - Fork 398
/
main.rs
145 lines (128 loc) · 3.84 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
extern crate bitflags;
#[macro_use]
extern crate error_chain;
mod align;
mod ansi;
mod cli;
mod color;
mod colors;
mod config;
mod delta;
mod edits;
mod env;
mod features;
mod format;
mod git_config;
mod handlers;
mod minusplus;
mod options;
mod paint;
mod parse_style;
mod parse_styles;
mod style;
mod utils;
mod wrapping;
mod subcommands;
mod tests;
use std::io::{self, ErrorKind};
use std::process;
use bytelines::ByteLinesReader;
use crate::delta::delta;
use crate::utils::bat::assets::{list_languages, HighlightingAssets};
use crate::utils::bat::output::OutputType;
pub fn fatal<T>(errmsg: T) -> !
where
T: AsRef<str> + std::fmt::Display,
{
#[cfg(not(test))]
{
eprintln!("{}", errmsg);
// As in Config::error_exit_code: use 2 for error
// because diff uses 0 and 1 for non-error.
process::exit(2);
}
#[cfg(test)]
panic!("{}\n", errmsg);
}
pub mod errors {
error_chain! {
foreign_links {
Io(::std::io::Error);
SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError);
}
}
}
#[cfg(not(tarpaulin_include))]
fn main() -> std::io::Result<()> {
// Ignore ctrl-c (SIGINT) to avoid leaving an orphaned pager process.
// See https://github.com/dandavison/delta/issues/681
ctrlc::set_handler(|| {})
.unwrap_or_else(|err| eprintln!("Failed to set ctrl-c handler: {}", err));
let exit_code = run_app()?;
// when you call process::exit, no destructors are called, so we want to do it only once, here
process::exit(exit_code);
}
#[cfg(not(tarpaulin_include))]
// An Ok result contains the desired process exit code. Note that 1 is used to
// report that two files differ when delta is called with two positional
// arguments and without standard input; 2 is used to report a real problem.
fn run_app() -> std::io::Result<i32> {
let assets = HighlightingAssets::new();
let opt = cli::Opt::from_args_and_git_config(git_config::GitConfig::try_create(), assets);
let subcommand_result = if opt.list_languages {
Some(list_languages())
} else if opt.list_syntax_themes {
Some(subcommands::list_syntax_themes::list_syntax_themes())
} else if opt.show_syntax_themes {
Some(subcommands::show_syntax_themes::show_syntax_themes())
} else if opt.show_themes {
Some(subcommands::show_themes::show_themes(
opt.dark,
opt.light,
opt.computed.is_light_mode,
))
} else if opt.show_colors {
Some(subcommands::show_colors::show_colors())
} else if opt.parse_ansi {
Some(subcommands::parse_ansi::parse_ansi())
} else {
None
};
if let Some(result) = subcommand_result {
if let Err(error) = result {
match error.kind() {
ErrorKind::BrokenPipe => {}
_ => fatal(format!("{}", error)),
}
}
return Ok(0);
};
let _show_config = opt.show_config;
let config = config::Config::from(opt);
if _show_config {
let stdout = io::stdout();
let mut stdout = stdout.lock();
subcommands::show_config::show_config(&config, &mut stdout)?;
return Ok(0);
}
let mut output_type =
OutputType::from_mode(config.paging_mode, config.pager.clone(), &config).unwrap();
let mut writer = output_type.handle().unwrap();
if atty::is(atty::Stream::Stdin) {
let exit_code = subcommands::diff::diff(
config.minus_file.as_ref(),
config.plus_file.as_ref(),
&config,
&mut writer,
);
return Ok(exit_code);
}
if let Err(error) = delta(io::stdin().lock().byte_lines(), &mut writer, &config) {
match error.kind() {
ErrorKind::BrokenPipe => return Ok(0),
_ => eprintln!("{}", error),
}
};
Ok(0)
}