Struct hyper_scripter::list::Grid
source · pub struct Grid { /* private fields */ }Implementations§
source§impl Grid
impl Grid
sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Examples found in repository?
More examples
src/list/list_impl.rs (line 230)
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(())
}sourcepub fn add(&mut self, contents: String, width: usize)
pub fn add(&mut self, contents: String, width: usize)
Examples found in repository?
src/list/list_impl.rs (line 149)
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
pub fn fmt_meta(
script: &ScriptInfo,
is_latest: bool,
opt: &mut ListOptionWithOutput,
) -> Result<()> {
let ty = get_display_type(&script.ty);
let color = ty.color();
match &mut opt.display_style {
DisplayStyle::Long(table) => {
// TODO: 整合長短的名字顯示法?
let last_txt = if is_latest && !opt.plain {
"*".color(Color::Yellow).bold()
} else {
" ".normal()
};
let name = script.name.key();
let name_width = name.len() + 2;
let name_txt = format!(
" {}{}",
last_txt,
style(opt.plain, name, |s| s.color(color).bold()),
);
let ty = ty.display();
let ty_width = ty.len();
let ty_txt = style(opt.plain, ty, |s| s.color(color).bold());
let help_msg = extract_help(script);
let row = vec![
Cell::new_with_len(name_txt, name_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),
];
table.add_row(row);
}
DisplayStyle::Short(ident_style, grid) => {
let mut display_str = String::new();
let mut width = 0;
if is_latest && !opt.plain {
width += 1;
write!(display_str, "{}", "*".color(Color::Yellow).bold())?;
}
let ident = ident_string(ident_style, &*ty.display(), script);
width += ident.len();
let ident = style(opt.plain, ident, |s| {
let s = s.color(color).bold();
if is_latest {
s.underline()
} else {
s
}
});
write!(display_str, "{}", ident)?;
grid.add(display_str, width);
}
}
Ok(())
}sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Examples found in repository?
src/list/list_impl.rs (line 317)
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
fn fmt_group<W: Write>(
w: &mut W,
mut scripts: Vec<&ScriptInfo>,
sorted: bool,
latest_script_id: i64,
opt: &mut ListOptionWithOutput,
) -> Result<()> {
if !sorted {
sort_scripts(&mut scripts);
}
for script in scripts.into_iter() {
let is_latest = script.id == latest_script_id;
fmt_meta(script, is_latest, opt)?;
}
match &mut opt.display_style {
DisplayStyle::Short(_, grid) => {
let grid_display = grid.fit_into_screen();
write!(w, "{}", grid_display)?;
drop(grid_display);
grid.clear();
}
_ => (),
}
Ok(())
}sourcepub fn fit_into_screen<'a>(&'a mut self) -> impl Display + 'a
pub fn fit_into_screen<'a>(&'a mut self) -> impl Display + 'a
Examples found in repository?
src/list/list_impl.rs (line 314)
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
fn fmt_group<W: Write>(
w: &mut W,
mut scripts: Vec<&ScriptInfo>,
sorted: bool,
latest_script_id: i64,
opt: &mut ListOptionWithOutput,
) -> Result<()> {
if !sorted {
sort_scripts(&mut scripts);
}
for script in scripts.into_iter() {
let is_latest = script.id == latest_script_id;
fmt_meta(script, is_latest, opt)?;
}
match &mut opt.display_style {
DisplayStyle::Short(_, grid) => {
let grid_display = grid.fit_into_screen();
write!(w, "{}", grid_display)?;
drop(grid_display);
grid.clear();
}
_ => (),
}
Ok(())
}