diff options
author | Silas Bartha <[email protected]> | 2024-06-04 15:00:16 -0400 |
---|---|---|
committer | Silas Bartha <[email protected]> | 2024-06-04 15:00:16 -0400 |
commit | a002e4d738535e6ca779c71231f7b84864b9a8d0 (patch) | |
tree | 8ec521b535f3820328f9ea6f2511ca29c630b82f /src/display/resources.rs | |
parent | 56aafda8495243fa939bdce01f36d4adbf4ec556 (diff) |
Refactored + Renamed + Added Docs
Diffstat (limited to 'src/display/resources.rs')
-rw-r--r-- | src/display/resources.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/display/resources.rs b/src/display/resources.rs new file mode 100644 index 0000000..7321aea --- /dev/null +++ b/src/display/resources.rs @@ -0,0 +1,43 @@ +use std::io::{stdout, Stdout}; + +use bevy::prelude::*; +use crossterm::{ + event::{ + DisableMouseCapture, EnableMouseCapture, KeyboardEnhancementFlags, + PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags, + }, + terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, + ExecutableCommand, +}; +use ratatui::backend::CrosstermBackend; + +/// Ratatui terminal instance. Enters alternate screen when constructed, and exits once dropped. +#[derive(Resource)] +pub struct Terminal(pub ratatui::Terminal<CrosstermBackend<Stdout>>); + +impl Default for Terminal { + fn default() -> Self { + stdout().execute(EnterAlternateScreen).unwrap(); + stdout().execute(EnableMouseCapture).unwrap(); + stdout() + .execute(PushKeyboardEnhancementFlags( + KeyboardEnhancementFlags::REPORT_EVENT_TYPES, + )) + .unwrap(); + enable_raw_mode().unwrap(); + let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout())) + .expect("Failed to create terminal"); + terminal.clear().expect("Failed to clear terminal"); + Self(terminal) + } +} + +impl Drop for Terminal { + fn drop(&mut self) { + let mut stdout = stdout(); + let _ = stdout.execute(PopKeyboardEnhancementFlags); + let _ = stdout.execute(DisableMouseCapture); + let _ = stdout.execute(LeaveAlternateScreen); + let _ = disable_raw_mode(); + } +} |