From 1a6c625521510fee059bec168e73a49aef834c8c Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Sun, 21 Feb 2021 19:56:48 +0100 Subject: [PATCH] feat(kotlin): Configure when the module is shown (#2359) This makes it possible to configure when the kotlin module is shown based on the contents of a directory. --- docs/config/README.md | 19 +++++++++++-------- src/configs/kotlin.rs | 6 ++++++ src/modules/kotlin.rs | 12 ++++++------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index d456abd8b..6d54249c2 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1472,19 +1472,22 @@ symbol = "∴ " ## Kotlin The `kotlin` module shows the currently installed version of Kotlin. -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 `.kt` or a `.kts` file ### Options -| Option | Default | Description | -| --------------- | ------------------------------------ | ----------------------------------------------------------------------------- | -| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | -| `symbol` | `"🅺 "` | A format string representing the symbol of Kotlin. | -| `style` | `"bold blue"` | The style for the module. | -| `kotlin_binary` | `"kotlin"` | Configures the kotlin binary that Starship executes when getting the version. | -| `disabled` | `false` | Disables the `kotlin` module. | +| Option | Default | Description | +| ------------------- | ------------------------------------ | ----------------------------------------------------------------------------- | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `detect_extensions` | `["kt", "kts"]` | Which extensions should trigger this module. | +| `detect_files` | `[]` | Which filenames should trigger this module. | +| `detect_folders` | `[]` | Which folders should trigger this modules. | +| `symbol` | `"🅺 "` | A format string representing the symbol of Kotlin. | +| `style` | `"bold blue"` | The style for the module. | +| `kotlin_binary` | `"kotlin"` | Configures the kotlin binary that Starship executes when getting the version. | +| `disabled` | `false` | Disables the `kotlin` module. | ### Variables diff --git a/src/configs/kotlin.rs b/src/configs/kotlin.rs index 33763a9fd..3ff15b330 100644 --- a/src/configs/kotlin.rs +++ b/src/configs/kotlin.rs @@ -9,6 +9,9 @@ pub struct KotlinConfig<'a> { pub style: &'a str, pub kotlin_binary: &'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 KotlinConfig<'a> { @@ -19,6 +22,9 @@ impl<'a> RootModuleConfig<'a> for KotlinConfig<'a> { style: "bold blue", kotlin_binary: "kotlin", disabled: false, + detect_extensions: vec!["kt", "kts"], + detect_files: vec![], + detect_folders: vec![], } } } diff --git a/src/modules/kotlin.rs b/src/modules/kotlin.rs index bebfb03aa..94015fa30 100644 --- a/src/modules/kotlin.rs +++ b/src/modules/kotlin.rs @@ -7,21 +7,21 @@ use regex::Regex; const KOTLIN_VERSION_PATTERN: &str = "(?P[\\d\\.]+[\\d\\.]+[\\d\\.]+)"; /// Creates a module with the current Kotlin version -/// -/// Will display the Kotlin version if any of the following criteria are met: -/// - Current directory contains a file with a `.kt` or `.kts` extension pub fn module<'a>(context: &'a Context) -> Option> { + let mut module = context.new_module("kotlin"); + let config = KotlinConfig::try_load(module.config); + let is_kotlin_project = context .try_begin_scan()? - .set_extensions(&["kt", "kts"]) + .set_files(&config.detect_files) + .set_extensions(&config.detect_extensions) + .set_folders(&config.detect_folders) .is_match(); if !is_kotlin_project { return None; } - let mut module = context.new_module("kotlin"); - let config = KotlinConfig::try_load(module.config); let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var {