From 01ffac500ab19d54548f0f69bf40c59f0ecb02b1 Mon Sep 17 00:00:00 2001 From: Sam Tay Date: Thu, 18 Jun 2020 20:30:07 -0700 Subject: Run cursive outside of tokio runtime --- TODO.md | 2 ++ src/main.rs | 50 ++++++++++++++++++++++++++++++++------------------ src/utils.rs | 4 +--- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/TODO.md b/TODO.md index ba1946b..f3724e3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,7 @@ # TODO +[ ] Fix resizing. Something in `wait_for_char` screws up the future cursive app... + ### v0.3.0 1. Duckduck go search ftw, e.g. ``` diff --git a/src/main.rs b/src/main.rs index c900765..f753cd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ mod term; mod tui; mod utils; +use crate::stackexchange::Question; +use crate::tui::markdown::Markdown; use crossterm::style::Color; use error::{Error, Result}; use lazy_static::lazy_static; @@ -13,27 +15,39 @@ use minimad::mad_inline; use stackexchange::{LocalStorage, StackExchange}; use term::mk_print_error; use termimad::{CompoundStyle, MadSkin}; +use tokio::runtime::Runtime; use tokio::task; -#[tokio::main] -async fn main() -> Result<(), Error> { +fn main() -> Result<()> { + // Markdown styles (outside of TUI) let mut skin = MadSkin::default(); - // TODO style configuration skin.inline_code = CompoundStyle::with_fg(Color::Cyan); skin.code_block.set_fgbg(Color::Cyan, termimad::gray(20)); let mut print_error = mk_print_error(&skin); - run(&mut skin).await.or_else(|e: Error| { - print_error(&e.to_string())?; - match e { - Error::EmptySites => { - print_notice!(skin, "This can likely be fixed by `so --update-sites`.") + + // Tokio runtime + let mut rt = Runtime::new()?; + rt.block_on(run(&mut skin)) + .and_then(|qs| { + // Run TUI + qs.map(tui::run); + Ok(()) + }) + .or_else(|e: Error| { + // Handle errors + print_error(&e.to_string())?; + match e { + Error::EmptySites => { + print_notice!(skin, "This can likely be fixed by `so --update-sites`.") + } + _ => Ok(()), } - _ => Ok(()), - } - }) + }) } -async fn run(skin: &mut MadSkin) -> Result<(), Error> { +/// Runs the CLI and, if the user wishes to enter the TUI, returns +/// question/answer data +async fn run(skin: &mut MadSkin) -> Result>>> { let opts = cli::get_opts()?; let config = opts.config; let sites = &config.sites; @@ -59,7 +73,7 @@ async fn run(skin: &mut MadSkin) -> Result<(), Error> { } md.push_str("|-\n"); termimad::print_text(&md); - return Ok(()); + return Ok(None); } if let Some(site) = ls.find_invalid_site(sites).await? { @@ -74,7 +88,7 @@ async fn run(skin: &mut MadSkin) -> Result<(), Error> { to update the cached site listing. You can also run `so --list-sites` \ to list all available sites.", )?; - return Ok(()); + return Ok(None); } if let Some(q) = opts.query { @@ -87,12 +101,12 @@ async fn run(skin: &mut MadSkin) -> Result<(), Error> { // Kick off the rest of the search in the background let qs = task::spawn(async move { se.search().await }); if !utils::wait_for_char(' ')? { - return Ok(()); + return Ok(None); } - tui::run(qs.await.unwrap()?)?; + return Ok(Some(qs.await.unwrap()?)); } else { - tui::run(se.search().await?)?; + return Ok(Some(se.search().await?)); } } - Ok(()) + Ok(None) } diff --git a/src/utils.rs b/src/utils.rs index 58319ea..560f186 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -37,9 +37,7 @@ pub fn wait_for_char(c: char) -> Result { pressed = true; break; } - Event::Key(_) => { - break; - } + Event::Key(_) => break, _ => (), } } -- cgit v1.2.3