Skip to content

Commit

Permalink
fuzzy finder (#8)
Browse files Browse the repository at this point in the history
* initial poc

* Point to versiont that works better in light mode

* simplify, remove old selection method
  • Loading branch information
keirlawson authored Jun 14, 2024
1 parent 91f254a commit 995384f
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 16 deletions.
205 changes: 203 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ anyhow = "1.0"
url = { version = "2", features = ["serde"] }
structopt = "0.3"
env_logger = "0.11"
fuzzy-select = "0.1.2"

[profile.release]
# per https://kobzol.github.io/rust/cargo/2024/01/23/making-rust-binaries-smaller-by-default.html
Expand Down
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{anyhow, Context, Result};
use dialoguer::{theme::ColorfulTheme, Select};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
Expand All @@ -24,6 +23,20 @@ pub struct Issue {
pub fields: HashMap<String, String>,
}

impl fuzzy_select::Select for Issue {
fn search_content(&self) -> &str {
self.fields.get("summary").unwrap()
}

fn render_before_content(&self) -> Option<impl fmt::Display + '_> {
None::<Self>
}

fn render_after_content(&self) -> Option<impl fmt::Display + '_> {
None::<Self>
}
}

impl fmt::Display for Issue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
Expand Down Expand Up @@ -71,14 +84,3 @@ pub fn search_issues(config: Config, query: &str) -> Result<Vec<Issue>> {
resp.issues
.ok_or_else(|| anyhow!("No issues found for query"))
}

pub fn select_issue(issues: &[Issue]) -> Result<&Issue> {
let selection = Select::with_theme(&ColorfulTheme::default())
.items(issues)
.default(0)
.interact_opt()?;

let index = selection.ok_or_else(|| anyhow!("No JIRA issue selected"))?;

Ok(&issues[index])
}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ fn main() -> Result<()> {
fields,
})
};
use fuzzy_select::FuzzySelect;

let issue = ji::select_issue(&issues)?;
let selected = FuzzySelect::new().with_options(issues).select()?;

println!("{}", issue.key);
println!("{}", selected.key);

Ok(())
}

0 comments on commit 995384f

Please sign in to comment.