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
#![forbid(unsafe_code)]
use crate::tui::TUI;
use std::{error::Error, io};
use termion::{input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
use tui::{backend::TermionBackend, text::Spans, Terminal};
#[derive(Debug, Clone)]
pub struct TUIOutput<'a> {
pub left_screen: Vec<Spans<'a>>,
pub right_screen: Vec<Spans<'a>>,
}
pub trait TUIInterface {
const LEFT_TITLE: &'static str;
const RIGHT_TITLE: &'static str;
fn on_redraw(&mut self, line_number: u16, column_number: u16) -> TUIOutput;
fn bound_line(&self, line_number: u16) -> u16;
fn bound_column(&self, line_number: u16, column_number: u16) -> u16;
}
#[derive(Debug, Clone)]
pub struct DebugInterface {
text: Vec<String>,
}
impl DebugInterface {
pub fn new(text: String) -> Self {
let text: Vec<_> = text.split('\n').map(|x| x.to_string()).collect();
Self { text }
}
}
impl TUIInterface for DebugInterface {
const LEFT_TITLE: &'static str = "Left pane";
const RIGHT_TITLE: &'static str = "Right pane";
fn on_redraw(&mut self, line_number: u16, column_number: u16) -> TUIOutput {
TUIOutput {
left_screen: self.text.iter().map(|x| Spans::from(x.clone())).collect(),
right_screen: vec![Spans::from(format!(
"line number: {} column number: {}",
line_number, column_number
))],
}
}
fn bound_line(&self, line_number: u16) -> u16 {
std::cmp::min(
line_number,
(self.text.len().checked_sub(1).unwrap()) as u16,
)
}
fn bound_column(&self, line_number: u16, column_number: u16) -> u16 {
std::cmp::min(column_number, self.text[line_number as usize].len() as u16)
}
}
pub fn start_tui_with_interface<Interface: TUIInterface>(
interface: Interface,
) -> Result<(), Box<dyn Error>> {
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let mut tui = TUI::new(interface);
loop {
terminal.draw(|frame| tui.redraw(frame))?;
if tui.handle_input()? {
break;
}
}
Ok(())
}