Added option to allow "invalid" inputs to be allowed in the Stdin plugin
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2022,6 +2022,8 @@ dependencies = [
|
|||||||
"abi_stable",
|
"abi_stable",
|
||||||
"anyrun-plugin",
|
"anyrun-plugin",
|
||||||
"fuzzy-matcher",
|
"fuzzy-matcher",
|
||||||
|
"ron",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@@ -12,3 +12,5 @@ crate-type = ["cdylib"]
|
|||||||
anyrun-plugin = { path = "../../anyrun-plugin" }
|
anyrun-plugin = { path = "../../anyrun-plugin" }
|
||||||
fuzzy-matcher = "0.3.7"
|
fuzzy-matcher = "0.3.7"
|
||||||
abi_stable = "0.11.1"
|
abi_stable = "0.11.1"
|
||||||
|
ron = "0.8.0"
|
||||||
|
serde = { version = "1.0.152", features = ["derive"] }
|
||||||
|
@@ -1,12 +1,42 @@
|
|||||||
use std::io::stdin;
|
use std::{fs, io::stdin};
|
||||||
|
|
||||||
use abi_stable::std_types::{ROption, RString, RVec};
|
use abi_stable::std_types::{ROption, RString, RVec};
|
||||||
use anyrun_plugin::*;
|
use anyrun_plugin::*;
|
||||||
use fuzzy_matcher::FuzzyMatcher;
|
use fuzzy_matcher::FuzzyMatcher;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Config {
|
||||||
|
allow_invalid: bool,
|
||||||
|
max_entries: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
allow_invalid: false,
|
||||||
|
max_entries: 5,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
config: Config,
|
||||||
|
lines: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[init]
|
#[init]
|
||||||
fn init(_config_dir: RString) -> Vec<String> {
|
fn init(config_dir: RString) -> State {
|
||||||
stdin().lines().filter_map(|line| line.ok()).collect()
|
let config = if let Ok(content) = fs::read_to_string(format!("{}/stdin.ron", config_dir)) {
|
||||||
|
ron::from_str(&content).unwrap_or_default()
|
||||||
|
} else {
|
||||||
|
Config::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
State {
|
||||||
|
config,
|
||||||
|
lines: stdin().lines().filter_map(|line| line.ok()).collect(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[handler]
|
#[handler]
|
||||||
@@ -15,10 +45,11 @@ fn handler(_match: Match) -> HandleResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get_matches]
|
#[get_matches]
|
||||||
fn get_matches(input: RString, lines: &Vec<String>) -> RVec<Match> {
|
fn get_matches(input: RString, state: &State) -> RVec<Match> {
|
||||||
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default().smart_case();
|
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default().smart_case();
|
||||||
|
|
||||||
let mut lines = lines
|
let mut lines = state
|
||||||
|
.lines
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|line| {
|
.filter_map(|line| {
|
||||||
@@ -28,9 +59,12 @@ fn get_matches(input: RString, lines: &Vec<String>) -> RVec<Match> {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
lines.sort_by(|a, b| b.1.cmp(&a.1));
|
if !lines.is_empty() {
|
||||||
|
lines.sort_by(|a, b| b.1.cmp(&a.1));
|
||||||
lines.truncate(5);
|
lines.truncate(state.config.max_entries);
|
||||||
|
} else if state.config.allow_invalid {
|
||||||
|
lines.push((input.into(), 0));
|
||||||
|
}
|
||||||
|
|
||||||
lines
|
lines
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
Reference in New Issue
Block a user