Skip to content
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

Tab completion and syntax checking #261

Merged
merged 66 commits into from
Oct 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
585f8e6
clipboard as a feature for android and server support
kernelPanic0x Sep 5, 2024
73790ef
cargo fmt
kernelPanic0x Sep 5, 2024
cb72ef0
improved wormhole code display for serve and send
kernelPanic0x Sep 5, 2024
b1c7928
cargo fmt
kernelPanic0x Sep 6, 2024
39585e8
clipboard as default feature
kernelPanic0x Sep 10, 2024
4affcc9
improved clipboard conditional wormhole code message
kernelPanic0x Sep 10, 2024
63e92c7
added whitespace for readability
kernelPanic0x Sep 10, 2024
d99d120
Merge remote-tracking branch 'upstream/main' into magic-wormhole-main
kernelPanic0x Sep 11, 2024
06b6c50
Merge remote-tracking branch 'upstream/main'
kernelPanic0x Sep 16, 2024
049bca7
first working draft of code word spelling checks
kernelPanic0x Sep 19, 2024
39224e6
removed regex as it is not needed anymore
kernelPanic0x Sep 19, 2024
f148556
improved coloring
kernelPanic0x Sep 19, 2024
45f7b7f
first working tab completion
kernelPanic0x Sep 20, 2024
99bcd64
working tab complete
kernelPanic0x Sep 20, 2024
c2a294e
temporary ctrlc workaround
kernelPanic0x Sep 20, 2024
98f948c
improved channel code checks
kernelPanic0x Sep 21, 2024
cddb3fa
completer optimizations
kernelPanic0x Sep 21, 2024
bbdcdbc
working tab completion with jaro winkler algorhythm
kernelPanic0x Sep 22, 2024
87c4fda
added documentation for wordlist module, allow missing_docs for core …
kernelPanic0x Sep 22, 2024
4fba926
bail on error while reading wormhole code
kernelPanic0x Sep 22, 2024
6ff966d
clipboard as a feature for android and server support
kernelPanic0x Sep 5, 2024
f450a27
added whitespace for readability
kernelPanic0x Sep 10, 2024
8ba3e69
first working draft of code word spelling checks
kernelPanic0x Sep 19, 2024
7abbb50
removed regex as it is not needed anymore
kernelPanic0x Sep 19, 2024
ba6ce02
improved coloring
kernelPanic0x Sep 19, 2024
7806217
first working tab completion
kernelPanic0x Sep 20, 2024
c80520d
working tab complete
kernelPanic0x Sep 20, 2024
9b0d6c9
temporary ctrlc workaround
kernelPanic0x Sep 20, 2024
4abfb29
improved channel code checks
kernelPanic0x Sep 21, 2024
d269554
completer optimizations
kernelPanic0x Sep 21, 2024
9c3a26b
working tab completion with jaro winkler algorhythm
kernelPanic0x Sep 22, 2024
2e7f038
added documentation for wordlist module, allow missing_docs for core …
kernelPanic0x Sep 22, 2024
487733b
bail on error while reading wormhole code
kernelPanic0x Sep 22, 2024
1564f9e
Merge branch 'tab_complete' of github.com:kernelPanic0x/magic-wormhol…
kernelPanic0x Sep 23, 2024
6782f5f
fixed index out of range crash when suggestion > current word, added …
kernelPanic0x Sep 23, 2024
6a13436
removed temporary documentation, fixed clippy::never_loop
kernelPanic0x Sep 23, 2024
e83e96f
fixed typo
kernelPanic0x Sep 24, 2024
ec02d66
wormhole code validation length check
kernelPanic0x Sep 25, 2024
cbfbf80
wormhole code validation length check, use defaults for fuzzy search
kernelPanic0x Sep 25, 2024
0ab6560
use std::sync::LazyLock instead of lazy_static
kernelPanic0x Oct 4, 2024
71ec08a
use 0.8 confidence for menu system to work
kernelPanic0x Oct 4, 2024
a958cdd
fixed docs for get_n_top
kernelPanic0x Oct 4, 2024
0fe7823
made wordlist public without exposing core
kernelPanic0x Oct 4, 2024
55b57cd
span_end using min
kernelPanic0x Oct 4, 2024
4210dce
removed partial completions
kernelPanic0x Oct 4, 2024
c9b2846
fixed typo
kernelPanic0x Oct 5, 2024
1007c8c
removed unnecessary comment
kernelPanic0x Oct 5, 2024
af7b17c
Merge branch 'tab_complete' of github.com:kernelPanic0x/magic-wormhol…
kernelPanic0x Oct 5, 2024
bd19d09
removed code highlighting because of custom wordlists
kernelPanic0x Oct 7, 2024
0e349cd
rust version 1.81 for LazyLock support
kernelPanic0x Oct 7, 2024
5cfdd41
removed code highlighting because of custom wordlists
kernelPanic0x Oct 7, 2024
cbb3caa
rust version 1.81 for LazyLock support
kernelPanic0x Oct 7, 2024
6fb8f86
dialoguer tab completion implementation
kernelPanic0x Oct 8, 2024
e7fefce
added complter, removed reedline crate
kernelPanic0x Oct 8, 2024
f341998
added documentation
kernelPanic0x Oct 8, 2024
ceeeef3
documentation, cleanup
kernelPanic0x Oct 8, 2024
711875a
removed fuzzt from cli
kernelPanic0x Oct 8, 2024
585a198
Merge branch 'tab_complete' of github.com:kernelPanic0x/magic-wormhol…
kernelPanic0x Oct 8, 2024
ece999b
removed reedline
kernelPanic0x Oct 9, 2024
3ccb0f4
fuzzy-complete feature tests
kernelPanic0x Oct 9, 2024
afa5090
fixed conditional compile errors
kernelPanic0x Oct 10, 2024
99dc318
made helper functions private
kernelPanic0x Oct 11, 2024
86abf9d
merged with upstream
kernelPanic0x Oct 11, 2024
3dead09
fixed compilation error with --no-default-features
kernelPanic0x Oct 11, 2024
faff3cd
fixed compilation error with --no-default-features
kernelPanic0x Oct 11, 2024
ca5d292
fixed wrong function for normal_complete
kernelPanic0x Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added documentation
  • Loading branch information
kernelPanic0x committed Oct 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit f3419988112b64819acdd0adb9ecb072a8d14a74
1 change: 1 addition & 0 deletions cli/src/completer.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ impl CustomCompletion {

impl Completion for CustomCompletion {
fn get(&self, input: &str) -> Option<String> {
dbg!(WORDLIST.get_completions(input));
WORDLIST.get_completions(input).first().cloned()
}
}
1 change: 0 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ pub mod rendezvous;
mod server_messages;
#[cfg(test)]
mod test;
#[doc(hidden)]
pub mod wordlist;

use serde_derive::{Deserialize, Serialize};
43 changes: 26 additions & 17 deletions src/core/wordlist.rs
Original file line number Diff line number Diff line change
@@ -2,9 +2,13 @@ use rand::{rngs::OsRng, seq::SliceRandom};
use serde_json::{self, Value};
use std::fmt;

/// Represents a list of words used to generate and complete wormhole codes.
/// A wormhole code is a sequence of words used for secure communication or identification.
#[derive(PartialEq)]
pub struct Wordlist {
kernelPanic0x marked this conversation as resolved.
Show resolved Hide resolved
/// Number of words in a wormhole code
num_words: usize,
/// Odd and even wordlist
words: Vec<Vec<String>>,
}

@@ -20,6 +24,10 @@ impl Wordlist {
Wordlist { num_words, words }
}

/// Completes a wormhole code
///
/// Completion can be done either with fuzzy search (approximate string matching)
/// or simple `starts_with` matching.
pub fn get_completions(&self, prefix: &str) -> Vec<String> {
let count_dashes = prefix.matches('-').count();
let words = &self.words[count_dashes % self.words.len()];
@@ -51,17 +59,16 @@ impl Wordlist {
}

/// Get either even or odd wordlist
pub fn get_wordlist(&self, prefix: &str, cursor_pos: Option<usize>) -> &Vec<String> {
let limited_prefix = match cursor_pos {
Some(pos) if pos < prefix.len() => &prefix[..pos],
_ => prefix,
};
let count_dashes = limited_prefix.matches('-').count();
pub fn get_wordlist(&self, prefix: &str) -> &Vec<String> {
let count_dashes = prefix.matches('-').count();
&self.words[count_dashes % self.words.len()]
}

/// Fuzzy wormhole code completion
#[cfg(feature = "fuzzy-complete")]
fn fuzzy_complete(&self, partial: &str, words: &[String]) -> Vec<String> {
// We use Jaro-Winkler algorithm because it emphasizes the beginning of a word

use fuzzt::algorithms::JaroWinkler;

let words = words.iter().map(|w| w.as_str()).collect::<Vec<&str>>();
@@ -72,6 +79,7 @@ impl Wordlist {
.collect()
}

/// Choose wormhole code word
pub fn choose_words(&self) -> String {
let mut rng = OsRng;
let components: Vec<String> = self
@@ -85,17 +93,6 @@ impl Wordlist {
}
}

/// Extract partial str from prefix with cursor position
pub fn extract_partial_from_prefix<'a>(prefix: &'a str, pos: usize) -> &'a str {
let current_word_start = prefix[..pos].rfind('-').map(|i| i + 1).unwrap_or(0);
let current_word_end = prefix[pos..]
.find('-')
.map(|i| i + pos)
.unwrap_or_else(|| prefix.len());

&prefix[current_word_start..current_word_end]
}

fn load_pgpwords() -> Vec<Vec<String>> {
let raw_words_value: Value = serde_json::from_str(include_str!("pgpwords.json")).unwrap();
let raw_words = raw_words_value.as_object().unwrap();
@@ -124,6 +121,7 @@ fn load_pgpwords() -> Vec<Vec<String>> {
vec![even_words, odd_words]
}

/// Construct Wordlist struct with given number of words in a wormhole code
pub fn default_wordlist(num_words: usize) -> Wordlist {
Wordlist {
num_words,
@@ -211,10 +209,21 @@ mod test {
let list = default_wordlist(2);

assert_eq!(list.get_completions("22"), Vec::<String>::new());
assert_eq!(list.get_completions("22-"), Vec::<String>::new());

// Invalid wormhole code check
assert_eq!(list.get_completions("trj"), Vec::<String>::new());

assert_eq!(
list.get_completions("22-chisel"),
["22-chisel", "22-chairlift", "22-christmas"]
);

assert_eq!(
list.get_completions("22-chle"),
["22-chisel", "22-chatter", "22-checkup"]
);

assert_eq!(list.get_completions("22-chisel-tba"), ["22-chisel-tobacco"]);
}
}