diff options
| author | lukeflo | 2024-10-02 21:45:26 +0200 |
|---|---|---|
| committer | lukeflo | 2024-10-02 21:45:26 +0200 |
| commit | 36f01f65b1804125e7d18a17ce57205248e1b7fd (patch) | |
| tree | c46e63486a3c96448db091174efdcf343693dd9b /src | |
| parent | c78ae222ea75d36f4aef6e274d2c986e37462276 (diff) | |
| download | bibiman-36f01f65b1804125e7d18a17ce57205248e1b7fd.tar.gz bibiman-36f01f65b1804125e7d18a17ce57205248e1b7fd.zip | |
reordered tui handling
Diffstat (limited to 'src')
| -rw-r--r-- | src/frontend/app.rs | 82 | ||||
| -rw-r--r-- | src/frontend/event.rs | 17 | ||||
| -rw-r--r-- | src/frontend/handler.rs | 5 | ||||
| -rw-r--r-- | src/frontend/tui.rs | 37 | ||||
| -rw-r--r-- | src/main.rs | 31 |
5 files changed, 113 insertions, 59 deletions
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 <https://www.gnu.org/licenses/>. ///// +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<T> = std::result::Result<T, Box<dyn error::Error>>; +// pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>; // 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> { + // 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<Event> { - 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<Event> { + 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 <https://www.gnu.org/licenses/>. ///// -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 <https://www.gnu.org/licenses/>. ///// -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<B: Backend> { +pub struct Tui { /// Interface to the Terminal. - terminal: Terminal<B>, + terminal: ratatui::Terminal<CrosstermBackend<Stdout>>, /// Terminal event handler. pub events: EventHandler, } -impl<B: Backend> Tui<B> { +impl Tui { /// Constructs a new instance of [`Tui`]. - pub fn new(terminal: Terminal<B>, events: EventHandler) -> Self { - Self { terminal, events } + pub fn new() -> Result<Self> { + 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<B: Backend> Tui<B> { /// /// [`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<B: Backend> Tui<B> { /// /// 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<B: Backend> Tui<B> { /// 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(()) diff --git a/src/main.rs b/src/main.rs index c7007ba..9fc9d17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,17 +21,19 @@ use backend::cliargs::{self, CLIArgs}; use ratatui::{backend::CrosstermBackend, Terminal}; use crate::{ - frontend::app::{App, AppResult}, + frontend::app::App, frontend::event::{Event, EventHandler}, frontend::handler::handle_key_events, frontend::tui::Tui, }; +use color_eyre::eyre::Result; + pub mod backend; pub mod frontend; #[tokio::main] -async fn main() -> AppResult<()> { +async fn main() -> Result<()> { // Parse CLI arguments let parsed_args = CLIArgs::parse_cli_args(); @@ -48,29 +50,8 @@ async fn main() -> AppResult<()> { } // Create an application. - let mut app = App::new(); - - // 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::new(terminal, events); - tui.init()?; - - // Start the main loop. - while app.running { - // Render the user interface. - tui.draw(&mut app)?; - // Handle events. - match tui.events.next().await? { - Event::Tick => app.tick(), - Event::Key(key_event) => handle_key_events(key_event, &mut app)?, - Event::Mouse(_) => {} - Event::Resize(_, _) => {} - } - } + let mut app = App::new()?; - // Exit the user interface. - tui.exit()?; + app.run().await?; Ok(()) } |
