From 509767adc02c39f206e7f0df15920ddba1b2444e Mon Sep 17 00:00:00 2001 From: David Knaack Date: Sun, 21 Feb 2021 18:01:31 +0100 Subject: [PATCH] feat(swift): Configure when the module is shown (#2349) This makes it possible to configure when the swift module is shown based on the contents of a directory. --- docs/config/README.md | 17 ++++++++++------- src/configs/swift.rs | 6 ++++++ src/modules/swift.rs | 14 ++++++-------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 7578e42b..a952e9f1 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2412,7 +2412,7 @@ disabled = false ## Swift -The `swift` module shows the currently installed version of Swift. +By default the `swift` module shows the currently installed version of Swift. The module will be shown if any of the following conditions are met: - The current directory contains a `Package.swift` file @@ -2420,12 +2420,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 Swift | -| `style` | `"bold 202"` | The style for the module. | -| `disabled` | `false` | Disables the `swift` module. | +| Option | Default | Description | +| ------------------- | ------------------------------------ | ------------------------------------------------ | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `symbol` | `"🐦 "` | A format string representing the symbol of Swift | +| `detect_extensions` | `["swift"]` | Which extensions should trigger this moudle. | +| `detect_files` | `["Package.swift"]` | Which filenames should trigger this module. | +| `detect_folders` | `[]` | Which folders should trigger this module. | +| `style` | `"bold 202"` | The style for the module. | +| `disabled` | `false` | Disables the `swift` module. | ### Variables diff --git a/src/configs/swift.rs b/src/configs/swift.rs index 9bedc217..8e88bfb9 100644 --- a/src/configs/swift.rs +++ b/src/configs/swift.rs @@ -8,6 +8,9 @@ pub struct SwiftConfig<'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 SwiftConfig<'a> { @@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for SwiftConfig<'a> { symbol: "🐦 ", style: "bold 202", disabled: false, + detect_extensions: vec!["swift"], + detect_files: vec!["Package.swift"], + detect_folders: vec![], } } } diff --git a/src/modules/swift.rs b/src/modules/swift.rs index 1cf2cd30..5ff5d6f9 100644 --- a/src/modules/swift.rs +++ b/src/modules/swift.rs @@ -4,23 +4,21 @@ use crate::configs::swift::SwiftConfig; use crate::formatter::StringFormatter; /// Creates a module with the current Swift version -/// -/// Will display the Swift version if any of the following criteria are met: -/// - The current directory contains a `Package.swift` file -/// - The current directory contains a file with extension `.swift` pub fn module<'a>(context: &'a Context) -> Option> { + let mut module = context.new_module("swift"); + let config: SwiftConfig = SwiftConfig::try_load(module.config); + let is_swift_project = context .try_begin_scan()? - .set_extensions(&["swift"]) + .set_files(&config.detect_files) + .set_folders(&config.detect_folders) + .set_extensions(&config.detect_extensions) .is_match(); if !is_swift_project { return None; } - let mut module = context.new_module("swift"); - let config: SwiftConfig = SwiftConfig::try_load(module.config); - let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var {