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
use crossterm::Command;
use std::{
fmt,
fmt::Write,
pin::Pin,
task::{Context, Poll},
};
use tokio::{
io,
io::{AsyncWrite, AsyncWriteExt, Stdout as TokioStdout},
sync::{Mutex, MutexGuard},
};
#[derive(Debug)]
pub struct Stdout {
inner: Mutex<TokioStdout>,
}
impl Stdout {
pub fn new() -> Stdout {
Self { inner: Mutex::new(io::stdout()) }
}
pub async fn lock<'stdout>(&'stdout self) -> LockedStdout<'stdout> {
LockedStdout { guard: self.inner.lock().await }
}
pub fn try_lock(&self) -> Option<LockedStdout> {
self.inner.try_lock().ok().map(|guard| LockedStdout { guard })
}
pub async fn write_and_flush(&self, buf: &[u8]) -> Result<(), io::Error> {
self.lock().await.write_and_flush(buf).await
}
}
#[derive(Debug)]
pub struct LockedStdout<'stdout> {
guard: MutexGuard<'stdout, TokioStdout>,
}
impl<'stdout> LockedStdout<'stdout> {
pub async fn write_and_flush(
&mut self,
buf: &[u8],
) -> Result<(), io::Error> {
self.write_all(buf).await?;
self.flush().await?;
Ok(())
}
}
impl<'stdout> AsyncWrite for LockedStdout<'stdout> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
Pin::new(&mut *self.as_mut().guard).poll_write(cx, buf)
}
fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), io::Error>> {
Pin::new(&mut *self.as_mut().guard).as_mut().poll_flush(cx)
}
fn poll_shutdown(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), io::Error>> {
Pin::new(&mut *self.as_mut().guard).as_mut().poll_shutdown(cx)
}
}
#[cfg(windows)]
pub fn save_screen(buf: &mut String) -> fmt::Result {
if crossterm::terminal::EnterAlternateScreen.is_ansi_code_supported() {
write!(
buf,
"{}",
crossterm::terminal::EnterAlternateScreen.ansi_code()
)?;
}
Ok(())
}
#[cfg(unix)]
pub fn save_screen(buf: &mut String) -> fmt::Result {
write!(buf, "{}", crossterm::terminal::EnterAlternateScreen.ansi_code())?;
Ok(())
}
#[cfg(windows)]
pub fn restore_screen(buf: &mut String) -> fmt::Result {
if crossterm::terminal::LeaveAlternateScreen.is_ansi_code_supported() {
write!(
buf,
"{}",
crossterm::terminal::LeaveAlternateScreen.ansi_code()
)?;
}
Ok(())
}
#[cfg(unix)]
pub fn restore_screen(buf: &mut String) -> fmt::Result {
write!(buf, "{}", crossterm::terminal::LeaveAlternateScreen.ansi_code())?;
Ok(())
}
#[cfg(windows)]
pub fn emergency_restore() {
let _ = crossterm::terminal::disable_raw_mode();
print!("{}", crossterm::cursor::Show);
print!(
"{}",
crossterm::style::SetBackgroundColor(crossterm::style::Color::Reset)
);
print!(
"{}",
crossterm::style::SetForegroundColor(crossterm::style::Color::Reset)
);
if crossterm::terminal::LeaveAlternateScreen.is_ansi_code_supported() {
print!("{}", crossterm::terminal::LeaveAlternateScreen.ansi_code());
}
println!();
}
#[cfg(unix)]
pub fn emergency_restore() {
let _ = crossterm::terminal::disable_raw_mode();
print!("{}", crossterm::cursor::Show);
print!(
"{}",
crossterm::style::SetBackgroundColor(crossterm::style::Color::Reset)
);
print!(
"{}",
crossterm::style::SetForegroundColor(crossterm::style::Color::Reset)
);
println!("{}", crossterm::terminal::LeaveAlternateScreen.ansi_code());
}