From d0951db35aaa46b21a604a08a96c878a4da72c87 Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Sun, 14 Feb 2021 22:21:52 +0100 Subject: [PATCH] feat(dart): Configure when the module is shown (#2312) * feat(dart): Configure when the module is shown This makes it possible to configure when the dart module is shown based on the contents of a directory. This should make it possible to be a lot more granular when configuring the module. * docs(dart): add missing detected files * removed invalid comment --- docs/config/README.md | 19 +++++++++++-------- src/configs/dart.rs | 6 ++++++ src/modules/dart.rs | 17 ++++++----------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index c195c8d5..9c785804 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -608,20 +608,23 @@ format = "via [✨ $version](bold blue) " ## Dart The `dart` module shows the currently installed version of Dart. -The module will be shown if any of the following conditions are met: +By default the module will be shown if any of the following conditions are met: - The current directory contains a file with `.dart` extension - The current directory contains a `.dart_tool` directory -- The current directory contains a `pubspec.yaml` or `pubspec.lock` file +- The current directory contains a `pubspec.yaml`, `pubspec.yml` or `pubspec.lock` file ### Options -| Option | Default | Description | -| ---------- | ------------------------------------ | ----------------------------------------------- | -| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | -| `symbol` | `"🎯 "` | A format string representing the symbol of Dart | -| `style` | `"bold blue"` | The style for the module. | -| `disabled` | `false` | Disables the `dart` module. | +| Option | Default | Description | +| ------------------- | ------------------------------------------------- | ----------------------------------------------- | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `symbol` | `"🎯 "` | A format string representing the symbol of Dart | +| `detect_extensions` | `['dart']` | Which extensions should trigger this moudle. | +| `detect_files` | `["pubspec.yaml", "pubspec.yml", "pubspec.lock"]` | Which filenames should trigger this module. | +| `detect_folders` | `[".dart_tool"]` | Which folders should trigger this module. | +| `style` | `"bold blue"` | The style for the module. | +| `disabled` | `false` | Disables the `dart` module. | ### Variables diff --git a/src/configs/dart.rs b/src/configs/dart.rs index 6fafdc6b..cde1832f 100644 --- a/src/configs/dart.rs +++ b/src/configs/dart.rs @@ -8,6 +8,9 @@ pub struct DartConfig<'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 DartConfig<'a> { @@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for DartConfig<'a> { symbol: "🎯 ", style: "bold blue", disabled: false, + detect_extensions: vec!["dart"], + detect_files: vec!["pubspec.yaml", "pubspec.yml", "pubspec.lock"], + detect_folders: vec![".dart_tool"], } } } diff --git a/src/modules/dart.rs b/src/modules/dart.rs index 5206bd7a..00b4cf35 100644 --- a/src/modules/dart.rs +++ b/src/modules/dart.rs @@ -4,26 +4,21 @@ use crate::configs::dart::DartConfig; use crate::formatter::StringFormatter; /// Creates a module with the current Dart version -/// -/// Will display the Dart version if any of the following criteria are met: -/// - Current directory contains a file with `.dart` extension -/// - Current directory contains a `.dart_tool` directory -/// - Current directory contains a `pubspec.yaml`/`pubspec.yml` or `pubspec.lock` file pub fn module<'a>(context: &'a Context) -> Option> { + let mut module = context.new_module("dart"); + let config: DartConfig = DartConfig::try_load(module.config); + let is_dart_project = context .try_begin_scan()? - .set_extensions(&["dart"]) - .set_folders(&[".dart_tool"]) - .set_files(&["pubspec.yaml", "pubspec.yml", "pubspec.lock"]) + .set_files(&config.detect_files) + .set_extensions(&config.detect_extensions) + .set_folders(&config.detect_folders) .is_match(); if !is_dart_project { return None; } - let mut module = context.new_module("dart"); - let config: DartConfig = DartConfig::try_load(module.config); - let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var {