From 36f01f65b1804125e7d18a17ce57205248e1b7fd Mon Sep 17 00:00:00 2001 From: lukeflo Date: Wed, 2 Oct 2024 21:45:26 +0200 Subject: reordered tui handling --- src/frontend/app.rs | 82 +++++++++++++++++++++++++++++++++++++++++++------ src/frontend/event.rs | 17 +++++----- src/frontend/handler.rs | 5 +-- src/frontend/tui.rs | 37 ++++++++++++++-------- 4 files changed, 107 insertions(+), 34 deletions(-) (limited to 'src/frontend') diff --git a/src/frontend/app.rs b/src/frontend/app.rs index 72d1020..0e1c9b9 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -15,15 +15,28 @@ // along with this program. If not, see . ///// +use std::io; + +use crate::backend::cliargs::{self, CLIArgs}; +use ratatui::{backend::CrosstermBackend, Terminal}; + use crate::backend::{bib::*, search::BibiSearch}; +use crate::{ + frontend::event::{Event, EventHandler}, + frontend::handler::handle_key_events, + frontend::tui::Tui, +}; use std::{error, net::SocketAddr}; use arboard::Clipboard; +use color_eyre::eyre::{Ok, Result}; use itertools::Itertools; use ratatui::widgets::{ListState, TableState}; +use super::tui; + // Application result type. -pub type AppResult = std::result::Result>; +// pub type AppResult = std::result::Result>; // Areas in which actions are possible #[derive(Debug)] @@ -47,6 +60,8 @@ pub enum FormerArea { pub struct App { // Is the application running? pub running: bool, + // // tui initialization + // pub tui: Tui, // main bibliography pub main_biblio: BibiMain, // bibliographic data @@ -184,17 +199,45 @@ impl EntryTableItem { } } -impl Default for App { - fn default() -> Self { +// impl Default for App { +// fn default() -> Self { +// let running = true; +// let main_biblio = BibiMain::new(); +// let biblio_data = BibiData::new(&main_biblio.bibliography, &main_biblio.citekeys); +// let tag_list = TagList::from_iter(main_biblio.keyword_list.clone()); +// let search_struct = BibiSearch::default(); +// let entry_table = EntryTable::from_iter(biblio_data.entry_list.bibentries.clone()); +// let current_area = CurrentArea::EntryArea; +// Self { +// running, +// main_biblio, +// biblio_data, +// tag_list, +// search_struct, +// entry_table, +// scroll_info: 0, +// current_area, +// former_area: None, +// // search_string: String::new(), +// } +// } +// } + +impl App { + // Constructs a new instance of [`App`]. + pub fn new() -> Result { + // Self::default() let running = true; + // let tui = Tui::new()?; let main_biblio = BibiMain::new(); let biblio_data = BibiData::new(&main_biblio.bibliography, &main_biblio.citekeys); let tag_list = TagList::from_iter(main_biblio.keyword_list.clone()); let search_struct = BibiSearch::default(); let entry_table = EntryTable::from_iter(biblio_data.entry_list.bibentries.clone()); let current_area = CurrentArea::EntryArea; - Self { + Ok(Self { running, + // tui, main_biblio, biblio_data, tag_list, @@ -204,14 +247,33 @@ impl Default for App { current_area, former_area: None, // search_string: String::new(), + }) + } + + pub async fn run(&mut self) -> Result<()> { + // Initialize the terminal user interface. + // let backend = CrosstermBackend::new(io::stdout()); + // let terminal = Terminal::new(backend)?; + // let events = EventHandler::new(250); + let mut tui = tui::Tui::new()?; + tui.init()?; + + // Start the main loop. + while self.running { + // Render the user interface. + tui.draw(self)?; + // Handle events. + match tui.events.next().await? { + Event::Tick => self.tick(), + Event::Key(key_event) => handle_key_events(key_event, self)?, + Event::Mouse(_) => {} + Event::Resize(_, _) => {} + } } - } -} -impl App { - // Constructs a new instance of [`App`]. - pub fn new() -> Self { - Self::default() + // Exit the user interface. + tui.exit()?; + Ok(()) } // Handles the tick event of the terminal. diff --git a/src/frontend/event.rs b/src/frontend/event.rs index 27dd059..65b61f1 100644 --- a/src/frontend/event.rs +++ b/src/frontend/event.rs @@ -17,12 +17,11 @@ use std::time::Duration; +use color_eyre::eyre::{OptionExt, Result}; use crossterm::event::{Event as CrosstermEvent, KeyEvent, MouseEvent}; use futures::{FutureExt, StreamExt}; use tokio::sync::mpsc; -use crate::frontend::app::AppResult; - /// Terminal events. #[derive(Clone, Copy, Debug)] pub enum Event { @@ -102,13 +101,11 @@ impl EventHandler { /// /// This function will always block the current thread if /// there is no data available and it's possible for more data to be sent. - pub async fn next(&mut self) -> AppResult { - self.receiver - .recv() - .await - .ok_or(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - "This is an IO error", - ))) + pub async fn next(&mut self) -> Result { + self.receiver.recv().await.ok_or_eyre("This is an IO error") + // .ok_or(Box::new(std::io::Error::new( + // std::io::ErrorKind::Other, + // "This is an IO error", + // ))) } } diff --git a/src/frontend/handler.rs b/src/frontend/handler.rs index ad899e2..11c2652 100644 --- a/src/frontend/handler.rs +++ b/src/frontend/handler.rs @@ -15,13 +15,14 @@ // along with this program. If not, see . ///// -use crate::frontend::app::{App, AppResult}; +use crate::frontend::app::App; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use super::app::{CurrentArea, FormerArea}; +use color_eyre::eyre::Result; /// Handles the key events and updates the state of [`App`]. -pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { +pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> Result<()> { // Keycodes activated for every area (high priority) match key_event.code { // Exit application on `ESC` or `q` diff --git a/src/frontend/tui.rs b/src/frontend/tui.rs index 477052f..add3e56 100644 --- a/src/frontend/tui.rs +++ b/src/frontend/tui.rs @@ -15,37 +15,50 @@ // along with this program. If not, see . ///// -use crate::frontend::app::{App, AppResult}; +use crate::frontend::app::App; use crate::frontend::event::EventHandler; +use color_eyre::eyre::Result; use crossterm::event::{DisableMouseCapture, EnableMouseCapture}; use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}; -use ratatui::backend::Backend; +use ratatui::backend::{Backend, CrosstermBackend}; +// use ratatui::backend::CrosstermBackend as Backend; use ratatui::Terminal; -use std::io; +use std::io::{self, stdout, Stdout}; use std::panic; +use std::{ + ops::{Deref, DerefMut}, + time::Duration, +}; +// pub type IO = std::io::{{crossterm_io | title_case}}; +// pub fn io() -> IO { +// std::io::{{crossterm_io}}() +// } /// Representation of a terminal user interface. /// /// It is responsible for setting up the terminal, /// initializing the interface and handling the draw events. #[derive(Debug)] -pub struct Tui { +pub struct Tui { /// Interface to the Terminal. - terminal: Terminal, + terminal: ratatui::Terminal>, /// Terminal event handler. pub events: EventHandler, } -impl Tui { +impl Tui { /// Constructs a new instance of [`Tui`]. - pub fn new(terminal: Terminal, events: EventHandler) -> Self { - Self { terminal, events } + pub fn new() -> Result { + let backend = CrosstermBackend::new(stdout()); + let terminal = Terminal::new(backend)?; + let events = EventHandler::new(250); + Ok(Self { terminal, events }) } /// Initializes the terminal interface. /// /// It enables the raw mode and sets terminal properties. - pub fn init(&mut self) -> AppResult<()> { + pub fn init(&mut self) -> Result<()> { terminal::enable_raw_mode()?; crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?; @@ -66,7 +79,7 @@ impl Tui { /// /// [`Draw`]: ratatui::Terminal::draw /// [`rendering`]: crate::ui::render - pub fn draw(&mut self, app: &mut App) -> AppResult<()> { + pub fn draw(&mut self, app: &mut App) -> Result<()> { // self.terminal.draw(|frame| ui::render(app, frame))?; self.terminal .draw(|frame| frame.render_widget(app, frame.area()))?; @@ -77,7 +90,7 @@ impl Tui { /// /// This function is also used for the panic hook to revert /// the terminal properties if unexpected errors occur. - fn reset() -> AppResult<()> { + fn reset() -> Result<()> { terminal::disable_raw_mode()?; crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?; Ok(()) @@ -86,7 +99,7 @@ impl Tui { /// Exits the terminal interface. /// /// It disables the raw mode and reverts back the terminal properties. - pub fn exit(&mut self) -> AppResult<()> { + pub fn exit(&mut self) -> Result<()> { Self::reset()?; self.terminal.show_cursor()?; Ok(()) -- cgit v1.2.3