aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 11ee50cdad2038365fbb206b88edcb4d8c796f1f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#![warn(missing_docs)]

//! Bevy plugin which allows a camera to render to a terminal window.

use std::{
    fs::OpenOptions,
    path::PathBuf,
    sync::{Arc, Mutex},
};

use bevy::{
    log::{
        tracing_subscriber::{self, prelude::*, Registry},
        Level, LogPlugin,
    },
    prelude::*,
    utils::tracing::level_filters::LevelFilter,
};
use bevy_dither_post_process::DitherPostProcessPlugin;
use bevy_framebuffer_extract::FramebufferExtractPlugin;

pub use crossterm;
use once_cell::sync::Lazy;
pub use ratatui;

/// Functions and types related to capture and display of world to terminal
pub mod display;

/// Functions and types related to capturing and processing user keyboard input
pub mod input;

/// Functions and types related to constructing and rendering TUI widgets
pub mod widgets;

static LOG_PATH: Lazy<Arc<Mutex<PathBuf>>> = Lazy::new(|| Arc::new(Mutex::new(PathBuf::default())));

/// Plugin providing terminal display functionality
pub struct TerminalDisplayPlugin {
    /// Path to redirect tracing logs to. Defaults to "debug.log"
    pub log_path: PathBuf,
}

impl Default for TerminalDisplayPlugin {
    fn default() -> Self {
        Self {
            log_path: "debug.log".into(),
        }
    }
}

impl Plugin for TerminalDisplayPlugin {
    fn build(&self, app: &mut App) {
        *LOG_PATH
            .lock()
            .expect("Failed to get lock on log path mutex") = self.log_path.clone();
        app.add_plugins((
            DitherPostProcessPlugin,
            FramebufferExtractPlugin,
            LogPlugin {
                update_subscriber: Some(|_| {
                    let log_file = OpenOptions::new()
                        .write(true)
                        .create(true)
                        .truncate(true)
                        .open(
                            LOG_PATH
                                .lock()
                                .expect("Failed to get lock on log path mutex")
                                .clone(),
                        )
                        .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, input::systems::setup_input)
        .add_systems(
            Update,
            (
                input::systems::input_handling,
                display::systems::resize_handling,
                display::systems::print_to_terminal,
                widgets::systems::widget_input_handling,
            ),
        )
        .insert_resource(display::resources::Terminal::default())
        .insert_resource(input::resources::EventQueue::default())
        .insert_resource(input::resources::TerminalInput::default())
        .add_event::<input::events::TerminalInputEvent>();
    }
}