1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-06-09 20:02:20 +00:00

feat(ruby): Configure when the module is shown (#2351)

This makes it possible to configure when the ruby module is shown
based on the contents of a directory.
This commit is contained in:
David Knaack 2021-02-21 18:01:01 +01:00 committed by GitHub
parent c0a0e85556
commit 9ba82e8d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 11 deletions

View File

@ -2162,7 +2162,7 @@ detect_extensions = []
## Ruby
The `ruby` module shows the currently installed version of Ruby.
By default the `ruby` module shows the currently installed version of Ruby.
The module will be shown if any of the following conditions are met:
- The current directory contains a `Gemfile` file
@ -2171,12 +2171,15 @@ 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 Ruby. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `ruby` module. |
| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------ |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"💎 "` | A format string representing the symbol of Ruby. |
| `detect_extensions` | `["rb"]` | Which extensions should trigger this module. |
| `detect_files` | `["Gemfile", ".ruby-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `ruby` module. |
### Variables

View File

@ -8,6 +8,9 @@ pub struct RubyConfig<'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 RubyConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for RubyConfig<'a> {
symbol: "💎 ",
style: "bold red",
disabled: false,
detect_extensions: vec!["rb"],
detect_files: vec!["Gemfile", ".ruby-version"],
detect_folders: vec![],
}
}
}

View File

@ -9,18 +9,20 @@ use crate::formatter::StringFormatter;
/// - Current directory contains a `.rb` file
/// - Current directory contains a `Gemfile` or `.ruby-version` file
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("ruby");
let config = RubyConfig::try_load(module.config);
let is_rb_project = context
.try_begin_scan()?
.set_files(&["Gemfile", ".ruby-version"])
.set_extensions(&["rb"])
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
if !is_rb_project {
return None;
}
let mut module = context.new_module("ruby");
let config = RubyConfig::try_load(module.config);
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {