Skip to content

Commit

Permalink
feat: simplify initial terminal interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpsoane committed Dec 1, 2024
1 parent 51f2edd commit 36fea05
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ async fn main() -> color_eyre::Result<()> {
let docker = new_local_docker_connection(&config.docker_path)
.await
.context(format!("failed to create docker connection, potentially due to misconfiguration (see {CONFIGURATION_DOC_PATH})"))?;

terminal::init_panic_hook();

let mut terminal = terminal::init().context("failed to initialise terminal")?;
let mut terminal = ratatui::init();
terminal.clear()?;

Check warning on line 53 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L52-L53

Added lines #L52 - L53 were not covered by tests

let mut events = EventLoop::new();
let events_tx = events.get_tx();
Expand Down Expand Up @@ -83,7 +83,8 @@ async fn main() -> color_eyre::Result<()> {
}
Message::Transition(t) => {
if t == events::Transition::ToNewTerminal {
terminal = terminal::init().context("failed to initialise terminal")?;
terminal = ratatui::init();
terminal.clear()?;

Check warning on line 87 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L86-L87

Added lines #L86 - L87 were not covered by tests
} else {
let _ = &app.transition(t).await;
}
Expand All @@ -99,7 +100,7 @@ async fn main() -> color_eyre::Result<()> {
}
}

terminal::restore().context("failed to restore terminal")?;
ratatui::restore();

Check warning on line 103 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L103

Added line #L103 was not covered by tests

Ok(())
}
25 changes: 2 additions & 23 deletions src/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::prelude::*;
use std::{
io::stdout,
panic::{set_hook, take_hook},
};

pub fn init() -> std::io::Result<Terminal<impl Backend>> {
enable_raw_mode()?;
stdout().execute(EnterAlternateScreen)?;
let terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
Ok(terminal)
}

pub fn restore() -> std::io::Result<()> {
stdout().execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
Ok(())
}
use std::panic::{set_hook, take_hook};

pub fn init_panic_hook() {
let original_hook = take_hook();
set_hook(Box::new(move |panic_info| {
// intentionally ignore errors here since we're already in a panic
let _ = restore();
let _ = ratatui::restore();

Check warning on line 7 in src/terminal.rs

View workflow job for this annotation

GitHub Actions / check / stable / clippy

this let-binding has unit value

warning: this let-binding has unit value --> src/terminal.rs:7:9 | 7 | let _ = ratatui::restore(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `ratatui::restore();` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value = note: `#[warn(clippy::let_unit_value)]` on by default

Check warning on line 7 in src/terminal.rs

View workflow job for this annotation

GitHub Actions / check / beta / clippy

this let-binding has unit value

warning: this let-binding has unit value --> src/terminal.rs:7:9 | 7 | let _ = ratatui::restore(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `ratatui::restore();` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value = note: `#[warn(clippy::let_unit_value)]` on by default

Check warning on line 7 in src/terminal.rs

View check run for this annotation

Codecov / codecov/patch

src/terminal.rs#L7

Added line #L7 was not covered by tests
original_hook(panic_info);
}));
}

0 comments on commit 36fea05

Please sign in to comment.