chore: Update dependencies
This commit is contained in:
parent
4eef0caae5
commit
8ec0ce9224
14 changed files with 823 additions and 465 deletions
|
|
@ -32,13 +32,11 @@ impl IpcClient {
|
|||
|
||||
let responses = LinesStream::new(tokio::io::BufReader::new(stdout).lines()).filter_map(
|
||||
|result| async move {
|
||||
if let Ok(line) = result {
|
||||
if let Ok(event) = serde_json::from_str::<Response>(&line) {
|
||||
return Some(event);
|
||||
}
|
||||
}
|
||||
let Ok(line) = result else {
|
||||
return None;
|
||||
};
|
||||
|
||||
None
|
||||
serde_json::from_str::<Response>(&line).ok()
|
||||
},
|
||||
);
|
||||
|
||||
|
|
@ -57,7 +55,7 @@ impl IpcClient {
|
|||
}
|
||||
|
||||
pub async fn exit(mut self) {
|
||||
let _ = self.send(Request::Exit).await;
|
||||
let _ = self.child.wait().await;
|
||||
let _res = self.send(Request::Exit).await;
|
||||
let _res = self.child.wait().await;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,24 +3,31 @@
|
|||
|
||||
mod client;
|
||||
mod plugins;
|
||||
mod recent;
|
||||
mod priority;
|
||||
mod recent;
|
||||
|
||||
pub use client::*;
|
||||
pub use plugins::config;
|
||||
pub use plugins::external::load;
|
||||
|
||||
use crate::plugins::*;
|
||||
use crate::recent::RecentUseStorage;
|
||||
use crate::plugins::{
|
||||
ExternalPlugin, HelpPlugin, Plugin, PluginConfig, PluginConnector, PluginPriority, PluginQuery,
|
||||
};
|
||||
use crate::priority::Priority;
|
||||
use crate::recent::RecentUseStorage;
|
||||
use flume::{Receiver, Sender};
|
||||
use futures::{future, SinkExt, Stream, StreamExt};
|
||||
use pop_launcher::*;
|
||||
use pop_launcher::{
|
||||
json_input_stream, plugin_paths, ContextOption, IconSource, Indice, PluginResponse,
|
||||
PluginSearchResult, Request, Response, SearchResult,
|
||||
};
|
||||
use regex::Regex;
|
||||
use slab::Slab;
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
collections::{HashMap, HashSet},
|
||||
io::{self, Write}, path::PathBuf,
|
||||
io::{self, Write},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
pub type PluginKey = usize;
|
||||
|
|
@ -38,9 +45,10 @@ pub struct PluginHelp {
|
|||
pub help: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
pub fn ensure_cache_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||
let cachepath = dirs::home_dir().ok_or("failed to find home dir")?.join(".cache/pop-launcher");
|
||||
let cachepath = dirs::home_dir()
|
||||
.ok_or("failed to find home dir")?
|
||||
.join(".cache/pop-launcher");
|
||||
std::fs::create_dir_all(&cachepath)?;
|
||||
Ok(cachepath.join("recent"))
|
||||
}
|
||||
|
|
@ -50,10 +58,10 @@ pub fn store_cache(storage: &RecentUseStorage) {
|
|||
let cachepath = ensure_cache_path()?;
|
||||
Ok(serde_json::to_writer(
|
||||
std::fs::File::create(cachepath)?,
|
||||
storage
|
||||
storage,
|
||||
)?)
|
||||
};
|
||||
if let Err(e)= write_recent() {
|
||||
if let Err(e) = write_recent() {
|
||||
eprintln!("could not write to cache file\n{}", e);
|
||||
}
|
||||
}
|
||||
|
|
@ -66,9 +74,11 @@ pub async fn main() {
|
|||
};
|
||||
let recent = match read_recent() {
|
||||
Ok(r) => r,
|
||||
Err(e) => {eprintln!("could not read cache file\n{}", e); RecentUseStorage::default()}
|
||||
Err(e) => {
|
||||
eprintln!("could not read cache file\n{}", e);
|
||||
RecentUseStorage::default()
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Listens for a stream of requests from stdin.
|
||||
let input_stream = json_input_stream(tokio::io::stdin()).filter_map(|result| {
|
||||
|
|
@ -163,7 +173,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
futures::pin_mut!(f1);
|
||||
futures::pin_mut!(f2);
|
||||
|
||||
let _ = futures::future::select(f1, f2).await.factor_first().0;
|
||||
futures::future::select(f1, f2).await.factor_first();
|
||||
}
|
||||
|
||||
async fn response_handler(&mut self, service_rx: Receiver<Event>) {
|
||||
|
|
@ -175,7 +185,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
Request::Interrupt => self.interrupt().await,
|
||||
Request::Activate(id) => self.activate(id).await,
|
||||
Request::ActivateContext { id, context } => {
|
||||
self.activate_context(id, context).await
|
||||
self.activate_context(id, context).await;
|
||||
}
|
||||
Request::Complete(id) => self.complete(id).await,
|
||||
Request::Context(id) => self.context(id).await,
|
||||
|
|
@ -186,7 +196,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
Request::Exit => {
|
||||
for (_key, plugin) in self.plugins.iter_mut() {
|
||||
let tx = plugin.sender_exec();
|
||||
let _ = tx.send_async(Request::Exit).await;
|
||||
let _res = tx.send_async(Request::Exit).await;
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -199,7 +209,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
PluginResponse::Clear => self.clear(),
|
||||
PluginResponse::Close => self.close().await,
|
||||
PluginResponse::Context { id, options } => {
|
||||
self.context_response(id, options).await
|
||||
self.context_response(id, options).await;
|
||||
}
|
||||
PluginResponse::Fill(text) => self.fill(text).await,
|
||||
PluginResponse::Finished => self.finished(plugin).await,
|
||||
|
|
@ -217,7 +227,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
// Report the plugin as finished and remove it from future polling
|
||||
PluginResponse::Deactivate => {
|
||||
self.finished(plugin).await;
|
||||
let _ = self.plugins.remove(plugin);
|
||||
let _res = self.plugins.remove(plugin);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -257,7 +267,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
.query
|
||||
.isolate_with
|
||||
.as_ref()
|
||||
.and_then(|expr| Regex::new(&*expr).ok());
|
||||
.and_then(|expr| Regex::new(expr).ok());
|
||||
|
||||
entry.insert(PluginConnector::new(
|
||||
config,
|
||||
|
|
@ -281,7 +291,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
let mut ex = None;
|
||||
if let Some((plugin, meta)) = self.search_result(id as usize) {
|
||||
ex = meta.cache_identifier();
|
||||
let _ = plugin
|
||||
let _res = plugin
|
||||
.sender_exec()
|
||||
.send_async(Request::Activate(meta.id))
|
||||
.await;
|
||||
|
|
@ -296,7 +306,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
let mut ex = None;
|
||||
if let Some((plugin, meta)) = self.search_result(id as usize) {
|
||||
ex = meta.cache_identifier();
|
||||
let _ = plugin
|
||||
let _res = plugin
|
||||
.sender_exec()
|
||||
.send_async(Request::ActivateContext {
|
||||
id: meta.id,
|
||||
|
|
@ -331,7 +341,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
|
||||
async fn complete(&mut self, id: Indice) {
|
||||
if let Some((plugin, meta)) = self.search_result(id as usize) {
|
||||
let _ = plugin
|
||||
let _res = plugin
|
||||
.sender_exec()
|
||||
.send_async(Request::Complete(meta.id))
|
||||
.await;
|
||||
|
|
@ -340,7 +350,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
|
||||
async fn context(&mut self, id: Indice) {
|
||||
if let Some((plugin, meta)) = self.search_result(id as usize) {
|
||||
let _ = plugin
|
||||
let _res = plugin
|
||||
.sender_exec()
|
||||
.send_async(Request::Context(meta.id))
|
||||
.await;
|
||||
|
|
@ -370,14 +380,14 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
async fn interrupt(&mut self) {
|
||||
for (_, plugin) in self.plugins.iter_mut() {
|
||||
if let Some(sender) = plugin.sender.as_mut() {
|
||||
let _ = sender.send_async(Request::Interrupt).await;
|
||||
let _res = sender.send_async(Request::Interrupt).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn quit(&mut self, id: Indice) {
|
||||
if let Some((plugin, meta)) = self.search_result(id as usize) {
|
||||
let _ = plugin
|
||||
let _res = plugin
|
||||
.sender_exec()
|
||||
.send_async(Request::Quit(meta.id))
|
||||
.await;
|
||||
|
|
@ -385,7 +395,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
}
|
||||
|
||||
async fn respond(&mut self, event: Response) {
|
||||
let _ = self.output.send(event).await;
|
||||
let _res = self.output.send(event).await;
|
||||
}
|
||||
|
||||
async fn search(&mut self, query: String) {
|
||||
|
|
@ -487,6 +497,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
})
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn sort(&mut self) -> Vec<SearchResult> {
|
||||
let &mut Self {
|
||||
ref mut active_search,
|
||||
|
|
@ -500,8 +511,6 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
|
||||
let query = &last_query.to_ascii_lowercase();
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
if *no_sort {
|
||||
*no_sort = false;
|
||||
} else {
|
||||
|
|
@ -527,9 +536,9 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
if exec.contains(query) {
|
||||
if exec.starts_with(query) {
|
||||
return 1.0;
|
||||
} else {
|
||||
weight = strsim::jaro_winkler(query, &exec) - 0.1;
|
||||
}
|
||||
|
||||
weight = strsim::jaro_winkler(query, &exec) - 0.1;
|
||||
}
|
||||
|
||||
weight
|
||||
|
|
@ -564,12 +573,12 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
match_score: calculate_weight(sr, query),
|
||||
recent_use_index: ex.as_ref().map(|s| recent.get_recent(s)).unwrap_or(0),
|
||||
use_freq: ex.as_ref().map(|s| recent.get_freq(s)).unwrap_or(0),
|
||||
execlen: sr.name.len()
|
||||
execlen: sr.name.len(),
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
get_prio(&b.1, plug2).cmp(&get_prio(&a.1, plug1))
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
let take = if last_query.starts_with('/') | last_query.starts_with('~') {
|
||||
|
|
@ -605,7 +614,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
|
|||
if result.window.is_some() {
|
||||
windows.push(result);
|
||||
} else {
|
||||
non_windows.push(result)
|
||||
non_windows.push(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -622,23 +631,23 @@ async fn request_handler(input: impl Stream<Item = Request>, tx: Sender<Event>)
|
|||
|
||||
while let Some(request) = input.next().await {
|
||||
if let Request::Exit = request {
|
||||
requested_to_exit = true
|
||||
requested_to_exit = true;
|
||||
}
|
||||
|
||||
let _ = tx.send_async(Event::Request(request)).await;
|
||||
let _res = tx.send_async(Event::Request(request)).await;
|
||||
|
||||
if requested_to_exit {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tracing::debug!("no longer listening for requests")
|
||||
tracing::debug!("no longer listening for requests");
|
||||
}
|
||||
|
||||
/// Serializes the launcher's response to stdout
|
||||
fn serialize_out<E: serde::Serialize>(output: &mut io::StdoutLock, event: &E) {
|
||||
if let Ok(mut vec) = serde_json::to_vec(event) {
|
||||
vec.push(b'\n');
|
||||
let _ = output.write_all(&vec);
|
||||
let _res = output.write_all(&vec);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ pub const CONFIG: PluginConfig = PluginConfig {
|
|||
regex: None,
|
||||
},
|
||||
icon: Some(IconSource::Name(Cow::Borrowed("system-help-symbolic"))),
|
||||
history: false
|
||||
history: false,
|
||||
};
|
||||
pub struct HelpPlugin {
|
||||
pub id: usize,
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
// Copyright 2021 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
pub(crate) mod external;
|
||||
pub mod config;
|
||||
pub(crate) mod external;
|
||||
pub mod help;
|
||||
|
||||
pub use external::load;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ use std::cmp::Ordering;
|
|||
|
||||
use crate::PluginPriority;
|
||||
|
||||
|
||||
// holds all values used for ordering search results
|
||||
pub struct Priority {
|
||||
pub plugin_priority: PluginPriority,
|
||||
|
|
@ -12,15 +11,18 @@ pub struct Priority {
|
|||
pub execlen: usize,
|
||||
}
|
||||
|
||||
|
||||
fn signum(val: i32) -> f64 {
|
||||
if val > 0 { return 1.0; }
|
||||
if val < 0 { return -1.0; }
|
||||
if val > 0 {
|
||||
return 1.0;
|
||||
}
|
||||
if val < 0 {
|
||||
return -1.0;
|
||||
}
|
||||
0.0
|
||||
}
|
||||
|
||||
impl Priority {
|
||||
fn compute_value(&self, other: &Self) -> f64{
|
||||
fn compute_value(&self, other: &Self) -> f64 {
|
||||
// increases compared jw-score if this search result
|
||||
// was activated more frequent or recent by constant values
|
||||
let score = self.match_score
|
||||
|
|
@ -30,12 +32,13 @@ impl Priority {
|
|||
if self.match_score < 1.0 {
|
||||
return score.min(0.99);
|
||||
}
|
||||
return score;
|
||||
|
||||
score
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Priority {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.plugin_priority == other.plugin_priority
|
||||
&& self.compute_value(other) == other.match_score
|
||||
&& self.execlen == other.execlen
|
||||
|
|
@ -45,10 +48,13 @@ impl PartialEq for Priority {
|
|||
impl Eq for Priority {}
|
||||
|
||||
impl PartialOrd for Priority {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
(other.plugin_priority, self.compute_value(other), self.execlen).partial_cmp(
|
||||
&(self.plugin_priority, other.match_score, other.execlen)
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
(
|
||||
other.plugin_priority,
|
||||
self.compute_value(other),
|
||||
self.execlen,
|
||||
)
|
||||
.partial_cmp(&(self.plugin_priority, other.match_score, other.execlen))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,4 +62,4 @@ impl Ord for Priority {
|
|||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.partial_cmp(other).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::collections::{HashMap, hash_map::DefaultHasher};
|
||||
use std::hash::{Hasher, Hash};
|
||||
use serde::{Deserialize, Serialize, Serializer, Deserializer};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use std::collections::{hash_map::DefaultHasher, HashMap};
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
const SHORTTERM_CAP: usize = 20;
|
||||
const LONGTERM_CAP: usize = 100;
|
||||
|
|
@ -13,25 +13,23 @@ const LONGTERM_CAP: usize = 100;
|
|||
// command string.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct RecentUseStorage {
|
||||
long_term: HashMap<usize, usize>,
|
||||
short_term: HashMap<usize, usize>,
|
||||
long_term: HashMap<u64, usize>,
|
||||
short_term: HashMap<u64, usize>,
|
||||
}
|
||||
|
||||
|
||||
fn hash_key<K: Hash>(key: K) -> usize {
|
||||
fn hash_key<K: Hash>(key: K) -> u64 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
key.hash(&mut hasher);
|
||||
hasher.finish() as usize
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
|
||||
impl RecentUseStorage {
|
||||
pub fn add<K: Hash>(&mut self, exec: &K) {
|
||||
let key = hash_key(exec);
|
||||
*self.long_term.entry(key).or_insert(0) += 1;
|
||||
let short_term_idx = self.short_term.values().max().unwrap_or( &0)+1;
|
||||
let short_term_idx = self.short_term.values().max().unwrap_or(&0) + 1;
|
||||
self.short_term.insert(key, short_term_idx);
|
||||
self.trim()
|
||||
self.trim();
|
||||
}
|
||||
|
||||
fn trim(&mut self) {
|
||||
|
|
@ -42,7 +40,7 @@ impl RecentUseStorage {
|
|||
|
||||
while self.long_term.values().sum::<usize>() > LONGTERM_CAP {
|
||||
let mut delete_keys = Vec::new();
|
||||
for (k, v) in self.long_term.iter_mut() {
|
||||
for (k, v) in &mut self.long_term {
|
||||
*v /= 2;
|
||||
if *v == 0 {
|
||||
delete_keys.push(*k);
|
||||
|
|
@ -79,12 +77,12 @@ impl<'de> Deserialize<'de> for RecentUseStorage {
|
|||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
type SerType = (HashMap<usize, usize>, Vec<usize>);
|
||||
type SerType = (HashMap<u64, usize>, Vec<u64>);
|
||||
let (long_term, stv) = SerType::deserialize(deserializer)?;
|
||||
let short_term: HashMap<_, _> = stv.into_iter().enumerate().map(|(v,k)| (k,v)).collect();
|
||||
let short_term: HashMap<_, _> = stv.into_iter().enumerate().map(|(v, k)| (k, v)).collect();
|
||||
Ok(RecentUseStorage {
|
||||
long_term,
|
||||
short_term,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue