Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ocaml): Configure when the module is shown #2354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions docs/config/README.md
Expand Up @@ -1832,7 +1832,7 @@ format = "via [🤖 $version](bold green) "
## OCaml

The `ocaml` module shows the currently installed version of OCaml.
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 `.opam` extension or `_opam` directory
- The current directory contains a `esy.lock` directory
Expand All @@ -1843,12 +1843,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 string for the module. |
| `symbol` | `"🐫 "` | The symbol used before displaying the version of OCaml. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `ocaml` module. |
| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format string for the module. |
| `symbol` | `"🐫 "` | The symbol used before displaying the version of OCaml. |
| `detect_extensions` | `["opam", "ml", "mli", "re", "rei"]` | Which extensions should trigger this moudle. |
| `detect_files` | `["dune", "dune-project", "jbuild", "jbuild-ignore", ".merlin"]` | Which filenames should trigger this module. |
| `detect_folders` | `["_opam", "esy.lock"]` | Which folders should trigger this module. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `ocaml` module. |

### Variables

Expand Down
6 changes: 6 additions & 0 deletions src/configs/ocaml.rs
Expand Up @@ -8,6 +8,9 @@ pub struct OCamlConfig<'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 OCamlConfig<'a> {
Expand All @@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for OCamlConfig<'a> {
symbol: "🐫 ",
style: "bold yellow",
disabled: false,
detect_extensions: vec!["opam", "ml", "mli", "re", "rei"],
detect_files: vec!["dune", "dune-project", "jbuild", "jbuild-ignore", ".merlin"],
detect_folders: vec!["_opam", "esy.lock"],
}
}
}
19 changes: 5 additions & 14 deletions src/modules/ocaml.rs
Expand Up @@ -4,29 +4,20 @@ use crate::configs::ocaml::OCamlConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the current OCaml version
///
/// Will display the OCaml version if any of the following criteria are met:
/// - Current directory contains a file with `.opam` extension or `_opam` directory
/// - Current directory contains a `esy.lock` directory
/// - Current directory contains a `dune` or `dune-project` file
/// - Current directory contains a `jbuild` or `jbuild-ignore` file
/// - Current directory contains a `.merlin` file
/// - Current directory contains a file with `.ml`, `.mli`, `.re` or `.rei` extension
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("ocaml");
let config: OCamlConfig = OCamlConfig::try_load(module.config);
let is_ocaml_project = context
.try_begin_scan()?
.set_files(&["dune", "dune-project", "jbuild", "jbuild-ignore", ".merlin"])
.set_folders(&["_opam", "esy.lock"])
.set_extensions(&["opam", "ml", "mli", "re", "rei"])
.set_files(&config.detect_files)
.set_folders(&config.detect_folders)
.set_extensions(&config.detect_extensions)
.is_match();

if !is_ocaml_project {
return None;
}

let mut module = context.new_module("ocaml");
let config: OCamlConfig = OCamlConfig::try_load(module.config);

let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
Expand Down