Function hyper_scripter::query::do_list_query
source · pub async fn do_list_query<'a>(
repo: &'a mut ScriptRepo,
queries: &[ListQuery]
) -> Result<Vec<RepoEntry<'a>>>Examples found in repository?
src/list/list_impl.rs (line 220)
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
pub async fn fmt_list<W: Write>(
w: &mut W,
script_repo: &mut ScriptRepo,
opt: ListOptions,
) -> Result<()> {
let latest_script_id = script_repo
.latest_mut(1, Visibility::Normal)
.map_or(-1, |s| s.id);
let scripts_iter = do_list_query(script_repo, &opt.queries)
.await?
.into_iter()
.map(|e| &*e.into_inner());
let scripts_either = ScriptsEither::new(scripts_iter, opt.limit.map(|l| l.get()));
let sorted = scripts_either.sorted();
let final_table: Option<Table>;
match opt.grouping {
Grouping::None => {
let mut opt = convert_opt(opt, Grid::new(scripts_either.len()));
let scripts = scripts_either.collect();
fmt_group(w, scripts, sorted, latest_script_id, &mut opt)?;
final_table = extract_table(opt);
}
Grouping::Tree => {
let mut opt = convert_opt(opt, &mut *w);
let scripts = scripts_either.collect();
tree::fmt(scripts, latest_script_id, &mut opt)?;
final_table = extract_table(opt);
}
Grouping::Tag => {
let mut opt = convert_opt(opt, Grid::new(scripts_either.len()));
let mut script_map: HashMap<TagsKey, Vec<&ScriptInfo>> = HashMap::default();
scripts_either.for_each(|script| {
let key = TagsKey::new(script.tags.iter().cloned());
let v = script_map.entry(key).or_default();
v.push(script);
});
let mut scripts: Vec<_> = script_map.into_iter().collect();
// NOTE: 以群組中執行次數的最大值排序, 無標籤永遠在上
scripts.sort_by_key(|(k, v)| {
if k.is_empty() {
None
} else {
v.iter()
.map(|s| {
if s.exec_time.is_none() {
0
} else {
s.exec_count
}
})
.max()
}
});
for (tags, scripts) in scripts.into_iter() {
if !opt.grouping.is_none() {
let tags = tags.to_string();
let tags_width = tags.len();
let tags_txt = style(opt.plain, tags, |s| s.dimmed().italic());
match &mut opt.display_style {
DisplayStyle::Long(table) => {
table.add_row(vec![Cell::new_with_len(
tags_txt.to_string(),
tags_width,
)]);
}
DisplayStyle::Short(_, _) => {
writeln!(w, "{}", tags_txt)?;
}
}
}
fmt_group(w, scripts, sorted, latest_script_id, &mut opt)?;
}
final_table = extract_table(opt);
}
}
if let Some(mut table) = final_table {
write!(w, "{}", table.display())?;
log::debug!("tree table: {:?}", table);
}
Ok(())
}