aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2021-04-18 23:44:03 +0200
committerAndreas Grois <andi@grois.info>2021-04-18 23:47:03 +0200
commit48068a3fae794bac7e3c9a674d4a2d314a3d9349 (patch)
tree339ae1977f059911dbf014aceb95cdf52766b163
parent99c3480323f2df0c1cd455e2e03e832c1196050a (diff)
Add commandline handling and hardcoded default paths.
-rw-r--r--Cargo.lock273
-rw-r--r--swaystatus/Cargo.toml2
-rw-r--r--swaystatus/src/commandline/mod.rs83
-rw-r--r--swaystatus/src/main.rs26
-rw-r--r--swaystatus/src/signalhandler/mod.rs4
5 files changed, 374 insertions, 14 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0970f40..206dd2b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -10,18 +10,58 @@ dependencies = [
]
[[package]]
+name = "arrayref"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
+
+[[package]]
+name = "arrayvec"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
+name = "base64"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
+
+[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
+name = "blake2b_simd"
+version = "0.5.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587"
+dependencies = [
+ "arrayref",
+ "arrayvec",
+ "constant_time_eq",
+]
+
+[[package]]
name = "block"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -54,6 +94,44 @@ dependencies = [
]
[[package]]
+name = "clap"
+version = "3.0.0-beta.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142"
+dependencies = [
+ "atty",
+ "bitflags",
+ "clap_derive",
+ "indexmap",
+ "lazy_static",
+ "os_str_bytes",
+ "strsim",
+ "termcolor",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "clap_derive"
+version = "3.0.0-beta.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1"
+dependencies = [
+ "heck",
+ "proc-macro-error",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "constant_time_eq"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
+
+[[package]]
name = "crossbeam-utils"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -65,6 +143,26 @@ dependencies = [
]
[[package]]
+name = "dirs"
+version = "3.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff"
+dependencies = [
+ "dirs-sys",
+]
+
+[[package]]
+name = "dirs-sys"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
+dependencies = [
+ "libc",
+ "redox_users",
+ "winapi",
+]
+
+[[package]]
name = "erased-serde"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -75,13 +173,24 @@ dependencies = [
[[package]]
name = "getrandom"
+version = "0.1.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi 0.9.0+wasi-snapshot-preview1",
+]
+
+[[package]]
+name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
- "wasi",
+ "wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
@@ -105,6 +214,40 @@ dependencies = [
]
[[package]]
+name = "hashbrown"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
+
+[[package]]
+name = "heck"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
+dependencies = [
+ "unicode-segmentation",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "indexmap"
+version = "1.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3"
+dependencies = [
+ "autocfg",
+ "hashbrown",
+]
+
+[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -203,12 +346,42 @@ dependencies = [
]
[[package]]
+name = "os_str_bytes"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
+
+[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
+name = "proc-macro-error"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
+dependencies = [
+ "proc-macro-error-attr",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro-error-attr"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "version_check",
+]
+
+[[package]]
name = "proc-macro2"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -254,7 +427,7 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
- "getrandom",
+ "getrandom 0.2.2",
]
[[package]]
@@ -268,6 +441,12 @@ dependencies = [
[[package]]
name = "redox_syscall"
+version = "0.1.57"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
+
+[[package]]
+name = "redox_syscall"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8270314b5ccceb518e7e578952f0b72b88222d02e8f77f5ecf7abbb673539041"
@@ -276,6 +455,17 @@ dependencies = [
]
[[package]]
+name = "redox_users"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
+dependencies = [
+ "getrandom 0.1.16",
+ "redox_syscall 0.1.57",
+ "rust-argon2",
+]
+
+[[package]]
name = "regex"
version = "1.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -302,6 +492,18 @@ dependencies = [
]
[[package]]
+name = "rust-argon2"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
+dependencies = [
+ "base64",
+ "blake2b_simd",
+ "constant_time_eq",
+ "crossbeam-utils",
+]
+
+[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -365,10 +567,18 @@ dependencies = [
]
[[package]]
+name = "strsim"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
+
+[[package]]
name = "swaystatus"
version = "0.1.0"
dependencies = [
+ "clap",
"crossbeam-utils",
+ "dirs",
"erased-serde",
"gettext-rs",
"libloading",
@@ -417,12 +627,30 @@ dependencies = [
"cfg-if",
"libc",
"rand",
- "redox_syscall",
+ "redox_syscall 0.2.6",
"remove_dir_all",
"winapi",
]
[[package]]
+name = "termcolor"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
+name = "textwrap"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -442,12 +670,42 @@ dependencies = [
]
[[package]]
+name = "unicode-segmentation"
+version = "1.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
+
+[[package]]
+name = "unicode-width"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
+
+[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "version_check"
+version = "0.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
+
+[[package]]
+name = "wasi"
+version = "0.9.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
+
+[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -470,6 +728,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/swaystatus/Cargo.toml b/swaystatus/Cargo.toml
index 20677f6..4728f34 100644
--- a/swaystatus/Cargo.toml
+++ b/swaystatus/Cargo.toml
@@ -15,6 +15,8 @@ erased-serde = "0.3"
toml = "0.5"
libloading = "0.7"
signal-hook = { version = "0.3", default-features = false, features = ["iterator"]}
+clap = "3.0.0-beta.2"
+dirs = "3.0"
swaystatus-plugin = { path = '../swaystatus-plugin', version = '*'}
diff --git a/swaystatus/src/commandline/mod.rs b/swaystatus/src/commandline/mod.rs
new file mode 100644
index 0000000..efa66dc
--- /dev/null
+++ b/swaystatus/src/commandline/mod.rs
@@ -0,0 +1,83 @@
+extern crate clap;
+extern crate dirs;
+use clap::*;
+use std::path;
+use gettextrs::*;
+
+pub struct CommandlineParameters {
+ pub config_file : path::PathBuf,
+ pub plugin_folder : path::PathBuf,
+ pub print_sample_config : bool
+}
+
+/// Gets the config and plugin paths. Either from command line or from hardcoded defaults.
+pub fn parse_commandline() -> CommandlineParameters {
+ let matches = App::new(crate_name!())
+ .version(crate_version!())
+ .author(crate_authors!())
+ .about(&*gettext("A simple status text app, inspired by i3status"))
+ .help_heading(&*gettext("Arguments"))
+ .arg(
+ Arg::new("config")
+ .short('c')
+ .long("config")
+ .value_name(&*gettext("FILE"))
+ .about(&*gettext("Path to the configuration file"))
+ .takes_value(true))
+ .arg(
+ Arg::new("plugins")
+ .short('p')
+ .long("plugins")
+ .value_name(&*gettext("FOLDER"))
+ .about(&*gettext("Directory from which the plugins should be loaded"))
+ .takes_value(true))
+ .arg(
+ Arg::new("sampleconfig")
+ .long("print-sample-config")
+ .about(&*gettext("Prints a sample config file. Beware that the contents of the sample file depend on the loaded plugins, so don't forget to supply the plugins parameter as needed."))
+ .takes_value(false))
+ .after_help(&*gettext!("If no config path is given, the code looks for the \"swaystatus/config\" file in your XDG config folder (typically \"$HOME/.config/\"). If that lookup fails, loading of \"/etc/swaystatus/config\" is attempted. Similarly, if no plugin folder is given, first the existence of a folder named \"$HOME/.local/lib/swaystatus\" is checked. If this folder does not exist, a default path set at compile time is used, which in your case is \"{}\"." , get_hardcoded_default_library_path()))
+ .help_template(&*gettext("\
+{before-help}{bin} {version}\n\
+{author-section}\
+{about-section}\n\
+USAGE\n {usage}\n\
+\n\
+{all-args}{after-help}")).get_matches();
+
+
+ let config_file = matches.value_of("config").map(path::PathBuf::from).unwrap_or_else(get_default_config);
+ let plugin_folder = matches.value_of("plugins").map(path::PathBuf::from).unwrap_or_else(get_default_plugin_directory);
+ let print_sample_config = matches.is_present("sampleconfig");
+ CommandlineParameters { config_file, plugin_folder , print_sample_config}
+}
+
+/// Searches for the config file in XDG paths. If not found there, instead the
+/// /etc/swaystatus/config path is returned.
+fn get_default_config() -> path::PathBuf {
+ if let Some(mut xdg) = dirs::config_dir() {
+ xdg.push("swaystatus/config");
+ if xdg.exists() {
+ return xdg;
+ }
+ }
+ //no XDG data dir config file found.
+ path::PathBuf::from("/etc/swaystatus/config")
+}
+
+fn get_hardcoded_default_library_path() -> &'static str {
+ option_env!("DEFAULT_PLUGIN_DIR").unwrap_or("/usr/lib/swaystatus/")
+}
+
+/// Searches for plugins. First checks the user's ~/.local/lib/swaystatus/ folder, then the folder
+/// passed as build parameter (DEFAULT_PLUGIN_DIR)
+fn get_default_plugin_directory() -> path::PathBuf {
+ if let Some(mut user_folder) = dirs::home_dir() {
+ user_folder.push(".local/lib/swaystatus/");
+ if user_folder.is_dir() {
+ return user_folder;
+ }
+ }
+
+ path::PathBuf::from(get_hardcoded_default_library_path())
+}
diff --git a/swaystatus/src/main.rs b/swaystatus/src/main.rs
index dc9213c..90d861d 100644
--- a/swaystatus/src/main.rs
+++ b/swaystatus/src/main.rs
@@ -3,6 +3,7 @@ mod config;
mod plugin_database;
mod communication;
mod signalhandler;
+mod commandline;
extern crate gettextrs;
use gettextrs::*;
@@ -16,22 +17,28 @@ fn main() {
if let Err(_e) = TextDomain::new("swaystatus").prepend("target").init() {
eprintln!("Localization could not be loaded. Will use English instead.");
}
- //TODO: Read those from command line
- let plugin_path = std::path::Path::new("/home/andi/Dokumente/Rust-Projects/swaystatus/target/debug/");
- let config_path = std::path::Path::new("/home/andi/Dokumente/Rust-Projects/swaystatus/testconfig");
- while !core_loop(plugin_path, config_path) {}
+ let commandline_parameters = commandline::parse_commandline();
+ while !core_loop(&commandline_parameters.plugin_folder, &commandline_parameters.config_file, commandline_parameters.print_sample_config) {}
}
/// Actually the main() function. Factored out so we can restart without actually restaring.
/// Because some people might expect that SIGHUP triggers a reload, and it's trivial to implement.
-fn core_loop(plugin_path : &std::path::Path, config_path : &std::path::Path) -> bool {
+fn core_loop(plugin_path : &std::path::Path, config_path : &std::path::Path, print_config : bool) -> bool {
//Read plugins first (needed for config deserialization, given the config files has
//plugin config as well...
- let libraries = plugin_database::Libraries::load_from_folder(plugin_path).unwrap();
+ let libraries = match plugin_database::Libraries::load_from_folder(plugin_path) {
+ Ok(x) => x,
+ Err(e) => {
+ eprintln!("{} {}", gettext!("Tried to load plugins from folder \"{}\", but failed. You might want to set a plugin directory on the command line. The actual error was:", plugin_path.display()), e);
+ return true;
+ }
+ };
let plugins = plugin_database::PluginDatabase::new(&libraries);
- //TODO: Remove once command line handling is implemented.
- //config::SwaystatusConfig::print_sample_config(&plugins);
+ if print_config {
+ config::SwaystatusConfig::print_sample_config(&plugins);
+ return true;
+ }
let (elements, main_config) = match config::SwaystatusConfig::read_config(config_path, &plugins) {
@@ -99,7 +106,8 @@ fn core_loop(plugin_path : &std::path::Path, config_path : &std::path::Path) ->
//print a general error here.
eprintln!("{}", gettext("At least one of the plugins panicked. For details please check the (hopefully existing) previous error messages."));
}
- return !should_restart;
+
+ !should_restart
}
//-----------------------------------------------------------------------------
diff --git a/swaystatus/src/signalhandler/mod.rs b/swaystatus/src/signalhandler/mod.rs
index 0f8696c..20a91e7 100644
--- a/swaystatus/src/signalhandler/mod.rs
+++ b/swaystatus/src/signalhandler/mod.rs
@@ -16,7 +16,7 @@ pub fn handle_signals(scope : &Scope, sender : mpsc::Sender<communication::Messa
signal::SIGPIPE, //quit, because nobody's listening
signal::SIGHUP, //quit, but send the Reload message instead of the Quit one.
signal::SIGUSR1, //trigger a refresh. The ONLY one upon which we dont break the loop!
- ]).expect(&gettextrs::gettext("Failed to register signal handler. Since without signal handler there's no proper way to cleanly exit any plugins, we bail now."));
+ ]).unwrap_or_else(|_| {panic!(gettextrs::gettext("Failed to register signal handler. Since without signal handler there's no proper way to cleanly exit any plugins, we bail now."))});
scope.spawn(move |_| {
for signal in &mut signals {
@@ -31,5 +31,5 @@ pub fn handle_signals(scope : &Scope, sender : mpsc::Sender<communication::Messa
}
fn send(sender : &mpsc::Sender<communication::Message>, message : communication::InternalMessage) {
- sender.send(communication::Message::Internal(message)).expect(&gettextrs::gettext("Message handler failed to send a message to main thread. This is supposed to be impossible. In any case it's a critical error."));
+ sender.send(communication::Message::Internal(message)).unwrap_or_else(|_| {panic!(gettextrs::gettext("Message handler failed to send a message to main thread. This is supposed to be impossible. In any case it's a critical error."))});
}