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
|
/// This module applies rewrite rules to the command line options, in order to
/// 1. Express deprecated usages in the new non-deprecated form
/// 2. Implement options such as --raw which are defined to be equivalent to some set of
/// other options.
use crate::cli;
use crate::config::user_supplied_option;
use crate::fatal;
pub fn apply_rewrite_rules(opt: &mut cli::Opt, arg_matches: &clap::ArgMatches) {
rewrite_style_strings_to_honor_deprecated_minus_plus_options(opt);
rewrite_options_to_implement_deprecated_commit_and_file_style_box_option(opt);
rewrite_options_to_implement_deprecated_hunk_style_option(opt);
rewrite_options_to_implement_deprecated_theme_option(opt, arg_matches);
}
/// Honor deprecated --theme
fn rewrite_options_to_implement_deprecated_theme_option(
opt: &mut cli::Opt,
arg_matches: &clap::ArgMatches,
) {
if user_supplied_option("deprecated-theme", arg_matches) {
if let Some(syntax_theme) = opt.deprecated_theme.as_ref() {
opt.syntax_theme = Some(syntax_theme.to_string());
}
}
}
/// Honor deprecated arguments by rewriting the canonical --*-style arguments if appropriate.
// TODO: How to avoid repeating the default values for style options here and in
// the structopt definition?
fn rewrite_style_strings_to_honor_deprecated_minus_plus_options(opt: &mut cli::Opt) {
// If --highlight-removed was passed then we should set minus and minus emph foreground to
// "syntax", if they are still at their default values.
let deprecated_minus_foreground_arg = if opt.deprecated_highlight_minus_lines {
Some("syntax")
} else {
None
};
if let Some(rewritten) = _get_rewritten_minus_plus_style_string(
&opt.minus_style,
("normal", "auto"),
(
deprecated_minus_foreground_arg,
opt.deprecated_minus_background_color.as_deref(),
),
"minus",
) {
opt.minus_style = rewritten;
}
if let Some(rewritten) = _get_rewritten_minus_plus_style_string(
&opt.minus_emph_style,
("normal", "auto"),
(
deprecated_minus_foreground_arg,
opt.deprecated_minus_emph_background_color.as_deref(),
),
"minus-emph",
) {
opt.minus_emph_style = rewritten;
}
if let Some(rewritten) = _get_rewritten_minus_plus_style_string(
&opt.plus_style,
("syntax", "auto"),
(None, opt.deprecated_plus_background_color.as_deref()),
"plus",
) {
opt.plus_style = rewritten;
}
if let Some(rewritten) = _get_rewritten_minus_plus_style_string(
&opt.plus_emph_style,
("syntax", "auto"),
(None, opt.deprecated_plus_emph_background_color.as_deref()),
"plus-emph",
) {
opt.plus_emph_style = rewritten;
}
}
/// For backwards-compatibility, --{commit,file}-style box means --element-decoration-style 'box ul'.
fn rewrite_options_to_implement_deprecated_commit_and_file_style_box_option(opt: &mut cli::Opt) {
if &opt.commit_style == "box" {
opt.commit_decoration_style = format!("box ul {}", opt.commit_decoration_style);
opt.commit_style.clear();
}
if &opt.file_style == "box" {
opt.file_decoration_style = format!("box ul {}", opt.file_decoration_style);
opt.file_style.clear();
}
}
fn rewrite_options_to_implement_deprecated_hunk_style_option(opt: &mut cli::Opt) {
// Examples of how --hunk-style was originally used are
// --hunk-style box => --hunk-header-decoration-style box
// --hunk-style underline => --hunk-header-decoration-style underline
// --hunk-style plain => --hunk-header-decoration-style ''
if opt.deprecated_hunk_style.is_some() {
// As in the other cases, we only honor the deprecated option if the replacement option has
// apparently been left at its default value.
let hunk_header_decoration_default = "blue box";
if opt.hunk_header_decoration_style != hunk_header_decoration_default {
fatal(
"Deprecated option --hunk-style cannot be used with --hunk-header-decoration-style. \
Use --hunk-header-decoration-style.");
}
match opt.deprecated_hunk_style.as_deref().map(str::to_lowercase) {
Some(attr) if attr == "plain" => opt.hunk_header_decoration_style = "".to_string(),
Some(attr) if attr.is_empty() => {}
Some(attr) => opt.hunk_header_decoration_style = attr,
None => {}
}
opt.deprecated_hunk_style = None;
}
}
fn _get_rewritten_commit_file_hunk_header_style_string(
style_default_pair: (&str, Option<&str>),
deprecated_args_style_pair: (Option<&str>, Option<&str>),
) -> Option<String> {
let format_style = |pair: (&str, Option<&str>)| {
format!(
"{}{}",
pair.0,
match pair.1 {
Some(s) => format!(" {}", s),
None => "".to_string(),
}
)
};
match deprecated_args_style_pair {
(None, None) => None,
deprecated_args_style_pair => Some(format_style((
deprecated_args_style_pair.0.unwrap_or(style_default_pair.0),
match deprecated_args_style_pair.1 {
Some(s) => Some(s),
None => style_default_pair.1,
},
))),
}
}
fn _get_rewritten_minus_plus_style_string(
style: &str,
style_default_pair: (&str, &str),
deprecated_args_style_pair: (Option<&str>, Option<&str>),
element_name: &str,
) -> Option<String> {
let format_style = |pair: (&str, &str)| format!("{} {}", pair.0, pair.1);
match (style, deprecated_args_style_pair) {
(_, (None, None)) => None, // no rewrite
(style, deprecated_args_style_pair) if style == format_style(style_default_pair) => {
// TODO: We allow the deprecated argument values to have effect if
// the style argument value is equal to its default value. This is
// non-ideal, because the user may have explicitly supplied the
// style argument (i.e. it might just happen to equal the default).
Some(format_style((
deprecated_args_style_pair.0.unwrap_or(style_default_pair.0),
deprecated_args_style_pair.1.unwrap_or(style_default_pair.1),
)))
}
(_, (_, Some(_))) => {
fatal(format!(
"--{name}-color cannot be used with --{name}-style. \
Use --{name}-style=\"fg bg attr1 attr2 ...\" to set \
foreground color, background color, and style attributes. \
--{name}-color can only be used to set the background color. \
(It is still available for backwards-compatibility.)",
name = element_name,
));
}
(_, (Some(_), None)) => {
fatal(format!(
"Deprecated option --highlight-removed cannot be used with \
--{name}-style. Use --{name}-style=\"fg bg attr1 attr2 ...\" \
to set foreground color, background color, and style \
attributes.",
name = element_name,
));
}
}
}
|