WIP: TUI actor
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-08-10 08:30:23 +01:00
parent ac069551d8
commit ddc04d2d0b
4 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1 @@
//

View file

@ -0,0 +1,4 @@
//
use git_next_core::message;
message!(Start, "Start the TUI");

View file

@ -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<Terminal<CrosstermBackend<Stderr>>>,
}
impl Actor for Tui {
type Context = Context<Self>;
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<Terminal<CrosstermBackend<Stderr>>> {
execute!(stderr(), EnterAlternateScreen)?;
enable_raw_mode()?;
Terminal::new(CrosstermBackend::new(stderr()))
}
fn restore() -> std::io::Result<()> {
execute!(stderr(), LeaveAlternateScreen)?;
disable_raw_mode()
}

View file

@ -0,0 +1,2 @@
//
mod actor;