petitcarnets 0.0.6

Terminal user interfaces in the Elm architecture (model, update, view), with no dependencies.
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
//! A pane that shows part of some content and scrolls over it, by line,
//! half page and page, with the keys or the mouse wheel, sideways too,
//! and soft wrapped if asked: Bubbles' `viewport` package. Go's
//! deprecated high performance mode is not ported, nor its search
//! highlights.
//!
//! ```
//! use petitcarnets as tea;
//! use tea::bubbles::viewport;
//!
//! let mut v = viewport::Model::new(10, 2);
//! v.set_content("one\ntwo\nthree");
//! assert_eq!(v.view(), "one       \ntwo       ");
//! let _ = v.update::<()>(&tea::Msg::KeyPress(tea::Key::char('j')));
//! assert_eq!(v.view(), "two       \nthree     ");
//! assert!(v.at_bottom());
//! ```

mod render;

use super::key::Binding;
use crate::style::{Style, width};
use crate::{Cmd, KeyMod, MouseButton, Msg};

/// A binding of the key map.
type Field = fn(&KeyMap) -> &Binding;

/// What a binding does.
type Scroll = fn(&mut Model);

/// How far one step sideways scrolls: Bubbles' default.
const HORIZONTAL_STEP: usize = 6;

/// What a gutter function is told about a line: Bubbles'
/// `viewport.GutterContext`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct GutterContext {
    /// The line's index in the content (past the end for filler lines).
    pub index: usize,
    /// How many lines the content has.
    pub total_lines: usize,
    /// Whether this is the second or a later part of a soft wrapped line.
    pub soft: bool,
}

/// Draws the gutter left of a line: Bubbles' `viewport.GutterFunc`.
pub type GutterFn = fn(GutterContext) -> String;

/// The keys of a viewport: Bubbles' `viewport.KeyMap`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyMap {
    /// A page down.
    pub page_down: Binding,
    /// A page up.
    pub page_up: Binding,
    /// Half a page up.
    pub half_page_up: Binding,
    /// Half a page down.
    pub half_page_down: Binding,
    /// A line down.
    pub down: Binding,
    /// A line up.
    pub up: Binding,
    /// A step left.
    pub left: Binding,
    /// A step right.
    pub right: Binding,
}

impl Default for KeyMap {
    /// Bubbles v2's `viewport.DefaultKeyMap`.
    fn default() -> Self {
        let b = |keys: &[&str], key: &str, desc: &str| Binding::new(keys).with_help(key, desc);
        Self {
            page_down: b(&["pgdown", "space", "f"], "f/pgdn", "page down"),
            page_up: b(&["pgup", "b"], "b/pgup", "page up"),
            half_page_up: b(&["u", "ctrl+u"], "u", "½ page up"),
            half_page_down: b(&["d", "ctrl+d"], "d", "½ page down"),
            up: b(&["up", "k"], "↑/k", "up"),
            down: b(&["down", "j"], "↓/j", "down"),
            left: b(&["left", "h"], "←/h", "move left"),
            right: b(&["right", "l"], "→/l", "move right"),
        }
    }
}

impl KeyMap {
    /// No bindings at all, for a viewport driven by its owner (the text
    /// area's).
    #[must_use]
    pub fn none() -> Self {
        let off = Binding::default;
        Self {
            page_down: off(),
            page_up: off(),
            half_page_up: off(),
            half_page_down: off(),
            down: off(),
            up: off(),
            left: off(),
            right: off(),
        }
    }
}

/// A viewport: Bubbles' `viewport.Model`.
#[derive(Debug, Clone)]
pub struct Model {
    width: usize,
    height: usize,
    /// The keys.
    pub key_map: KeyMap,
    /// Whether long lines wrap rather than scroll sideways.
    pub soft_wrap: bool,
    /// Whether short content is padded with blank lines to the height.
    pub fill_height: bool,
    /// Whether the mouse wheel scrolls.
    pub mouse_wheel_enabled: bool,
    /// How many lines a wheel turn scrolls.
    pub mouse_wheel_delta: usize,
    /// The row the viewport starts at on the screen, for the owner's use.
    pub y_position: usize,
    /// The style around the content (padding, border).
    pub style: Style,
    /// Draws a gutter left of each line.
    pub left_gutter: Option<GutterFn>,
    /// A style for each line, by index.
    pub style_line: Option<fn(usize) -> Style>,
    y_offset: usize,
    x_offset: usize,
    horizontal_step: usize,
    lines: Vec<String>,
    longest: usize,
}

impl Default for Model {
    fn default() -> Self {
        Self::new(0, 0)
    }
}

impl Model {
    /// An empty viewport `width` by `height` cells: `viewport.New` with
    /// `WithWidth` and `WithHeight`.
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            width,
            height,
            key_map: KeyMap::default(),
            soft_wrap: false,
            fill_height: false,
            mouse_wheel_enabled: true,
            mouse_wheel_delta: 3,
            y_position: 0,
            style: Style::new(),
            left_gutter: None,
            style_line: None,
            y_offset: 0,
            x_offset: 0,
            horizontal_step: HORIZONTAL_STEP,
            lines: Vec::new(),
            longest: 0,
        }
    }

    /// The width.
    #[must_use]
    pub const fn width(&self) -> usize {
        self.width
    }

    /// Sets the width.
    pub const fn set_width(&mut self, columns: usize) {
        self.width = columns;
    }

    /// The height.
    #[must_use]
    pub const fn height(&self) -> usize {
        self.height
    }

    /// Sets the height.
    pub const fn set_height(&mut self, rows: usize) {
        self.height = rows;
    }

    /// Whether the top shows.
    #[must_use]
    pub const fn at_top(&self) -> bool {
        self.y_offset == 0
    }

    /// Whether the bottom shows.
    #[must_use]
    pub fn at_bottom(&self) -> bool {
        self.y_offset >= self.max_y_offset()
    }

    /// Whether it scrolled past the bottom (content got shorter).
    #[must_use]
    pub fn past_bottom(&self) -> bool {
        self.y_offset > self.max_y_offset()
    }

    /// How far down it has scrolled, from 0.0 to 1.0.
    #[must_use]
    pub fn scroll_percent(&self) -> f64 {
        let most = self.max_y_offset();
        if most == 0 {
            return 1.0;
        }
        (crate::style::to_f64(self.y_offset) / crate::style::to_f64(most)).clamp(0.0, 1.0)
    }

    /// How far right it has scrolled, from 0.0 to 1.0.
    #[must_use]
    pub fn horizontal_scroll_percent(&self) -> f64 {
        if self.x_offset + self.width >= self.longest {
            return 1.0;
        }
        let f = crate::style::to_f64;
        (f(self.x_offset) / (f(self.longest) - f(self.width))).clamp(0.0, 1.0)
    }

    /// Replaces the content; lines end at `\n` (or `\r\n`).
    pub fn set_content(&mut self, content: &str) {
        let lines: Vec<String> = content
            .replace("\r\n", "\n")
            .split('\n')
            .map(String::from)
            .collect();
        self.set_content_lines(lines);
    }

    /// Replaces the content with these lines.
    pub fn set_content_lines(&mut self, lines: Vec<String>) {
        self.lines = if lines.len() == 1 && width(&lines[0]) == 0 {
            Vec::new()
        } else {
            lines
        };
        self.longest = self.lines.iter().map(|l| width(l)).max().unwrap_or(0);
        if self.y_offset > self.max_y_offset() {
            self.goto_bottom();
        }
    }

    /// The content.
    #[must_use]
    pub fn content(&self) -> String {
        self.lines.join("\n")
    }

    /// The first line showing.
    #[must_use]
    pub const fn y_offset(&self) -> usize {
        self.y_offset
    }

    /// Scrolls so line `n` is at the top, as far as the content goes.
    pub fn set_y_offset(&mut self, n: usize) {
        self.y_offset = n.min(self.max_y_offset());
    }

    /// The first column showing.
    #[must_use]
    pub const fn x_offset(&self) -> usize {
        self.x_offset
    }

    /// Scrolls sideways to column `n`; nothing when soft wrapping.
    pub fn set_x_offset(&mut self, n: usize) {
        if !self.soft_wrap {
            self.x_offset = n.min(self.longest.saturating_sub(self.width));
        }
    }

    /// How far a step sideways goes.
    pub const fn set_horizontal_step(&mut self, n: usize) {
        self.horizontal_step = n;
    }

    /// A page down.
    pub fn page_down(&mut self) {
        if !self.at_bottom() {
            self.scroll_down(self.inner_height());
        }
    }

    /// A page up.
    pub fn page_up(&mut self) {
        if !self.at_top() {
            self.scroll_up(self.inner_height());
        }
    }

    /// Half a page down.
    pub fn half_page_down(&mut self) {
        if !self.at_bottom() {
            self.scroll_down(self.inner_height() / 2);
        }
    }

    /// Half a page up.
    pub fn half_page_up(&mut self) {
        if !self.at_top() {
            self.scroll_up(self.inner_height() / 2);
        }
    }

    /// `n` lines down.
    pub fn scroll_down(&mut self, n: usize) {
        if self.at_bottom() || n == 0 || self.lines.is_empty() {
            return;
        }
        self.set_y_offset(self.y_offset + n);
    }

    /// `n` lines up.
    pub fn scroll_up(&mut self, n: usize) {
        if self.at_top() || n == 0 || self.lines.is_empty() {
            return;
        }
        self.set_y_offset(self.y_offset.saturating_sub(n));
    }

    /// `n` columns left.
    pub fn scroll_left(&mut self, n: usize) {
        self.set_x_offset(self.x_offset.saturating_sub(n));
    }

    /// `n` columns right.
    pub fn scroll_right(&mut self, n: usize) {
        self.set_x_offset(self.x_offset + n);
    }

    /// To the top.
    pub const fn goto_top(&mut self) {
        self.y_offset = 0;
    }

    /// To the bottom.
    pub fn goto_bottom(&mut self) {
        self.y_offset = self.max_y_offset();
    }

    /// Lines the content takes, soft wrapped or not.
    #[must_use]
    pub fn total_line_count(&self) -> usize {
        self.locate(0).0
    }

    /// Lines showing.
    #[must_use]
    pub fn visible_line_count(&self) -> usize {
        self.visible_lines().len()
    }

    /// Scrolls on its keys and the mouse wheel.
    pub fn update<M>(&mut self, msg: &Msg<M>) -> Cmd<M> {
        match msg {
            Msg::KeyPress(key) => self.press(key),
            Msg::MouseWheel(mouse) if self.mouse_wheel_enabled => {
                let shift = mouse.mods.contains(KeyMod::SHIFT);
                self.wheel(mouse.button, shift);
            }
            _ => {}
        }
        Cmd::none()
    }

    /// What each key does, in the order Bubbles tries them.
    const KEYS: [(Field, Scroll); 8] = [
        (|k| &k.page_down, Self::page_down),
        (|k| &k.page_up, Self::page_up),
        (|k| &k.half_page_down, Self::half_page_down),
        (|k| &k.half_page_up, Self::half_page_up),
        (|k| &k.down, |m| m.scroll_down(1)),
        (|k| &k.up, |m| m.scroll_up(1)),
        (|k| &k.left, |m| m.scroll_left(m.horizontal_step)),
        (|k| &k.right, |m| m.scroll_right(m.horizontal_step)),
    ];

    /// A key.
    fn press(&mut self, key: &crate::Key) {
        if let Some((_, act)) = Self::KEYS
            .iter()
            .find(|(b, _)| b(&self.key_map).matches(key))
        {
            act(self);
        }
    }

    /// A wheel turn; Shift turns up and down into left and right.
    fn wheel(&mut self, button: MouseButton, shift: bool) {
        match (button, shift) {
            (MouseButton::WheelDown, false) => self.scroll_down(self.mouse_wheel_delta),
            (MouseButton::WheelUp, false) => self.scroll_up(self.mouse_wheel_delta),
            (MouseButton::WheelDown | MouseButton::WheelRight, _) => {
                self.scroll_right(self.horizontal_step);
            }
            (MouseButton::WheelUp | MouseButton::WheelLeft, _) => {
                self.scroll_left(self.horizontal_step);
            }
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests;