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.
This commit is contained in:
David Knaack 2021-02-21 18:01:31 +01:00 committed by GitHub
parent a499f30157
commit 509767adc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 15 deletions

View File

@ -2412,7 +2412,7 @@ disabled = false
## Swift ## 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 module will be shown if any of the following conditions are met:
- The current directory contains a `Package.swift` file - 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 ### Options
| Option | Default | Description | | Option | Default | Description |
| ---------- | ------------------------------------ | ------------------------------------------------ | | ------------------- | ------------------------------------ | ------------------------------------------------ |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | | `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"🐦 "` | A format string representing the symbol of Swift | | `symbol` | `"🐦 "` | A format string representing the symbol of Swift |
| `style` | `"bold 202"` | The style for the module. | | `detect_extensions` | `["swift"]` | Which extensions should trigger this moudle. |
| `disabled` | `false` | Disables the `swift` module. | | `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 ### Variables

View File

@ -8,6 +8,9 @@ pub struct SwiftConfig<'a> {
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
pub disabled: bool, 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> { impl<'a> RootModuleConfig<'a> for SwiftConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for SwiftConfig<'a> {
symbol: "🐦 ", symbol: "🐦 ",
style: "bold 202", style: "bold 202",
disabled: false, disabled: false,
detect_extensions: vec!["swift"],
detect_files: vec!["Package.swift"],
detect_folders: vec![],
} }
} }
} }

View File

@ -4,23 +4,21 @@ use crate::configs::swift::SwiftConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
/// Creates a module with the current Swift version /// 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<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("swift");
let config: SwiftConfig = SwiftConfig::try_load(module.config);
let is_swift_project = context let is_swift_project = context
.try_begin_scan()? .try_begin_scan()?
.set_extensions(&["swift"]) .set_files(&config.detect_files)
.set_folders(&config.detect_folders)
.set_extensions(&config.detect_extensions)
.is_match(); .is_match();
if !is_swift_project { if !is_swift_project {
return None; 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| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|var, _| match var { .map_meta(|var, _| match var {