From a499f30157bb231d8f39557cdf504e7c094b68aa Mon Sep 17 00:00:00 2001 From: David Knaack Date: Sun, 21 Feb 2021 18:01:17 +0100 Subject: [PATCH] feat(rust): Configure when the module is shown (#2350) This makes it possible to configure when the rust module is shown based on the contents of a directory. --- docs/config/README.md | 17 ++++++++++------- src/configs/rust.rs | 6 ++++++ src/modules/rust.rs | 14 ++++++-------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index db61963c..7578e42b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2202,7 +2202,7 @@ symbol = "🔺 " ## Rust -The `rust` module shows the currently installed version of Rust. +By default the `rust` module shows the currently installed version of Rust. The module will be shown if any of the following conditions are met: - The current directory contains a `Cargo.toml` file @@ -2210,12 +2210,15 @@ The module will be shown if any of the following conditions are met: ### Options -| Option | Default | Description | -| ---------- | ---------------------------------- | ----------------------------------------------- | -| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | -| `symbol` | `"🦀 "` | A format string representing the symbol of Rust | -| `style` | `"bold red"` | The style for the module. | -| `disabled` | `false` | Disables the `rust` module. | +| Option | Default | Description | +| ------------------- | ------------------------------------ | ----------------------------------------------- | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `symbol` | `"🦀 "` | A format string representing the symbol of Rust | +| `detect_extensions` | `["rs"]` | Which extensions should trigger this module. | +| `detect_files` | `["Cargo.toml"]` | Which filenames should trigger this module. | +| `detect_folders` | `[]` | Which folders should trigger this module. | +| `style` | `"bold red"` | The style for the module. | +| `disabled` | `false` | Disables the `rust` module. | ### Variables diff --git a/src/configs/rust.rs b/src/configs/rust.rs index 974c5ede..1b5657c3 100644 --- a/src/configs/rust.rs +++ b/src/configs/rust.rs @@ -8,6 +8,9 @@ pub struct RustConfig<'a> { pub symbol: &'a str, pub style: &'a str, pub disabled: bool, + pub detect_extensions: Vec<&'a str>, + pub detect_files: Vec<&'a str>, + pub detect_folders: Vec<&'a str>, } impl<'a> RootModuleConfig<'a> for RustConfig<'a> { @@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for RustConfig<'a> { symbol: "🦀 ", style: "bold red", disabled: false, + detect_extensions: vec!["rs"], + detect_files: vec!["Cargo.toml"], + detect_folders: vec![], } } } diff --git a/src/modules/rust.rs b/src/modules/rust.rs index d33fe747..3f10ee19 100644 --- a/src/modules/rust.rs +++ b/src/modules/rust.rs @@ -10,23 +10,21 @@ use crate::configs::rust::RustConfig; use crate::formatter::StringFormatter; /// Creates a module with the current Rust version -/// -/// Will display the Rust version if any of the following criteria are met: -/// - Current directory contains a file with a `.rs` extension -/// - Current directory contains a `Cargo.toml` file pub fn module<'a>(context: &'a Context) -> Option> { + let mut module = context.new_module("rust"); + let config = RustConfig::try_load(module.config); + let is_rs_project = context .try_begin_scan()? - .set_files(&["Cargo.toml"]) - .set_extensions(&["rs"]) + .set_files(&config.detect_files) + .set_extensions(&config.detect_extensions) + .set_folders(&config.detect_folders) .is_match(); if !is_rs_project { return None; } - let mut module = context.new_module("rust"); - let config = RustConfig::try_load(module.config); let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var {