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
use super::{
exec_time_str, extract_help, style,
table_lib::{Cell, Table},
time_fmt,
tree_lib::{self, TreeFormatter},
DisplayIdentStyle, DisplayStyle, ListOptions,
};
use crate::error::Result;
use crate::script::ScriptInfo;
use crate::util::get_display_type;
use colored::{Color, Colorize};
use fxhash::FxHashMap as HashMap;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::io::Write;
use unicode_width::UnicodeWidthStr;
struct ShortFormatter {
plain: bool,
ident_style: DisplayIdentStyle,
latest_script_id: i64,
}
struct LongFormatter<'a> {
table: &'a mut Table,
plain: bool,
latest_script_id: i64,
}
struct TrimmedScriptInfo<'b>(Cow<'b, str>, &'b ScriptInfo);
fn ident_string(style: DisplayIdentStyle, ty: &str, t: &TrimmedScriptInfo<'_>) -> String {
let TrimmedScriptInfo(name, script) = t;
match style {
DisplayIdentStyle::Normal => format!("{}({})", name, ty),
DisplayIdentStyle::File => script.file_path_fallback().to_string_lossy().to_string(),
DisplayIdentStyle::Name => name.to_string(),
DisplayIdentStyle::NameAndFile => format!(
"{}({})",
name.to_string(),
script.file_path_fallback().to_string_lossy().to_string()
),
}
}
impl<'b> tree_lib::TreeValue<'b> for TrimmedScriptInfo<'b> {
fn tree_cmp(&self, other: &Self) -> Ordering {
other.1.last_time().cmp(&self.1.last_time())
}
fn display_key(&self) -> Cow<'b, str> {
match &self.0 {
Cow::Borrowed(s) => Cow::Borrowed(s),
Cow::Owned(_) => self.1.name.key(),
}
}
}
impl<'b, W: Write> TreeFormatter<'b, TrimmedScriptInfo<'b>, W> for ShortFormatter {
fn fmt_leaf(&mut self, f: &mut W, t: &TrimmedScriptInfo<'b>) -> Result {
let TrimmedScriptInfo(_, script) = t;
let ty = get_display_type(&script.ty);
let ident = ident_string(self.ident_style, &*ty.display(), t);
let ident = style(self.plain, ident, |s| s.color(ty.color()).bold());
if self.latest_script_id == script.id && !self.plain {
write!(f, "{}", "*".color(Color::Yellow).bold())?;
}
writeln!(f, "{}", ident)?;
Ok(())
}
fn fmt_nonleaf(&mut self, f: &mut W, t: &str) -> Result {
let ident = style(self.plain, t, |s| s.dimmed().italic());
writeln!(f, "{}", ident)?;
Ok(())
}
}
impl<'b> TreeFormatter<'b, TrimmedScriptInfo<'b>, Vec<u8>> for LongFormatter<'b> {
fn fmt_leaf(&mut self, f: &mut Vec<u8>, t: &TrimmedScriptInfo<'b>) -> Result {
let TrimmedScriptInfo(name, script) = t;
let ty = get_display_type(&script.ty);
let color = ty.color();
let mut ident_width = {
let t = std::str::from_utf8(&f)?;
t.width()
};
ident_width += name.len();
let ident = style(self.plain, name, |s| s.color(color).bold());
if self.latest_script_id == script.id && !self.plain {
write!(f, "{}", "*".color(Color::Yellow).bold())?;
ident_width += 1;
}
write!(f, "{}", ident)?;
let ty = ty.display();
let ty_width = ty.len();
let ty_txt = style(self.plain, ty, |s| s.color(color).bold());
let help_msg = extract_help(script);
let row = vec![
Cell::new_with_len(std::str::from_utf8(&f)?.to_string(), ident_width),
Cell::new_with_len(ty_txt.to_string(), ty_width),
Cell::new(time_fmt::fmt(&script.write_time)),
Cell::new(exec_time_str(script).to_string()),
Cell::new(help_msg),
];
self.table.add_row(row);
f.clear();
Ok(())
}
fn fmt_nonleaf(&mut self, f: &mut Vec<u8>, name: &str) -> Result {
let mut ident_width = {
let t = std::str::from_utf8(&f)?;
t.width()
};
let ident = style(self.plain, name, |s| s.dimmed().italic());
ident_width += name.len();
write!(f, "{}", ident)?;
let row = vec![Cell::new_with_len(
std::str::from_utf8(&f)?.to_string(),
ident_width,
)];
self.table.add_row(row);
f.clear();
Ok(())
}
}
type TreeNode<'b> = tree_lib::TreeNode<'b, TrimmedScriptInfo<'b>>;
fn build_forest(scripts: Vec<&ScriptInfo>) -> Vec<TreeNode<'_>> {
let mut m = HashMap::default();
for script in scripts.into_iter() {
let name = script.name.key();
let name_key = match name {
Cow::Borrowed(s) => s,
_ => {
m.insert(
(false, name.clone()),
TreeNode::new_leaf(TrimmedScriptInfo(name, script)),
);
continue;
}
};
let mut path: Vec<_> = name_key.split('/').collect();
let name = Cow::Borrowed(path.pop().unwrap());
let leaf = TreeNode::new_leaf(TrimmedScriptInfo(name, script));
TreeNode::insert_to_map(&mut m, &path, leaf);
}
let mut forest: Vec<_> = m.into_iter().map(|(_, t)| t).collect();
forest.sort_by(|a, b| a.simple_cmp(b));
forest
}
pub fn fmt<W: Write>(
scripts: Vec<&ScriptInfo>,
latest_script_id: i64,
opt: &mut ListOptions<Table, &mut W>,
) -> Result<()> {
let forest = build_forest(scripts);
match &mut opt.display_style {
DisplayStyle::Long(table) => {
let mut fmter = LongFormatter {
plain: opt.plain,
latest_script_id,
table,
};
let mut buff = Vec::<u8>::new();
fmter.fmt_all(&mut buff, forest.into_iter())?;
}
DisplayStyle::Short(ident_style, w) => {
let mut fmter = ShortFormatter {
plain: opt.plain,
ident_style: *ident_style,
latest_script_id,
};
fmter.fmt_all(w, forest.into_iter())?;
}
}
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use crate::script::IntoScriptName;
use chrono::NaiveDateTime;
fn build(v: Vec<(&'static str, &'static str)>) -> Vec<ScriptInfo> {
v.into_iter()
.enumerate()
.map(|(id, (name, ty))| {
let id = id as i64;
let time = NaiveDateTime::from_timestamp(id, 0);
let mut builder = ScriptInfo::builder(
id,
name.to_owned().into_script_name().unwrap(),
ty.into(),
vec![].into_iter(),
);
builder.created_time(time);
builder.build()
})
.collect()
}
#[test]
fn test_fmt_tree_short() {
let _ = env_logger::try_init();
let scripts = build(vec![
("bbb/ccc/ggg/rrr", "tmux"),
("aaa/bbb", "rb"),
("bbb/ccc/ddd", "tmux"),
("bbb/ccc/ggg/fff", "tmux"),
("aaa", "sh"),
("bbb/ccc/ddd/eee", "tmux"),
(".2", "txt"),
("bbb/ccc/yyy", "js"),
("bbb/ccc/ddd/www", "rb"),
("bbb/ccc/ggg/xxx", "tmux"),
("bbb/ddd", "tmux"),
]);
let forest = build_forest(scripts.iter().collect());
let mut fmter = ShortFormatter {
plain: true,
ident_style: DisplayIdentStyle::Normal,
latest_script_id: 1,
};
let ans = "
.2(txt)
aaa(sh)
aaa
└── bbb(rb)
bbb
├── ddd(tmux)
└── ccc
├── yyy(js)
├── ddd(tmux)
├── ddd
│ ├── www(rb)
│ └── eee(tmux)
└── ggg
├── xxx(tmux)
├── fff(tmux)
└── rrr(tmux)
"
.trim();
let mut v8 = Vec::<u8>::new();
fmter.fmt_all(&mut v8, forest.into_iter()).unwrap();
assert_eq!(std::str::from_utf8(&v8).unwrap().trim(), ans);
}
}