Skip to content

Commit

Permalink
feat(ruby): Configure when the module is shown (starship#2351)
Browse files Browse the repository at this point in the history
This makes it possible to configure when the ruby module is shown
based on the contents of a directory.
  • Loading branch information
davidkna authored Feb 21, 2021
1 parent c0a0e85 commit 9ba82e8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
17 changes: 10 additions & 7 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
6 changes: 6 additions & 0 deletions src/configs/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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![],
}
}
}
10 changes: 6 additions & 4 deletions src/modules/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9ba82e8

Please sign in to comment.