rsvim_core 0.1.3-alpha.2

The core library for RSVIM text editor.
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
//! Canvas.

pub mod frame;
pub mod internal;

#[cfg(test)]
mod frame_tests;

use crate::prelude::*;
use crossterm;
use crossterm::style::Attribute;
use crossterm::style::Stylize;
pub use frame::cell::*;
pub use frame::cursor::*;
pub use frame::*;
use itertools::Itertools;
use std::fmt::Debug;
use std::sync::Arc;
use std::sync::Mutex;

#[derive(Debug, Clone)]
/// Logical canvas.
///
/// It manages both the current frame and the last frame as a screenshot, and internally uses a
/// diff-algorithm to compare the TUI changes, thus only flushing the changed parts to reduce IO
/// operations.
///
/// NOTE: APIs named without `prev_` are current frame, with `prev_` are for previous frame.
pub struct Canvas {
  frame: Frame,
  prev_frame: Frame,
  shaders: Arc<Mutex<Vec<ShaderCommand>>>,
}

arc_mutex_ptr!(Canvas);

impl Canvas {
  /// Make new canvas with terminal actual size.
  pub fn new(size: U16Size) -> Self {
    let shaders =
      Vec::with_capacity((size.height() as usize) * (size.width() as usize));
    Canvas {
      prev_frame: Frame::new(size, Cursor::default()),
      frame: Frame::new(size, Cursor::default()),
      shaders: Arc::new(Mutex::new(shaders)),
    }
  }

  // Current frame {

  /// Get current frame.
  pub fn frame(&self) -> &Frame {
    &self.frame
  }

  /// Get mutable current frame.
  pub fn frame_mut(&mut self) -> &mut Frame {
    &mut self.frame
  }

  pub fn size(&self) -> U16Size {
    self.frame.size()
  }

  /// Get current frame cells.
  pub fn cells(&self) -> &Vec<Cell> {
    self.frame.get_cells()
  }

  /// Get current frame cursor.
  pub fn cursor(&self) -> &Cursor {
    self.frame.cursor()
  }

  // Current frame }

  // Previous frame {

  /// Get previous frame.
  pub fn prev_frame(&self) -> &Frame {
    &self.prev_frame
  }

  pub fn prev_size(&self) -> U16Size {
    self.prev_frame.size()
  }

  /// Get previous frame cells.
  pub fn prev_cells(&self) -> &Vec<Cell> {
    self.prev_frame.get_cells()
  }

  /// Get previous frame cells at specified range.
  pub fn prev_cells_at(&self, pos: U16Pos, n: usize) -> &[Cell] {
    self.prev_frame.get_cells_at(pos, n)
  }

  /// Get previous frame cursor.
  pub fn prev_cursor(&self) -> &Cursor {
    self.prev_frame.cursor()
  }
}

// Shade {
impl Canvas {
  /// Get the shader commands that should print to the terminal device, it internally uses a
  /// diff-algorithm to reduce the outputs.
  pub fn shade(&mut self) -> Arc<Mutex<Vec<ShaderCommand>>> {
    lock!(self.shaders).clear();

    self._shade_cells(self.shaders.clone());

    // Since cursor position will be changed while we are printing a lot of
    // text to the terminal (in the `_shade_cells` method).
    // Thus we need to save and restore the previous cursor position
    let saved_prev_cursor_pos = *self.prev_cursor().pos();

    {
      let mut shaders = lock!(self.shaders);
      if !shaders.is_empty() {
        // Hide cursor to avoid terminal cursor twinkling/jumping while rendering.
        //
        // NOTE: On Windows Terminal, flushing shaders without hiding cursor makes
        // the cursor twinkling/jumping while refreshing the TUI screen.
        // So here let's hide cursor before flushing shaders, and restore the
        // cursor after flushing is done.
        if !self.cursor().hidden() {
          shaders.insert(0, ShaderCommand::CursorHide(crossterm::cursor::Hide));
        }

        // Revert hide cursor.
        if !self.cursor().hidden() {
          shaders.push(ShaderCommand::CursorShow(crossterm::cursor::Show));
        }
      }
      // Here restore the previous cursor position
      shaders.push(ShaderCommand::CursorMoveTo(crossterm::cursor::MoveTo(
        saved_prev_cursor_pos.x(),
        saved_prev_cursor_pos.y(),
      )));
    }

    // For cursor
    self._shade_cursor(self.shaders.clone());

    // Finish shade.
    self._shade_done();

    self.shaders.clone()
  }

  /// Shade done.
  pub fn _shade_done(&mut self) {
    // Save current frame.
    self.prev_frame.clone_from(&self.frame);
    // Reset all dirty marks.
    self.frame.reset_dirty_marks();
  }

  /// Shade cursor and append results into shader vector.
  pub fn _shade_cursor(
    &mut self,
    output_shaders: Arc<Mutex<Vec<ShaderCommand>>>,
  ) {
    let mut output_shaders = lock!(output_shaders);

    let cursor = self.frame.cursor();
    let prev_cursor = self.prev_frame.cursor();

    // If cursor is changed.
    if cursor != prev_cursor {
      if cursor.blinking() != prev_cursor.blinking() {
        if cursor.blinking() {
          output_shaders.push(ShaderCommand::CursorEnableBlinking(
            crossterm::cursor::EnableBlinking,
          ));
        } else {
          output_shaders.push(ShaderCommand::CursorDisableBlinking(
            crossterm::cursor::DisableBlinking,
          ));
        }
      }
      if cursor.hidden() != prev_cursor.hidden() {
        if cursor.hidden() {
          output_shaders
            .push(ShaderCommand::CursorHide(crossterm::cursor::Hide));
        } else {
          output_shaders
            .push(ShaderCommand::CursorShow(crossterm::cursor::Show));
        }
      }
      if cursor.style() != prev_cursor.style() {
        output_shaders
          .push(ShaderCommand::CursorSetCursorStyle(cursor.style()));
      }
      if cursor.pos() != prev_cursor.pos() {
        output_shaders.push(ShaderCommand::CursorMoveTo(
          crossterm::cursor::MoveTo(cursor.pos().x(), cursor.pos().y()),
        ));
      }
    }
  }

  /// Shade cells and append results into shader vector.
  pub fn _shade_cells(
    &mut self,
    output_shaders: Arc<Mutex<Vec<ShaderCommand>>>,
  ) {
    let mut output_shaders = lock!(output_shaders);

    if self.size() == self.prev_size() {
      // When terminal size doesn't change, use dirty-marks diff-algorithm.
      self._dirty_marks_diff(&mut output_shaders);
    } else {
      // When terminal size changes, use brute-force diff-algorithm.
      self._brute_force_diff(&mut output_shaders)
    }
  }

  pub fn _make_consequential_shaders(
    &self,
    row: u16,
    start_col: u16,
    end_col: u16,
    output_shaders: &mut Vec<ShaderCommand>,
  ) {
    debug_assert!(end_col > start_col);
    let new_cells = self.frame().get_cells_at(
      point!(start_col, row),
      end_col as usize - start_col as usize,
    );
    output_shaders.push(ShaderCommand::CursorMoveTo(
      crossterm::cursor::MoveTo(start_col, row),
    ));

    let get_content = |start_idx, end_idx| {
      let contents = new_cells[start_idx..end_idx]
        .iter()
        .map(|c| {
          if c.symbol().is_empty() {
            " "
          } else {
            c.symbol()
          }
        })
        .join("");
      let fg = new_cells[start_idx].fg();
      let bg = new_cells[start_idx].bg();
      let attrs = new_cells[start_idx].attrs();
      let mut contents = contents.with(*fg).on(*bg);
      if attrs.has(Attribute::Bold) {
        contents = contents.bold();
      }
      if attrs.has(Attribute::Dim) {
        contents = contents.dim();
      }
      if attrs.has(Attribute::Italic) {
        contents = contents.italic();
      }
      if attrs.has(Attribute::Underlined) {
        contents = contents.underlined();
      }
      contents
    };

    let mut start_i: usize = 0;
    for (i, cell) in new_cells.iter().enumerate() {
      let start_cell = &new_cells[start_i];
      if *cell.fg() != *start_cell.fg()
        || *cell.bg() != *start_cell.bg()
        || cell.attrs() != start_cell.attrs()
      {
        let contents = get_content(start_i, i);
        // trace!(
        //   "[{:>2},{:>2}-{:>2},{:>2}-{:>2}], content:{} ({:?})",
        //   row,
        //   start_col,
        //   end_col,
        //   start_i,
        //   i,
        //   contents.content(),
        //   contents.style()
        // );
        output_shaders.push(ShaderCommand::StylePrintStyledContentString(
          crossterm::style::PrintStyledContent(contents),
        ));
        start_i = i;
      }
    }
    if start_i < new_cells.len() {
      let contents = get_content(start_i, new_cells.len());
      // trace!(
      //   "[{:>2},{:>2}-{:>2},{:>2}-{:>2}], content:{} ({:?})",
      //   row,
      //   start_col,
      //   end_col,
      //   start_i,
      //   new_cells.len(),
      //   contents.content(),
      //   contents.style()
      // );
      output_shaders.push(ShaderCommand::StylePrintStyledContentString(
        crossterm::style::PrintStyledContent(contents),
      ));
    }
  }

  /// Brute force diff-algorithm, it iterates all cells on current frame, and compares with
  /// previous frame to find out the changed cells.
  ///
  /// This algorithm is useful when the whole terminal size is changed, and row/column based
  /// diff-algorithm becomes invalid.
  pub fn _brute_force_diff(&mut self, output_shaders: &mut Vec<ShaderCommand>) {
    let size = self.size();
    // trace!("brute force diff, size:{:?}", size);

    if self.frame().is_zero_sized() {
      return;
    }

    for row in 0..size.height() {
      self._make_consequential_shaders(
        row,
        0_u16,
        size.width(),
        output_shaders,
      );
    }
  }

  /// Dirty marks diff-algorithm, it only iterates on the area that has been
  /// marked as dirty by UI widgets.
  ///
  /// This algorithm is more performant when the whole terminal size remains
  /// unchanged.
  pub fn _dirty_marks_diff(&mut self, output_shaders: &mut Vec<ShaderCommand>) {
    let size = self.size();
    // trace!("dirty marks diff, size:{:?}", size);

    if self.frame().is_zero_sized() {
      return;
    }

    let n = (size.height() as usize) * (size.width() as usize);
    let mut start_idx: Option<u32> = None;
    let mut start_pos: Option<U16Pos> = None;
    let mut last_idx: Option<u32> = None;
    let mut last_pos: Option<U16Pos> = None;
    for idx in self.frame().dirty_marks().iter() {
      if (idx as usize) < n {
        let pos = self.frame().idx2pos(idx as usize);
        // trace!(
        //   "dirty idx:{:?},pos:{:?}, start idx:{:?},pos:{:?}",
        //   idx, pos, start_idx, start_pos
        // );
        if start_idx.is_none() && start_pos.is_none() {
          start_idx = Some(idx);
          start_pos = Some(pos);
        } else {
          debug_assert!(start_idx.is_some());
          debug_assert!(start_pos.is_some());
          debug_assert!(last_idx.is_some());
          debug_assert!(last_pos.is_some());

          if let Some(start_pos_unwrap) = start_pos
            && let Some(last_idx) = last_idx
            && let Some(last_pos) = last_pos
          {
            // On the same row and column is consequential.
            if start_pos_unwrap.y() == pos.y()
              && last_pos.y() == pos.y()
              && last_idx + 1 == idx
            {
              // Do nothing and continue iterating dirty marks.
            } else {
              // Render a consequential range of cells.
              if last_pos.x() > start_pos_unwrap.x() {
                self._make_consequential_shaders(
                  start_pos_unwrap.y(),
                  start_pos_unwrap.x(),
                  last_pos.x() + 1,
                  output_shaders,
                );
                start_idx = Some(idx);
                start_pos = Some(pos);
              }
            }
          }
        }
        last_idx = Some(idx);
        last_pos = Some(pos);
      }
    }
    if let Some(start_pos) = start_pos
      && let Some(last_pos) = last_pos
      && last_pos.x() > start_pos.x()
    {
      self._make_consequential_shaders(
        start_pos.y(),
        start_pos.x(),
        last_pos.x() + 1,
        output_shaders,
      );
    }
  }
}
// Shade }

#[derive(Debug, Clone)]
/// Shader command enums.
///
/// All-in-one wrapper to wrap all the [`crossterm::Command`], thus helps to return the rendering
/// updates for the terminal.
pub enum ShaderCommand {
  CursorSetCursorStyle(crossterm::cursor::SetCursorStyle),
  CursorDisableBlinking(crossterm::cursor::DisableBlinking),
  CursorEnableBlinking(crossterm::cursor::EnableBlinking),
  CursorHide(crossterm::cursor::Hide),
  CursorMoveDown(crossterm::cursor::MoveDown),
  CursorMoveLeft(crossterm::cursor::MoveLeft),
  CursorMoveRight(crossterm::cursor::MoveRight),
  CursorMoveTo(crossterm::cursor::MoveTo),
  CursorMoveToColumn(crossterm::cursor::MoveToColumn),
  CursorMoveToNextLine(crossterm::cursor::MoveToNextLine),
  CursorMoveToPreviousLine(crossterm::cursor::MoveToPreviousLine),
  CursorMoveToRow(crossterm::cursor::MoveToRow),
  CursorMoveUp(crossterm::cursor::MoveUp),
  CursorRestorePosition(crossterm::cursor::RestorePosition),
  CursorSavePosition(crossterm::cursor::SavePosition),
  CursorShow(crossterm::cursor::Show),
  EventDisableBracketedPaste(crossterm::event::DisableBracketedPaste),
  EventDisableFocusChange(crossterm::event::DisableFocusChange),
  EventDisableMouseCapture(crossterm::event::DisableMouseCapture),
  EventEnableBracketedPaste(crossterm::event::EnableBracketedPaste),
  EventEnableFocusChange(crossterm::event::EnableFocusChange),
  EventEnableMouseCapture(crossterm::event::EnableMouseCapture),
  EventPopKeyboardEnhancementFlags(
    crossterm::event::PopKeyboardEnhancementFlags,
  ),
  EventPushKeyboardEnhancementFlags(
    crossterm::event::PushKeyboardEnhancementFlags,
  ),
  StyleResetColor(crossterm::style::ResetColor),
  StyleSetAttribute(crossterm::style::SetAttribute),
  StyleSetAttributes(crossterm::style::SetAttributes),
  StyleSetBackgroundColor(crossterm::style::SetBackgroundColor),
  StyleSetColors(crossterm::style::SetColors),
  StyleSetForegroundColor(crossterm::style::SetForegroundColor),
  StyleSetStyle(crossterm::style::SetStyle),
  StyleSetUnderlineColor(crossterm::style::SetUnderlineColor),
  StylePrintStyledContentString(crossterm::style::PrintStyledContent<String>),
  StylePrintString(crossterm::style::Print<String>),
  TerminalBeginSynchronizedUpdate(crossterm::terminal::BeginSynchronizedUpdate),
  TerminalClear(crossterm::terminal::Clear),
  TerminalDisableLineWrap(crossterm::terminal::DisableLineWrap),
  TerminalEnableLineWrap(crossterm::terminal::EnableLineWrap),
  TerminalEndSynchronizedUpdate(crossterm::terminal::EndSynchronizedUpdate),
  TerminalEnterAlternateScreen(crossterm::terminal::EnterAlternateScreen),
  TerminalLeaveAlternateScreen(crossterm::terminal::LeaveAlternateScreen),
  TerminalScrollDown(crossterm::terminal::ScrollDown),
  TerminalScrollUp(crossterm::terminal::ScrollUp),
  TerminalSetSize(crossterm::terminal::SetSize),
}