From ae8aafed5bad8edd594556bce079b1545aea9bec Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 21 Nov 2022 14:47:36 +0100 Subject: Show errors in TUI instead of nothing Once the terminal was setup, errors were not shown in case the application stopped because of an error. So, show errors explicitely in TUI to be able to debug easily the deck for example. --- src/deck.rs | 2 +- src/gui/mod.rs | 2 +- src/gui/question.rs | 6 +++++- src/main.rs | 22 ++++++++++++++++++++-- 4 files changed, 27 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/deck.rs b/src/deck.rs index 82566bd..d23529f 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -13,7 +13,7 @@ struct ParseError { impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{} (parsing line {})", self.message, self.line) + write!(f, "Line {}: {}", self.line, self.message) } } diff --git a/src/gui/mod.rs b/src/gui/mod.rs index b39cbcf..719f39a 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -11,7 +11,7 @@ use std::{fs, io, time::Duration}; use termion::{raw::IntoRawMode, raw::RawTerminal, screen::AlternateScreen}; use tui::{backend::TermionBackend, Terminal}; -type Term = Terminal>>>; +pub type Term = Terminal>>>; pub fn terminal() -> Result { let stdout = io::stdout().into_raw_mode()?; diff --git a/src/gui/question.rs b/src/gui/question.rs index 2060cb7..426daa9 100644 --- a/src/gui/question.rs +++ b/src/gui/question.rs @@ -203,7 +203,11 @@ pub fn ask( fn is_correct(input: &str, responses: &[String]) -> bool { // Remove whitespaces - let input = input.split_whitespace().map(|word| word.trim()).collect::>().join(" "); + let input = input + .split_whitespace() + .map(|word| word.trim()) + .collect::>() + .join(" "); responses .iter() diff --git a/src/main.rs b/src/main.rs index bed2ce1..c2373f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod util; use crate::util::event::Events; use anyhow::Result; +use rusqlite::Connection; use std::path::PathBuf; use structopt::StructOpt; @@ -23,8 +24,25 @@ fn main() -> Result<()> { let deck_name = deck::pp_from_path(&deck_path).unwrap_or_else(|| "Deck".to_string()); let mut term = gui::terminal()?; let events = Events::new(); - gui::synchronize(&conn, &mut term, &events, &deck_path, &deck_name)?; - gui::start(&conn, &mut term, &events, &deck_name) + match run_tui(conn, &deck_path, &deck_name, &mut term, &events) { + Ok(()) => Ok(()), + Err(msg) => { + // Show errors in TUI, otherwise they are hidden + gui::message::show(&mut term, &events, &deck_name, &format!("{}", msg), true)?; + Err(msg) + } + } +} + +fn run_tui( + conn: Connection, + deck_path: &str, + deck_name: &str, + term: &mut gui::Term, + events: &Events, +) -> Result<()> { + gui::synchronize(&conn, term, &events, &deck_path, &deck_name)?; + gui::start(&conn, term, &events, &deck_name) } fn db_path(deck_path: &str) -> String { -- cgit v1.2.3