-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/completion #202
Closed
Closed
Feat/completion #202
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4176a05
first try with dialoguer crate
a-moreira 83e0285
feat: add code completion
a-moreira b737838
cargo fmt
a-moreira 528dac8
re-use some code
a-moreira 7dc9c43
cargo clippy
a-moreira 7ee645d
Merge branch 'master' into feat/completion
a-moreira 103bb24
fix CI
a-moreira 7f1b8a7
fix wordlist.rs tests
a-moreira 2665131
split wordlist functionalities between crate and cli
a-moreira 15a8be5
fmt
a-moreira 987c9f4
remove unecessary deps from crate
a-moreira ab35282
fix clippy
a-moreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,14 @@ use clap::{Args, CommandFactory, Parser, Subcommand}; | |
use cli_clipboard::{ClipboardContext, ClipboardProvider}; | ||
use color_eyre::{eyre, eyre::Context}; | ||
use console::{style, Term}; | ||
use dialoguer::Input; | ||
use futures::{future::Either, Future, FutureExt}; | ||
use indicatif::{MultiProgress, ProgressBar}; | ||
use std::{io::Write, path::PathBuf}; | ||
use magic_wormhole::PgpWordList; | ||
use std::{ | ||
io::Write, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use magic_wormhole::{ | ||
dilated_transfer, forwarding, transfer, transit, MailboxConnection, Wormhole, | ||
|
@@ -570,7 +575,7 @@ async fn main() -> eyre::Result<()> { | |
out, | ||
); | ||
|
||
std::io::stdout().write_all(&out.as_bytes())?; | ||
std::io::stdout().write_all(out.as_bytes())?; | ||
}, | ||
shell => { | ||
let mut out = std::io::stdout(); | ||
|
@@ -761,12 +766,14 @@ fn create_progress_handler(pb: ProgressBar) -> impl FnMut(u64, u64) { | |
} | ||
|
||
fn enter_code() -> eyre::Result<String> { | ||
use dialoguer::Input; | ||
|
||
Input::new() | ||
let completion = WordList::default(); | ||
let input = Input::new() | ||
.with_prompt("Enter code") | ||
.completion_with(&completion) | ||
.interact_text() | ||
.map_err(From::from) | ||
.map_err(From::from); | ||
|
||
input | ||
} | ||
|
||
fn print_welcome(term: &mut Term, welcome: &Option<String>) -> eyre::Result<()> { | ||
|
@@ -1059,15 +1066,14 @@ async fn receive_inner_v1( | |
.truncate(true) | ||
.open(&file_path) | ||
.await?; | ||
Ok(req | ||
.accept( | ||
&transit::log_transit_connection, | ||
&mut file, | ||
create_progress_handler(pb), | ||
ctrl_c(), | ||
) | ||
.await | ||
.context("Receive process failed")?) | ||
req.accept( | ||
&transit::log_transit_connection, | ||
&mut file, | ||
create_progress_handler(pb), | ||
ctrl_c(), | ||
) | ||
.await | ||
.context("Receive process failed") | ||
} | ||
|
||
async fn receive_inner_v2( | ||
|
@@ -1172,6 +1178,74 @@ async fn receive_inner_v2( | |
Ok(()) | ||
} | ||
|
||
use dialoguer::Completion; | ||
use magic_wormhole::core::wordlist; | ||
use std::{collections::HashMap, fs}; | ||
|
||
struct WordList(PgpWordList); | ||
|
||
impl Default for WordList { | ||
fn default() -> Self { | ||
let json = fs::read_to_string("./src/core/pgpwords.json").unwrap(); | ||
let word_map: HashMap<String, Vec<String>> = serde_json::from_str(&json).unwrap(); | ||
let mut even_words: Vec<String> = vec![]; | ||
let mut odd_words: Vec<String> = vec![]; | ||
for (_idx, words) in word_map { | ||
even_words.push(words[0].to_lowercase()); | ||
odd_words.push(words[1].to_lowercase()); | ||
} | ||
let words = vec![even_words, odd_words]; | ||
|
||
WordList { | ||
0: PgpWordList { | ||
words: words.clone(), | ||
num_words: words.len(), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl Completion for WordList { | ||
fn get(&self, input: &str) -> Option<String> { | ||
let count_dashes = input.matches('-').count(); | ||
let mut completions = Vec::new(); | ||
let words = &self.0.words[count_dashes % self.0.words.len()]; | ||
|
||
let last_partial_word = input.split('-').last(); | ||
let lp = if let Some(w) = last_partial_word { | ||
w.len() | ||
} else { | ||
0 | ||
}; | ||
|
||
for word in words { | ||
let mut suffix: String = input.to_owned(); | ||
if word.starts_with(last_partial_word.unwrap()) { | ||
if lp == 0 { | ||
suffix.push_str(word); | ||
} else { | ||
let p = input.len() - lp; | ||
suffix.truncate(p); | ||
suffix.push_str(word); | ||
} | ||
|
||
if count_dashes + 1 < self.0.num_words { | ||
suffix.push('-'); | ||
} | ||
|
||
completions.push(suffix); | ||
} | ||
} | ||
if completions.len() == 1 { | ||
Some(completions.first().unwrap().clone()) | ||
} else { | ||
// TODO: show vector of suggestions somehow | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd consider that out of scope for now. |
||
// println!("Suggestions: {:#?}", &completions); | ||
None | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
@@ -1189,4 +1263,21 @@ mod test { | |
String::from_utf8(out).unwrap(); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_passphrase_completion() { | ||
let words: Vec<Vec<String>> = vec![ | ||
wordlist::vecstrings("purple green yellow"), | ||
wordlist::vecstrings("sausages seltzer snobol"), | ||
]; | ||
|
||
let w = WordList(PgpWordList { | ||
words, | ||
num_words: 2, | ||
}); | ||
assert_eq!(w.get(""), None); | ||
assert_eq!(w.get("pur").unwrap(), "purple-"); | ||
assert_eq!(w.get("blu"), None); | ||
assert_eq!(w.get("purple-sa").unwrap(), "purple-sausages"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a re-implementation of
load_pgpwords
. Why not make use of that?