From 081ecdba635d748026bfde0ba540415b013b6bb0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 10 Aug 2024 08:30:23 +0100 Subject: [PATCH] WIP: TUI actor --- crates/cli/src/tui/actor/handlers/mod.rs | 1 + crates/cli/src/tui/actor/messages.rs | 4 ++ crates/cli/src/tui/actor/mod.rs | 61 ++++++++++++++++++++++++ crates/cli/src/tui/mod.rs | 2 + 4 files changed, 68 insertions(+) create mode 100644 crates/cli/src/tui/actor/handlers/mod.rs create mode 100644 crates/cli/src/tui/actor/messages.rs create mode 100644 crates/cli/src/tui/actor/mod.rs create mode 100644 crates/cli/src/tui/mod.rs diff --git a/crates/cli/src/tui/actor/handlers/mod.rs b/crates/cli/src/tui/actor/handlers/mod.rs new file mode 100644 index 0000000..8337712 --- /dev/null +++ b/crates/cli/src/tui/actor/handlers/mod.rs @@ -0,0 +1 @@ +// diff --git a/crates/cli/src/tui/actor/messages.rs b/crates/cli/src/tui/actor/messages.rs new file mode 100644 index 0000000..8366f31 --- /dev/null +++ b/crates/cli/src/tui/actor/messages.rs @@ -0,0 +1,4 @@ +// +use git_next_core::message; + +message!(Start, "Start the TUI"); diff --git a/crates/cli/src/tui/actor/mod.rs b/crates/cli/src/tui/actor/mod.rs new file mode 100644 index 0000000..3466668 --- /dev/null +++ b/crates/cli/src/tui/actor/mod.rs @@ -0,0 +1,61 @@ +// +mod handlers; +pub mod messages; + +use std::io::{stderr, Stderr}; + +use actix::{Actor, Context}; + +use ratatui::{ + crossterm::{ + execute, + terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, + }, + prelude::CrosstermBackend, + Terminal, +}; + +pub struct Tui { + terminal: Option>>, +} + +impl Actor for Tui { + type Context = Context; + + fn started(&mut self, _ctx: &mut Self::Context) { + match init() { + Ok(terminal) => { + self.terminal.replace(terminal); + } + Err(err) => tracing::error!(?err, "Failed to start terminal UI"), + } + } + + fn stopped(&mut self, _ctx: &mut Self::Context) { + if let Err(err) = restore() { + match std::env::consts::OS { + "linux" | "macos" => { + tracing::error!( + ?err, + "Failed to restore terminal: Type `reset` to restore terminal" + ); + } + "windows" => { + tracing::error!(?err, "Failed to restore terminal: Reopen a new terminal"); + } + _ => tracing::error!(?err, "Failed to restore terminal"), + } + } + } +} + +fn init() -> std::io::Result>> { + execute!(stderr(), EnterAlternateScreen)?; + enable_raw_mode()?; + Terminal::new(CrosstermBackend::new(stderr())) +} + +fn restore() -> std::io::Result<()> { + execute!(stderr(), LeaveAlternateScreen)?; + disable_raw_mode() +} diff --git a/crates/cli/src/tui/mod.rs b/crates/cli/src/tui/mod.rs new file mode 100644 index 0000000..0c0cfe6 --- /dev/null +++ b/crates/cli/src/tui/mod.rs @@ -0,0 +1,2 @@ +// +mod actor;