aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 3d5b144d85cfdfeb89d00861555d86711e960942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::{io::stdout, fs::OpenOptions};

use bevy::{
    log::{
        tracing_subscriber::{self, Registry, prelude::*},
        LogPlugin, Level,
    },
    prelude::*, utils::tracing::level_filters::LevelFilter,
};
use crossterm::{
    event::{DisableMouseCapture, PopKeyboardEnhancementFlags},
    terminal::{disable_raw_mode, LeaveAlternateScreen},
    ExecutableCommand,
};
use grex_dither_post_process::DitherPostProcessPlugin;
use grex_framebuffer_extract::FramebufferExtractPlugin;

pub use crossterm;
pub use ratatui;

pub mod components;
pub mod events;
pub mod resources;
mod systems;

pub struct TerminalDisplayPlugin;

impl Plugin for TerminalDisplayPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins((
            DitherPostProcessPlugin,
            FramebufferExtractPlugin,
            LogPlugin {
                update_subscriber: Some(|_| {
                    let log_file = OpenOptions::new()
                        .write(true)
                        .create(true)
                        .open("debug.log")
                        .unwrap();
                    let file_layer = tracing_subscriber::fmt::Layer::new()
                        .with_writer(log_file)
                        .with_filter(LevelFilter::from_level(Level::INFO));
                    Box::new(Registry::default().with(file_layer))
                }),
                ..Default::default()
            },
        ))
        .add_systems(Startup, systems::setup)
        .add_systems(
            Update,
            (
                systems::input_handling,
                systems::resize_handling,
                systems::print_to_terminal,
                systems::widget_input_handling,
            ),
        )
        .insert_resource(resources::Terminal::default())
        .insert_resource(resources::EventQueue::default())
        .insert_resource(resources::TerminalInput::default())
        .insert_resource(resources::TerminalUI::default())
        .add_event::<events::TerminalInputEvent>();
    }
}

impl Drop for TerminalDisplayPlugin {
    fn drop(&mut self) {
        let mut stdout = stdout();
        let _ = stdout.execute(PopKeyboardEnhancementFlags);
        let _ = stdout.execute(DisableMouseCapture);
        let _ = stdout.execute(LeaveAlternateScreen);
        let _ = disable_raw_mode();
    }
}