Skip to content

Commit 7690b2e

Browse files
committed
Apply terminal modes consistently
1 parent 6da7afa commit 7690b2e

1 file changed

Lines changed: 7 additions & 39 deletions

File tree

‎promkit/src/terminal_session.rs‎

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use crossterm::{
77
cursor,
88
event::{DisableMouseCapture, EnableMouseCapture},
99
execute,
10-
terminal::{
11-
disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, EnterAlternateScreen,
12-
LeaveAlternateScreen,
13-
},
10+
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
1411
};
1512

1613
bitflags! {
@@ -63,8 +60,7 @@ fn crossterm_backend(operation: Operation) -> io::Result<()> {
6360
/// unwinding.
6461
///
6562
/// If setup fails partway through, all modes whose setup was attempted are
66-
/// restored before the setup error is returned. Raw mode is not disabled if it
67-
/// was already enabled before the session started.
63+
/// restored before the setup error is returned.
6864
///
6965
/// # Example
7066
///
@@ -93,14 +89,10 @@ impl TerminalSession {
9389
/// then mouse capture. If a step returns an error or unwinds, [`Drop`]
9490
/// attempts to reverse every step that had started.
9591
pub fn try_new(modes: TerminalModes) -> io::Result<Self> {
96-
Self::try_new_with(crossterm_backend, modes, is_raw_mode_enabled()?)
92+
Self::try_new_with(crossterm_backend, modes)
9793
}
9894

99-
fn try_new_with(
100-
backend: Backend,
101-
modes: TerminalModes,
102-
raw_mode_was_enabled: bool,
103-
) -> io::Result<Self> {
95+
fn try_new_with(backend: Backend, modes: TerminalModes) -> io::Result<Self> {
10496
let mut session = Self {
10597
backend,
10698
pending_restore: TerminalModes::empty(),
@@ -115,7 +107,7 @@ impl TerminalSession {
115107
(TerminalModes::HIDDEN_CURSOR, Operation::HideCursor),
116108
(TerminalModes::MOUSE_CAPTURE, Operation::EnableMouseCapture),
117109
] {
118-
if !modes.contains(mode) || (mode == TerminalModes::RAW_MODE && raw_mode_was_enabled) {
110+
if !modes.contains(mode) {
119111
continue;
120112
}
121113

@@ -223,7 +215,7 @@ mod tests {
223215
}
224216

225217
fn try_new(modes: TerminalModes) -> io::Result<TerminalSession> {
226-
TerminalSession::try_new_with(mock_backend, modes, false)
218+
TerminalSession::try_new_with(mock_backend, modes)
227219
}
228220

229221
#[test]
@@ -310,7 +302,7 @@ mod tests {
310302
| TerminalModes::HIDDEN_CURSOR
311303
| TerminalModes::MOUSE_CAPTURE;
312304
let panic = std::panic::catch_unwind(|| {
313-
TerminalSession::try_new_with(panicking_backend, modes, false).ok();
305+
TerminalSession::try_new_with(panicking_backend, modes).ok();
314306
});
315307

316308
assert!(panic.is_err());
@@ -373,28 +365,4 @@ mod tests {
373365
]
374366
);
375367
}
376-
377-
#[test]
378-
fn preserves_raw_mode_that_was_already_enabled() {
379-
reset([]);
380-
381-
let modes = TerminalModes::RAW_MODE
382-
| TerminalModes::ALTERNATE_SCREEN
383-
| TerminalModes::HIDDEN_CURSOR
384-
| TerminalModes::MOUSE_CAPTURE;
385-
let mut session = TerminalSession::try_new_with(mock_backend, modes, true).unwrap();
386-
session.restore().unwrap();
387-
388-
assert_eq!(
389-
operations(),
390-
[
391-
Operation::EnterAlternateScreen,
392-
Operation::HideCursor,
393-
Operation::EnableMouseCapture,
394-
Operation::DisableMouseCapture,
395-
Operation::ShowCursor,
396-
Operation::LeaveAlternateScreen,
397-
]
398-
);
399-
}
400368
}

0 commit comments

Comments
 (0)