Skip to content

Commit

Permalink
extract terminal helpers into function
Browse files Browse the repository at this point in the history
  • Loading branch information
tofubert committed Dec 17, 2024
1 parent a75ec3d commit 5c61104
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 126 deletions.
137 changes: 11 additions & 126 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,150 +2,35 @@ use crate::{
backend::{nc_room::NCRoomInterface, nc_talk::NCBackend},
config::Config,
ui::{
chat_box::ChatBox, chat_selector::ChatSelector, help_box::HelpBox, input_box::InputBox,
title_bar::TitleBar, users::Users,
chat_box::ChatBox,
chat_selector::ChatSelector,
help_box::HelpBox,
input_box::InputBox,
terminal_helpers::{init, install_hooks, restore},
title_bar::TitleBar,
users::Users,
},
};
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Position},
style::{Style, Stylize},
widgets::Paragraph,
Frame,
Frame, Terminal,
};
use ratatui::{prelude::CrosstermBackend, Terminal};
use strum_macros::Display;

use tui_textarea::Input;

use cfg_if::cfg_if;
use color_eyre::{
config::{EyreHook, HookBuilder, PanicHook},
eyre,
};
use crossterm::{
event::{
poll, read, DisableBracketedPaste, DisableMouseCapture, EnableBracketedPaste,
EnableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers,
KeyboardEnhancementFlags, MouseEventKind, PopKeyboardEnhancementFlags,
PushKeyboardEnhancementFlags,
},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
use crossterm::event::{
poll, read, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEventKind,
};
use tracing::error;
use tui_textarea::Key;

enum ProcessEventResult {
Continue,
Exit,
}

pub fn install_hooks(config: &Config) -> eyre::Result<()> {
let (panic_hook, eyre_hook) = HookBuilder::default()
.panic_section(format!(
"This is a bug. Consider reporting it at {}",
env!("CARGO_PKG_REPOSITORY")
))
.capture_span_trace_by_default(false)
.display_location_section(false)
.display_env_section(false)
.into_hooks();

cfg_if! {
if #[cfg(debug_assertions)] {
install_better_panic();
} else {
human_panic::setup_panic!();
}
}
install_color_eyre_panic_hook(panic_hook, config);
install_eyre_hook(eyre_hook, config)?;

Ok(())
}

#[allow(dead_code)]
fn install_better_panic() {
better_panic::Settings::auto()
.most_recent_first(false)
.verbosity(better_panic::Verbosity::Full)
.install();
}

fn install_color_eyre_panic_hook(panic_hook: PanicHook, config: &Config) {
// convert from a `color_eyre::config::PanicHook`` to a `Box<dyn
// Fn(&PanicInfo<'_>`
let panic_hook = panic_hook.into_panic_hook();
let get_enable_mouse = config.get_enable_mouse();
let get_enable_paste = config.get_enable_paste();
std::panic::set_hook(Box::new(move |panic_info| {
if let Err(err) = restore(get_enable_mouse, get_enable_paste) {
error!("Unable to restore terminal: {err:?}");
}

// TODO not sure about this
// let msg = format!("{}", panic_hook.panic_report(panic_info));
// error!("Error: {}", strip_ansi_escapes::strip_str(msg));
panic_hook(panic_info);
}));
}

fn install_eyre_hook(eyre_hook: EyreHook, config: &Config) -> color_eyre::Result<()> {
let eyre_hook = eyre_hook.into_eyre_hook();
let get_enable_mouse = config.get_enable_mouse();
let get_enable_paste = config.get_enable_paste();
eyre::set_hook(Box::new(move |error| {
restore(get_enable_mouse, get_enable_paste).unwrap();
eyre_hook(error)
}))?;
Ok(())
}

fn init(
get_enable_mouse: bool,
get_enable_paste: bool,
) -> eyre::Result<Terminal<CrosstermBackend<std::io::Stdout>>> {
use std::io::stdout;

enable_raw_mode()?;
execute!(stdout(), EnterAlternateScreen)?;
if execute!(
stdout(),
PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES)
)
.is_err()
{
log::warn!("Consider using a Terminal that supports KeyboardEnhancementFlags.");
}
if get_enable_mouse {
execute!(stdout(), EnableMouseCapture)?;
}
if get_enable_paste {
execute!(stdout(), EnableBracketedPaste)?;
}
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?;
terminal.hide_cursor()?;
Ok(terminal)
}

fn restore(get_enable_mouse: bool, get_enable_paste: bool) -> eyre::Result<()> {
use std::io::stdout;

if get_enable_paste {
execute!(stdout(), DisableBracketedPaste)?;
}
if get_enable_mouse {
execute!(stdout(), DisableMouseCapture)?;
}

//proceed here regardless of error, since this will fail if the terminal doesn't support this.
let _ = execute!(stdout(), PopKeyboardEnhancementFlags);
execute!(stdout(), LeaveAlternateScreen)?;
disable_raw_mode()?;

Ok(())
}

#[derive(PartialEq, Clone, Copy, Display)]
pub enum CurrentScreen {
Reading,
Expand Down
1 change: 1 addition & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod chat_box;
pub mod chat_selector;
pub mod help_box;
pub mod input_box;
pub mod terminal_helpers;
pub mod title_bar;
pub mod users;

0 comments on commit 5c61104

Please sign in to comment.