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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
extern crate bitflags;
#[macro_use]
extern crate error_chain;
mod align;
mod bat;
mod cli;
mod color;
mod config;
mod delta;
mod draw;
mod edits;
mod env;
mod features;
mod get_option_value;
mod git_config;
mod option_value;
mod paint;
mod parse;
mod rewrite_options;
mod set_options;
mod style;
mod syntax_theme;
mod syntect_color;
mod tests;
use std::io::{self, ErrorKind, Read, Write};
use std::path::PathBuf;
use std::process;
use ansi_term::{self, Color};
use atty;
use bytelines::ByteLinesReader;
use structopt::StructOpt;
use crate::bat::assets::{list_languages, HighlightingAssets};
use crate::bat::output::{OutputType, PagingMode};
use crate::delta::delta;
mod errors {
error_chain! {
foreign_links {
Io(::std::io::Error);
SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError);
}
}
}
fn main() -> std::io::Result<()> {
let opt = cli::Opt::from_args_and_git_config(&mut git_config::GitConfig::try_create());
let config = config::Config::from(opt);
if config.list_languages {
list_languages()?;
process::exit(0);
} else if config.list_syntax_theme_names {
list_syntax_theme_names()?;
process::exit(0);
} else if config.list_syntax_themes {
list_syntax_themes()?;
process::exit(0);
} else if config.show_background_colors {
show_background_colors(&config);
process::exit(0);
} else if atty::is(atty::Stream::Stdin) {
return diff(
config.minus_file.as_ref(),
config.plus_file.as_ref(),
&config,
);
}
let mut output_type = OutputType::from_mode(config.paging_mode, None, &config).unwrap();
let mut writer = output_type.handle().unwrap();
if let Err(error) = delta(io::stdin().lock().byte_lines(), &mut writer, &config) {
match error.kind() {
ErrorKind::BrokenPipe => process::exit(0),
_ => eprintln!("{}", error),
}
};
Ok(())
}
/// Run `diff -u` on the files provided on the command line and display the output.
fn diff(
minus_file: Option<&PathBuf>,
plus_file: Option<&PathBuf>,
config: &config::Config,
) -> std::io::Result<()> {
use std::io::BufReader;
let die = || {
eprintln!("Usage: delta minus_file plus_file");
process::exit(1);
};
let diff_process = process::Command::new(PathBuf::from("diff"))
.arg("-u")
.args(&[
minus_file.unwrap_or_else(die),
plus_file.unwrap_or_else(die),
])
.stdout(process::Stdio::piped())
.spawn();
let mut output_type = OutputType::from_mode(config.paging_mode, None, &config).unwrap();
let mut writer = output_type.handle().unwrap();
if let Err(error) = delta(
BufReader::new(diff_process.unwrap().stdout.unwrap()).byte_lines(),
&mut writer,
&config,
) {
match error.kind() {
ErrorKind::BrokenPipe => process::exit(0),
_ => eprintln!("{}", error),
}
};
Ok(())
}
fn show_background_colors(config: &config::Config) {
println!(
"delta \
--minus-color=\"{minus_color}\" \
--minus-emph-color=\"{minus_emph_color}\" \
--plus-color=\"{plus_color}\" \
--plus-emph-color=\"{plus_emph_color}\"",
minus_color =
get_painted_rgb_string(config.minus_style.ansi_term_style.background.unwrap()),
minus_emph_color =
get_painted_rgb_string(config.minus_emph_style.ansi_term_style.background.unwrap()),
plus_color = get_painted_rgb_string(config.plus_style.ansi_term_style.background.unwrap()),
plus_emph_color =
get_painted_rgb_string(config.plus_emph_style.ansi_term_style.background.unwrap()),
)
}
fn get_painted_rgb_string(color: Color) -> String {
color.paint(format!("{:?}", color)).to_string()
}
fn list_syntax_themes() -> std::io::Result<()> {
use bytelines::ByteLines;
use std::io::BufReader;
let opt = cli::Opt::from_args();
let input = if !atty::is(atty::Stream::Stdin) {
let mut buf = Vec::new();
io::stdin().lock().read_to_end(&mut buf)?;
buf
} else {
b"\
diff --git a/example.rs b/example.rs
index f38589a..0f1bb83 100644
--- a/example.rs
+++ b/example.rs
@@ -1,5 +1,5 @@
-// Output the square of a number.
-fn print_square(num: f64) {
- let result = f64::powf(num, 2.0);
- println!(\"The square of {:.2} is {:.2}.\", num, result);
+// Output the cube of a number.
+fn print_cube(num: f64) {
+ let result = f64::powf(num, 3.0);
+ println!(\"The cube of {:.2} is {:.2}.\", num, result);
}"
.to_vec()
};
let stdout = io::stdout();
let mut stdout = stdout.lock();
let style = ansi_term::Style::new().bold();
let assets = HighlightingAssets::new();
for (syntax_theme, _) in assets.theme_set.themes.iter() {
if opt.light && !syntax_theme::is_light_theme(syntax_theme)
|| opt.dark && syntax_theme::is_light_theme(syntax_theme)
{
continue;
}
writeln!(stdout, "\n\nTheme: {}\n", style.paint(syntax_theme))?;
let opt_2 = cli::Opt::from_iter(&[
"--syntax-theme",
syntax_theme,
"--file-style",
"omit",
"--hunk-header-style",
"omit",
]);
let config = config::Config::from(opt_2);
let mut output_type =
OutputType::from_mode(PagingMode::QuitIfOneScreen, None, &config).unwrap();
let mut writer = output_type.handle().unwrap();
if let Err(error) = delta(
ByteLines::new(BufReader::new(&input[0..])),
&mut writer,
&config,
) {
match error.kind() {
ErrorKind::BrokenPipe => process::exit(0),
_ => eprintln!("{}", error),
}
};
}
Ok(())
}
pub fn list_syntax_theme_names() -> std::io::Result<()> {
let assets = HighlightingAssets::new();
let themes = &assets.theme_set.themes;
let stdout = io::stdout();
let mut stdout = stdout.lock();
writeln!(stdout, "Light themes:")?;
for (theme, _) in themes.iter() {
if syntax_theme::is_light_theme(theme) {
writeln!(stdout, " {}", theme)?;
}
}
writeln!(stdout, "Dark themes:")?;
for (theme, _) in themes.iter() {
if !syntax_theme::is_light_theme(theme)
|