Skip to content

Commit

Permalink
Add sort function to plugin trait (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-kenji authored Apr 2, 2024
1 parent 62d1d33 commit 4c1727f
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 17 deletions.
4 changes: 4 additions & 0 deletions client/src/plugin/applications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ impl Plugin for ApplicationsPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn update_entries(&mut self) -> anyhow::Result<()> {
self.entries.clear();

Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/brave/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl Plugin for BookmarksPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn new() -> Self {
Self { entries: vec![] }
}
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/brave/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl Plugin for HistoryPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn new() -> Self {
Self { entries: vec![] }
}
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/brave/progressive_web_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl Plugin for ProgressiveWebAppsPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn new() -> Self {
Self { entries: vec![] }
}
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ impl Plugin for ClockPlugin {
fn entries(&self) -> Vec<crate::model::Entry> {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}
}
4 changes: 4 additions & 0 deletions client/src/plugin/git_repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ impl Plugin for GitRepositoriesPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn new() -> Self {
Self {
entries: vec![],
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/resource_monitor/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ impl Plugin for BatteryPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn update_entries(&mut self) -> anyhow::Result<()> {
self.entries.clear();

Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/resource_monitor/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Plugin for CpuPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn update_entries(&mut self) -> anyhow::Result<()> {
self.sysinfo.refresh_cpu();
self.entries.clear();
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/resource_monitor/disks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ impl Plugin for DisksPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn update_entries(&mut self) -> anyhow::Result<()> {
self.disks.refresh_list();
self.entries.clear();
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/resource_monitor/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Plugin for MemoryPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn update_entries(&mut self) -> anyhow::Result<()> {
self.sysinfo.refresh_memory();
self.entries.clear();
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/sway_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl Plugin for SwayWindowsPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn new() -> Self {
let connection_result =
swayipc::Connection::new().context("Failed to establish sway ipc connection.");
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl Plugin for SystemPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn activate(
&mut self,
entry: crate::model::Entry,
Expand Down
34 changes: 17 additions & 17 deletions client/src/plugin/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub trait Plugin {

fn entries(&self) -> Vec<crate::model::Entry>;

fn set_entries(&mut self, entries: Vec<crate::model::Entry>);

fn update_entries(&mut self) -> anyhow::Result<()> {
Ok(())
}
Expand Down Expand Up @@ -122,12 +124,26 @@ pub trait Plugin {
return Ok(());
}

fn sort(&self, entries: &mut Vec<crate::model::Entry>) {
entries.sort_by_key(|entry| entry.title.clone());
}

fn search(
&mut self,
query: &str,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
) -> 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::<Vec<crate::model::Entry>>();

plugin_channel_out
.try_send(crate::Message::UpdateEntries(
Expand All @@ -151,22 +167,6 @@ pub trait Plugin {
}
}

pub fn search(entries: Vec<crate::model::Entry>, query: &str) -> Vec<crate::model::Entry> {
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::<Vec<crate::model::Entry>>()
}

pub fn config_directory() -> anyhow::Result<String> {
let home_directory = std::env::var("HOME")?;
let config_in_home = format!("{home_directory}/.config");
Expand Down
4 changes: 4 additions & 0 deletions client/src/plugin/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ impl Plugin for WifiPlugin {
self.entries.clone()
}

fn set_entries(&mut self, entries: Vec<crate::model::Entry>) {
self.entries = entries;
}

fn activate(
&mut self,
entry: crate::model::Entry,
Expand Down

0 comments on commit 4c1727f

Please sign in to comment.