aboutsummaryrefslogtreecommitdiff
path: root/src/resources.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-05-02 02:53:51 -0400
committerLibravatar Silas Bartha <[email protected]>2024-05-02 02:53:51 -0400
commit6834194b9e16a7b5ee68b539ceb1a99b8f1ff90b (patch)
treebac5d0ff699f1c3c4f58d47c2f127f5d3432a528 /src/resources.rs
parent646db8328611f21a5850cc9834b6c72bfdf0c829 (diff)
Added ratatui integration + logger redirectv0.1.1
Diffstat (limited to 'src/resources.rs')
-rw-r--r--src/resources.rs58
1 files changed, 55 insertions, 3 deletions
diff --git a/src/resources.rs b/src/resources.rs
index 343967d..e8fb946 100644
--- a/src/resources.rs
+++ b/src/resources.rs
@@ -1,7 +1,10 @@
-use std::sync::{Arc, Mutex};
+use std::{sync::{Arc, Mutex}, io::{stdout, Stdout}, fs::{File, OpenOptions}};
-use bevy::{prelude::*, utils::HashSet};
-use crossterm::event::{Event, KeyCode};
+use bevy::{prelude::*, utils::{HashSet, Uuid, HashMap, tracing::{subscriber, level_filters::LevelFilter}}, log::tracing_subscriber};
+use crossterm::{event::{Event, KeyCode, EnableMouseCapture}, terminal::{EnterAlternateScreen, enable_raw_mode}, ExecutableCommand};
+use ratatui::{backend::CrosstermBackend, Frame, layout::Rect};
+
+use crate::events::TerminalInputEvent;
#[derive(Resource, Default)]
pub struct TerminalInput {
@@ -36,3 +39,52 @@ impl TerminalInput {
#[derive(Resource, Default)]
pub(super) struct EventQueue(pub(super) Arc<Mutex<Vec<Event>>>);
+
+#[derive(Resource)]
+pub struct Terminal(pub ratatui::Terminal<CrosstermBackend<Stdout>>);
+
+impl Default for Terminal {
+ fn default() -> Self {
+ stdout().execute(EnterAlternateScreen).unwrap();
+ stdout().execute(EnableMouseCapture).unwrap();
+ enable_raw_mode().unwrap();
+ let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout())).expect("Failed to create terminal");
+ terminal.clear().expect("Failed to clear terminal");
+ Self(terminal)
+ }
+}
+
+#[derive(Resource, Default)]
+pub struct TerminalUI {
+ widgets: HashMap<Uuid, Box<dyn TerminalWidget + Sync + Send>>
+}
+
+impl TerminalUI {
+ pub fn insert_widget(&mut self, widget: Box<dyn TerminalWidget + Sync + Send>) -> Uuid {
+ let id = Uuid::new_v4();
+ self.widgets.insert(id, widget);
+ id
+ }
+
+ pub fn get_widget(&mut self, id: Uuid) -> Option<&mut Box<dyn TerminalWidget + Sync + Send>> {
+ self.widgets.get_mut(&id)
+ }
+
+ pub fn destroy_widget(&mut self, id: Uuid) {
+ self.widgets.remove(&id);
+ }
+
+ pub fn widgets(&mut self) -> Vec<&mut Box<dyn TerminalWidget + Sync + Send>> {
+ let mut vec = self.widgets.values_mut().collect::<Vec<_>>();
+ vec.sort_by(|a, b| { a.depth().cmp(&b.depth()).reverse() });
+ vec
+ }
+}
+
+pub trait TerminalWidget {
+ fn init(&mut self) {}
+ fn update(&mut self) {}
+ fn render(&mut self, frame: &mut Frame, rect: Rect);
+ fn handle_events(&mut self, _event: &TerminalInputEvent) {}
+ fn depth(&self) -> u32 { 0 }
+}