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
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
@ -2421,9 +2421,12 @@ 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 |
| `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. |

View File

@ -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![],
}
}
}

View File

@ -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<Module<'a>> {
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 {