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
|
use {
super::CropWriter,
crate::{
display::cond_bg,
errors::ProgramError,
git::TreeGitStatus,
skin::StyleMap,
},
};
pub struct GitStatusDisplay<'a, 's> {
status: &'a TreeGitStatus,
skin: &'s StyleMap,
show_branch: bool,
show_wide: bool,
show_stats: bool,
pub width: usize,
}
impl<'a, 's> GitStatusDisplay<'a, 's> {
pub fn from(status: &'a TreeGitStatus, skin: &'s StyleMap, available_width: usize) -> Self {
let mut show_branch = false;
let mut width = 0;
if let Some(branch) = &status.current_branch_name {
let branch_width = branch.chars().count();
if branch_width < available_width {
width += branch_width;
show_branch = true;
}
}
let mut show_stats = false;
let unstyled_stats = format!("+{}-{}", status.insertions, status.deletions);
let stats_width = unstyled_stats.len();
if width + stats_width < available_width {
width += stats_width;
show_stats = true;
}
let show_wide = width + 3 < available_width;
if show_wide {
width += 3; // difference between compact and wide format widths
}
Self {
status,
skin,
show_branch,
show_wide,
show_stats,
width,
}
}
pub fn write<'w, W>(
&self,
cw: &mut CropWriter<'w, W>,
selected: bool,
) -> Result<(), ProgramError>
where
W: std::io::Write,
{
if self.show_branch {
cond_bg!(branch_style, self, selected, self.skin.git_branch);
if let Some(name) = &self.status.current_branch_name {
if self.show_wide {
cw.queue_str(branch_style, " ᚜ ")?;
} else {
cw.queue_char(branch_style, ' ')?;
}
cw.queue_str(branch_style, name)?;
cw.queue_char(branch_style, ' ')?;
}
}
if self.show_stats {
cond_bg!(insertions_style, self, selected, self.skin.git_insertions);
cw.queue_g_string(insertions_style, format!("+{}", self.status.insertions))?;
cond_bg!(deletions_style, self, selected, self.skin.git_deletions);
cw.queue_g_string(deletions_style, format!("-{}", self.status.deletions))?;
}
Ok(())
}
}
|