Skip to content

Commit

Permalink
Merge pull request #17 from Shizcow/develop
Browse files Browse the repository at this point in the history
New plugins
  • Loading branch information
Shizcow authored Jul 7, 2020
2 parents 449b98b + fc5583b commit 5414362
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 10 deletions.
5 changes: 5 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ man: config
test: all
seq 1 100 | target/dmenu $(ARGS)

debug: m4
cd src/build && cargo build $(XINERAMA_FLAGS)
cp src/build/target/debug/dmenu target
seq 1 100 | RUST_BACKTRACE=1 target/dmenu $(ARGS)

plugins:
cd src/config && cargo run --bin list-plugins

Expand Down
9 changes: 5 additions & 4 deletions src/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ fn main() {

// prepare to edit cli_base args
let mut yaml = get_yaml("../dmenu/cli_base.yml");
let yaml_args: &mut Vec<yaml::Yaml> = get_yaml_args(&mut yaml);
let yaml_args: &mut Vec<yaml::Yaml> = get_yaml_args(&mut yaml).unwrap();

// For every plugin, check if it has arguements. If so, add them to clap and overrider
// While we're here, set proc_use to watch the plugin entry points
for plugin in plugins {
let mut plugin_yaml = get_yaml(&format!("../plugins/{}/plugin.yml", plugin));
let plugin_yaml_args: &mut Vec<yaml::Yaml> = get_yaml_args(&mut plugin_yaml);

yaml_args.append(plugin_yaml_args);

if let Some(plugin_yaml_args) = get_yaml_args(&mut plugin_yaml) {
yaml_args.append(plugin_yaml_args);
}

watch_globs.push((
format!("../plugins/{}/{}", plugin, get_yaml_top_level(&mut plugin_yaml, "entry")
Expand Down
6 changes: 3 additions & 3 deletions src/config/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn get_yaml_top_level<'a>(yaml: &'a mut Yaml, fieldsearch: &str) -> Option<&
}

#[allow(unused)]
pub fn get_yaml_args(yaml: &mut Yaml) -> &mut Vec<yaml::Yaml> {
pub fn get_yaml_args(yaml: &mut Yaml) -> Option<&mut Vec<yaml::Yaml>> {
match yaml {
Yaml::Hash(hash) => {
for field in hash {
Expand All @@ -64,7 +64,7 @@ pub fn get_yaml_args(yaml: &mut Yaml) -> &mut Vec<yaml::Yaml> {
match field.1 {
Yaml::Array(arr) => {
sanitize_args(arr);
return arr;
return Some(arr);
},
_ => panic!("Incorrect arg format on cli_base"),
}
Expand All @@ -74,7 +74,7 @@ pub fn get_yaml_args(yaml: &mut Yaml) -> &mut Vec<yaml::Yaml> {
},
_ => panic!("Incorrect yaml format on cli_base"),
}
panic!("No args found in yaml object");
None
}

fn sanitize_args(args: &mut Vec<yaml::Yaml>) {
Expand Down
2 changes: 2 additions & 0 deletions src/dmenu/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::drw::{Drw, TextOption::*};
use crate::config::{Schemes::*, DefaultWidth};
use regex::Regex;

#[allow(unused_imports)]
pub enum MatchCode {Exact, Prefix, Substring, None}
pub use MatchCode::*;
#[derive(Debug)]
Expand All @@ -24,6 +25,7 @@ impl Item {
pub fn draw(&self, x: c_int, y: c_int, w: c_int, drw: &mut Drw) -> Result<c_int, String> {
drw.text(x, y, w as u32, drw.pseudo_globals.bh as u32, drw.pseudo_globals.lrpad as u32/2, Other(&self.text), false)
}
#[allow(unused)] // won't be used if overriden
pub fn matches(&self, re: &Regex) -> MatchCode {
match re.find_iter(&self.text)
.nth(0).map(|m| (m.start(), m.end()))
Expand Down
1 change: 1 addition & 0 deletions src/dmenu/plugin_entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[allow(unused_imports)]
use crate::clapflags::CLAP_FLAGS;
use crate::drw::Drw;
#[allow(unused_imports)]
use crate::item::{Item, MatchCode};

use overrider::*;
Expand Down
4 changes: 1 addition & 3 deletions src/dmenu/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,7 @@ impl Drw {
}
},
}
if self.draw().is_err() {
return Ok(true);
}
self.draw()?;
}
Ok(false)
}
Expand Down
1 change: 1 addition & 0 deletions src/plugins/fuzzy/deps.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fuzzy-matcher = "0.3.4"
32 changes: 32 additions & 0 deletions src/plugins/fuzzy/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use overrider::*;

#[allow(unused_imports)]
use crate::clapflags::CLAP_FLAGS;

use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2;

use crate::drw::Drw;
use crate::item::Item;

#[override_default]
impl Drw {
pub fn gen_matches(&mut self) -> Result<Vec<Item>, String> {
let searchterm = self.input.clone();
let mut items: Vec<(Item, i64)> =
self.items.as_mut().unwrap().data.iter().map(|item| {
let matcher: Box<dyn FuzzyMatcher> = Box::new(SkimMatcherV2::default());
(item.clone(),
if let Some(score) = matcher.fuzzy_match(&item.text, &searchterm) {
-score
} else {
1
})
}).collect();
items.retain(|(_, score)| *score <= 0);
items.sort_by_key(|(item, _)| item.text.len()); // this prioritizes exact matches
items.sort_by_key(|(_, score)| *score);

Ok(items.into_iter().map(|(item, _)| item).collect())
}
}
3 changes: 3 additions & 0 deletions src/plugins/fuzzy/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
about: Fuzzy string matching for searches
entry: main.rs
cargo_dependencies: deps.toml
1 change: 1 addition & 0 deletions src/plugins/spellcheck/deps.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ispell = "0.3.1"
71 changes: 71 additions & 0 deletions src/plugins/spellcheck/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use overrider::*;

use ispell::{SpellLauncher};
use std::process::{Command, Stdio};
use std::io::Write;

use crate::drw::Drw;
use crate::item::Item;

#[override_flag(flag = spellcheck)]
impl Drw {
pub fn gen_matches(&mut self) -> Result<Vec<Item>, String> {
let checker = SpellLauncher::new()
.aspell()
.launch();

let (first, second) = self.input.split_at(self.pseudo_globals.cursor);
let first_replaced = first.replace(" ", "");
let second_replaced = second.replace(" ", "");
self.pseudo_globals.cursor = first_replaced.chars().count();
self.input = first_replaced+&second_replaced;

match checker {
Ok(mut checker) => {
match checker.check(&self.input) {
Ok(mut res) => {
if res.is_empty() {
Ok(vec![Item::new(self.input.clone(), false, self)?])
} else {
let mut ret = Vec::new();
for word in res.swap_remove(0).suggestions.into_iter() {
ret.push(Item::new(word, false, self)?);
}
Ok(ret)
}
},
Err(err) => Err(format!("Error: could not run aspell: {}", err))
}
},
Err(err) => Err(format!("Error: could not start aspell: {}", err))
}
}
pub fn dispose(&mut self, output: String, recommendation: bool) -> Result<bool, String> {
if output.len() > 0 {
let mut child = Command::new("xclip")
.arg("-sel")
.arg("clip")
.stdin(Stdio::piped())
.spawn()
.map_err(|_| "Failed to spawn child process".to_owned())?;

child.stdin.as_mut().ok_or("Failed to open stdin".to_owned())?
.write_all(output.as_bytes()).map_err(|_| "Failed to write to stdin".to_owned())?;
}
Ok(recommendation)
}
}

use crate::config::{ConfigDefault, DefaultWidth};
#[override_flag(flag = spellcheck)]
impl ConfigDefault {
pub fn nostdin() -> bool {
true
}
pub fn render_flex() -> bool {
true
}
pub fn render_default_width() -> DefaultWidth {
DefaultWidth::Custom(10)
}
}
11 changes: 11 additions & 0 deletions src/plugins/spellcheck/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
about: Single word spellcheck
entry: main.rs
cargo_dependencies: deps.toml

args:
- spellcheck:
help: Enter spellcheck mode
long_help: "Enter spellcheck mode. Begin typing a word for spelling suggestions.\nOnly accepts
single word queries.\nPressing Enter will copy to clipboard before exiting."
long: spellcheck
visible_aliases: sc

0 comments on commit 5414362

Please sign in to comment.