diff options
author | Silas Bartha <[email protected]> | 2024-05-02 02:53:51 -0400 |
---|---|---|
committer | Silas Bartha <[email protected]> | 2024-05-02 02:53:51 -0400 |
commit | 6834194b9e16a7b5ee68b539ceb1a99b8f1ff90b (patch) | |
tree | bac5d0ff699f1c3c4f58d47c2f127f5d3432a528 /src/systems.rs | |
parent | 646db8328611f21a5850cc9834b6c72bfdf0c829 (diff) |
Added ratatui integration + logger redirectv0.1.1
Diffstat (limited to 'src/systems.rs')
-rw-r--r-- | src/systems.rs | 63 |
1 files changed, 39 insertions, 24 deletions
diff --git a/src/systems.rs b/src/systems.rs index 77b8d11..e1a9bef 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -1,25 +1,20 @@ -use std::{ - io::{stdout, Write}, - usize, -}; - use bevy::{ prelude::*, render::render_resource::{Extent3d, TextureFormat}, }; -use crossterm::{ - cursor::{self, MoveTo}, - event::{read, Event, KeyEventKind, KeyboardEnhancementFlags, PushKeyboardEnhancementFlags}, - terminal::enable_raw_mode, - ExecutableCommand, QueueableCommand, -}; +use crossterm::event::{read, Event, KeyEventKind}; use grex_framebuffer_extract::{ components::FramebufferExtractDestination, render_assets::FramebufferExtractSource, }; use crate::{ events::TerminalInputEvent, - resources::{EventQueue, TerminalInput}, + resources::{EventQueue, Terminal, TerminalInput, TerminalUI}, +}; + +use ratatui::{ + prelude::*, + widgets::{Paragraph, Wrap}, }; const BRAILLE_CODE_MIN: u16 = 0x2800; @@ -41,16 +36,13 @@ pub fn setup(event_queue: Res<EventQueue>) { } } }); - - let mut stdout = stdout(); - enable_raw_mode().expect("Failed to put terminal into raw mode"); - let _ = stdout.execute(PushKeyboardEnhancementFlags( - KeyboardEnhancementFlags::REPORT_EVENT_TYPES, - )); - let _ = stdout.execute(cursor::Hide); } -pub fn print_to_terminal(image_exports: Query<&FramebufferExtractDestination>) { +pub fn print_to_terminal( + mut terminal: ResMut<Terminal>, + mut terminal_ui: ResMut<TerminalUI>, + image_exports: Query<&FramebufferExtractDestination>, +) { for image_export in image_exports.iter() { let mut image = image_export .0 @@ -93,10 +85,22 @@ pub fn print_to_terminal(image_exports: Query<&FramebufferExtractDestination>) { } let string = output_buffer.into_iter().collect::<String>(); - let mut stdout = stdout(); - stdout.queue(MoveTo(0, 0)).unwrap(); - stdout.write_all(string.as_bytes()).unwrap(); - stdout.flush().unwrap(); + terminal + .0 + .draw(|frame| { + let area = frame.size(); + frame.render_widget( + Paragraph::new(string) + .white() + .bold() + .wrap(Wrap { trim: true }), + area, + ); + for widget in terminal_ui.widgets().iter_mut() { + widget.render(frame, area); + } + }) + .expect("Failed to draw terminal frame"); } } @@ -112,6 +116,17 @@ fn braille_char(mask: u8) -> char { } } +pub fn widget_input_handling( + mut terminal_ui: ResMut<TerminalUI>, + mut event_reader: EventReader<TerminalInputEvent>, +) { + for event in event_reader.read() { + for widget in terminal_ui.widgets().iter_mut() { + widget.handle_events(event); + } + } +} + pub fn input_handling( event_queue: Res<EventQueue>, mut input: ResMut<TerminalInput>, |