diff options
author | Joris | 2022-11-21 14:47:36 +0100 |
---|---|---|
committer | Joris | 2022-11-21 14:47:36 +0100 |
commit | ae8aafed5bad8edd594556bce079b1545aea9bec (patch) | |
tree | 88cff38eff55c9da1fc44d99dae2e72bd5a8d2eb /src | |
parent | 3fbdc20859a7f795ab9396ead363ab7a9581405d (diff) |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/deck.rs | 2 | ||||
-rw-r--r-- | src/gui/mod.rs | 2 | ||||
-rw-r--r-- | src/gui/question.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 22 |
4 files changed, 27 insertions, 5 deletions
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<TermionBackend<AlternateScreen<RawTerminal<io::Stdout>>>>; +pub type Term = Terminal<TermionBackend<AlternateScreen<RawTerminal<io::Stdout>>>>; pub fn terminal() -> Result<Term> { 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<B: Backend>( fn is_correct(input: &str, responses: &[String]) -> bool { // Remove whitespaces - let input = input.split_whitespace().map(|word| word.trim()).collect::<Vec<&str>>().join(" "); + let input = input + .split_whitespace() + .map(|word| word.trim()) + .collect::<Vec<&str>>() + .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 { |