Skip to content

Commit

Permalink
[CLI] Use Fuzzy selct instead of select.
Browse files Browse the repository at this point in the history
  • Loading branch information
AureliaDolo committed Jan 17, 2025
1 parent acd5060 commit 782aaa8
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 51 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ libparsec_platform_ipc = { workspace = true }

anyhow = { workspace = true }
clap = { workspace = true, features = ["default", "derive", "env"] }
dialoguer = { workspace = true }
dialoguer = { workspace = true, features = ["fuzzy-select"] }
env_logger = { workspace = true, features = ["auto-color", "humantime", "regex"] }
log = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
Expand Down
6 changes: 3 additions & 3 deletions cli/src/commands/invite/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use libparsec_client::{
};

use crate::utils::*;
use dialoguer::{Input, Select};
use dialoguer::{FuzzySelect, Input};

crate::clap_parser_with_shared_opts_builder!(
#[with = config_dir, data_dir, password_stdin]
Expand Down Expand Up @@ -105,7 +105,7 @@ pub async fn main(args: Args) -> anyhow::Result<()> {
let ctx = step5_shamir(device_ctx).await?;
match ctx {
ShamirRecoveryClaimMaybeFinalizeCtx::Offline(ctx) => {
let retry = Select::new()
let retry = FuzzySelect::new()
.default(0)
.with_prompt("Unable to join server, do you want to retry ?")
.items(&["yes", "no"])
Expand Down Expand Up @@ -170,7 +170,7 @@ fn shamir_pick_recipient(
.fold(0_u8, |acc, (_, s)| acc + u8::from(*s))
);
}
let selection = Select::new()
let selection = FuzzySelect::new()
.default(0)
.with_prompt("Choose a person to contact now")
.items(&human_recipients)
Expand Down
6 changes: 3 additions & 3 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{fmt::Display, ops::Deref, path::Path, sync::Arc};

use anyhow::anyhow;
use dialoguer::Select;
use dialoguer::FuzzySelect;
use libparsec::{
internal::{Client, EventBus},
list_available_devices, AuthenticatedCmds, AvailableDevice, DeviceAccessStrategy,
Expand Down Expand Up @@ -404,7 +404,7 @@ pub fn choose_sas_code(
expected: &SASCode,
provided_by: &str,
) -> anyhow::Result<()> {
let selected_sas = Select::new()
let selected_sas = FuzzySelect::new()
.default(0)
.items(sas_codes)
.with_prompt(format!("Select code provided by {provided_by}"))
Expand All @@ -421,7 +421,7 @@ pub fn choose_user_profile() -> anyhow::Result<UserProfile> {
UserProfile::Admin,
UserProfile::Outsider,
];
let selected_profile = Select::new()
let selected_profile = FuzzySelect::new()
.default(0)
.items(&profiles)
.with_prompt("Which profile?")
Expand Down
9 changes: 4 additions & 5 deletions cli/tests/integration/invitations/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rexpect::spawn;

use crate::{
integration_tests::bootstrap_cli_test,
match_sas_code,
testenv_utils::{TestOrganization, DEFAULT_DEVICE_PASSWORD},
};

Expand Down Expand Up @@ -107,7 +106,7 @@ async fn invite_device_dance(tmp_path: TmpPath) {
let mut locked = claimer_cloned.lock().unwrap();

locked
.exp_string("Select code provided by greeter:")
.exp_string("Select code provided by greeter")
.unwrap();
});
greeter.await.unwrap();
Expand All @@ -126,7 +125,7 @@ async fn invite_device_dance(tmp_path: TmpPath) {
{
let mut locked = cloned_claimer.lock().unwrap();

match_sas_code!(locked, sas_code);
locked.send_line(&sas_code).unwrap();
}

// retrieve claimer code
Expand All @@ -135,7 +134,7 @@ async fn invite_device_dance(tmp_path: TmpPath) {
let mut locked = greeter_cloned.lock().unwrap();
locked.exp_string("Waiting for claimer").unwrap();
locked
.exp_string("Select code provided by claimer:")
.exp_string("Select code provided by claimer")
.unwrap();
});

Expand All @@ -153,7 +152,7 @@ async fn invite_device_dance(tmp_path: TmpPath) {
{
let mut locked = greeter_cloned.lock().unwrap();

match_sas_code!(locked, sas_code);
locked.send_line(&sas_code).unwrap();
locked
.exp_string(&format!("Select code provided by claimer: {sas_code}"))
.unwrap();
Expand Down
16 changes: 6 additions & 10 deletions cli/tests/integration/invitations/shared_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rexpect::{session::PtySession, spawn};

use crate::{
integration_tests::{bootstrap_cli_test, shared_recovery_create},
match_sas_code,
testenv_utils::{TestOrganization, DEFAULT_DEVICE_PASSWORD},
};

Expand Down Expand Up @@ -95,14 +94,11 @@ async fn invite_shared_recovery_dance(tmp_path: TmpPath) {

let claimer = tokio::task::spawn(async move {
let mut locked = claimer_cloned.lock().unwrap();
locked
.exp_string("Choose a person to contact now:")
.unwrap();
// down to choose bob
locked.send_line("j").unwrap();
locked.exp_string("Choose a person to contact now").unwrap();
locked.send_line("bob").unwrap();

locked
.exp_string("Select code provided by greeter:")
.exp_string("Select code provided by greeter")
.unwrap();
});
greeter.await.unwrap();
Expand All @@ -121,7 +117,7 @@ async fn invite_shared_recovery_dance(tmp_path: TmpPath) {
{
let mut locked = cloned_claimer.lock().unwrap();

match_sas_code!(locked, sas_code);
locked.send_line(&sas_code).unwrap();
}

// retrieve claimer code
Expand All @@ -130,7 +126,7 @@ async fn invite_shared_recovery_dance(tmp_path: TmpPath) {
let mut locked = greeter_cloned.lock().unwrap();
locked.exp_string("Waiting for claimer").unwrap();
locked
.exp_string("Select code provided by claimer:")
.exp_string("Select code provided by claimer")
.unwrap();
});

Expand All @@ -146,7 +142,7 @@ async fn invite_shared_recovery_dance(tmp_path: TmpPath) {
{
let mut locked = p_greeter.lock().unwrap();

match_sas_code!(locked, sas_code);
locked.send_line(&sas_code).unwrap();
}
let mut greeter = Arc::<Mutex<PtySession>>::try_unwrap(p_greeter)
.ok()
Expand Down
11 changes: 5 additions & 6 deletions cli/tests/integration/invitations/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rexpect::spawn;

use crate::{
integration_tests::bootstrap_cli_test,
match_sas_code,
testenv_utils::{TestOrganization, DEFAULT_DEVICE_PASSWORD},
utils::YELLOW,
};
Expand Down Expand Up @@ -120,7 +119,7 @@ async fn invite_user_dance(tmp_path: TmpPath) {
let mut locked = claimer_cloned.lock().unwrap();

locked
.exp_string("Select code provided by greeter:")
.exp_string("Select code provided by greeter")
.unwrap();
});
greeter.await.unwrap();
Expand All @@ -139,7 +138,7 @@ async fn invite_user_dance(tmp_path: TmpPath) {
{
let mut locked = cloned_claimer.lock().unwrap();

match_sas_code!(locked, sas_code);
locked.send_line(&sas_code).unwrap();
}

// retrieve claimer code
Expand All @@ -148,7 +147,7 @@ async fn invite_user_dance(tmp_path: TmpPath) {
let mut locked = greeter_cloned.lock().unwrap();
locked.exp_string("Waiting for claimer").unwrap();
locked
.exp_string("Select code provided by claimer:")
.exp_string("Select code provided by claimer")
.unwrap();
});

Expand All @@ -166,7 +165,7 @@ async fn invite_user_dance(tmp_path: TmpPath) {
{
let mut locked = greeter_cloned.lock().unwrap();

match_sas_code!(locked, sas_code);
locked.send_line(&sas_code).unwrap();
locked
.exp_string(&format!("Select code provided by claimer: {sas_code}"))
.unwrap();
Expand All @@ -191,7 +190,7 @@ async fn invite_user_dance(tmp_path: TmpPath) {
.exp_string("New user: [alice2 <[email protected]>]")
.unwrap();
locked.exp_string("Which profile?").unwrap();
locked.send_line("\n").unwrap(); // selection on standard by default
locked.send_line("standard").unwrap();

locked
.exp_string("Creating the user in the server")
Expand Down
23 changes: 0 additions & 23 deletions cli/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,6 @@ macro_rules! assert_cmd_failure {
}
}

#[macro_export]
macro_rules! match_sas_code {
($locked:ident, $sas_code:ident) => {
$locked.read_line().unwrap(); //empty line
let first = dbg!($locked.read_line().unwrap());
let second = dbg!($locked.read_line().unwrap());
let third = dbg!($locked.read_line().unwrap());

if $sas_code == first[first.len() - 4..] {
dbg!("first");
$locked.send_line("").unwrap();
} else if $sas_code == second[second.len() - 4..] {
dbg!("second");
$locked.send_line("j").unwrap();
} else if $sas_code == third[third.len() - 4..] {
dbg!("third");
$locked.send_line("jj").unwrap();
} else {
panic!("no corresponding sas code available")
}
};
}

/// For Alice, with Bob and Toto as recipients
fn shared_recovery_create(
alice: &Arc<LocalDevice>,
Expand Down

0 comments on commit 782aaa8

Please sign in to comment.