From 4ba0459bb6bf2efec6c9cece2ddf158e239e0977 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sat, 18 Mar 2023 10:20:37 -0400 Subject: [PATCH] fix prompts sometimes not showing up (again) (#187) - fix tui::pause only accept enter to continue so it's less confusing - replace unwrap with flush - replace some prints with eprints - make tui::prompt flush stdout and stderr fixes #178 --- src/cli.rs | 2 ++ src/demos.rs | 9 +++++++++ src/main.rs | 11 +++++++---- src/tui.rs | 10 +++++++--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 71bc355..bddca32 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -103,6 +103,8 @@ impl FromStr for Verbosity { pub(crate) struct ArgsDebug { #[clap(long, help = "Show a text prompt.")] pub demo_prompt: bool, + #[clap(long, help = "Show a \"press any key\" prompt.")] + pub demo_pause: bool, #[clap(long, help = "Show a character prompt.")] pub demo_prompt_char: bool, #[clap(long, help = "Show an example confirmation menu using dummy data.")] diff --git a/src/demos.rs b/src/demos.rs index 83d5aff..80f4d85 100644 --- a/src/demos.rs +++ b/src/demos.rs @@ -8,6 +8,15 @@ pub fn demo_prompt() { println!("Result: {}", result); } +pub fn demo_pause() { + let mut x = 0; + loop { + tui::pause(); + x += 1; + println!("looped {} times", x); + } +} + pub fn demo_prompt_char() { println!("Showing prompt"); let result = tui::prompt_char("Continue?", "yn"); diff --git a/src/main.rs b/src/main.rs index 6ec435f..cf2c118 100644 --- a/src/main.rs +++ b/src/main.rs @@ -222,9 +222,9 @@ fn get_selected_accounts( fn do_login(account: &mut SteamGuardAccount) -> anyhow::Result<()> { if account.account_name.len() > 0 { - println!("Username: {}", account.account_name); + info!("Username: {}", account.account_name); } else { - print!("Username: "); + eprint!("Username: "); account.account_name = tui::prompt(); } let _ = std::io::stdout().flush(); @@ -339,6 +339,9 @@ fn do_subcmd_debug(args: cli::ArgsDebug) -> anyhow::Result<()> { if args.demo_prompt { demos::demo_prompt(); } + if args.demo_pause { + demos::demo_pause(); + } if args.demo_prompt_char { demos::demo_prompt_char(); } @@ -358,8 +361,8 @@ fn do_subcmd_setup( _args: cli::ArgsSetup, manifest: &mut accountmanager::Manifest, ) -> anyhow::Result<()> { - println!("Log in to the account that you want to link to steamguard-cli"); - print!("Username: "); + eprintln!("Log in to the account that you want to link to steamguard-cli"); + eprint!("Username: "); let username = tui::prompt().to_lowercase(); let account_name = username.clone(); if manifest.account_exists(&username) { diff --git a/src/tui.rs b/src/tui.rs index b78b5b3..a0047ee 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -41,7 +41,8 @@ fn test_validate_captcha_text() { /// Prompt the user for text input. pub(crate) fn prompt() -> String { - stderr().flush().unwrap(); + stdout().flush().expect("failed to flush stdout"); + stderr().flush().expect("failed to flush stderr"); let mut line = String::new(); while let Event::Key(KeyEvent { code, .. }) = crossterm::event::read().unwrap() { @@ -273,11 +274,14 @@ pub(crate) fn prompt_confirmation_menu( } pub(crate) fn pause() { - eprintln!("Press any key to continue..."); + let _ = write!(stderr(), "Press enter to continue..."); let _ = stderr().flush(); loop { match crossterm::event::read().expect("could not read terminal events") { - Event::Key(_) => break, + Event::Key(KeyEvent { + code: KeyCode::Enter, + .. + }) => break, _ => continue, } }