pub fn get_display_type(ty: &ScriptType) -> DisplayType<'_>
Examples found in repository?
src/list/tree.rs (line 58)
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
    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(())
    }
More examples
Hide additional examples
src/query/util.rs (line 175)
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
fn prompt_fuzz_acceptable(script: &ScriptInfo) -> Result {
    use colored::{Color, Colorize};
    use console::{Key, Term};

    let term = Term::stderr();

    let ty = get_display_type(&script.ty);
    let msg = format!(
        "{} [Y/N]",
        format!("{}({})?", script.name, ty.display())
            .color(ty.color())
            .bold(),
    );

    term.hide_cursor()?;
    hijack_ctrlc_once();

    enum Res {
        Y,
        N,
        Exit,
    }

    let res = loop {
        term.write_str(&msg)?;
        match term.read_key() {
            Ok(Key::Char('Y' | 'y') | Key::Enter) => break Res::Y,
            Ok(Key::Char('N' | 'n')) => break Res::N,
            Ok(Key::Char(ch)) => term.write_line(&format!(" Unknown key '{}'", ch))?,
            Ok(Key::Escape) => {
                break Res::Exit;
            }
            Err(e) => {
                if e.kind() == std::io::ErrorKind::Interrupted {
                    break Res::Exit;
                } else {
                    return Err(e.into());
                }
            }
            _ => term.write_line(" Unknown key")?,
        }
    };
    term.show_cursor()?;

    match res {
        Res::Exit => {
            term.show_cursor()?;
            std::process::exit(1);
        }
        Res::Y => {
            term.write_line(&" Y".color(Color::Green).to_string())?;
            Ok(())
        }
        Res::N => {
            term.write_line(&" N".color(Color::Red).to_string())?;
            Err(Error::DontFuzz)
        }
    }
}
src/list/list_impl.rs (line 98)
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(())
}