diff options
author | Javier Goday <jgoday@gmail.com> | 2021-06-13 08:23:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-13 08:23:46 +0200 |
commit | fa3899719b473c35029722b2a7dbcea8ab474880 (patch) | |
tree | 4ca8703402ee83861f0e8dc078791d685ac3514a | |
parent | dc92d664e22f945645c3ac124a592285d5109894 (diff) |
feat: Add Operating System condition to custom commands (#2751)
* #2750: Add Operating System condition to custom commands
* update custom module config docs
* fix os field name in custom module
* Fix custom module false positives (when && os conditions)
* Custom module operation system: check unix family
* Custom module operation system: fix check unix family (use cfg!(unix))
* Update docs/config/README.md
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
-rw-r--r-- | docs/config/README.md | 4 | ||||
-rw-r--r-- | src/configs/custom.rs | 3 | ||||
-rw-r--r-- | src/modules/custom.rs | 7 |
3 files changed, 13 insertions, 1 deletions
diff --git a/docs/config/README.md b/docs/config/README.md index 8832230c8..67a685018 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -3024,6 +3024,7 @@ These modules will be shown if any of the following conditions are met: - The current directory contains a directory whose name is in `directories` - The current directory contains a file whose extension is in `extensions` - The `when` command returns 0 +- The current Operating System (std::env::consts::OS) matchs with `os` field if defined. ::: tip @@ -3060,7 +3061,8 @@ If you have an interesting example not covered there, feel free to share it ther | `symbol` | `""` | The symbol used before displaying the command output. | | `style` | `"bold green"` | The style for the module. | | `format` | `"[$symbol($output )]($style)"` | The format for the module. | -| `disabled` | `false` | Disables this `custom` module. | +| `disabled` | `false` | Disables this `custom` module. | +| `os` | | Operating System name on which the module will be shown (unix, linux, macos, windows, ... ) [See possible values](https://doc.rust-lang.org/std/env/consts/constant.OS.html). | ### Variables diff --git a/src/configs/custom.rs b/src/configs/custom.rs index bced89319..d1f758aa4 100644 --- a/src/configs/custom.rs +++ b/src/configs/custom.rs @@ -17,6 +17,8 @@ pub struct CustomConfig<'a> { pub files: Vec<&'a str>, pub extensions: Vec<&'a str>, pub directories: Vec<&'a str>, + #[serde(skip_serializing_if = "Option::is_none")] + pub os: Option<&'a str>, } impl<'a> Default for CustomConfig<'a> { @@ -33,6 +35,7 @@ impl<'a> Default for CustomConfig<'a> { files: Vec::default(), extensions: Vec::default(), directories: Vec::default(), + os: None, } } } diff --git a/src/modules/custom.rs b/src/modules/custom.rs index 03cf6d1b6..02c2f6f1c 100644 --- a/src/modules/custom.rs +++ b/src/modules/custom.rs @@ -1,3 +1,4 @@ +use std::env; use std::io::Write; use std::process::{Command, Output, Stdio}; use std::time::Instant; @@ -37,6 +38,12 @@ pub fn module<'a>(name: &str, context: &'a Context) -> Option<Module<'a>> { } } + if let Some(os) = config.os { + if os != env::consts::OS && !(os == "unix" && cfg!(unix)) { + return None; + } + } + let mut module = Module::new(name, config.description, Some(toml_config)); let parsed = StringFormatter::new(config.format).and_then(|formatter| { |