diff options
Diffstat (limited to 'src/gui/mod.rs')
-rw-r--r-- | src/gui/mod.rs | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 358e4b5..1053de1 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -2,31 +2,44 @@ pub mod message; pub mod question; pub mod util; -use crate::{db, space_repetition, util::event::Events, util::time}; +use crate::{db, space_repetition, util::time}; use anyhow::Result; use rusqlite::Connection; -use std::io; -use termion::{raw::IntoRawMode, raw::RawTerminal, screen::AlternateScreen}; -use tui::{backend::TermionBackend, Terminal}; +use std::io::Stdout; +use tui::{backend::CrosstermBackend, Terminal}; +use crossterm::{terminal}; -pub type Term = Terminal<TermionBackend<AlternateScreen<RawTerminal<io::Stdout>>>>; +pub type Term = Terminal<CrosstermBackend<Stdout>>; -pub fn terminal() -> Result<Term> { - let stdout = io::stdout().into_raw_mode()?; - let stdout = AlternateScreen::from(stdout); - let backend = TermionBackend::new(stdout); +pub fn setup_terminal() -> Result<Term> { + terminal::enable_raw_mode()?; + let mut stdout = std::io::stdout(); + crossterm::execute!(stdout, terminal::EnterAlternateScreen)?; + let backend = CrosstermBackend::new(stdout); Ok(Terminal::new(backend)?) } -pub fn start(conn: &Connection, term: &mut Term, events: &Events, deck_name: &str) -> Result<()> { +pub fn restore_terminal(term: &mut Term) -> Result<()> { + terminal::disable_raw_mode()?; + crossterm::execute!(term.backend_mut(), terminal::LeaveAlternateScreen)?; + term.show_cursor()?; + Ok(()) +} + +pub fn start( + conn: &Connection, + term: &mut Term, + deck_name: &str, + hide_progress: bool, +) -> Result<()> { let mut answers = 0; loop { let now = time::seconds_since_unix_epoch()?; - let title = title(deck_name, answers, db::count_available(conn).unwrap_or(0)); + let title = title(deck_name, answers, db::count_available(conn).unwrap_or(0), hide_progress); match db::pick_random_ready(conn) { - Some(card) => match question::ask(term, events, &title, &card)? { + Some(card) => match question::ask(term, &title, &card)? { question::Response::Aborted => break, question::Response::Answered { difficulty } => { answers += 1; @@ -45,7 +58,7 @@ pub fn start(conn: &Connection, term: &mut Term, events: &Events, deck_name: &st } None => "Aucune carte n’est disponible. Votre deck est-il vide ?".to_string(), }; - let _ = message::show(term, events, &title, &message, true); + let _ = message::show(term, &title, &message, true); break; } } @@ -54,8 +67,8 @@ pub fn start(conn: &Connection, term: &mut Term, events: &Events, deck_name: &st Ok(()) } -fn title(deck_name: &str, answers: i32, available_cards: i32) -> String { - if answers == 0 && available_cards == 0 { +fn title(deck_name: &str, answers: i32, available_cards: i32, hide_progress: bool) -> String { + if answers == 0 && available_cards == 0 || hide_progress { deck_name.to_string() } else if available_cards == 0 { let from = answers; |