Skip to content

Commit

Permalink
fix(hok): trim yes_no prompt input
Browse files Browse the repository at this point in the history
Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
  • Loading branch information
chawyehsu committed Aug 16, 2023
1 parent 658ef7d commit 0a01f1e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 51 deletions.
18 changes: 2 additions & 16 deletions src/cmd/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,8 @@ pub fn cmd_install(matches: &ArgMatches, session: &Session) -> Result<()> {

let mut stdout = std::io::stdout();
let _ = stdout.execute(cursor::Show);
loop {
print!("\nDo you want to continue? [y/N]: ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
//
if input.chars().count() == 3 {
let ch: char = input.chars().next().unwrap();
if ['y', 'Y', 'n', 'N'].contains(&ch) {
let ret = ch == 'y' || ch == 'Y';
let _ = tx.send(Event::PromptTransactionNeedConfirmResult(ret));
break;
}
}
}

let answer = cui::prompt_yes_no();
let _ = tx.send(Event::PromptTransactionNeedConfirmResult(answer));
let _ = stdout.execute(cursor::Hide);
}
Event::PackageSyncDone => break,
Expand Down
20 changes: 3 additions & 17 deletions src/cmd/uninstall.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use clap::ArgMatches;
use crossterm::style::Stylize;
use libscoop::{operation, Event, Session, SyncOption};
use std::io::Write;

use crate::Result;
use crate::{cui, Result};

pub fn cmd_uninstall(matches: &ArgMatches, session: &Session) -> Result<()> {
let queries = matches
Expand Down Expand Up @@ -57,21 +56,8 @@ pub fn cmd_uninstall(matches: &ArgMatches, session: &Session) -> Result<()> {
println!(" {}", output);
}

loop {
print!("\nDo you want to continue? [y/N]: ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
//
if input.chars().count() == 3 {
let ch: char = input.chars().next().unwrap();
if ['y', 'Y', 'n', 'N'].contains(&ch) {
let ret = ch == 'y' || ch == 'Y';
let _ = tx.send(Event::PromptTransactionNeedConfirmResult(ret));
break;
}
}
}
let answer = cui::prompt_yes_no();
let _ = tx.send(Event::PromptTransactionNeedConfirmResult(answer));
}
Event::PackageCommitStart(ctx) => {
println!("Uninstalling {}...", ctx);
Expand Down
19 changes: 2 additions & 17 deletions src/cmd/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crossterm::{
ExecutableCommand,
};
use libscoop::{operation, Event, Session, SyncOption};
use std::io::Write;

use crate::{cui, util, Result};

Expand Down Expand Up @@ -159,22 +158,8 @@ pub fn cmd_upgrade(matches: &ArgMatches, session: &Session) -> Result<()> {

let mut stdout = std::io::stdout();
let _ = stdout.execute(cursor::Show);
loop {
print!("\nDo you want to continue? [y/N]: ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
//
if input.chars().count() == 3 {
let ch: char = input.chars().next().unwrap();
if ['y', 'Y', 'n', 'N'].contains(&ch) {
let ret = ch == 'y' || ch == 'Y';
let _ = tx.send(Event::PromptTransactionNeedConfirmResult(ret));
break;
}
}
}

let answer = cui::prompt_yes_no();
let _ = tx.send(Event::PromptTransactionNeedConfirmResult(answer));
let _ = stdout.execute(cursor::Hide);
}
Event::PackageSyncDone => break,
Expand Down
22 changes: 21 additions & 1 deletion src/cui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crossterm::{
ExecutableCommand,
};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::{collections::HashMap, io::stdout};
use std::{
collections::HashMap,
io::{stdout, Write},
};

static BAR_FMT: &str = " {wide_msg} {total_bytes:>12} [{bar:>20}] {percent:>3}%";

Expand Down Expand Up @@ -147,3 +150,20 @@ impl BucketUpdateUI {
.unwrap();
}
}

/// Prompt user to continue or not.
pub fn prompt_yes_no() -> bool {
loop {
print!("\nDo you want to continue? [y/N]: ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let c = input.trim_end();
if c.chars().count() == 1 {
let ch: char = c.chars().next().unwrap();
if ['y', 'Y', 'n', 'N'].contains(&ch) {
return ch == 'y' || ch == 'Y';
}
}
}
}

0 comments on commit 0a01f1e

Please sign in to comment.