1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-05 04:47:58 +00:00

feat(lua): Configure when the module is shown (#2326)

This makes it possible to configure when the lua 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.
This commit is contained in:
Shu Kutsuzawa 2021-02-18 03:03:09 +09:00 committed by GitHub
parent 97fbfffd7e
commit 5e93160456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 18 deletions

View File

@ -1569,7 +1569,7 @@ disabled = true
## Lua
The `lua` module shows the currently installed version of Lua.
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 `.lua-version` file
- The current directory contains a `lua` directory
@ -1578,9 +1578,12 @@ 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 Lua. |
| `detect_extensions` | `["lua"]` | Which extensions should trigger this moudle. |
| `detect_files` | `[".lua-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `["lua"]` | Which folders should trigger this module. |
| `style` | `"bold blue"` | The style for the module. |
| `lua_binary` | `"lua"` | Configures the lua binary that Starship executes when getting the version. |
| `disabled` | `false` | Disables the `lua` module. |

View File

@ -9,6 +9,9 @@ pub struct LuaConfig<'a> {
pub style: &'a str,
pub lua_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 LuaConfig<'a> {
@ -19,6 +22,9 @@ impl<'a> RootModuleConfig<'a> for LuaConfig<'a> {
style: "bold blue",
lua_binary: "lua",
disabled: false,
detect_extensions: vec!["lua"],
detect_files: vec![".lua-version"],
detect_folders: vec!["lua"],
}
}
}

View File

@ -7,25 +7,21 @@ use regex::Regex;
const LUA_VERSION_PATERN: &str = "(?P<version>[\\d\\.]+[a-z\\-]*[1-9]*)[^\\s]*";
/// Creates a module with the current Lua version
///
/// Will display the Lua version if any of the following criteria are met:
/// - Current directory contains a `.lua-version` file
/// - Current directory contains a `lua` directory
/// - Current directory contains a file with the `.lua` extension
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("lua");
let config = LuaConfig::try_load(module.config);
let is_lua_project = context
.try_begin_scan()?
.set_files(&[".lua-version"])
.set_folders(&["lua"])
.set_extensions(&["lua"])
.set_files(&config.detect_files)
.set_folders(&config.detect_folders)
.set_extensions(&config.detect_extensions)
.is_match();
if !is_lua_project {
return None;
}
let mut module = context.new_module("lua");
let config = LuaConfig::try_load(module.config);
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {