Added prefix & config to Randr
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -1478,6 +1478,8 @@ dependencies = [
|
||||
"anyrun-plugin",
|
||||
"fuzzy-matcher",
|
||||
"hyprland",
|
||||
"ron",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1713,18 +1715,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.159"
|
||||
version = "1.0.160"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065"
|
||||
checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.159"
|
||||
version = "1.0.160"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585"
|
||||
checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@@ -13,3 +13,5 @@ fuzzy-matcher = "0.3.7"
|
||||
anyrun-plugin = { path = "../../anyrun-plugin" }
|
||||
abi_stable = "0.11.1"
|
||||
hyprland = "0.3"
|
||||
ron = "0.8.0"
|
||||
serde = { version = "1.0.160", features = ["derive"] }
|
||||
|
@@ -1,12 +1,26 @@
|
||||
use std::env;
|
||||
use std::{env, fs};
|
||||
|
||||
use abi_stable::std_types::{ROption, RString, RVec};
|
||||
use anyrun_plugin::{anyrun_interface::HandleResult, plugin, Match, PluginInfo};
|
||||
use fuzzy_matcher::FuzzyMatcher;
|
||||
use randr::{dummy::Dummy, hyprland::Hyprland, Configure, Monitor, Randr};
|
||||
use serde::Deserialize;
|
||||
|
||||
mod randr;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
prefix: String,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
prefix: ":dp".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum InnerState {
|
||||
None,
|
||||
Position(Monitor),
|
||||
@@ -14,10 +28,11 @@ enum InnerState {
|
||||
|
||||
pub struct State {
|
||||
randr: Box<dyn Randr + Send + Sync>,
|
||||
config: Config,
|
||||
inner: InnerState,
|
||||
}
|
||||
|
||||
pub fn init(_config_dir: RString) -> State {
|
||||
pub fn init(config_dir: RString) -> State {
|
||||
// Determine which Randr implementation should be used
|
||||
let randr: Box<dyn Randr + Send + Sync> = if env::var("HYPRLAND_INSTANCE_SIGNATURE").is_ok() {
|
||||
Box::new(Hyprland::new())
|
||||
@@ -27,6 +42,13 @@ pub fn init(_config_dir: RString) -> State {
|
||||
|
||||
State {
|
||||
randr,
|
||||
config: match fs::read_to_string(format!("{}/randr.ron", config_dir)) {
|
||||
Ok(content) => ron::from_str(&content).unwrap_or_default(),
|
||||
Err(why) => {
|
||||
eprintln!("Error reading Randr config file: {}", why);
|
||||
Config::default()
|
||||
}
|
||||
},
|
||||
inner: InnerState::None,
|
||||
}
|
||||
}
|
||||
@@ -77,6 +99,12 @@ pub fn handler(_match: Match, state: &mut State) -> HandleResult {
|
||||
}
|
||||
|
||||
pub fn get_matches(input: RString, state: &mut State) -> RVec<Match> {
|
||||
if !input.starts_with(&state.config.prefix) {
|
||||
return RVec::new();
|
||||
}
|
||||
|
||||
let input = &input[state.config.prefix.len()..].trim();
|
||||
|
||||
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default().smart_case();
|
||||
let mut vec = match &state.inner {
|
||||
InnerState::None => state
|
||||
@@ -148,7 +176,7 @@ pub fn get_matches(input: RString, state: &mut State) -> RVec<Match> {
|
||||
.into_iter()
|
||||
.filter_map(|_match| {
|
||||
matcher
|
||||
.fuzzy_match(&_match.title, &input)
|
||||
.fuzzy_match(&_match.title, input)
|
||||
.map(|score| (_match, score))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
Reference in New Issue
Block a user