gpui-cn 0.7.0

shadcn-style components for GPUI, built on gpui-base.
Documentation
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Immutable frames the engine publishes to the UI.
//!
//! A frame is everything a renderer needs to draw one viewport: the rows as
//! text with cell spans and style runs, the cursor, the colors, the scroll
//! metrics and the input modes. Rows are `Arc`s so a frame that changes one
//! row shares the rest with the previous frame.
//!
//! Derived from Herdr (Apache-2.0).

use std::ops::Range;
use std::sync::Arc;

use ghostty_vt::render::RowId;
use ghostty_vt::style::Palette;

/// An 8-bit RGB color.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Rgb(pub u8, pub u8, pub u8);

/// A cell color as the program set it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Color {
    /// The terminal default for that slot.
    #[default]
    Default,
    /// A 256-color palette index.
    Indexed(u8),
    /// A direct color.
    Rgb(Rgb),
}

/// Underline styles, in SGR 4:n order.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Underline {
    /// No underline.
    #[default]
    None,
    /// A single line.
    Single,
    /// Two lines.
    Double,
    /// A wavy line.
    Curly,
    /// A dotted line.
    Dotted,
    /// A dashed line.
    Dashed,
}

/// The visual attributes of a run of cells.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Style {
    /// Foreground color.
    pub foreground: Color,
    /// Background color.
    pub background: Color,
    /// Underline color; `Default` means the foreground.
    pub underline_color: Color,
    /// Bold.
    pub bold: bool,
    /// Italic.
    pub italic: bool,
    /// Faint, drawn at half opacity.
    pub faint: bool,
    /// Blink.
    pub blink: bool,
    /// Inverse video: foreground and background swapped.
    pub inverse: bool,
    /// Invisible: the text is drawn in the background color.
    pub invisible: bool,
    /// Underline style.
    pub underline: Underline,
    /// Strikethrough.
    pub strikethrough: bool,
    /// Overline.
    pub overline: bool,
}

/// One grid cell of a row: its bytes in the row text and its width.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct CellSpan {
    /// The cell's bytes in [`TerminalRow::text`]. Empty for a spacer.
    pub text: Range<usize>,
    /// The column of the cell.
    pub column: u16,
    /// Width in columns: 1, or 2 for a wide character. 0 for a spacer.
    pub width: u8,
    /// Whether this is the tail of a wide character or a wrap spacer, which
    /// is never drawn.
    pub spacer: bool,
    /// The OSC 8 hyperlink on the cell.
    pub hyperlink: Option<Arc<str>>,
}

/// A run of consecutive cells with one style.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct StyleRun {
    /// The run's bytes in [`TerminalRow::text`].
    pub text: Range<usize>,
    /// The run's columns.
    pub columns: Range<u16>,
    /// The style of every cell in the run.
    pub style: Style,
}

/// One row of the viewport.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TerminalRow {
    /// The engine's identity for the row. It stays with the row as it
    /// scrolls, so a renderer can key a cache on it.
    pub id: RowId,
    /// The frame revision the row was last rebuilt in.
    pub revision: u64,
    /// The row's text, one grapheme cluster per non-spacer cell, with a
    /// space for an empty cell.
    pub text: Arc<str>,
    /// One span per column.
    pub cells: Arc<[CellSpan]>,
    /// The style runs covering the row.
    pub runs: Arc<[StyleRun]>,
    /// Whether the row continues on the next row.
    pub soft_wrapped: bool,
}

/// Cursor shapes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CursorShape {
    /// A filled block.
    #[default]
    Block,
    /// A vertical bar.
    Bar,
    /// An underline.
    Underline,
    /// An outlined block, drawn when the terminal is not focused.
    Hollow,
}

/// The cursor as the program left it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Cursor {
    /// Column in the viewport.
    pub column: u16,
    /// Row in the viewport.
    pub row: u16,
    /// Whether the cursor is visible and inside the viewport.
    pub visible: bool,
    /// Whether the program asked for a blinking cursor.
    pub blinking: bool,
    /// The shape the program asked for.
    pub shape: CursorShape,
}

/// The colors a frame is drawn with.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TerminalColors {
    /// Default foreground.
    pub(crate) foreground: Rgb,
    /// Default background.
    pub(crate) background: Rgb,
    /// Cursor color; `None` means the foreground.
    pub(crate) cursor: Option<Rgb>,
    /// Selection background; `None` means the default foreground.
    pub(crate) selection_background: Option<Rgb>,
    /// Selection foreground; `None` means the default background.
    pub(crate) selection_foreground: Option<Rgb>,
    /// The 256-color palette.
    pub(crate) palette: [Rgb; 256],
}

impl Default for TerminalColors {
    /// Ghostty's default palette with a light foreground on a dark
    /// background.
    fn default() -> Self {
        Self {
            foreground: Rgb(0xff, 0xff, 0xff),
            background: Rgb(0x28, 0x2c, 0x34),
            cursor: None,
            selection_background: None,
            selection_foreground: None,
            palette: Palette::default().0.map(|c| Rgb(c.r, c.g, c.b)),
        }
    }
}

impl TerminalColors {
    /// The default palette with the given foreground and background.
    #[must_use]
    pub fn new(foreground: Rgb, background: Rgb) -> Self {
        Self {
            foreground,
            background,
            ..Self::default()
        }
    }

    /// Set the cursor color.
    #[must_use]
    pub fn with_cursor(mut self, cursor: Rgb) -> Self {
        self.cursor = Some(cursor);
        self
    }

    /// Set the selection colors.
    #[must_use]
    pub fn with_selection(mut self, background: Rgb, foreground: Rgb) -> Self {
        self.selection_background = Some(background);
        self.selection_foreground = Some(foreground);
        self
    }

    /// Set one palette entry.
    #[must_use]
    pub fn with_palette_entry(mut self, index: u8, color: Rgb) -> Self {
        self.palette[usize::from(index)] = color;
        self
    }

    /// The default foreground.
    pub fn foreground(&self) -> Rgb {
        self.foreground
    }

    /// The default background.
    pub fn background(&self) -> Rgb {
        self.background
    }

    /// The cursor color; `None` means the foreground.
    pub fn cursor(&self) -> Option<Rgb> {
        self.cursor
    }

    /// The selection background; `None` means the default foreground, as
    /// in Ghostty.
    pub fn selection_background(&self) -> Option<Rgb> {
        self.selection_background
    }

    /// The selection foreground; `None` means the default background, as
    /// in Ghostty.
    pub fn selection_foreground(&self) -> Option<Rgb> {
        self.selection_foreground
    }

    /// The 256-color palette.
    pub fn palette(&self) -> &[Rgb; 256] {
        &self.palette
    }

    /// A gpui-cn theme config on these colors, for a [`ThemeScope`] that
    /// draws the chrome around a terminal, such as its tab strip, in the
    /// terminal's colors: the background is the surface and the
    /// foreground the ink. The accent, contrast, fonts, and semantic colors
    /// come from `theme`'s dark config on a dark background and its light
    /// config otherwise.
    ///
    /// [`ThemeScope`]: crate::ThemeScope
    pub fn theme_config(&self, theme: &crate::Theme) -> crate::ThemeConfig {
        let surface = crate::terminal::colors::hsla(self.background);
        let mut config = if crate::theme::lightness(surface) < 0.5 {
            theme.dark.clone()
        } else {
            theme.light.clone()
        };
        config.surface = surface;
        config.ink = crate::terminal::colors::hsla(self.foreground);
        config
    }
}

/// A linear or rectangular selection in viewport cells.
///
/// `start` and `end` are `(column, row)`, both inclusive.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CellSelection {
    /// The first cell.
    pub start: (u16, u16),
    /// The last cell.
    pub end: (u16, u16),
    /// Whether the selection is a rectangle instead of a run of lines.
    pub rectangle: bool,
}

impl CellSelection {
    /// A selection from `start` to `end`.
    #[must_use]
    pub fn new(start: (u16, u16), end: (u16, u16), rectangle: bool) -> Self {
        Self {
            start,
            end,
            rectangle,
        }
    }
}

/// Where the viewport sits in the scrollable area.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ScrollMetrics {
    /// Rows in scrollback plus the active area.
    pub total_rows: usize,
    /// The viewport's first row within `total_rows`.
    pub offset: usize,
    /// Rows in the viewport.
    pub viewport_rows: usize,
}

impl ScrollMetrics {
    /// Whether the viewport shows the bottom of the scrollback.
    #[must_use]
    pub fn at_bottom(self) -> bool {
        self.offset + self.viewport_rows >= self.total_rows
    }
}

/// Terminal modes the UI has to know about.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct InputModes {
    /// A program is receiving mouse reports.
    pub mouse_reporting: bool,
    /// The alternate screen is active.
    pub alternate_screen: bool,
    /// Bracketed paste is on.
    pub bracketed_paste: bool,
    /// Focus reporting (mode 1004) is on.
    pub focus_reporting: bool,
    /// Alternate scroll (mode 1007): wheel events become arrow keys on the
    /// alternate screen.
    pub alternate_scroll: bool,
}

/// The grid size and the cell size in device pixels.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Viewport {
    columns: u16,
    rows: u16,
    cell_width: u32,
    cell_height: u32,
}

impl Default for Viewport {
    fn default() -> Self {
        Self {
            columns: 80,
            rows: 24,
            cell_width: 8,
            cell_height: 16,
        }
    }
}

/// The largest grid the engine accepts on either axis.
pub const MAX_GRID: u16 = 1000;

impl Viewport {
    /// A viewport, rejecting empty grids, grids over [`MAX_GRID`] and cells
    /// over 4096 pixels.
    pub fn new(columns: u16, rows: u16, cell_width: u32, cell_height: u32) -> Option<Self> {
        if columns == 0
            || rows == 0
            || columns > MAX_GRID
            || rows > MAX_GRID
            || cell_width == 0
            || cell_height == 0
            || cell_width > 4096
            || cell_height > 4096
        {
            return None;
        }
        Some(Self {
            columns,
            rows,
            cell_width,
            cell_height,
        })
    }
    /// Columns.
    #[must_use]
    pub fn columns(self) -> u16 {
        self.columns
    }
    /// Rows.
    #[must_use]
    pub fn rows(self) -> u16 {
        self.rows
    }
    /// Cell width in device pixels.
    #[must_use]
    pub fn cell_width(self) -> u32 {
        self.cell_width
    }
    /// Cell height in device pixels.
    #[must_use]
    pub fn cell_height(self) -> u32 {
        self.cell_height
    }
}

/// One rendered viewport.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TerminalFrame {
    /// Increments on every published frame.
    pub revision: u64,
    /// Increments when the rows, the viewport or the scroll position change,
    /// so a selection anchored to content knows when it moved.
    pub content_revision: u64,
    /// The grid this frame was built for.
    pub viewport: Viewport,
    /// The viewport rows, top to bottom.
    pub rows: Arc<[Arc<TerminalRow>]>,
    /// The cursor.
    pub cursor: Cursor,
    /// The colors.
    pub colors: TerminalColors,
    /// The scroll position.
    pub scroll: ScrollMetrics,
    /// The input modes.
    pub modes: InputModes,
}

impl TerminalFrame {
    /// The rows as text, trailing spaces trimmed, joined with newlines.
    #[must_use]
    pub fn text(&self) -> String {
        self.rows
            .iter()
            .map(|row| row.text.trim_end())
            .collect::<Vec<_>>()
            .join("\n")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_default_colors_are_ghosttys_built_in_ones() {
        // Ghostty's src/config/Config.zig at the pinned commit: `background`
        // #282c34, `foreground` #ffffff, and no cursor or selection color.
        let colors = TerminalColors::default();
        assert_eq!(colors.background(), Rgb(0x28, 0x2c, 0x34));
        assert_eq!(colors.foreground(), Rgb(0xff, 0xff, 0xff));
        assert_eq!(colors.cursor(), None);
        assert_eq!(colors.selection_background(), None);
        assert_eq!(colors.selection_foreground(), None);
        assert_eq!(colors.palette()[1], Rgb(0xcc, 0x66, 0x66));
    }

    #[test]
    fn viewport_rejects_bad_sizes() {
        assert!(Viewport::new(0, 1, 8, 16).is_none());
        assert!(Viewport::new(1, 0, 8, 16).is_none());
        assert!(Viewport::new(1001, 1, 8, 16).is_none());
        assert!(Viewport::new(1, 1, 0, 16).is_none());
        assert!(Viewport::new(80, 24, 8, 16).is_some());
    }

    #[test]
    fn scroll_metrics_know_the_bottom() {
        assert!(
            ScrollMetrics {
                total_rows: 30,
                offset: 6,
                viewport_rows: 24
            }
            .at_bottom()
        );
        assert!(
            !ScrollMetrics {
                total_rows: 30,
                offset: 0,
                viewport_rows: 24
            }
            .at_bottom()
        );
    }
}