petitcarnets 0.0.7

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
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
//! 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 keymap;
mod render;
mod scroll;

pub use keymap::KeyMap;

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`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::viewport;
///
/// let mut v = viewport::Model::new(6, 2);
/// v.left_gutter = Some(|g: viewport::GutterContext| format!("{} ", g.index + 1));
/// v.set_content("a\nb");
/// assert_eq!(v.view(), "1 a   \n2 b   ");
/// ```
#[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;

/// A viewport: Bubbles' `viewport.Model`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::bubbles::viewport;
///
/// let mut v = viewport::Model::new(3, 2);
/// v.soft_wrap = true;
/// v.set_content("abcdef");
/// assert_eq!(v.view(), "abc\ndef");
/// ```
#[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`.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let v = viewport::Model::new(10, 2);
    /// assert_eq!((v.width(), v.height()), (10, 2));
    /// assert_eq!(v.content(), "");
    /// ```
    #[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.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(10, 2);
    /// assert_eq!(v.width(), 10);
    /// v.set_width(4);
    /// assert_eq!(v.width(), 4);
    /// ```
    #[must_use]
    pub const fn width(&self) -> usize {
        self.width
    }

    /// Sets the width.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(10, 1);
    /// v.set_content("abcdef");
    /// v.set_width(3);
    /// assert_eq!(v.view(), "abc");
    /// ```
    pub const fn set_width(&mut self, columns: usize) {
        self.width = columns;
    }

    /// The height.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(10, 2);
    /// assert_eq!(v.height(), 2);
    /// v.set_height(5);
    /// assert_eq!(v.height(), 5);
    /// ```
    #[must_use]
    pub const fn height(&self) -> usize {
        self.height
    }

    /// Sets the height.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(1, 1);
    /// v.set_content("1\n2\n3\n4\n5");
    /// v.set_height(3);
    /// assert_eq!(v.visible_line_count(), 3);
    /// ```
    pub const fn set_height(&mut self, rows: usize) {
        self.height = rows;
    }

    /// Whether the top shows.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(1, 2);
    /// v.set_content("1\n2\n3\n4\n5");
    /// assert!(v.at_top());
    /// v.scroll_down(1);
    /// assert!(!v.at_top());
    /// ```
    #[must_use]
    pub const fn at_top(&self) -> bool {
        self.y_offset == 0
    }

    /// Whether the bottom shows.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(1, 2);
    /// v.set_content("1\n2\n3\n4\n5");
    /// assert!(!v.at_bottom());
    /// v.goto_bottom();
    /// assert!(v.at_bottom());
    /// ```
    #[must_use]
    pub fn at_bottom(&self) -> bool {
        self.y_offset >= self.max_y_offset()
    }

    /// Whether it scrolled past the bottom (content got shorter).
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(1, 2);
    /// v.set_content("1\n2\n3\n4\n5");
    /// v.goto_bottom();
    /// assert!(!v.past_bottom());
    /// v.set_height(5);
    /// assert!(v.past_bottom(), "taller now, so scrolled too far");
    /// ```
    #[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.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(1, 3);
    /// v.set_content("1\n2\n3\n4\n5");
    /// assert_eq!(v.scroll_percent(), 0.0);
    /// v.scroll_down(1);
    /// assert_eq!(v.scroll_percent(), 0.5);
    /// ```
    #[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.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(2, 1);
    /// v.set_content("abcdef");
    /// assert_eq!(v.horizontal_scroll_percent(), 0.0);
    /// v.set_x_offset(2);
    /// assert_eq!(v.horizontal_scroll_percent(), 0.5);
    /// ```
    #[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`).
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(3, 2);
    /// v.set_content("a\r\nb");
    /// assert_eq!(v.content(), "a\nb");
    /// assert_eq!(v.view(), "a  \nb  ");
    /// ```
    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.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(3, 2);
    /// v.set_content_lines(vec!["x".into(), "y".into()]);
    /// assert_eq!(v.view(), "x  \ny  ");
    /// ```
    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.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(3, 1);
    /// v.set_content_lines(vec!["x".into(), "y".into()]);
    /// assert_eq!(v.content(), "x\ny");
    /// ```
    #[must_use]
    pub fn content(&self) -> String {
        self.lines.join("\n")
    }

    /// Lines the content takes, soft wrapped or not.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(3, 1);
    /// v.set_content("abcdef\ng");
    /// assert_eq!(v.total_line_count(), 2);
    /// v.soft_wrap = true;
    /// assert_eq!(v.total_line_count(), 3, "the long line wraps in two");
    /// ```
    #[must_use]
    pub fn total_line_count(&self) -> usize {
        self.locate(0).0
    }

    /// Lines showing.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(1, 2);
    /// v.set_content("1\n2\n3\n4\n5");
    /// assert_eq!(v.visible_line_count(), 2);
    /// v.set_content("1");
    /// assert_eq!(v.visible_line_count(), 1);
    /// ```
    #[must_use]
    pub fn visible_line_count(&self) -> usize {
        self.visible_lines().len()
    }

    /// Scrolls on its keys and the mouse wheel.
    ///
    /// ```
    /// use petitcarnets as tea;
    /// use tea::bubbles::viewport;
    ///
    /// let mut v = viewport::Model::new(1, 2);
    /// v.set_content("1\n2\n3\n4\n5");
    /// let _ = v.update::<()>(&tea::Msg::KeyPress(tea::Key::char('j')));
    /// assert_eq!(v.y_offset(), 1, "j is a line down");
    /// let _ = v.update::<()>(&tea::Msg::KeyPress(tea::Key::char('k')));
    /// assert_eq!(v.y_offset(), 0);
    /// ```
    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;