首页 > 其他分享 >0067-Tui-panic 处理

0067-Tui-panic 处理

时间:2022-09-30 21:23:27浏览次数:75  
标签:hook tui terminal 0067 let Spans Tui panic

环境

  • Time 2022-08-17
  • Rust 1.63.0
  • Tui 0.18.0

前言

说明

参考:https://github.com/fdehau/tui-rs/blob/master/examples/panic.rs

目标

使用 tui-rs 定义一个 panic hook。

定义组件

#[derive(Default)]
struct App {
    hook_enabled: bool,
}

impl App {
    fn chain_hook(&mut self) {
        let original_hook = std::panic::take_hook();

        std::panic::set_hook(Box::new(move |panic| {
            reset_terminal().unwrap();
            original_hook(panic);
        }));

        self.hook_enabled = true;
    }
}

reset_terminal

fn reset_terminal() -> Result<()> {
    terminal::disable_raw_mode()?;
    std::io::stdout()
        .execute(terminal::Clear(terminal::ClearType::All))?
        .execute(terminal::LeaveAlternateScreen)?;
    Ok(())
}

ui

fn ui<B: Backend>(frame: &mut Frame<B>, app: &App) {
    let text = vec![
        if app.hook_enabled {
            Spans::from("HOOK IS CURRENTLY **ENABLED**")
        } else {
            Spans::from("HOOK IS CURRENTLY **DISABLED**")
        },
        Spans::from(""),
        Spans::from("press `p` to panic"),
        Spans::from("press `e` to enable the terminal-resetting panic hook"),
        Spans::from("press any other key to quit without panic"),
        Spans::from(""),
        Spans::from("when you panic without the chained hook,"),
        Spans::from("you will likely have to reset your terminal afterwards"),
        Spans::from("with the `reset` command"),
        Spans::from(""),
        Spans::from("with the chained panic hook enabled,"),
        Spans::from("you should see the panic report as you would without tui"),
        Spans::from(""),
        Spans::from("try first without the panic handler to see the difference"),
    ];

    let b = Block::default()
        .title("Panic Handler Demo")
        .borders(Borders::ALL);

    let p = Paragraph::new(text).block(b).alignment(Alignment::Center);

    frame.render_widget(p, frame.size());
}

总结

使用 tui-rs 创建 panic hook,即使在 panic 的时候,也可以重置终端。

附录

源码

use anyhow::Result;
use crossterm::{event, terminal, ExecutableCommand};
use tui::backend::{Backend, CrosstermBackend};
use tui::layout::Alignment;
use tui::text::Spans;
use tui::widgets::{Block, Borders, Paragraph};
use tui::{Frame, Terminal};

pub fn main() -> Result<()> {
    terminal::enable_raw_mode()?;
    let mut backend = CrosstermBackend::new(std::io::stdout());
    backend
        .execute(terminal::EnterAlternateScreen)?
        .execute(terminal::Clear(terminal::ClearType::All))?
        .hide_cursor()?;
    let mut terminal = tui::Terminal::new(backend)?;
    run(&mut terminal)?;
    reset_terminal()
}

fn reset_terminal() -> Result<()> {
    terminal::disable_raw_mode()?;
    std::io::stdout()
        .execute(terminal::Clear(terminal::ClearType::All))?
        .execute(terminal::LeaveAlternateScreen)?;
    Ok(())
}

#[derive(Default)]
struct App {
    hook_enabled: bool,
}

impl App {
    fn chain_hook(&mut self) {
        let original_hook = std::panic::take_hook();

        std::panic::set_hook(Box::new(move |panic| {
            reset_terminal().unwrap();
            original_hook(panic);
        }));

        self.hook_enabled = true;
    }
}

fn run<B: Backend>(terminal: &mut Terminal<B>) -> Result<()> {
    let timeout = std::time::Duration::from_millis(500);
    let mut app = App::default();
    loop {
        terminal.draw(|frame| ui(frame, &app))?;

        if event::poll(timeout)? {
            if let event::Event::Key(key) = event::read()? {
                use event::KeyCode::{Char, Esc};
                match key.code {
                    Char('q') | Char('Q') | Esc => return Ok(()),
                    Char('p') => panic!("intentional demo panic"),
                    Char('e') => app.chain_hook(),
                    _ => {}
                }
            }
        }
    }
}

fn ui<B: Backend>(frame: &mut Frame<B>, app: &App) {
    let text = vec![
        if app.hook_enabled {
            Spans::from("HOOK IS CURRENTLY **ENABLED**")
        } else {
            Spans::from("HOOK IS CURRENTLY **DISABLED**")
        },
        Spans::from(""),
        Spans::from("press `p` to panic"),
        Spans::from("press `e` to enable the terminal-resetting panic hook"),
        Spans::from("press any other key to quit without panic"),
        Spans::from(""),
        Spans::from("when you panic without the chained hook,"),
        Spans::from("you will likely have to reset your terminal afterwards"),
        Spans::from("with the `reset` command"),
        Spans::from(""),
        Spans::from("with the chained panic hook enabled,"),
        Spans::from("you should see the panic report as you would without tui"),
        Spans::from(""),
        Spans::from("try first without the panic handler to see the difference"),
    ];

    let b = Block::default()
        .title("Panic Handler Demo")
        .borders(Borders::ALL);

    let p = Paragraph::new(text).block(b).alignment(Alignment::Center);

    frame.render_widget(p, frame.size());
}

标签:hook,tui,terminal,0067,let,Spans,Tui,panic
From: https://www.cnblogs.com/jiangbo4444/p/16746276.html

相关文章

  • 0068-Tui-用户输入
    环境Time2022-08-18Rust1.63.0Tui0.18.0前言说明参考:https://github.com/fdehau/tui-rs/blob/master/examples/user_input.rs目标使用tui-rs来处理用户的输......
  • 0062-Tui-表格示例
    环境Time2022-08-16Rust1.63.0Tui0.18.0前言说明参考:https://github.com/fdehau/tui-rs/blob/master/examples/table.rs目标使用tui-rs显示表格。定义应用......
  • 0063-Tui-页签示例
    环境Time2022-08-16Rust1.63.0Tui0.18.0前言说明参考:https://github.com/fdehau/tui-rs/blob/master/examples/tabs.rs目标使用tui-rs显示页签。定义应用......
  • 0058-Tui-段落示例
    环境Time2022-08-12Rust1.63.0Tui0.18.0前言说明参考:https://github.com/fdehau/tui-rs/blob/master/examples/paragraph.rs目标使用tui-rs显示段落。定义......
  • 0055-Tui-条形图示例
    环境Time2022-08-09Rust1.62.0Tui0.18.0前言说明参考:https://github.com/fdehau/tui-rs/blob/master/examples/barchart.rs目标使用tui-rs显示条形图。第......
  • 0056-Tui-简单进度条
    环境Time2022-08-11Rust1.62.0Tui0.18.0前言说明参考:https://github.com/fdehau/tui-rs/blob/master/examples/gauge.rs目标使用tui-rs显示进度条。定义数......
  • 0057-Tui-进度条示例
    环境Time2022-08-11Rust1.62.0Tui0.18.0前言说明参考:https://github.com/fdehau/tui-rs/blob/master/examples/gauge.rs目标使用tui-rs显示进度条。render......
  • 0049-Tui-创建控制台界面
    环境Time2022-08-08Rust1.62.0Tui0.18.0前言说明参考:https://docs.rs/tui/latest/tui/index.html目标使用tui-rs和crossterm启动一个控制台的终端界面。......
  • C#使用CurrentUICulture切换语言
    C#使用CurrentUICulture切换语言-quanzhan-博客园(cnblogs.com)1、创建2个窗口   2、窗口1属性Localizable设置为True,Language选择英语(美国) 然后把窗口......
  • 我学到了为 AWS 编写 Python TUI 控制台的五件事
    我学到了为AWS编写PythonTUI控制台的五件事AWSCloudExplorerrunningonLinux通常,我喜欢使用Terraform和Ansible使用声明性语言编写代码以部署基础设施。......