Skip to content

Commit

Permalink
fuzzy-find when searching
Browse files Browse the repository at this point in the history
  • Loading branch information
xvxx committed Sep 30, 2020
1 parent bb399ef commit e68dc59
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.8-dev

- Search is now a fuzzy-find instead of a literal match! Thanks [fuzzy-matcher](https://crates.io/crates/fuzzy-matcher).

## 0.1.7

- Fix for dependencies getting out of sync.
Expand Down
25 changes: 25 additions & 0 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ exclude = [
"img/*"
]

# Change v1.4.1 -> v1.4.2 in README on `cargo release`
# Change v1.4.2-dev -> v1.4.2 in CHANGELOG on `cargo release`
[package.metadata.release]
#pre-release-replacements = [
#{file="README.md", search="shy-v\\d+\\.\\d+\\.\\d+-", replace="{{crate_name}}-v{{version}}-"},
#{file="README.md", search="/v\\d+\\.\\d+\\.\\d+/", replace="/v{{version}}/"},
#]
pre-release-replacements = [
{file="CHANGELOG.md", search="\\d+\\.\\d+\\.\\d+-dev", replace="{{version}}"},
]
dev-version-ext = "dev"

[dependencies]
termion = "=1.5.5"
flume = { version = "=0.7.1", default-features = false, features = ['select'] }
signal-hook = "=0.1.14"
indexmap = "=1.3.2"
fuzzy-matcher = "=0.3.5"
13 changes: 8 additions & 5 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
ssh_config::{load_ssh_config, HostMap},
};
use flume::{unbounded, Receiver, Selector};
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use std::{
io::{self, Stdout, Write},
thread,
Expand All @@ -27,6 +28,7 @@ pub struct TUI {
size: (u16, u16),
hosts: HostMap,
stdout: RawTerminal<Stdout>,
matcher: SkimMatcherV2,
}

/// UI mode
Expand Down Expand Up @@ -58,6 +60,7 @@ impl TUI {
size: terminal_size()?,
hosts: load_ssh_config(config_path)?,
stdout: Self::setup_terminal()?,
matcher: Default::default(),
})
}

Expand Down Expand Up @@ -243,7 +246,7 @@ impl TUI {
while i > 0 {
i -= 1;
if let Some(host) = hosts.get(i) {
if Self::host_matches(host, &self.input) {
if self.host_matches(host, &self.input) {
self.select(i);
return;
}
Expand Down Expand Up @@ -275,7 +278,7 @@ impl TUI {
while i < hosts.len() {
i += 1;
if let Some(host) = hosts.get(i) {
if Self::host_matches(host, &self.input) {
if self.host_matches(host, &self.input) {
self.select(i);
return;
}
Expand All @@ -302,7 +305,7 @@ impl TUI {
/// select a match.
fn select_search_host(&mut self) {
for (i, (host, _)) in self.hosts.iter().enumerate() {
if Self::host_matches(host, &self.input) {
if self.host_matches(host, &self.input) {
self.select(i);
return;
}
Expand All @@ -313,8 +316,8 @@ impl TUI {

/// Does a given host match our search string? Just a prefix
/// match, for now.
fn host_matches(host: &str, search: &str) -> bool {
host.to_lowercase().starts_with(&search.to_lowercase())
fn host_matches(&self, host: &str, search: &str) -> bool {
self.matcher.fuzzy_match(host, search).is_some()
}

/// The name of the currently selected host pattern.
Expand Down

0 comments on commit e68dc59

Please sign in to comment.