1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-10 15:20:55 +00:00

feat(golang): Configure when the module is shown (#2325)

This makes it possible to configure when the golang 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 02:58:21 +09:00 committed by GitHub
parent 82c7fd6742
commit 97fbfffd7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 29 deletions

View File

@ -1233,7 +1233,7 @@ behind = "⇣${count}"
## Golang ## Golang
The `golang` module shows the currently installed version of Golang. The `golang` module shows the currently installed version of Golang.
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 `go.mod` file - The current directory contains a `go.mod` file
- The current directory contains a `go.sum` file - The current directory contains a `go.sum` file
@ -1247,9 +1247,12 @@ The module will be shown if any of the following conditions are met:
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ---------- | ---------------------------------- | ---------------------------------------------- | | -------------------- | ------------------------------------------------------------------------------ | ---------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | | `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"🐹 "` | A format string representing the symbol of Go. | | `symbol` | `"🐹 "` | A format string representing the symbol of Go. |
| `detect_extensions` | `["go"]` | Which extensions should trigger this moudle. |
| `detect_files` | `["go.mod", "go.sum", "glide.yaml", "Gopkg.yml", "Gopkg.lock", ".go-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `["Godeps"]` | Which folders should trigger this module. |
| `style` | `"bold cyan"` | The style for the module. | | `style` | `"bold cyan"` | The style for the module. |
| `disabled` | `false` | Disables the `golang` module. | | `disabled` | `false` | Disables the `golang` module. |

View File

@ -8,6 +8,9 @@ pub struct GoConfig<'a> {
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
pub disabled: bool, 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 GoConfig<'a> { impl<'a> RootModuleConfig<'a> for GoConfig<'a> {
@ -17,6 +20,16 @@ impl<'a> RootModuleConfig<'a> for GoConfig<'a> {
symbol: "🐹 ", symbol: "🐹 ",
style: "bold cyan", style: "bold cyan",
disabled: false, disabled: false,
detect_extensions: vec!["go"],
detect_files: vec![
"go.mod",
"go.sum",
"glide.yaml",
"Gopkg.yml",
"Gopkg.lock",
".go-version",
],
detect_folders: vec!["Godeps"],
} }
} }
} }

View File

@ -4,37 +4,20 @@ use crate::configs::go::GoConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
/// Creates a module with the current Go version /// Creates a module with the current Go version
///
/// Will display the Go version if any of the following criteria are met:
/// - Current directory contains a `go.mod` file
/// - Current directory contains a `go.sum` file
/// - Current directory contains a `glide.yaml` file
/// - Current directory contains a `Gopkg.yml` file
/// - Current directory contains a `Gopkg.lock` file
/// - Current directory contains a `.go-version` file
/// - Current directory contains a `Godeps` directory
/// - Current directory contains a file with the `.go` extension
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("golang");
let config = GoConfig::try_load(module.config);
let is_go_project = context let is_go_project = context
.try_begin_scan()? .try_begin_scan()?
.set_files(&[ .set_files(&config.detect_files)
"go.mod", .set_extensions(&config.detect_extensions)
"go.sum", .set_folders(&config.detect_folders)
"glide.yaml",
"Gopkg.yml",
"Gopkg.lock",
".go-version",
])
.set_extensions(&["go"])
.set_folders(&["Godeps"])
.is_match(); .is_match();
if !is_go_project { if !is_go_project {
return None; return None;
} }
let mut module = context.new_module("golang");
let config = GoConfig::try_load(module.config);
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|var, _| match var { .map_meta(|var, _| match var {