forked from drewdeponte/git-ps-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.rs
255 lines (233 loc) · 6.78 KB
/
list.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use ansi_term::{ANSIGenericString, Style};
use std::{fmt, str::Utf8Error};
use super::{hooks, utils};
#[derive(Debug, PartialEq, Clone)]
struct ListCell {
width: Option<usize>,
color: Option<ansi_term::Colour>,
bg_color: Option<ansi_term::Colour>,
value: String,
}
impl ListCell {
fn get_str_fixed_width(&self, str: String) -> String {
self.width
.map(|w| utils::set_string_width(&str, w))
.unwrap_or(str)
}
fn get_colored_text<'a>(&'a self, str: &'a str) -> ANSIGenericString<str> {
if self.color.is_some() && self.bg_color.is_some() {
self.color.unwrap().on(self.bg_color.unwrap()).paint(str)
} else if self.color.is_some() && self.bg_color.is_none() {
self.color.unwrap().paint(str)
} else if self.color.is_none() & self.bg_color.is_some() {
Style::new().on(self.bg_color.unwrap()).paint(str)
} else {
ANSIGenericString::from(str)
}
}
}
impl fmt::Display for ListCell {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let without_newlines = utils::strip_newlines(&self.value);
let str_fixed_width = self.get_str_fixed_width(without_newlines);
let colored_text = self.get_colored_text(&str_fixed_width);
write!(f, "{}", colored_text)
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct ListRow {
cells: Vec<ListCell>,
with_color: bool,
}
impl ListRow {
pub fn new(with_color: bool) -> Self {
Self {
with_color,
cells: vec![],
}
}
pub fn add_cell(
&mut self,
width: Option<usize>,
text_color: Option<ansi_term::Colour>,
bg_color: Option<ansi_term::Colour>,
value: impl fmt::Display,
) {
let color = if self.with_color { text_color } else { None };
let bg_color = if self.with_color { bg_color } else { None };
let cell = ListCell {
width,
color,
bg_color,
value: value.to_string(),
};
self.cells.push(cell)
}
}
impl fmt::Display for ListRow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut row_str = String::new();
for column in &self.cells {
row_str.push_str(&column.to_string());
}
write!(f, "{}", row_str)
}
}
#[derive(Debug)]
pub enum ListHookError {
GetHookOutputError(hooks::HookOutputError),
HookOutputInvalid(Utf8Error),
}
impl std::fmt::Display for ListHookError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::GetHookOutputError(e) => write!(f, "get hook output failed, {}", e),
Self::HookOutputInvalid(e) => write!(f, "hook output invalid, {}", e),
}
}
}
impl std::error::Error for ListHookError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::GetHookOutputError(e) => Some(e),
Self::HookOutputInvalid(e) => Some(e),
}
}
}
pub fn execute_list_additional_info_hook(
repo_root_str: &str,
repo_gitdir_str: &str,
args: &[&str],
) -> Result<String, ListHookError> {
let hook_output = hooks::find_and_execute_hook_with_output(
repo_root_str,
repo_gitdir_str,
"list_additional_information",
args,
)
.map_err(ListHookError::GetHookOutputError)?;
String::from_utf8(hook_output.stdout)
.map_err(|e| ListHookError::HookOutputInvalid(e.utf8_error()))
}
#[cfg(test)]
mod tests {
use crate::ps::private::list::{ListCell, ListRow};
use ansi_term::Colour::Blue;
#[test]
fn test_list_cell_fmt_shorter_blue() {
let cell = ListCell {
width: Some(4),
color: Some(Blue),
bg_color: None,
value: "hello".to_string(),
};
assert_eq!(format!("{}", cell), "\u{1b}[34mhell\u{1b}[0m");
}
#[test]
fn test_list_cell_fmt_longer_no_color() {
let cell = ListCell {
width: Some(6),
color: None,
bg_color: None,
value: "hello".to_string(),
};
assert_eq!(format!("{}", cell), "hello ");
}
#[test]
fn test_list_cell_fmt_no_width_or_color() {
let cell = ListCell {
width: None,
color: None,
bg_color: None,
value: "hello".to_string(),
};
assert_eq!(format!("{}", cell), "hello");
}
#[test]
fn test_list_row_new() {
let row = ListRow::new(true);
assert_eq!(
row,
ListRow {
with_color: true,
cells: vec![]
}
);
}
#[test]
fn test_list_row_add_cell() {
let mut row = ListRow::new(false);
let cell_value = "hello".to_string();
row.add_cell(None, None, None, &cell_value);
assert_eq!(
row,
ListRow {
with_color: false,
cells: vec![ListCell {
width: None,
color: None,
bg_color: None,
value: cell_value
}]
}
);
}
#[test]
fn test_list_row_fmt_with_color() {
let mut row = ListRow::new(true);
let first_cell = ListCell {
width: Some(10),
color: Some(Blue),
bg_color: None,
value: "Hello".to_string(),
};
let second_cell = ListCell {
width: None,
color: None,
bg_color: None,
value: "World".to_string(),
};
row.add_cell(
first_cell.width,
first_cell.color,
first_cell.bg_color,
first_cell.value,
);
row.add_cell(
second_cell.width,
second_cell.color,
second_cell.bg_color,
second_cell.value,
);
assert_eq!(format!("{}", row), "\u{1b}[34mHello \u{1b}[0mWorld")
}
#[test]
fn test_list_row_fmt_without_color() {
let mut row = ListRow::new(false);
let first_cell = ListCell {
width: Some(10),
color: Some(Blue),
bg_color: None,
value: "Hello".to_string(),
};
let second_cell = ListCell {
width: None,
color: None,
bg_color: None,
value: "World".to_string(),
};
row.add_cell(
first_cell.width,
first_cell.color,
first_cell.bg_color,
first_cell.value,
);
row.add_cell(
second_cell.width,
second_cell.color,
second_cell.bg_color,
second_cell.value,
);
assert_eq!(format!("{}", row), "Hello World")
}
}