From 81d6f6aa98e420a65fa5ec717dc530c0b2e54140 Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Sun, 14 Feb 2021 18:14:55 +0100 Subject: [PATCH] feat(crystal): Configure when the module is shown (#2310) * feat(crystal): Configure when the module is shown This makes it possible to configure when the crystal 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. * Update docs/config/README.md Co-authored-by: David Knaack Co-authored-by: David Knaack --- docs/config/README.md | 17 ++++++++++------- src/configs/crystal.rs | 6 ++++++ src/modules/crystal.rs | 15 ++++++--------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 1a5958e7..68b80138 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -569,19 +569,22 @@ format = "[$symbol$environment](dimmed green) " ## Crystal The `crystal` module shows the currently installed version of Crystal. -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 `shard.yml` file - The current directory contains a `.cr` file ### Options -| Option | Default | Description | -| ---------- | ------------------------------------ | --------------------------------------------------------- | -| `symbol` | `"🔮 "` | The symbol used before displaying the version of crystal. | -| `style` | `"bold red"` | The style for the module. | -| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | -| `disabled` | `false` | Disables the `crystal` module. | +| Option | Default | Description | +| ------------------- | ------------------------------------ | --------------------------------------------------------- | +| `symbol` | `"🔮 "` | The symbol used before displaying the version of crystal. | +| `style` | `"bold red"` | The style for the module. | +| `detect_extensions` | `["cr"]` | Which extensions should trigger this module. | +| `detect_files` | `["shard.yml"]` | Which filenames should trigger this module. | +| `detect_folders` | `[]` | Which folders should trigger this module. | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `disabled` | `false` | Disables the `crystal` module. | ### Variables diff --git a/src/configs/crystal.rs b/src/configs/crystal.rs index 248f0ec4..d135b9e5 100644 --- a/src/configs/crystal.rs +++ b/src/configs/crystal.rs @@ -8,6 +8,9 @@ pub struct CrystalConfig<'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 CrystalConfig<'a> { @@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for CrystalConfig<'a> { symbol: "🔮 ", style: "bold red", disabled: false, + detect_extensions: vec!["cr"], + detect_files: vec!["shard.yml"], + detect_folders: vec![], } } } diff --git a/src/modules/crystal.rs b/src/modules/crystal.rs index 9ba121eb..8049580f 100644 --- a/src/modules/crystal.rs +++ b/src/modules/crystal.rs @@ -4,24 +4,21 @@ use crate::configs::crystal::CrystalConfig; use crate::formatter::StringFormatter; /// Creates a module with the current Crystal version -/// -/// Will display the Crystal version if any of the following criteria are met: -/// - Current directory contains a `.cr` file -/// - Current directory contains a `shard.yml` file pub fn module<'a>(context: &'a Context) -> Option> { + let mut module = context.new_module("crystal"); + let config: CrystalConfig = CrystalConfig::try_load(module.config); + let is_crystal_project = context .try_begin_scan()? - .set_files(&["shard.yml"]) - .set_extensions(&["cr"]) + .set_files(&config.detect_files) + .set_extensions(&config.detect_extensions) + .set_folders(&config.detect_folders) .is_match(); if !is_crystal_project { return None; } - let mut module = context.new_module("crystal"); - let config: CrystalConfig = CrystalConfig::try_load(module.config); - let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|variable, _| match variable {