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
use anyhow::{Error, Result};
use git::Config;
use super::{
utils::{get_color, get_string},
Color,
};
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Theme {
pub character_vertical_spacing: String,
pub color_action_break: Color,
pub color_action_drop: Color,
pub color_action_edit: Color,
pub color_action_exec: Color,
pub color_action_fixup: Color,
pub color_action_pick: Color,
pub color_action_reword: Color,
pub color_action_squash: Color,
pub color_action_label: Color,
pub color_action_reset: Color,
pub color_action_merge: Color,
pub color_background: Color,
pub color_diff_add: Color,
pub color_diff_change: Color,
pub color_diff_context: Color,
pub color_diff_remove: Color,
pub color_diff_whitespace: Color,
pub color_foreground: Color,
pub color_indicator: Color,
pub color_selected_background: Color,
}
impl Theme {
#[must_use]
#[inline]
pub fn new() -> Self {
Self::new_with_config(None).expect("Panic without git config instance")
}
pub(super) fn new_with_config(git_config: Option<&Config>) -> Result<Self> {
Ok(Self {
character_vertical_spacing: get_string(
git_config,
"interactive-rebase-tool.verticalSpacingCharacter",
"~",
)?,
color_action_break: get_color(git_config, "interactive-rebase-tool.breakColor", Color::LightWhite)?,
color_action_drop: get_color(git_config, "interactive-rebase-tool.dropColor", Color::LightRed)?,
color_action_edit: get_color(git_config, "interactive-rebase-tool.editColor", Color::LightBlue)?,
color_action_exec: get_color(git_config, "interactive-rebase-tool.execColor", Color::LightWhite)?,
color_action_fixup: get_color(git_config, "interactive-rebase-tool.fixupColor", Color::LightMagenta)?,
color_action_pick: get_color(git_config, "interactive-rebase-tool.pickColor", Color::LightGreen)?,
color_action_reword: get_color(git_config, "interactive-rebase-tool.rewordColor", Color::LightYellow)?,
color_action_squash: get_color(git_config, "interactive-rebase-tool.squashColor", Color::LightCyan)?,
color_action_label: get_color(git_config, "interactive-rebase-tool.labelColor", Color::DarkYellow)?,
color_action_reset: get_color(git_config, "interactive-rebase-tool.resetColor", Color::DarkYellow)?,
color_action_merge: get_color(git_config, "interactive-rebase-tool.mergeColor", Color::DarkYellow)?,
color_background: get_color(git_config, "interactive-rebase-tool.backgroundColor", Color::Default)?,
color_diff_add: get_color(git_config, "interactive-rebase-tool.diffAddColor", Color::LightGreen)?,
color_diff_change: get_color(
git_config,
"interactive-rebase-tool.diffChangeColor",
Color::LightYellow,
)?,
color_diff_context: get_color(
git_config,
"interactive-rebase-tool.diffContextColor",
Color::LightWhite,
)?,
color_diff_remove: get_color(git_config, "interactive-rebase-tool.diffRemoveColor", Color::LightRed)?,
color_diff_whitespace: get_color(git_config, "interactive-rebase-tool.diffWhitespace", Color::LightBlack)?,
color_foreground: get_color(git_config, "interactive-rebase-tool.foregroundColor", Color::Default)?,
color_indicator: get_color(git_config, "interactive-rebase-tool.indicatorColor", Color::LightCyan)?,
color_selected_background: get_color(
git_config,
"interactive-rebase-tool.selectedBackgroundColor",
Color::Index(237),
)?,
})
}
}
impl Default for Theme {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl TryFrom<&Config> for Theme {
type Error = Error;
#[inline]
fn try_from(config: &Config) -> core::result::Result<Self, Error> {
Self::new_with_config(Some(config))
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
use crate::testutils::{assert_error, invalid_utf, with_git_config};
#[test]
fn new() {
let _config = Theme::new();
}
#[test]
fn default() {
let _config = Theme::default();
}
#[test]
fn try_from_git_config() {
with_git_config(&[], |git_config| {
assert!(Theme::try_from(&git_config).is_ok());
});
}
#[test]
fn try_from_git_config_error() {
with_git_config(&["[interactive-rebase-tool]", "breakColor = invalid"], |git_config| {
assert!(Theme::try_from(&git_config).is_err());
});
}
#[test]
fn character_vertical_spacing() {
assert_eq!(Theme::new().character_vertical_spacing, "~");
with_git_config(
&["[interactive-rebase-tool]", "verticalSpacingCharacter = \"X\""],
|config| {
let theme = Theme::new_with_config(Some(&config)).unwrap();
assert_eq!(theme.character_vertical_spacing, "X");
},
);
}
#[rstest]
#[case::color_action_break("breakColor", Color::LightWhite, |theme: Theme| theme.color_action_break)]
#[case::color_action_drop("dropColor", Color::LightRed, |theme: Theme| theme.color_action_drop)]
#[case::color_action_edit("editColor", Color::LightBlue, |theme: Theme| theme.color_action_edit)]
#[case::color_action_exec("execColor", Color::LightWhite, |theme: Theme| theme.color_action_exec)]
#[case::color_action_fixup("fixupColor", Color::LightMagenta, |theme: Theme| theme.color_action_fixup)]
#[case::color_action_pick("pickColor", Color::LightGreen, |theme: Theme| theme.color_action_pick)]
#[case::color_action_reword("rewordColor", Color::LightYellow, |theme: Theme| theme.color_action_reword)]
#[case::color_action_squash("squashColor", Color::LightCyan, |theme: Theme| theme.color_action_squash)]
#[case::color_action_label("labelColor", Color::DarkYellow, |theme: Theme| theme.color_action_label)]
#[case::color_action_reset("resetColor", Color::DarkYellow, |theme: Theme| theme.color_action_reset)]
#[case::color_action_merge("mergeColor", Color::DarkYellow, |theme: Theme| theme.color_action_merge)]
#[case::color_background("backgroundColor", Color::Default, |theme: Theme| theme.color_background)]
#[case::color_diff_add("diffAddColor", Color::LightGreen, |theme: Theme| theme.color_diff_add)]
#[case::color_diff_change("diffChangeColor", Color::LightYellow, |theme: Theme| theme.color_diff_change)]
#[case::color_diff_context("diffContextColor", Color::LightWhite, |theme: Theme| theme.color_diff_context)]
#[case::color_diff_remove("diffRemoveColor", Color::LightRed, |theme: Theme| theme.color_diff_remove)]
#[case::color_diff_whitespace("diffWhitespace", Color::LightBlack, |theme: Theme| theme.color_diff_whitespace)]
#[case::color_foreground("foregroundColor", Color::Default, |theme: Theme| theme.color_foreground)]
#[case::color_indicator("indicatorColor", Color::LightCyan, |theme: Theme| theme.color_indicator)]
#[case::color_selected_background(
"selectedBackgroundColor",
Color::Index(237),
|theme: Theme| theme.color_selected_background)
]
pub(crate) fn theme_color<F: 'static>(#[case] config_name: &str, #[case] default: Color, #[case] access: F)
where F: Fn(Theme) -> Color {
let default_theme = Theme::new();
let color = access(default_theme);
assert_eq!(color, default);
let config_value = format!("{} = \"42\"", config_name);
with_git_config(&["[interactive-rebase-tool]", config_value.as_str()], |config| {
let theme = Theme::new_with_config(Some(&config)).unwrap();
let color = access(theme);
assert_eq!(color, Color::Index(42));
});
}
#[rstest]
#[case::color_invalid_utf(
invalid_utf(),
"\"interactive-rebase-tool.breakColor\" is not valid: configuration value is not valid utf8"
)]
#[case::color_invalid_range_under(
"-2",
"\"interactive-rebase-tool.breakColor\" is not valid: \"-2\" is not a valid color index. Index must be \
between 0-255."
)]
#[case::color_invalid_range_above(
"256",
"\"interactive-rebase-tool.breakColor\" is not valid: \"256\" is not a valid color index. Index must be \
between 0-255."
)]
fn value_parsing_invalid(#[case] value: &str, #[case] expected_error: &str) {
with_git_config(
&["[interactive-rebase-tool]", format!("breakColor = {}", value).as_str()],
|git_config| {
assert_error(Theme::new_with_config(Some(&git_config)), expected_error);
},
);
}
}