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
#[cfg(all(feature = "crossterm", not(feature = "termion")))]
mod _impl {
use crate::crossterm_utils::into_io_error;
use crossterm::{
execute,
terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
};
use std::io;
pub fn size() -> io::Result<(u16, u16)> {
crossterm::terminal::size().map_err(into_io_error)
}
pub struct AlternateRawScreen<T: io::Write> {
inner: T,
}
impl<T: io::Write> AlternateRawScreen<T> {
pub fn try_from(mut write: T) -> Result<Self, io::Error> {
terminal::enable_raw_mode().map_err(into_io_error)?;
execute!(write, EnterAlternateScreen).map_err(crate::crossterm_utils::into_io_error)?;
Ok(AlternateRawScreen { inner: write })
}
}
impl<T: io::Write> io::Write for AlternateRawScreen<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
impl<T: io::Write> Drop for AlternateRawScreen<T> {
fn drop(&mut self) {
terminal::disable_raw_mode().ok();
execute!(self.inner, LeaveAlternateScreen).ok();
}
}
#[cfg(all(feature = "tui-crossterm-backend", not(feature = "tui-react")))]
pub mod tui {
use tui::backend::CrosstermBackend;
pub fn new_terminal<W: std::io::Write>(write: W) -> Result<tui::Terminal<CrosstermBackend<W>>, std::io::Error> {
let backend = CrosstermBackend::new(write);
Ok(tui::Terminal::new(backend)?)
}
}
#[cfg(all(feature = "tui-crossterm-backend", feature = "tui-react"))]
pub mod tui {
use tui::backend::CrosstermBackend;
pub fn new_terminal<W: std::io::Write>(
write: W,
) -> Result<tui_react::Terminal<CrosstermBackend<W>>, std::io::Error> {
let backend = CrosstermBackend::new(write);
Ok(tui_react::Terminal::new(backend)?)
}
}
}
#[cfg(feature = "termion")]
mod _impl {
use std::io;
pub use termion::terminal_size as size;
pub struct AlternateRawScreen<T: io::Write> {
inner: termion::screen::AlternateScreen<T>,
}
impl<T: io::Write> AlternateRawScreen<termion::raw::RawTerminal<T>> {
pub fn try_from(write: T) -> Result<Self, io::Error> {
use termion::raw::IntoRawMode;
let write = write.into_raw_mode()?;
Ok(AlternateRawScreen {
inner: termion::screen::AlternateScreen::from(write),
})
}
}
impl<T: io::Write> io::Write for AlternateRawScreen<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
#[cfg(all(feature = "tui-termion-backend", not(feature = "tui-react")))]
pub mod tui {
use tui::backend::TermionBackend;
pub fn new_terminal<W: std::io::Write>(write: W) -> Result<tui::Terminal<TermionBackend<W>>, std::io::Error> {
let backend = TermionBackend::new(write);
Ok(tui::Terminal::new(backend)?)
}
}
#[cfg(all(feature = "tui-termion-backend", feature = "tui-react"))]
pub mod tui {
use tui::backend::TermionBackend;
pub fn new_terminal<W: std::io::Write>(
write: W,
) -> Result<tui_react::Terminal<TermionBackend<W>>, std::io::Error> {
let backend = TermionBackend::new(write);
Ok(tui_react::Terminal::new(backend)?)
}
}
}
pub use _impl::*;