Skip to content

Commit c122991

Browse files
committed
refactor(ui): extract terminal restore and external signal shutdown into testable helpers
1 parent 9e2c80d commit c122991

1 file changed

Lines changed: 56 additions & 17 deletions

File tree

‎src/ui.rs‎

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,37 @@ impl<'a> UI<'a> {
161161

162162
fn setup_signal_handler(should_exit: Arc<AtomicBool>) {
163163
ctrlc::set_handler(move || {
164-
// Restore terminal state before exiting
165-
let _ = disable_raw_mode();
166-
let _ = execute!(
167-
io::stdout(),
168-
LeaveAlternateScreen,
169-
DisableMouseCapture,
170-
crossterm::cursor::Show
171-
);
172-
should_exit.store(true, Ordering::SeqCst);
173-
// Exit immediately for external signals (SIGTERM)
174-
std::process::exit(0);
164+
let mut stdout = io::stdout();
165+
Self::handle_external_signal(should_exit.as_ref(), &mut stdout, std::process::exit);
175166
})
176167
.expect("Error setting Ctrl-C handler");
177168
}
178169

170+
fn handle_external_signal<W: io::Write, F, T>(
171+
should_exit: &AtomicBool,
172+
writer: &mut W,
173+
exit: F,
174+
) -> T
175+
where
176+
F: FnOnce(i32) -> T,
177+
{
178+
// Restore terminal state before exiting
179+
let _ = disable_raw_mode();
180+
let _ = Self::leave_terminal_ui(writer);
181+
should_exit.store(true, Ordering::SeqCst);
182+
// Exit immediately for external signals (SIGTERM)
183+
exit(0)
184+
}
185+
186+
fn leave_terminal_ui<W: io::Write>(writer: &mut W) -> io::Result<()> {
187+
execute!(
188+
writer,
189+
LeaveAlternateScreen,
190+
DisableMouseCapture,
191+
crossterm::cursor::Show
192+
)
193+
}
194+
179195
/// Loads a commit and starts the animation.
180196
pub fn load_commit(&mut self, metadata: CommitMetadata) {
181197
self.play_commit(metadata, true);
@@ -489,12 +505,7 @@ impl<'a> UI<'a> {
489505

490506
fn cleanup(&mut self, terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> Result<()> {
491507
disable_raw_mode()?;
492-
execute!(
493-
terminal.backend_mut(),
494-
LeaveAlternateScreen,
495-
DisableMouseCapture
496-
)?;
497-
terminal.show_cursor()?;
508+
Self::leave_terminal_ui(terminal.backend_mut())?;
498509
Ok(())
499510
}
500511

@@ -788,6 +799,7 @@ mod tests {
788799
use chrono::{DateTime, Utc};
789800
use git2::{Repository, Signature, Time};
790801
use ratatui::{backend::TestBackend, buffer::Buffer, Terminal};
802+
use std::cell::Cell;
791803
use std::fs;
792804
use std::path::{Path, PathBuf};
793805
use std::sync::atomic::{AtomicU64, Ordering, Ordering as CounterOrdering};
@@ -961,6 +973,33 @@ mod tests {
961973
.join("\n")
962974
}
963975

976+
#[test]
977+
fn leave_terminal_ui_writes_restore_escape_sequences() {
978+
let mut output = Vec::new();
979+
980+
UI::leave_terminal_ui(&mut output).unwrap();
981+
982+
let output = String::from_utf8(output).unwrap();
983+
assert!(output.contains("\u{1b}[?1049l"));
984+
assert!(output.contains("\u{1b}[?1000l"));
985+
assert!(output.contains("\u{1b}[?25h"));
986+
}
987+
988+
#[test]
989+
fn handle_external_signal_restores_terminal_sets_exit_flag_and_requests_exit() {
990+
let should_exit = AtomicBool::new(false);
991+
let exit_code = Cell::new(None);
992+
let mut output = Vec::new();
993+
994+
UI::handle_external_signal(&should_exit, &mut output, |code| {
995+
exit_code.set(Some(code));
996+
});
997+
998+
assert!(should_exit.load(Ordering::SeqCst));
999+
assert_eq!(exit_code.get(), Some(0));
1000+
assert!(!output.is_empty());
1001+
}
1002+
9641003
#[test]
9651004
fn load_commit_tracks_history_navigation_and_truncates_future_entries() {
9661005
let mut ui = test_ui();

0 commit comments

Comments
 (0)