-
Notifications
You must be signed in to change notification settings - Fork 7
/
widget.rs
615 lines (590 loc) · 19.5 KB
/
widget.rs
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier, Style},
widgets::{Block, Clear, Widget},
};
use crate::state;
/// A trait representing a pseudo-terminal screen.
///
/// Implementing this trait allows for backends other than `vt100` to be used
/// with the `PseudoTerminal` widget.
pub trait Screen {
/// The type of cell this screen contains
type C: Cell;
/// Returns the cell at the given location if it exists.
fn cell(&self, row: u16, col: u16) -> Option<&Self::C>;
/// Returns whether the terminal should be hidden
fn hide_cursor(&self) -> bool;
/// Returns cursor position of screen.
///
/// The return value is expected to be (row, column)
fn cursor_position(&self) -> (u16, u16);
}
/// A trait for representing a single cell on a screen.
pub trait Cell {
/// Whether the cell has any contents that could be rendered to the screen.
fn has_contents(&self) -> bool;
/// Apply the contents and styling of this cell to the provided buffer cell.
fn apply(&self, cell: &mut ratatui::buffer::Cell);
}
/// A widget representing a pseudo-terminal screen.
///
/// The `PseudoTerminal` widget displays the contents of a pseudo-terminal screen,
/// which is typically populated with text and control sequences from a terminal emulator.
/// It provides a visual representation of the terminal output within a TUI application.
///
/// The contents of the pseudo-terminal screen are represented by a `vt100::Screen` object.
/// The `vt100` library provides functionality for parsing and processing terminal control sequences
/// and handling terminal state, allowing the `PseudoTerminal` widget to accurately render the
/// terminal output.
///
/// # Examples
///
/// ```rust
/// use ratatui::{
/// style::{Color, Modifier, Style},
/// widgets::{Block, Borders},
/// };
/// use tui_term::widget::PseudoTerminal;
/// use vt100::Parser;
///
/// let mut parser = vt100::Parser::new(24, 80, 0);
/// let pseudo_term = PseudoTerminal::new(parser.screen())
/// .block(Block::default().title("Terminal").borders(Borders::ALL))
/// .style(
/// Style::default()
/// .fg(Color::White)
/// .bg(Color::Black)
/// .add_modifier(Modifier::BOLD),
/// );
/// ```
#[non_exhaustive]
pub struct PseudoTerminal<'a, S> {
screen: &'a S,
pub(crate) block: Option<Block<'a>>,
style: Option<Style>,
pub(crate) cursor: Cursor,
}
#[non_exhaustive]
pub struct Cursor {
pub(crate) show: bool,
pub(crate) symbol: String,
pub(crate) style: Style,
pub(crate) overlay_style: Style,
}
impl Cursor {
/// Sets the symbol for the cursor.
///
/// # Arguments
///
/// * `symbol`: The symbol to set as the cursor.
///
/// # Example
///
/// ```
/// use ratatui::style::Style;
/// use tui_term::widget::Cursor;
///
/// let cursor = Cursor::default().symbol("|");
/// ```
#[inline]
#[must_use]
pub fn symbol(mut self, symbol: &str) -> Self {
self.symbol = symbol.into();
self
}
/// Sets the style for the cursor.
///
/// # Arguments
///
/// * `style`: The `Style` to set for the cursor.
///
/// # Example
///
/// ```
/// use ratatui::style::Style;
/// use tui_term::widget::Cursor;
///
/// let cursor = Cursor::default().style(Style::default());
/// ```
#[inline]
#[must_use]
pub const fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
/// Sets the overlay style for the cursor.
///
/// The overlay style is used when the cursor overlaps with existing content on the screen.
///
/// # Arguments
///
/// * `overlay_style`: The `Style` to set as the overlay style for the cursor.
///
/// # Example
///
/// ```
/// use ratatui::style::Style;
/// use tui_term::widget::Cursor;
///
/// let cursor = Cursor::default().overlay_style(Style::default());
/// ```
#[inline]
#[must_use]
pub const fn overlay_style(mut self, overlay_style: Style) -> Self {
self.overlay_style = overlay_style;
self
}
/// Set the visibility of the cursor (default = shown)
#[inline]
#[must_use]
pub const fn visibility(mut self, show: bool) -> Self {
self.show = show;
self
}
/// Show the cursor (default)
#[inline]
pub fn show(&mut self) {
self.show = true;
}
/// Hide the cursor
#[inline]
pub fn hide(&mut self) {
self.show = false;
}
}
impl Default for Cursor {
#[inline]
fn default() -> Self {
Self {
show: true,
symbol: "\u{2588}".into(), //"█".
style: Style::default().fg(Color::Gray),
overlay_style: Style::default().add_modifier(Modifier::REVERSED),
}
}
}
impl<'a, S: Screen> PseudoTerminal<'a, S> {
/// Creates a new instance of `PseudoTerminal`.
///
/// # Arguments
///
/// * `screen`: The reference to the `Screen`.
///
/// # Example
///
/// ```
/// use tui_term::widget::PseudoTerminal;
/// use vt100::Parser;
///
/// let mut parser = vt100::Parser::new(24, 80, 0);
/// let pseudo_term = PseudoTerminal::new(parser.screen());
/// ```
#[inline]
#[must_use]
pub fn new(screen: &'a S) -> Self {
PseudoTerminal {
screen,
block: None,
style: None,
cursor: Cursor::default(),
}
}
/// Sets the block for the `PseudoTerminal`.
///
/// # Arguments
///
/// * `block`: The `Block` to set.
///
/// # Example
///
/// ```
/// use ratatui::widgets::Block;
/// use tui_term::widget::PseudoTerminal;
/// use vt100::Parser;
///
/// let mut parser = vt100::Parser::new(24, 80, 0);
/// let block = Block::default();
/// let pseudo_term = PseudoTerminal::new(parser.screen()).block(block);
/// ```
#[inline]
#[must_use]
pub fn block(mut self, block: Block<'a>) -> Self {
self.block = Some(block);
self
}
/// Sets the cursor configuration for the `PseudoTerminal`.
///
/// The `cursor` method allows configuring the appearance of the cursor within the
/// `PseudoTerminal` widget.
///
/// # Arguments
///
/// * `cursor`: The `Cursor` configuration to set.
///
/// # Example
///
/// ```rust
/// use ratatui::style::Style;
/// use tui_term::widget::{Cursor, PseudoTerminal};
///
/// let mut parser = vt100::Parser::new(24, 80, 0);
/// let cursor = Cursor::default().symbol("|").style(Style::default());
/// let pseudo_term = PseudoTerminal::new(parser.screen()).cursor(cursor);
/// ```
#[inline]
#[must_use]
pub fn cursor(mut self, cursor: Cursor) -> Self {
self.cursor = cursor;
self
}
/// Sets the style for `PseudoTerminal`.
///
/// # Arguments
///
/// * `style`: The `Style` to set.
///
/// # Example
///
/// ```
/// use ratatui::style::Style;
/// use tui_term::widget::PseudoTerminal;
///
/// let mut parser = vt100::Parser::new(24, 80, 0);
/// let style = Style::default();
/// let pseudo_term = PseudoTerminal::new(parser.screen()).style(style);
/// ```
#[inline]
#[must_use]
pub const fn style(mut self, style: Style) -> Self {
self.style = Some(style);
self
}
#[inline]
#[must_use]
pub const fn screen(&self) -> &S {
self.screen
}
}
impl<S: Screen> Widget for PseudoTerminal<'_, S> {
#[inline]
fn render(self, area: Rect, buf: &mut Buffer) {
Clear.render(area, buf);
let area = self.block.as_ref().map_or(area, |b| {
let inner_area = b.inner(area);
b.clone().render(area, buf);
inner_area
});
state::handle(&self, area, buf);
}
}
#[cfg(all(test, feature = "vt100"))]
mod tests {
use ratatui::{backend::TestBackend, widgets::Borders, Terminal};
use super::*;
fn snapshot_typescript(stream: &[u8]) -> String {
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
parser.process(stream);
let pseudo_term = PseudoTerminal::new(parser.screen());
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
format!("{:?}", terminal.backend().buffer())
}
#[test]
fn empty_actions() {
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
parser.process(b" ");
let pseudo_term = PseudoTerminal::new(parser.screen());
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn boundary_rows_overshot_no_panic() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
// Make the backend on purpose much smaller
let backend = TestBackend::new(80, 4);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
parser.process(stream);
let pseudo_term = PseudoTerminal::new(parser.screen());
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn simple_ls() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn simple_cursor_alternate_symbol() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
let cursor = Cursor::default().symbol("|");
parser.process(stream);
let pseudo_term = PseudoTerminal::new(parser.screen()).cursor(cursor);
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn simple_cursor_styled() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
let style = Style::default().bg(Color::Cyan).fg(Color::LightRed);
let cursor = Cursor::default().symbol("|").style(style);
parser.process(stream);
let pseudo_term = PseudoTerminal::new(parser.screen()).cursor(cursor);
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn simple_cursor_hide() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
let cursor = Cursor::default().visibility(false);
parser.process(stream);
let pseudo_term = PseudoTerminal::new(parser.screen()).cursor(cursor);
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn simple_cursor_hide_alt() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
let mut cursor = Cursor::default();
cursor.hide();
parser.process(stream);
let pseudo_term = PseudoTerminal::new(parser.screen()).cursor(cursor);
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn overlapping_cursor() {
let stream = include_bytes!("../test/typescript/overlapping_cursor.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn overlapping_cursor_alternate_style() {
let stream = include_bytes!("../test/typescript/overlapping_cursor.typescript");
let backend = TestBackend::new(80, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
let style = Style::default().bg(Color::Cyan).fg(Color::LightRed);
let cursor = Cursor::default().overlay_style(style);
parser.process(stream);
let pseudo_term = PseudoTerminal::new(parser.screen()).cursor(cursor);
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn simple_ls_with_block() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
let backend = TestBackend::new(100, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
parser.process(stream);
let block = Block::default().borders(Borders::ALL).title("ls");
let pseudo_term = PseudoTerminal::new(parser.screen()).block(block);
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn simple_ls_no_style_from_block() {
let stream = include_bytes!("../test/typescript/simple_ls.typescript");
let backend = TestBackend::new(100, 24);
let mut terminal = Terminal::new(backend).unwrap();
let mut parser = vt100::Parser::new(24, 80, 0);
parser.process(stream);
let block = Block::default()
.borders(Borders::ALL)
.style(Style::default().add_modifier(Modifier::BOLD))
.title("ls");
let pseudo_term = PseudoTerminal::new(parser.screen()).block(block);
terminal
.draw(|f| {
f.render_widget(pseudo_term, f.size());
})
.unwrap();
let view = format!("{:?}", terminal.backend().buffer());
insta::assert_snapshot!(view);
}
#[test]
fn italic_text() {
let stream = b"[3mThis line will be displayed in italic.[0m This should have no style.";
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn underlined_text() {
let stream =
b"[4mThis line will be displayed with an underline.[0m This should have no style.";
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn bold_text() {
let stream = b"[1mThis line will be displayed bold.[0m This should have no style.";
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn inverse_text() {
let stream = b"[7mThis line will be displayed inversed.[0m This should have no style.";
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn combined_modifier_text() {
let stream =
b"[4m[3mThis line will be displayed in italic and underlined.[0m This should have no style.";
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_01() {
let stream = include_bytes!("../test/typescript/vttest_02_01.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_02() {
let stream = include_bytes!("../test/typescript/vttest_02_02.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_03() {
let stream = include_bytes!("../test/typescript/vttest_02_03.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_04() {
let stream = include_bytes!("../test/typescript/vttest_02_04.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_05() {
let stream = include_bytes!("../test/typescript/vttest_02_05.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_06() {
let stream = include_bytes!("../test/typescript/vttest_02_06.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_07() {
let stream = include_bytes!("../test/typescript/vttest_02_07.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_08() {
let stream = include_bytes!("../test/typescript/vttest_02_08.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_09() {
let stream = include_bytes!("../test/typescript/vttest_02_09.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_10() {
let stream = include_bytes!("../test/typescript/vttest_02_10.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_11() {
let stream = include_bytes!("../test/typescript/vttest_02_11.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_12() {
let stream = include_bytes!("../test/typescript/vttest_02_12.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_13() {
let stream = include_bytes!("../test/typescript/vttest_02_13.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_14() {
let stream = include_bytes!("../test/typescript/vttest_02_14.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
#[test]
fn vttest_02_15() {
let stream = include_bytes!("../test/typescript/vttest_02_15.typescript");
let view = snapshot_typescript(stream);
insta::assert_snapshot!(view);
}
}