Skip to content

Commit c7a8ced

Browse files
committed
Add modes that run shell scripts
1 parent 0fab4d0 commit c7a8ced

7 files changed

Lines changed: 67 additions & 20 deletions

File tree

src/app.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//! Main logic for the app
2+
use std::collections::HashMap;
3+
4+
use crate::app::apps::App;
25
use crate::commands::Function;
36
use crate::{app::tile::ExtSender, clipboard::ClipBoardContentType};
47

@@ -59,6 +62,7 @@ pub enum Message {
5962
WindowFocusChanged(Id, bool),
6063
ClearSearchQuery,
6164
HideTrayIcon,
65+
SwitchMode(String),
6266
ReloadConfig,
6367
SetSender(ExtSender),
6468
SwitchToPage(Page),
@@ -82,3 +86,26 @@ pub fn default_settings() -> Settings {
8286
..Default::default()
8387
}
8488
}
89+
90+
pub trait ToApp {
91+
fn to_app(&self) -> App;
92+
}
93+
94+
pub trait ToApps {
95+
fn to_apps(&self) -> Vec<App>;
96+
}
97+
98+
impl ToApps for HashMap<String, String> {
99+
fn to_apps(&self) -> Vec<App> {
100+
self.keys()
101+
.map(|key| App {
102+
ranking: 0,
103+
open_command: apps::AppCommand::Message(Message::SwitchMode(key.trim().to_owned())),
104+
search_name: key.to_owned(),
105+
desc: "Switch Modes".to_string(),
106+
icons: None,
107+
display_name: format!("Switch mode to: {}", key.trim()),
108+
})
109+
.collect()
110+
}
111+
}

src/app/tile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub struct Tile {
102102
pub theme: iced::Theme,
103103
pub focus_id: u32,
104104
pub query: String,
105+
pub current_mode: String,
105106
query_lc: String,
106107
results: Vec<App>,
107108
options: AppIndex,
@@ -354,7 +355,7 @@ fn handle_clipboard_history() -> impl futures::Stream<Item = Message> {
354355
.ok();
355356
prev_byte_rep = byte_rep;
356357
}
357-
tokio::time::sleep(Duration::from_millis(10)).await;
358+
tokio::time::sleep(Duration::from_millis(100)).await;
358359
}
359360
})
360361
}

src/app/tile/elm.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use iced::{Length::Fill, widget::text_input};
1313
use log::info;
1414
use rayon::slice::ParallelSliceMut;
1515

16-
use crate::app::DEFAULT_WINDOW_HEIGHT;
1716
use crate::app::pages::emoji::emoji_page;
1817
use crate::app::tile::{AppIndex, Hotkeys};
18+
use crate::app::{DEFAULT_WINDOW_HEIGHT, ToApp, ToApps};
1919
use crate::config::Theme;
2020
use crate::styles::{contents_style, glass_border, glass_surface, rustcast_text_input_style};
2121
use crate::{app::WINDOW_WIDTH, platform};
@@ -44,6 +44,9 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
4444
options.extend(config.shells.iter().map(|x| x.to_app()));
4545
info!("Loaded shell commands");
4646

47+
options.extend(config.modes.to_apps());
48+
info!("Loaded modes");
49+
4750
options.extend(App::basic_apps());
4851
info!("Loaded basic apps / default apps");
4952
options.par_sort_by_key(|x| x.display_name.len());
@@ -59,6 +62,7 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
5962

6063
(
6164
Tile {
65+
current_mode: "".to_string(),
6266
query: String::new(),
6367
query_lc: String::new(),
6468
focus_id: 0,
@@ -70,7 +74,7 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
7074
frontmost: None,
7175
focused: false,
7276
config: config.clone(),
73-
theme: config.theme.to_owned().into(),
77+
theme: config.theme.to_owned().clone().into(),
7478
clipboard_content: vec![],
7579
tray_icon: None,
7680
sender: None,

src/app/tile/update.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use iced::window::Id;
1313
use log::info;
1414
use rayon::slice::ParallelSliceMut;
1515

16+
use crate::app::ToApp;
1617
use crate::app::WINDOW_WIDTH;
1718
use crate::app::apps::App;
1819
use crate::app::apps::AppCommand;
@@ -40,6 +41,20 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
4041
tile.visible = true;
4142
Task::none()
4243
}
44+
45+
Message::SwitchMode(mode) => {
46+
if let Some(command) = tile.config.modes.get(mode.trim()) {
47+
tile.current_mode = mode.clone();
48+
info!("switched mode");
49+
Task::done(Message::RunFunction(Function::RunShellCommand(
50+
command.to_owned(),
51+
)))
52+
} else {
53+
info!("no presentation found");
54+
Task::none()
55+
}
56+
}
57+
4358
Message::HideTrayIcon => {
4459
tile.tray_icon = None;
4560
tile.config.show_trayicon = false;
@@ -275,7 +290,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
275290
}
276291

277292
Message::RunFunction(command) => {
278-
command.execute(&tile.config, &tile.query);
293+
command.execute(&tile.config);
279294

280295
let return_focus_task = match &command {
281296
Function::OpenApp(_) | Function::OpenPrefPane | Function::GoogleSearch(_) => {

src/commands.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{calculator::Expr, clipboard::ClipBoardContentType, config::Config};
1212
#[derive(Debug, Clone, PartialEq)]
1313
pub enum Function {
1414
OpenApp(String),
15-
RunShellCommand(String, String),
15+
RunShellCommand(String),
1616
OpenWebsite(String),
1717
RandomVar(i32), // Easter egg function
1818
CopyToClipboard(ClipBoardContentType),
@@ -24,7 +24,7 @@ pub enum Function {
2424

2525
impl Function {
2626
/// Run the command
27-
pub fn execute(&self, config: &Config, query: &str) {
27+
pub fn execute(&self, config: &Config) {
2828
match self {
2929
Function::OpenApp(path) => {
3030
let path = path.to_owned();
@@ -34,13 +34,10 @@ impl Function {
3434
));
3535
});
3636
}
37-
Function::RunShellCommand(command, alias) => {
38-
let query = query.to_string();
39-
let final_command =
40-
format!(r#"{} {}"#, command, query.strip_prefix(alias).unwrap_or(""));
37+
Function::RunShellCommand(command) => {
4138
Command::new("sh")
4239
.arg("-c")
43-
.arg(final_command.trim())
40+
.arg(format!("\"{}\"", command))
4441
.spawn()
4542
.ok();
4643
}

src/config.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! This is the config file type definitions for rustcast
2-
use std::{path::Path, sync::Arc};
2+
use std::{collections::HashMap, path::Path, sync::Arc};
33

44
use iced::{Font, font::Family, theme::Custom, widget::image::Handle};
55
use serde::{Deserialize, Serialize};
66

77
use crate::{
8-
app::apps::{App, AppCommand},
8+
app::{
9+
ToApp,
10+
apps::{App, AppCommand},
11+
},
912
commands::Function,
1013
utils::handle_from_icns,
1114
};
@@ -23,6 +26,7 @@ pub struct Config {
2326
pub haptic_feedback: bool,
2427
pub show_trayicon: bool,
2528
pub shells: Vec<Shelly>,
29+
pub modes: HashMap<String, String>,
2630
pub log_path: String,
2731
}
2832

@@ -39,6 +43,7 @@ impl Default for Config {
3943
haptic_feedback: false,
4044
show_trayicon: true,
4145
log_path: "/tmp/rustcast.log".to_string(),
46+
modes: HashMap::new(),
4247
shells: vec![],
4348
}
4449
}
@@ -173,9 +178,8 @@ pub struct Shelly {
173178
alias_lc: String,
174179
}
175180

176-
impl Shelly {
177-
/// Converts the shelly struct to an app so that it can be added to the app list
178-
pub fn to_app(&self) -> App {
181+
impl ToApp for Shelly {
182+
fn to_app(&self) -> App {
179183
let self_clone = self.clone();
180184
let icon = self_clone.icon_path.and_then(|x| {
181185
let x = x.replace("~", &std::env::var("HOME").unwrap());
@@ -187,10 +191,7 @@ impl Shelly {
187191
});
188192
App {
189193
ranking: 0,
190-
open_command: AppCommand::Function(Function::RunShellCommand(
191-
self_clone.command,
192-
self_clone.alias_lc.clone(),
193-
)),
194+
open_command: AppCommand::Function(Function::RunShellCommand(self_clone.command)),
194195
desc: "Shell Command".to_string(),
195196
icons: icon,
196197
display_name: self_clone.alias,

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(clippy::dbg_macro)]
2+
13
mod app;
24
mod calculator;
35
mod clipboard;

0 commit comments

Comments
 (0)