Test rendering with fork
This commit is contained in:
parent
ee74676da4
commit
9c5f027898
2 changed files with 54 additions and 37 deletions
|
|
@ -3,48 +3,21 @@ mod never;
|
||||||
mod replies;
|
mod replies;
|
||||||
mod room;
|
mod room;
|
||||||
mod textline;
|
mod textline;
|
||||||
|
mod ui;
|
||||||
|
|
||||||
use std::io::{self, Stdout};
|
use std::io::{self, Stdout};
|
||||||
|
|
||||||
use crossterm::event::{DisableMouseCapture, EnableMouseCapture, Event, KeyCode};
|
use crossterm::event::{DisableMouseCapture, EnableMouseCapture, Event, KeyCode};
|
||||||
use crossterm::execute;
|
use crossterm::execute;
|
||||||
use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen};
|
use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen};
|
||||||
use textline::{TextLine, TextLineState};
|
use tui::backend::CrosstermBackend;
|
||||||
use tui::backend::{Backend, CrosstermBackend};
|
use tui::Terminal;
|
||||||
use tui::layout::{Constraint, Direction, Layout, Margin, Rect};
|
use ui::Ui;
|
||||||
use tui::widgets::{Block, Borders};
|
|
||||||
use tui::{Frame, Terminal};
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
async fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> anyhow::Result<()> {
|
||||||
struct Ui {
|
|
||||||
text: TextLineState,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ui {
|
|
||||||
fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
|
|
||||||
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<CrosstermBackend<Stdout>>) -> anyhow::Result<()> {
|
|
||||||
let mut ui = Ui::default();
|
let mut ui = Ui::default();
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|f| ui.draw(f))?;
|
ui.render_to_terminal(terminal).await?;
|
||||||
|
|
||||||
let event = crossterm::event::read()?;
|
let event = crossterm::event::read()?;
|
||||||
|
|
||||||
|
|
@ -53,13 +26,12 @@ fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> anyhow::Result<()>
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.text.process_input(event);
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
let mut terminal = Terminal::new(CrosstermBackend::new(io::stdout()))?;
|
let mut terminal = Terminal::new(CrosstermBackend::new(io::stdout()))?;
|
||||||
|
|
||||||
crossterm::terminal::enable_raw_mode()?;
|
crossterm::terminal::enable_raw_mode()?;
|
||||||
|
|
@ -70,7 +42,7 @@ fn main() -> anyhow::Result<()> {
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Defer error handling so the terminal always gets restored properly
|
// 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()?;
|
crossterm::terminal::disable_raw_mode()?;
|
||||||
execute!(
|
execute!(
|
||||||
|
|
|
||||||
45
cove-tui/src/ui.rs
Normal file
45
cove-tui/src/ui.rs
Normal file
|
|
@ -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<String, Arc<Mutex<Room>>>,
|
||||||
|
room: Option<Arc<Mutex<Room>>>,
|
||||||
|
overlay: Option<Overlay>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ui {
|
||||||
|
pub async fn render_to_terminal(
|
||||||
|
&mut self,
|
||||||
|
terminal: &mut Terminal<CrosstermBackend<Stdout>>,
|
||||||
|
) -> 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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue