diff --git a/cove-tui/src/main.rs b/cove-tui/src/main.rs index 0b3d8a1..beba029 100644 --- a/cove-tui/src/main.rs +++ b/cove-tui/src/main.rs @@ -3,48 +3,21 @@ mod never; mod replies; mod room; mod textline; +mod ui; use std::io::{self, Stdout}; use crossterm::event::{DisableMouseCapture, EnableMouseCapture, Event, KeyCode}; use crossterm::execute; use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen}; -use textline::{TextLine, TextLineState}; -use tui::backend::{Backend, CrosstermBackend}; -use tui::layout::{Constraint, Direction, Layout, Margin, Rect}; -use tui::widgets::{Block, Borders}; -use tui::{Frame, Terminal}; +use tui::backend::CrosstermBackend; +use tui::Terminal; +use ui::Ui; -#[derive(Debug, Default)] -struct Ui { - text: TextLineState, -} - -impl Ui { - fn draw(&mut self, f: &mut Frame) { - let outer = Rect { - x: 0, - y: 0, - width: 50 + 2, - height: 1 + 2, - }; - let inner = Rect { - x: 1, - y: 1, - width: 50, - height: 1, - }; - - f.render_widget(Block::default().borders(Borders::ALL), outer); - f.render_stateful_widget(TextLine, inner, &mut self.text); - self.text.set_cursor(f, inner); - } -} - -fn run(terminal: &mut Terminal>) -> anyhow::Result<()> { +async fn run(terminal: &mut Terminal>) -> anyhow::Result<()> { let mut ui = Ui::default(); loop { - terminal.draw(|f| ui.draw(f))?; + ui.render_to_terminal(terminal).await?; let event = crossterm::event::read()?; @@ -53,13 +26,12 @@ fn run(terminal: &mut Terminal>) -> anyhow::Result<()> break; } } - - ui.text.process_input(event); } Ok(()) } -fn main() -> anyhow::Result<()> { +#[tokio::main] +async fn main() -> anyhow::Result<()> { let mut terminal = Terminal::new(CrosstermBackend::new(io::stdout()))?; crossterm::terminal::enable_raw_mode()?; @@ -70,7 +42,7 @@ fn main() -> anyhow::Result<()> { )?; // Defer error handling so the terminal always gets restored properly - let result = run(&mut terminal); + let result = run(&mut terminal).await; crossterm::terminal::disable_raw_mode()?; execute!( diff --git a/cove-tui/src/ui.rs b/cove-tui/src/ui.rs new file mode 100644 index 0000000..b331afd --- /dev/null +++ b/cove-tui/src/ui.rs @@ -0,0 +1,45 @@ +use std::collections::HashMap; +use std::io::Stdout; +use std::sync::Arc; + +use tokio::sync::Mutex; +use tui::backend::CrosstermBackend; +use tui::widgets::Paragraph; +use tui::Terminal; + +use crate::room::Room; + +pub enum Overlay { + Error(String), + ChooseRoom(String), +} + +#[derive(Default)] +pub struct Ui { + rooms: HashMap>>, + room: Option>>, + overlay: Option, +} + +impl Ui { + pub async fn render_to_terminal( + &mut self, + terminal: &mut Terminal>, + ) -> anyhow::Result<()> { + terminal.autoresize()?; + + let mut frame = terminal.get_frame(); + frame.render_widget(Paragraph::new("Hello world!"), frame.size()); + + // Do a little dance to please the borrow checker + let cursor = frame.cursor(); + drop(frame); + terminal.set_cursor_opt(cursor)?; + + terminal.flush()?; + terminal.flush_backend()?; + terminal.swap_buffers(); + + Ok(()) + } +}