rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
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
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
297
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Laying a panel out line by line: the colors, buttons, hints and the `Flow` that
//! places them and notes where they can be clicked.

use super::*;

#[derive(Clone, Copy)]
pub(super) struct Colors {
    pub(super) fg: Rgba,
    pub(super) soft: Rgba,
    pub(super) muted: Rgba,
    pub(super) accent: Rgba,
    pub(super) code: Rgba,
    pub(super) panel: Rgba,
    pub(super) raised: Rgba,
    pub(super) line: Rgba,
    pub(super) red: Rgba,
    pub(super) green: Rgba,
    pub(super) yellow: Rgba,
    pub(super) cyan: Rgba,
}

impl Colors {
    pub(super) fn new(ctx: &Ctx) -> Colors {
        let p = ctx.palette;
        let panel = mix(p.surface, p.foreground, 0.025);
        let solid = |c: Rgba| [c[0], c[1], c[2], 1.0];
        Colors {
            fg: p.foreground,
            soft: mix(p.foreground, p.surface, 0.2),
            muted: mix(p.foreground, p.surface, 0.5),
            accent: p.accent,
            code: mix(p.accent, p.foreground, 0.35),
            // Opaque: text has to stay readable over whatever is behind.
            panel: solid(panel),
            raised: solid(mix(panel, p.foreground, 0.06)),
            line: solid(mix(panel, p.foreground, 0.14)),
            red: p.ansi[1],
            green: p.ansi[2],
            yellow: p.ansi[3],
            cyan: p.ansi[6],
        }
    }

    pub(super) fn level(&self, level: Level) -> Rgba {
        match level {
            Level::Risk => self.red,
            Level::Warn => self.yellow,
            Level::Note => self.cyan,
        }
    }

    /// The mark and color for a step's state.
    pub(super) fn state(&self, state: State) -> (&'static str, Rgba) {
        match state {
            State::Untested => ("\u{25cb}", self.muted),
            State::Done(Verdict::Passed) => ("\u{2713}", self.green),
            State::Done(Verdict::Failed) => ("\u{2717}", self.red),
            State::Done(Verdict::Skipped) => ("\u{2013}", self.muted),
            State::Stale(_) => ("\u{21bb}", self.yellow),
        }
    }
}

pub(super) struct Button {
    pub(super) label: String,
    /// The key that presses it, shown after the label.
    pub(super) key: &'static str,
    pub(super) click: Click,
    /// Its color, and whether it is the one in effect.
    pub(super) tone: Option<Rgba>,
    pub(super) on: bool,
}

pub(super) fn button(label: &str, click: Click) -> Button {
    Button { label: label.to_string(), key: "", click, tone: None, on: false }
}

/// How many characters a button's text takes.
pub(super) fn button_chars(b: &Button) -> usize {
    b.label.chars().count() + if b.key.is_empty() { 0 } else { b.key.chars().count() + 1 }
}

impl Button {
    pub(super) fn key(mut self, key: &'static str) -> Button {
        self.key = key;
        self
    }

    pub(super) fn tone(mut self, color: Rgba, on: bool) -> Button {
        self.tone = Some(color);
        self.on = on;
        self
    }
}

/// Keys and what they do, for the bottom of a screen.
pub(super) type Hints = Vec<(&'static str, &'static str)>;

/// The width of one hint: the key in its cap, then what it does.
pub(super) fn hint_width((key, does): (&str, &str), cw: f32) -> f32 {
    (key.chars().count() as f32 + does.chars().count() as f32 + 1.8) * cw
}

/// How many rows the hints take in `width`.
pub(super) fn hint_rows(hints: &[(&str, &str)], width: f32, cw: f32) -> usize {
    let (mut rows, mut used) = (1, 0.0);
    for &hint in hints {
        let w = hint_width(hint, cw);
        if used > 0.0 && used + w > width {
            rows += 1;
            used = 0.0;
        }
        used += w + 2.0 * cw;
    }
    rows
}

pub(super) fn hint_row_height(ch: f32) -> f32 {
    (ch * 1.45).round()
}

/// A column being filled from the top down. What falls outside `area` is not drawn and
/// cannot be clicked.
pub(super) struct Flow<'a, 'c> {
    pub(super) ctx: &'a mut Ctx<'c>,
    pub(super) out: &'a mut Vec<Quad>,
    pub(super) hits: &'a mut Vec<(Rect, Click)>,
    pub(super) colors: Colors,
    pub(super) mouse: (f32, f32),
    pub(super) area: Rect,
    pub(super) x: f32,
    pub(super) w: f32,
    pub(super) y: f32,
    pub(super) cw: f32,
    pub(super) ch: f32,
    pub(super) s: f32,
}

impl<'a, 'c> Flow<'a, 'c> {
    pub(super) fn new(
        ctx: &'a mut Ctx<'c>,
        out: &'a mut Vec<Quad>,
        hits: &'a mut Vec<(Rect, Click)>,
        mouse: (f32, f32),
        area: Rect,
        scroll: f32,
    ) -> Self {
        let colors = Colors::new(ctx);
        let (cw, ch, s) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h, ctx.scale);
        Flow { ctx, out, hits, colors, mouse, area, x: area.x, w: area.w, y: area.y - scroll.round(), cw, ch, s }
    }

    pub(super) fn clip(&self) -> [f32; 4] {
        [self.area.x, self.area.y, self.area.right(), self.area.bottom()]
    }

    pub(super) fn shows(&self, y: f32, h: f32) -> bool {
        y + h >= self.area.y && y <= self.area.bottom()
    }

    pub(super) fn line_height(&self) -> f32 {
        (self.ch * 1.25).round()
    }

    pub(super) fn cols(&self, width: f32) -> usize {
        (width / self.cw).floor().max(4.0) as usize
    }

    pub(super) fn gap(&mut self, lines: f32) {
        self.y += (self.ch * lines).round();
    }

    pub(super) fn fill(&mut self, r: Rect, color: Rgba, radius: f32) {
        if self.shows(r.y, r.h) {
            let clip = self.clip();
            self.out.push(rounded(r, color, radius * self.s, 0.0, [0.0; 4], clip));
        }
    }

    pub(super) fn framed(&mut self, r: Rect, color: Rgba, border: Rgba, radius: f32) {
        if self.shows(r.y, r.h) {
            let clip = self.clip();
            self.out.push(rounded(r, color, radius * self.s, self.s.max(1.0), border, clip));
        }
    }

    /// One line at a point; returns the width used.
    pub(super) fn put(&mut self, x: f32, y: f32, content: &str, color: Rgba, style: u8, max_w: f32) -> f32 {
        if !self.shows(y, self.ch) || max_w < self.cw {
            return 0.0;
        }
        let content: String = content.chars().map(|c| if c.is_control() { ' ' } else { c }).collect();
        let clip = self.clip();
        text(self.ctx, self.out, x.round(), y.round(), &content, color, style, max_w, clip)
    }

    /// Wrapped text, with backticked parts picked out.
    pub(super) fn para(&mut self, content: &str, color: Rgba, style: u8, indent: f32) {
        let width = self.w - indent;
        let lines = wrap(content, self.cols(width));
        let (lh, code) = (self.line_height(), self.colors.code);
        for line in lines {
            let mut x = self.x + indent;
            for (piece, is_code) in line {
                let chars = piece.chars().count() as f32;
                let color = if is_code { code } else { color };
                self.put(x, self.y, &piece, color, style, (chars + 0.5) * self.cw);
                x += chars * self.cw;
            }
            self.y += lh;
        }
    }

    /// Remember where something clickable is, as far as it can be seen.
    pub(super) fn hit(&mut self, r: Rect, click: Click) {
        let (top, bottom) = (r.y.max(self.area.y), r.bottom().min(self.area.bottom()));
        if bottom > top {
            self.hits.push((Rect::new(r.x, top, r.w, bottom - top), click));
        }
    }

    pub(super) fn hovered(&self, r: Rect) -> bool {
        r.contains(self.mouse.0, self.mouse.1) && self.area.contains(self.mouse.0, self.mouse.1)
    }

    pub(super) fn button_width(&self, b: &Button) -> f32 {
        (button_chars(b) as f32 + 2.0) * self.cw
    }

    /// Keys in caps with what they do, in as many rows as it takes.
    pub(super) fn keys(&mut self, hints: &[(&str, &str)]) {
        let c = self.colors;
        let row = hint_row_height(self.ch);
        let mut x = self.x;
        for &(key, does) in hints {
            let w = hint_width((key, does), self.cw);
            if x > self.x && x + w > self.x + self.w {
                x = self.x;
                self.y += row;
            }
            let chars = key.chars().count() as f32;
            let cap = Rect::new(x, self.y, ((chars + 1.0) * self.cw).round(), (self.ch * 1.2).round());
            self.framed(cap, c.raised, c.line, 4.0);
            let ty = cap.y + (cap.h - self.ch) * 0.5;
            self.put(cap.x + self.cw * 0.5, ty, key, c.soft, 0, chars * self.cw + 1.0);
            let after = cap.right() + self.cw * 0.7;
            self.put(after, ty, does, c.muted, 0, (self.x + self.w - after).max(0.0));
            x += w + 2.0 * self.cw;
        }
        self.y += row;
    }

    pub(super) fn button_at(&mut self, r: Rect, b: Button) {
        let c = self.colors;
        let tone = b.tone.unwrap_or(c.fg);
        let hover = self.hovered(r);
        let fill = if b.on {
            mix(c.panel, tone, 0.34)
        } else if hover {
            mix(c.raised, c.fg, 0.08)
        } else {
            c.raised
        };
        let border = if b.on || (hover && b.tone.is_some()) { tone } else { c.line };
        self.framed(r, fill, border, 6.0);
        let width = button_chars(&b) as f32 * self.cw;
        let color = if b.tone.is_some() && !b.on { mix(tone, c.fg, 0.25) } else { c.fg };
        let (x, y) = (r.x + (r.w - width) * 0.5, r.y + (r.h - self.ch) * 0.5);
        let used = b.label.chars().count() as f32 * self.cw;
        self.put(x, y, &b.label, color, 0, used + 1.0);
        if !b.key.is_empty() {
            let key_w = b.key.chars().count() as f32 * self.cw;
            self.put(x + used + self.cw, y, b.key, c.muted, 0, key_w + 1.0);
        }
        self.hit(r, b.click);
    }

    /// Buttons side by side, going to a new row when they run out of room.
    pub(super) fn buttons(&mut self, buttons: Vec<Button>) {
        let (h, gap) = ((self.ch * 1.6).round(), (6.0 * self.s).round());
        let mut x = self.x;
        for b in buttons {
            let w = self.button_width(&b).min(self.w);
            if x > self.x && x + w > self.x + self.w {
                x = self.x;
                self.y += h + gap;
            }
            self.button_at(Rect::new(x, self.y, w, h), b);
            x += w + gap;
        }
        self.y += h;
    }

    /// A text field with a caret at the end, showing the end of long text.
    pub(super) fn field(&mut self, content: &str, placeholder: &str, focused: bool, click: Option<Click>) {
        let c = self.colors;
        let r = Rect::new(self.x, self.y, self.w, (self.ch * 1.8).round());
        self.framed(r, c.raised, if focused { c.accent } else { c.line }, 7.0);
        let ty = r.y + (r.h - self.ch) * 0.5;
        let room = self.cols(r.w - 2.0 * self.cw).saturating_sub(1);
        let chars: Vec<char> = content.chars().collect();
        let shown: String = chars[chars.len().saturating_sub(room)..].iter().collect();
        if shown.is_empty() {
            self.put(r.x + self.cw, ty, placeholder, c.muted, 0, r.w - 2.0 * self.cw);
        } else {
            self.put(r.x + self.cw, ty, &shown, c.fg, 0, r.w - self.cw);
        }
        if focused && self.shows(r.y, r.h) {
            let cx = r.x + self.cw + shown.chars().count() as f32 * self.cw + 1.0;
            let caret = Rect::new(cx, ty + self.ch * 0.12, (1.5 * self.s).round().max(1.0), self.ch * 0.76);
            let clip = self.clip();
            self.out.push(quad(caret, with_alpha(c.accent, self.ctx.cursor_alpha.max(0.25)), clip, MODE_SOLID));
        }
        if let Some(click) = click {
            self.hit(r, click);
        }
        self.y = r.bottom();
    }

    /// A box for text of many lines, with the caret at its end.
    pub(super) fn text_box(&mut self, content: &str, focused: bool, click: Click) {
        let c = self.colors;
        let cols = self.cols(self.w - 2.0 * self.cw).saturating_sub(1).max(1);
        let mut lines: Vec<String> = Vec::new();
        for line in content.split('\n') {
            let chars: Vec<char> = line.chars().collect();
            if chars.is_empty() {
                lines.push(String::new());
            }
            lines.extend(chars.chunks(cols).map(|part| part.iter().collect::<String>()));
        }
        let lh = self.line_height();
        let height = (lines.len().max(5) as f32 * lh + self.ch * 0.6).round();
        let r = Rect::new(self.x, self.y, self.w, height);
        self.framed(r, c.raised, if focused { c.accent } else { c.line }, 7.0);
        let top = r.y + self.ch * 0.3;
        for (i, line) in lines.iter().enumerate() {
            self.put(r.x + self.cw, top + i as f32 * lh, line, c.fg, 0, r.w - self.cw);
        }
        if focused {
            let last = lines.last().map_or(0, |l| l.chars().count());
            let cx = r.x + self.cw * (last as f32 + 1.0) + 1.0;
            let cy = top + (lines.len() - 1) as f32 * lh;
            if self.shows(cy, self.ch) {
                let caret = Rect::new(cx, cy + self.ch * 0.12, (1.5 * self.s).round().max(1.0), self.ch * 0.76);
                let clip = self.clip();
                self.out.push(quad(caret, with_alpha(c.accent, self.ctx.cursor_alpha.max(0.25)), clip, MODE_SOLID));
            }
        }
        self.hit(r, click);
        self.y = r.bottom();
    }

    pub(super) fn rule(&mut self) {
        let r = Rect::new(self.x, self.y, self.w, self.s.max(1.0).round());
        let line = self.colors.line;
        self.fill(r, line, 0.0);
        self.y = r.bottom();
    }

    /// A tinted box with a bar on its left, around wrapped text.
    pub(super) fn callout(&mut self, content: &str, color: Rgba) {
        let pad = self.cw;
        let height = wrap(content, self.cols(self.w - 2.5 * pad)).len() as f32 * self.line_height() + self.ch * 0.5;
        let r = Rect::new(self.x, self.y, self.w, height.round());
        let tint = mix(self.colors.panel, color, 0.12);
        self.fill(r, tint, 6.0);
        self.fill(Rect::new(r.x, r.y, (3.0 * self.s).round(), r.h), color, 1.0);
        self.y += (self.ch * 0.25).round();
        let (x, w) = (self.x, self.w);
        self.x += 1.5 * pad;
        self.w -= 2.5 * pad;
        let fg = self.colors.fg;
        self.para(content, fg, 0, 0.0);
        (self.x, self.w) = (x, w);
        self.y = r.bottom();
    }

    /// A file position as a link, then the line itself with the part that matters underlined.
    pub(super) fn place(&mut self, f: &Finding) {
        let Some(span) = &f.span else { return };
        let c = self.colors;
        let from_description = span.file == "PR description" || span.file == "commit messages";
        let place = format!("{}:{}", span.file, span.line);
        let width = (place.chars().count() as f32 * self.cw).min(self.w);
        let r = Rect::new(self.x, self.y, width, self.line_height());
        let color = if from_description {
            c.muted
        } else if self.hovered(r) {
            c.accent
        } else {
            c.code
        };
        self.put(self.x, self.y, &place, color, 0, self.w);
        if !from_description {
            self.hit(r, Click::File(span.file.clone(), span.line));
        }
        self.y += self.line_height();

        let (shown, col, len) = snippet(span, self.cols(self.w - 2.0 * self.cw));
        let r = Rect::new(self.x, self.y, self.w, (self.ch * 1.7).round());
        self.fill(r, c.raised, 6.0);
        let ty = r.y + self.ch * 0.25;
        self.put(r.x + self.cw, ty, &shown, c.fg, 0, r.w - self.cw);
        let under = Rect::new(
            r.x + self.cw * (col as f32 + 1.0),
            ty + self.ch * 1.08,
            len as f32 * self.cw,
            (2.0 * self.s).round(),
        );
        let color = c.level(f.level);
        self.fill(under, color, 1.0);
        self.y = r.bottom() + (self.ch * 0.15).round();
        self.para(&span.label, color, 0, self.cw);
    }
}