Skip to content

Commit

Permalink
add term_command config, bump version to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianRudolph committed Oct 17, 2021
1 parent a99a3a3 commit 24c7151
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 31 deletions.
23 changes: 21 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sirula"
version = "0.0.1"
version = "1.0.0"
authors = ["Dorian Rudolph"]
edition = "2018"

Expand All @@ -22,6 +22,7 @@ serde = "1.0.130"
serde_derive = "1.0.130"
toml = "0.5.8"
regex = "1.5.4"
osstrtools = "0.2.2"

[profile.release]
lto = true
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ I'd be happy to hear any criticism of my code.
- Build: `cargo build --release`
- Optionally, `strip` the binary to reduce size
- Alternatively, install with `cargo install --path .`
- There is also an unofficial [AUR package](https://aur.archlinux.org/packages/sirula-git/)

## Configuration

Expand Down
4 changes: 3 additions & 1 deletion sample-config/default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ command_prefix = ":"

recent_first = true # sort matches of equal quality by most recently used

# term_command = "alacritty -e {}" # command for applications run in terminal (default uses "$TERMINAL -e")

# specify name overrides (id is the name of the desktop file)
[name_overrides]
# id = "name\rextra"hide_extra_if_contained
# id = "name\rextra"
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ make_config!(Config {
name_overrides: HashMap<String, String> = (HashMap::new()) "name_overrides",
hide_extra_if_contained: bool = (true) "hide_extra_if_contained",
command_prefix: String = (":".into()) "command_prefix",
exclude: Vec<String> = (Vec::new()) "exclude"
exclude: Vec<String> = (Vec::new()) "exclude",
term_command: Option<String> = (None) "term_command"
});

fn deserialize_markup<'de, D>(deserializer: D) -> Result<Vec<Attribute>, D::Error>
Expand Down
4 changes: 2 additions & 2 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fs::File;

#[derive(Deserialize, Serialize)]
pub struct History {
pub last_used : HashMap<String, u64>
pub last_used: HashMap<String, u64>
}

impl History {
Expand All @@ -17,7 +17,7 @@ impl History {
let config_str = std::fs::read_to_string(file).expect("Cannot read history file");
toml::from_str(&config_str).expect("Cannot parse config: {}")
},
_ => History { last_used : HashMap::new() }
_ => History { last_used: HashMap::new() }
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn app_startup(application: &gtk::Application) {
}));

let matcher = SkimMatcherV2::default();
let term_command = config.term_command.clone();
entry.connect_changed(clone!(entries, listbox, cmd_prefix => move |e| {
let text = e.text();
let is_cmd = is_cmd(&text, &cmd_prefix);
Expand Down Expand Up @@ -151,7 +152,7 @@ fn app_startup(application: &gtk::Application) {
let es = entries.borrow();
let e = &es[r];
if e.score >= min_score {
launch_app(&e.info);
launch_app(&e.info, term_command.as_deref());

let mut history = history.borrow_mut();
history.update(e.info.id().unwrap().as_str());
Expand Down
52 changes: 29 additions & 23 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::path::PathBuf;
use gio::{AppInfo, prelude::{AppInfoExt, AppInfoExtManual}, AppInfoCreateFlags};
use gtk::{CssProvider, prelude::CssProviderExt};
use freedesktop_entry_parser::parse_entry;
use osstrtools::OsStrTools;
use std::ffi::OsStr;

pub fn get_xdg_dirs() -> xdg::BaseDirectories {
xdg::BaseDirectories::with_prefix(APP_NAME).unwrap()
Expand Down Expand Up @@ -69,32 +71,36 @@ pub fn launch_cmd(cmd_line: &str) {
child.spawn().expect("Error spawning command");
}

pub fn launch_app(info: &AppInfo) {
pub fn launch_app(info: &AppInfo, term_command: Option<&str>) {
let context = gdk::Display::default().unwrap().app_launch_context().unwrap();

// launch terminal applications ourselves because GTK ignores the TERMINAL environment variable
if let Some(term) = std::env::var_os("TERMINAL") {
let use_terminal = info.property("filename").ok().and_then(|p| p.get::<GString>().ok())
.and_then(|s| parse_entry(s.to_string()).ok())
.and_then(|e| e.section("Desktop Entry").attr("Terminal").map(|t| t == "1" || t == "true"))
.unwrap_or(false);
if use_terminal {
let command = match info.commandline() {
Some(c) => c,
_ => info.executable()
};
let mut cmd_line = term;
cmd_line.push(" -e ");
cmd_line.push(command);
if let Ok(info) = AppInfo::create_from_commandline(cmd_line, None, AppInfoCreateFlags::NONE) {
info.launch(&[], Some(&context)).expect("Error while launching terminal app");
return;
}
}
if info.property("filename").ok().and_then(|p| p.get::<GString>().ok())
.and_then(|s| parse_entry(s.to_string()).ok())
.and_then(|e| e.section("Desktop Entry").attr("Terminal").map(|t| t == "1" || t == "true"))
.unwrap_or_default() {

let command = (match info.commandline() {
Some(c) => c,
_ => info.executable()
}).as_os_str().quote_single();

let commandline = if let Some(term) = term_command {
OsStr::new(term).replace("{}", command)
} else if let Some(mut term) = std::env::var_os("TERMINAL") {
term.push(" -e ");
term.push(command);
term
} else {
return;
};

let info = AppInfo::create_from_commandline(commandline, None, AppInfoCreateFlags::NONE)
.expect("Failed to create AppInfo from commandline");
info.launch(&[], Some(&context)).expect("Error while launching terminal app");
} else {
let future = info.launch_uris_async_future(&[], Some(&context));
MainContext::default().block_on(future).expect("Error while launching app");
}

let future = info.launch_uris_async_future(&[], Some(&context));
MainContext::default().block_on(future).expect("Error while launching app");
}

#[macro_export]
Expand Down

0 comments on commit 24c7151

Please sign in to comment.