feat: add ranking based on usage to launcher widget
Signed-off-by: Cyril Levis <git@levis.name>
This commit is contained in:
committed by
Kenny Levinsen
parent
d291e36b3f
commit
43fe73bf09
+57
@@ -0,0 +1,57 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::{create_dir_all, File, OpenOptions};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Default)]
|
||||||
|
pub struct Data {
|
||||||
|
pub entries: HashMap<String, i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Data {
|
||||||
|
pub fn load() -> Result<Data, Box<dyn Error>> {
|
||||||
|
Ok(serde_yaml::from_reader(Self::read_file()?)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) -> Result<(), Box<dyn Error>> {
|
||||||
|
Ok(serde_yaml::to_writer(Self::write_file()?, self)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_file() -> Result<File, Box<dyn Error>> {
|
||||||
|
Ok(OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.write(true)
|
||||||
|
.read(true)
|
||||||
|
.open(Self::path()?)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_file() -> Result<File, Box<dyn Error>> {
|
||||||
|
Ok(OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.write(true)
|
||||||
|
.truncate(true)
|
||||||
|
.open(Self::path()?)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn path() -> Result<PathBuf, Box<dyn Error>> {
|
||||||
|
let xdg_cache = match env::var_os("XDG_CACHE_HOME") {
|
||||||
|
Some(s) => s
|
||||||
|
.into_string()
|
||||||
|
.map_err(|_| "Unable to resolve $XDG_CACHE_HOME")?,
|
||||||
|
None => format!(
|
||||||
|
"{}/.cache",
|
||||||
|
env::var_os("HOME")
|
||||||
|
.ok_or("Unable to resolve $HOME")?
|
||||||
|
.into_string()
|
||||||
|
.map_err(|_| "Unable to resolve $HOME")?
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
let cache_dir = PathBuf::from(xdg_cache).join("wldash");
|
||||||
|
create_dir_all(&cache_dir)?;
|
||||||
|
|
||||||
|
Ok(cache_dir.join("data.yaml"))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ mod cmd;
|
|||||||
mod color;
|
mod color;
|
||||||
mod config;
|
mod config;
|
||||||
mod configfmt;
|
mod configfmt;
|
||||||
|
mod data;
|
||||||
mod desktop;
|
mod desktop;
|
||||||
mod doublemempool;
|
mod doublemempool;
|
||||||
mod draw;
|
mod draw;
|
||||||
|
|||||||
+17
-4
@@ -14,6 +14,7 @@ use std::collections::HashMap;
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
|
use crate::data::Data;
|
||||||
use crate::keyboard::keysyms;
|
use crate::keyboard::keysyms;
|
||||||
use fuzzy_matcher::skim::SkimMatcherV2;
|
use fuzzy_matcher::skim::SkimMatcherV2;
|
||||||
use fuzzy_matcher::FuzzyMatcher;
|
use fuzzy_matcher::FuzzyMatcher;
|
||||||
@@ -34,6 +35,7 @@ pub struct Launcher<'a> {
|
|||||||
length: u32,
|
length: u32,
|
||||||
dirty: bool,
|
dirty: bool,
|
||||||
tx: Sender<Cmd>,
|
tx: Sender<Cmd>,
|
||||||
|
counter: Data,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Launcher<'a> {
|
impl<'a> Launcher<'a> {
|
||||||
@@ -61,6 +63,7 @@ impl<'a> Launcher<'a> {
|
|||||||
length,
|
length,
|
||||||
dirty: true,
|
dirty: true,
|
||||||
tx: listener,
|
tx: listener,
|
||||||
|
counter: Data::load().unwrap_or_default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,6 +104,7 @@ impl<'a> Launcher<'a> {
|
|||||||
let (_, indices) = fuzzy_matcher
|
let (_, indices) = fuzzy_matcher
|
||||||
.fuzzy_indices(&m.name.to_lowercase(), &self.input.to_lowercase())
|
.fuzzy_indices(&m.name.to_lowercase(), &self.input.to_lowercase())
|
||||||
.unwrap_or((0, vec![]));
|
.unwrap_or((0, vec![]));
|
||||||
|
|
||||||
let mut colors = Vec::with_capacity(m.name.len());
|
let mut colors = Vec::with_capacity(m.name.len());
|
||||||
for pos in 0..m.name.len() {
|
for pos in 0..m.name.len() {
|
||||||
if indices.contains(&pos) {
|
if indices.contains(&pos) {
|
||||||
@@ -215,12 +219,14 @@ fn wlcopy(s: &str) -> Result<(), String> {
|
|||||||
|
|
||||||
struct Matcher {
|
struct Matcher {
|
||||||
matches: HashMap<Desktop, i64>,
|
matches: HashMap<Desktop, i64>,
|
||||||
|
counter: Data,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Matcher {
|
impl Matcher {
|
||||||
fn new() -> Self {
|
fn new(counter: Data) -> Self {
|
||||||
Self {
|
Self {
|
||||||
matches: HashMap::new(),
|
matches: HashMap::new(),
|
||||||
|
counter,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,9 +254,12 @@ impl Matcher {
|
|||||||
.collect::<Vec<(i64, Desktop)>>();
|
.collect::<Vec<(i64, Desktop)>>();
|
||||||
|
|
||||||
m.sort_by(|(ma1, d1), (ma2, d2)| {
|
m.sort_by(|(ma1, d1), (ma2, d2)| {
|
||||||
if ma1 > ma2 {
|
let count1 = self.counter.entries.get(&d1.name).unwrap_or(&0);
|
||||||
|
let count2 = self.counter.entries.get(&d2.name).unwrap_or(&0);
|
||||||
|
|
||||||
|
if ma1 + count1 > ma2 + count2 {
|
||||||
Ordering::Less
|
Ordering::Less
|
||||||
} else if ma1 < ma2 {
|
} else if ma1 + count1 < ma2 + count2 {
|
||||||
Ordering::Greater
|
Ordering::Greater
|
||||||
} else if d1.name.len() < d2.name.len() {
|
} else if d1.name.len() < d2.name.len() {
|
||||||
Ordering::Less
|
Ordering::Less
|
||||||
@@ -306,7 +315,7 @@ impl<'a> Widget for Launcher<'a> {
|
|||||||
}
|
}
|
||||||
Some('!') => (),
|
Some('!') => (),
|
||||||
_ => {
|
_ => {
|
||||||
let mut matcher = Matcher::new();
|
let mut matcher = Matcher::new(self.counter.clone());
|
||||||
|
|
||||||
for desktop in self.options.iter() {
|
for desktop in self.options.iter() {
|
||||||
matcher.try_match(
|
matcher.try_match(
|
||||||
@@ -428,6 +437,10 @@ impl<'a> Widget for Launcher<'a> {
|
|||||||
} else {
|
} else {
|
||||||
lexed
|
lexed
|
||||||
};
|
};
|
||||||
|
|
||||||
|
*self.counter.entries.entry(d.name.clone()).or_insert(0) += 1;
|
||||||
|
self.counter.save().expect("Unable to save data");
|
||||||
|
|
||||||
if !lexed.is_empty() {
|
if !lexed.is_empty() {
|
||||||
let _ =
|
let _ =
|
||||||
Command::new(lexed[0].clone()).args(&lexed[1..]).spawn();
|
Command::new(lexed[0].clone()).args(&lexed[1..]).spawn();
|
||||||
|
|||||||
Reference in New Issue
Block a user