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
mod list_impl;
pub use list_impl::*;

mod grid;
pub use grid::Grid;

mod table_lib;
mod time_fmt;
mod tree;
mod tree_lib;

use crate::color::{Color, StyleObj, Stylize};
use crate::util::writable::{write_writable, FmtWrite, Writable};
use crate::{
    error::{DisplayError, DisplayResult, Result},
    query::ListQuery,
    script::ScriptInfo,
};
use serde::Serialize;
use std::borrow::Cow;
use std::num::NonZeroUsize;
use std::str::FromStr;

#[derive(Clone, Copy, Debug)]
struct LatestTxt(&'static str, &'static str);
const SHORT_LATEST_TXT: LatestTxt = LatestTxt("*", "");
const LONG_LATEST_TXT: LatestTxt = LatestTxt(" *", "  ");

fn style_name_w(
    mut w: impl Writable,
    plain: bool,
    is_latest: bool,
    latest_txt: LatestTxt,
    color: Color,
    name: &str,
) -> Result<usize> {
    let mut width = name.len();
    if is_latest && !plain {
        write_writable!(w, "{}", latest_txt.0.stylize().color(Color::Yellow).bold())?;
        width += latest_txt.0.len();
    } else {
        write_writable!(w, "{}", latest_txt.1)?;
        width += latest_txt.1.len();
    }
    let name = style(plain, name, |s| {
        s.color(color).bold();
        if is_latest {
            s.underline();
        }
    });
    write_writable!(w, "{}", name)?;
    Ok(width)
}

fn style_name(
    plain: bool,
    is_latest: bool,
    latest_txt: LatestTxt,
    color: Color,
    name: &str,
) -> Result<(String, usize)> {
    let mut s = String::new();
    let width = style_name_w(FmtWrite(&mut s), plain, is_latest, latest_txt, color, name)?;
    Ok((s, width))
}

fn extract_help(script: &ScriptInfo) -> String {
    let mut buff = String::new();
    fn inner(buff: &mut String, script: &ScriptInfo) -> Result {
        let script_path = crate::path::open_script(&script.name, &script.ty, Some(true))?;
        *buff = crate::util::read_file(&script_path)?;
        Ok(())
    }
    match inner(&mut buff, script) {
        Err(e) => {
            log::warn!("讀取腳本失敗{},直接回空的幫助字串", e);
            return String::new();
        }
        Ok(()) => (),
    };
    let mut helps = crate::extract_msg::extract_help_from_content(&buff);
    helps.next().unwrap_or_default().to_owned()
}

fn exec_time_str(script: &ScriptInfo) -> Cow<'static, str> {
    match &script.exec_time {
        None => Cow::Borrowed("Never"),
        Some(t) => Cow::Owned(format!("{} ({})", time_fmt::fmt(t), script.exec_count)),
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum DisplayIdentStyle {
    File,
    Name,
    Normal,
    NameAndFile,
}
#[derive(Debug, Eq, PartialEq)]
pub enum DisplayStyle<T, U> {
    Short(DisplayIdentStyle, U),
    Long(T),
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize)]
pub enum Grouping {
    Tag,
    Tree,
    None,
}
impl Grouping {
    pub fn is_none(self) -> bool {
        self == Grouping::None
    }
}
impl Default for Grouping {
    fn default() -> Self {
        Grouping::None
    }
}
impl FromStr for Grouping {
    type Err = DisplayError;
    fn from_str(s: &str) -> DisplayResult<Self> {
        let g = match s {
            "tag" => Grouping::Tag,
            "tree" => Grouping::Tree,
            "none" => Grouping::None,
            _ => unreachable!(),
        };
        Ok(g)
    }
}

#[derive(Debug)]
pub struct ListOptions<T = (), U = ()> {
    pub grouping: Grouping,
    pub plain: bool,
    pub limit: Option<NonZeroUsize>,
    pub queries: Vec<ListQuery>,
    pub display_style: DisplayStyle<T, U>,
}

#[inline]
fn style<T: std::fmt::Display, F: for<'a> FnOnce(&'a mut StyleObj<T>)>(
    plain: bool,
    s: T,
    f: F,
) -> StyleObj<T> {
    let mut s = s.stylize();
    if !plain {
        f(&mut s);
    }
    s
}

pub fn get_screen_width() -> u16 {
    console::Term::stdout().size_checked().map_or(0, |s| s.1)
}