From 2ea21412b8b6efd2449383c89bb4b073b6cc62ec Mon Sep 17 00:00:00 2001 From: a-kenji Date: Mon, 1 Apr 2024 20:05:38 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20plugins:=20add=20sort=20function=20?= =?UTF-8?q?to=20plugin=20trait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `sort` function to the plugin trait. This allows easy overriding of default sorting behavior. --- client/src/plugin/applications.rs | 4 +++ client/src/plugin/brave/bookmarks.rs | 4 +++ client/src/plugin/brave/history.rs | 4 +++ .../src/plugin/brave/progressive_web_apps.rs | 4 +++ client/src/plugin/clock.rs | 4 +++ client/src/plugin/git_repositories.rs | 4 +++ client/src/plugin/resource_monitor/battery.rs | 4 +++ client/src/plugin/resource_monitor/cpu.rs | 4 +++ client/src/plugin/resource_monitor/disks.rs | 4 +++ client/src/plugin/resource_monitor/memory.rs | 4 +++ client/src/plugin/sway_windows.rs | 4 +++ client/src/plugin/system.rs | 4 +++ client/src/plugin/utils.rs | 34 +++++++++---------- client/src/plugin/wifi.rs | 4 +++ 14 files changed, 69 insertions(+), 17 deletions(-) diff --git a/client/src/plugin/applications.rs b/client/src/plugin/applications.rs index e9d2f18..d8094e7 100644 --- a/client/src/plugin/applications.rs +++ b/client/src/plugin/applications.rs @@ -113,6 +113,10 @@ impl Plugin for ApplicationsPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn update_entries(&mut self) -> anyhow::Result<()> { self.entries.clear(); diff --git a/client/src/plugin/brave/bookmarks.rs b/client/src/plugin/brave/bookmarks.rs index fc4f376..fbf63aa 100644 --- a/client/src/plugin/brave/bookmarks.rs +++ b/client/src/plugin/brave/bookmarks.rs @@ -22,6 +22,10 @@ impl Plugin for BookmarksPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn new() -> Self { Self { entries: vec![] } } diff --git a/client/src/plugin/brave/history.rs b/client/src/plugin/brave/history.rs index 2815aa7..0ac5dab 100644 --- a/client/src/plugin/brave/history.rs +++ b/client/src/plugin/brave/history.rs @@ -22,6 +22,10 @@ impl Plugin for HistoryPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn new() -> Self { Self { entries: vec![] } } diff --git a/client/src/plugin/brave/progressive_web_apps.rs b/client/src/plugin/brave/progressive_web_apps.rs index b1c1ad9..2341041 100644 --- a/client/src/plugin/brave/progressive_web_apps.rs +++ b/client/src/plugin/brave/progressive_web_apps.rs @@ -22,6 +22,10 @@ impl Plugin for ProgressiveWebAppsPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn new() -> Self { Self { entries: vec![] } } diff --git a/client/src/plugin/clock.rs b/client/src/plugin/clock.rs index 3656d39..81386b2 100644 --- a/client/src/plugin/clock.rs +++ b/client/src/plugin/clock.rs @@ -52,4 +52,8 @@ impl Plugin for ClockPlugin { fn entries(&self) -> Vec { self.entries.clone() } + + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } } diff --git a/client/src/plugin/git_repositories.rs b/client/src/plugin/git_repositories.rs index af5cc94..3ad54cf 100644 --- a/client/src/plugin/git_repositories.rs +++ b/client/src/plugin/git_repositories.rs @@ -23,6 +23,10 @@ impl Plugin for GitRepositoriesPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn new() -> Self { Self { entries: vec![], diff --git a/client/src/plugin/resource_monitor/battery.rs b/client/src/plugin/resource_monitor/battery.rs index 1c4b5ef..e77e56e 100644 --- a/client/src/plugin/resource_monitor/battery.rs +++ b/client/src/plugin/resource_monitor/battery.rs @@ -27,6 +27,10 @@ impl Plugin for BatteryPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn update_entries(&mut self) -> anyhow::Result<()> { self.entries.clear(); diff --git a/client/src/plugin/resource_monitor/cpu.rs b/client/src/plugin/resource_monitor/cpu.rs index f76fde8..de6e234 100644 --- a/client/src/plugin/resource_monitor/cpu.rs +++ b/client/src/plugin/resource_monitor/cpu.rs @@ -26,6 +26,10 @@ impl Plugin for CpuPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn update_entries(&mut self) -> anyhow::Result<()> { self.sysinfo.refresh_cpu(); diff --git a/client/src/plugin/resource_monitor/disks.rs b/client/src/plugin/resource_monitor/disks.rs index e33317a..abf458d 100644 --- a/client/src/plugin/resource_monitor/disks.rs +++ b/client/src/plugin/resource_monitor/disks.rs @@ -27,6 +27,10 @@ impl Plugin for DisksPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn update_entries(&mut self) -> anyhow::Result<()> { self.disks.refresh_list(); self.entries.clear(); diff --git a/client/src/plugin/resource_monitor/memory.rs b/client/src/plugin/resource_monitor/memory.rs index 34d1a1e..07dd1d5 100644 --- a/client/src/plugin/resource_monitor/memory.rs +++ b/client/src/plugin/resource_monitor/memory.rs @@ -26,6 +26,10 @@ impl Plugin for MemoryPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn update_entries(&mut self) -> anyhow::Result<()> { self.sysinfo.refresh_memory(); self.entries.clear(); diff --git a/client/src/plugin/sway_windows.rs b/client/src/plugin/sway_windows.rs index 1128ce1..b94022c 100644 --- a/client/src/plugin/sway_windows.rs +++ b/client/src/plugin/sway_windows.rs @@ -39,6 +39,10 @@ impl Plugin for SwayWindowsPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn new() -> Self { let connection_result = swayipc::Connection::new().context("Failed to establish sway ipc connection."); diff --git a/client/src/plugin/system.rs b/client/src/plugin/system.rs index 8bb6d7d..5ff5f02 100644 --- a/client/src/plugin/system.rs +++ b/client/src/plugin/system.rs @@ -64,6 +64,10 @@ impl Plugin for SystemPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn activate( &mut self, entry: crate::model::Entry, diff --git a/client/src/plugin/utils.rs b/client/src/plugin/utils.rs index 1e19aa6..358bbf4 100644 --- a/client/src/plugin/utils.rs +++ b/client/src/plugin/utils.rs @@ -39,6 +39,8 @@ pub trait Plugin { fn entries(&self) -> Vec; + fn set_entries(&mut self, entries: Vec); + fn update_entries(&mut self) -> anyhow::Result<()> { Ok(()) } @@ -122,12 +124,26 @@ pub trait Plugin { return Ok(()); } + fn sort(&self, entries: &mut Vec) { + entries.sort_by_key(|entry| entry.title.clone()); + } + fn search( &mut self, query: &str, plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, ) -> anyhow::Result<()> { - let filtered_entries = crate::plugin::utils::search(self.entries(), query); + let mut entries = self.entries(); + if query.is_empty() { + self.sort(&mut entries); + } + let filtered_entries = entries + .into_iter() + .filter(|entry| { + let keywords = format!("{} {}", entry.title, entry.meta).to_lowercase(); + keywords.contains(&query.to_lowercase()) + }) + .collect::>(); plugin_channel_out .try_send(crate::Message::UpdateEntries( @@ -151,22 +167,6 @@ pub trait Plugin { } } -pub fn search(entries: Vec, query: &str) -> Vec { - if query.is_empty() { - let mut sorted_entries = entries.clone(); - sorted_entries.sort_by_key(|entry| entry.title.clone()); - return sorted_entries; - } - - entries - .into_iter() - .filter(|entry| { - let keywords = format!("{} {}", entry.title, entry.meta).to_lowercase(); - keywords.contains(&query.to_lowercase()) - }) - .collect::>() -} - pub fn config_directory() -> anyhow::Result { let home_directory = std::env::var("HOME")?; let config_in_home = format!("{home_directory}/.config"); diff --git a/client/src/plugin/wifi.rs b/client/src/plugin/wifi.rs index 51911e2..bd82996 100644 --- a/client/src/plugin/wifi.rs +++ b/client/src/plugin/wifi.rs @@ -126,6 +126,10 @@ impl Plugin for WifiPlugin { self.entries.clone() } + fn set_entries(&mut self, entries: Vec) { + self.entries = entries; + } + fn activate( &mut self, entry: crate::model::Entry,