diff options
Diffstat (limited to '.config/awesome/quarrel/native')
| -rw-r--r-- | .config/awesome/quarrel/native/Cargo.toml | 2 | ||||
| -rw-r--r-- | .config/awesome/quarrel/native/src/lenses/application.rs | 14 | ||||
| -rw-r--r-- | .config/awesome/quarrel/native/src/lenses/calculator.rs | 27 | ||||
| -rw-r--r-- | .config/awesome/quarrel/native/src/lenses/mod.rs | 90 | ||||
| -rw-r--r-- | .config/awesome/quarrel/native/src/net/mod.rs | 4 | ||||
| -rw-r--r-- | .config/awesome/quarrel/native/src/net/wireless.rs | 4 |
6 files changed, 82 insertions, 59 deletions
diff --git a/.config/awesome/quarrel/native/Cargo.toml b/.config/awesome/quarrel/native/Cargo.toml index 1b98681..8d1bd80 100644 --- a/.config/awesome/quarrel/native/Cargo.toml +++ b/.config/awesome/quarrel/native/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "qnative" version = "0.1.0" -edition = "2021" +edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/.config/awesome/quarrel/native/src/lenses/application.rs b/.config/awesome/quarrel/native/src/lenses/application.rs index 38a7762..cc12e82 100644 --- a/.config/awesome/quarrel/native/src/lenses/application.rs +++ b/.config/awesome/quarrel/native/src/lenses/application.rs @@ -3,13 +3,13 @@ use std::{ fs::read_dir, path::PathBuf, sync::{ + Arc, + OnceLock, + RwLock, atomic::{ AtomicBool, Ordering, }, - Arc, - OnceLock, - RwLock, }, }; @@ -20,7 +20,10 @@ use rayon::prelude::*; use url::Url; use crate::lenses::{ - Cache, Entries, Entry, Lense + Cache, + Entries, + Entry, + Lense, }; static APPS_DIR: &'static str = "/usr/share/applications"; @@ -34,6 +37,7 @@ pub struct Application { impl Lense for Application { const NAME: &str = "Application"; + const PREFIX: Option<&'static str> = None; fn init() -> Arc<Self> { let this = Arc::new(Application::default()); @@ -94,7 +98,7 @@ impl Lense for Application { // self.should_interrupt.load(Ordering::Relaxed) } - fn entries(&self, _: &Lua, _: String) -> Result<Entries, anyhow::Error> { + fn entries(&self, _: &Lua, _: &str) -> Result<Entries, anyhow::Error> { let entries = read_dir(APPS_DIR)? .map(|result| result.map(|e| e.path())) .collect::<Result<Vec<_>, std::io::Error>>()?; diff --git a/.config/awesome/quarrel/native/src/lenses/calculator.rs b/.config/awesome/quarrel/native/src/lenses/calculator.rs index 36f9805..a483d10 100644 --- a/.config/awesome/quarrel/native/src/lenses/calculator.rs +++ b/.config/awesome/quarrel/native/src/lenses/calculator.rs @@ -1,22 +1,25 @@ use std::sync::{ + Arc, + LazyLock, + Mutex, atomic::{ AtomicBool, Ordering, }, - Arc, - LazyLock, - Mutex, }; use fend_core::{ - evaluate_with_interrupt, Context, Interrupt, + evaluate_with_interrupt, }; use mlua::prelude::*; use crate::lenses::{ - Cache, Entries, Entry, Lense + Cache, + Entries, + Entry, + Lense, }; static CTX: LazyLock<Mutex<Context>> = LazyLock::new(|| { @@ -32,6 +35,7 @@ pub struct Calculator { impl Lense for Calculator { const NAME: &str = "Calculator"; + const PREFIX: Option<&'static str> = Some("#"); fn init() -> std::sync::Arc<Self> { Arc::new(Calculator::default()) @@ -55,18 +59,14 @@ impl Lense for Calculator { self.should_interrupt.load(Ordering::Relaxed) } - fn entries(&self, _: &Lua, input: String) -> Result<Entries, anyhow::Error> { + fn entries(&self, _: &Lua, input: &str) -> Result<Entries, anyhow::Error> { let result = match evaluate_with_interrupt( input.trim(), &mut CTX.lock().expect("Failed to acquire Fend context lock"), self, ) { - Ok(result) => { - result.get_main_result().to_string() - } - Err(err) => { - err - } + Ok(result) => result.get_main_result().to_string(), + Err(err) => err, }; Ok(if result.is_empty() { @@ -77,11 +77,10 @@ impl Lense for Calculator { exec: None, }) }) - } #[inline] - fn filter(&self, entries: Entries, _: String) -> Entries { + fn filter(&self, entries: Entries, _: &str) -> Entries { entries } } diff --git a/.config/awesome/quarrel/native/src/lenses/mod.rs b/.config/awesome/quarrel/native/src/lenses/mod.rs index bb7f727..0b7864e 100644 --- a/.config/awesome/quarrel/native/src/lenses/mod.rs +++ b/.config/awesome/quarrel/native/src/lenses/mod.rs @@ -5,8 +5,8 @@ use std::sync::Arc; use itertools::Itertools; use mlua::{ - prelude::*, LuaSerdeExt, + prelude::*, }; use rayon::iter::FromParallelIterator; use serde::{ @@ -18,7 +18,7 @@ use serde::{ pub enum Entries { Multiple(Vec<Entry>), Single(Entry), - None + None, } impl FromIterator<Entry> for Entries { @@ -32,7 +32,7 @@ impl FromIterator<Entry> for Entries { let mut vec = Vec::from([first, second]); vec.extend(iter); Self::Multiple(vec) - }, + } } } } @@ -44,8 +44,8 @@ impl From<Vec<Entry>> for Entries { 1 => { let entry = entries.into_iter().exactly_one().unwrap(); Self::Single(entry) - }, - _ => Self::Multiple(entries) + } + _ => Self::Multiple(entries), } } } @@ -55,7 +55,7 @@ impl IntoLua for Entries { match self { Entries::Multiple(entries) => entries.into_lua(lua), Entries::Single(entry) => entry.into_lua(lua), - Entries::None => Ok(LuaValue::Nil) + Entries::None => Ok(LuaValue::Nil), } } } @@ -89,6 +89,7 @@ pub struct _Lense<T: Lense>(pub Arc<T>); pub trait Lense { const NAME: &'static str; + const PREFIX: Option<&'static str>; fn init() -> Arc<Self>; @@ -98,51 +99,70 @@ pub trait Lense { fn set_interrupt(&self, interrupt: bool); fn get_interrupt(&self) -> bool; - fn entries(&self, lua: &Lua, input: String) -> Result<Entries, anyhow::Error>; - fn filter(&self, entries: Entries, input: String) -> Entries { - let entry_contains = |entry: &Entry| { - entry.message.to_lowercase().contains(&input.to_lowercase()) - }; + fn entries(&self, lua: &Lua, input: &str) -> Result<Entries, anyhow::Error>; + fn filter(&self, entries: Entries, input: &str) -> Entries { + let entry_contains = + |entry: &Entry| entry.message.to_lowercase().contains(&input.to_lowercase()); match entries { - Entries::Multiple(entries) => entries.into_iter() - .filter(entry_contains) - .collect(), - Entries::Single(entry) => if entry_contains(&entry) { Entries::Single(entry) } else { Entries::None } - Entries::None => Entries::None + Entries::Multiple(entries) => entries.into_iter().filter(entry_contains).collect(), + Entries::Single(entry) => { + if entry_contains(&entry) { + Entries::Single(entry) + } else { + Entries::None + } + } + Entries::None => Entries::None, } } } impl<T: Lense + 'static> LuaUserData for _Lense<T> { fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) { - fields.add_field_method_get("stale", |_, this| { - Ok(matches!(this.0.get_cache(), Cache::Stale)) + fields.add_field_method_get("stale", |_, Self(this)| { + Ok(matches!(this.get_cache(), Cache::Stale)) }); fields.add_field("name", T::NAME); } fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) { - methods.add_method_mut("query", |lua, this, input: String| { - this.0.set_interrupt(false); // reset interrupt so that we can use the lense again - return Ok(match this.0.get_cache() { - Cache::Valid(entries) => this.0.filter(entries.clone(), input), - Cache::Stale => { - let entries = this.0.entries(lua, input.clone()).map_err(LuaError::external)?; - let mut entries = this.0.filter(entries, input); - match entries { - Entries::Multiple(ref mut entries) => entries.sort_by(|a, b| a.message.cmp(&b.message)), - _ => {} - } - this.0.set_cache(Cache::Valid(entries.clone())); - entries + methods.add_method_mut("query", |lua, Self(this), input: String| { + if T::PREFIX.is_some_and(|prefix| input.starts_with(prefix)) || T::PREFIX.is_none() { + let input = if let Some(prefix) = T::PREFIX { + input.trim_start_matches(prefix) + } else { + &input } - }); + .trim(); + + this.set_interrupt(false); // reset interrupt so that we can use the lense again + + return Ok(match this.get_cache() { + Cache::Valid(entries) => this.filter(entries.clone(), input), + Cache::Stale => { + let entries = this.entries(lua, input).map_err(LuaError::external)?; + let mut entries = this.filter(entries, input); + match entries { + Entries::Multiple(ref mut entries) => { + entries.sort_by(|a, b| a.message.cmp(&b.message)) + } + _ => {} + } + this.set_cache(Cache::Valid(entries.clone())); + entries + } + }); + } else { + return Ok(Entries::None); + } }); - methods.add_method_mut("mark_stale", |_, this, _: ()| { - Ok(this.0.set_cache(Cache::Stale)) + methods.add_method_mut("mark_stale", |_, Self(this), _: ()| { + Ok(this.set_cache(Cache::Stale)) + }); + methods.add_method_mut("interrupt", |_, Self(this), _: ()| { + Ok(this.set_interrupt(true)) }); - methods.add_method_mut("interrupt", |_, this, _: ()| Ok(this.0.set_interrupt(true))); } } diff --git a/.config/awesome/quarrel/native/src/net/mod.rs b/.config/awesome/quarrel/native/src/net/mod.rs index 383e800..728e6ec 100644 --- a/.config/awesome/quarrel/native/src/net/mod.rs +++ b/.config/awesome/quarrel/native/src/net/mod.rs @@ -20,21 +20,21 @@ use nix::{ ioctl_read_bad, libc::IF_NAMESIZE, sys::socket::{ - socket as open_socket, AddressFamily, SockFlag, SockType, + socket as open_socket, }, unistd::close, }; use wireless::{ + IW_ESSID_MAX_SIZE, IfConf, IfConfData, IwPoint, IwReq, IwReqData, IwReqName, - IW_ESSID_MAX_SIZE, SIOCGIFCONF, SIOCGIWESSID, }; diff --git a/.config/awesome/quarrel/native/src/net/wireless.rs b/.config/awesome/quarrel/native/src/net/wireless.rs index d3999db..3500751 100644 --- a/.config/awesome/quarrel/native/src/net/wireless.rs +++ b/.config/awesome/quarrel/native/src/net/wireless.rs @@ -7,11 +7,11 @@ use std::ffi::{ use nix::libc::{ __s16, __s32, - __u16, __u8, + __u16, + IF_NAMESIZE, ifreq as IfReq, sockaddr as SockAddr, - IF_NAMESIZE, }; pub static SIOCGIFCONF: c_int = 0x8912; |
