mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
feat(segment): add bazel
This commit is contained in:
parent
4fb6f64f8f
commit
8b0854a424
|
@ -104,6 +104,8 @@ const (
|
|||
AZFUNC SegmentType = "azfunc"
|
||||
// BATTERY writes the battery percentage
|
||||
BATTERY SegmentType = "battery"
|
||||
// BAZEL writes the bazel version
|
||||
BAZEL SegmentType = "bazel"
|
||||
// Brewfather segment
|
||||
BREWFATHER SegmentType = "brewfather"
|
||||
// Buf segment writes the active buf version
|
||||
|
@ -265,6 +267,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
|
|||
AZ: func() SegmentWriter { return &segments.Az{} },
|
||||
AZFUNC: func() SegmentWriter { return &segments.AzFunc{} },
|
||||
BATTERY: func() SegmentWriter { return &segments.Battery{} },
|
||||
BAZEL: func() SegmentWriter { return &segments.Bazel{} },
|
||||
BREWFATHER: func() SegmentWriter { return &segments.Brewfather{} },
|
||||
BUF: func() SegmentWriter { return &segments.Buf{} },
|
||||
CARBONINTENSITY: func() SegmentWriter { return &segments.CarbonIntensity{} },
|
||||
|
|
44
src/segments/bazel.go
Normal file
44
src/segments/bazel.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package segments
|
||||
|
||||
import (
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
)
|
||||
|
||||
type Bazel struct {
|
||||
language
|
||||
|
||||
Icon string
|
||||
}
|
||||
|
||||
const (
|
||||
// Bazel's icon
|
||||
Icon properties.Property = "icon"
|
||||
)
|
||||
|
||||
func (c *Bazel) Template() string {
|
||||
return " {{ if .Error }}{{ .Icon }} {{ .Error }}{{ else }}{{ url .Icon .URL }} {{ .Full }}{{ end }} "
|
||||
}
|
||||
|
||||
func (c *Bazel) Init(props properties.Properties, env platform.Environment) {
|
||||
c.language = language{
|
||||
env: env,
|
||||
props: props,
|
||||
extensions: []string{"*.bazel", "*.bzl", "BUILD", "WORKSPACE", ".bazelrc", ".bazelversion"},
|
||||
folders: []string{"bazel-bin", "bazel-out", "bazel-testlogs"},
|
||||
commands: []*cmd{
|
||||
{
|
||||
executable: "bazel",
|
||||
args: []string{"--version"},
|
||||
regex: `bazel (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
|
||||
},
|
||||
},
|
||||
// Use the correct URL for Bazel 6.x. it does not have the docs subdomain.
|
||||
versionURLTemplate: "https://{{ if ne .Major \"6\" }}docs.{{ end }}bazel.build/versions/{{ .Major }}.{{ .Minor }}.{{ .Patch }}",
|
||||
}
|
||||
c.Icon = props.GetString(Icon, "\ue63a")
|
||||
}
|
||||
|
||||
func (c *Bazel) Enabled() bool {
|
||||
return c.language.Enabled()
|
||||
}
|
38
src/segments/bazel_test.go
Normal file
38
src/segments/bazel_test.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package segments
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const icon = "\ue63a"
|
||||
|
||||
func TestBazel(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
ExpectedString string
|
||||
Version string
|
||||
Template string
|
||||
}{
|
||||
{Case: "bazel 6.4.0", ExpectedString: fmt.Sprintf("«%s»(https://bazel.build/versions/6.4.0) 6.4.0", icon), Version: "bazel 6.4.0", Template: ""},
|
||||
{Case: "bazel 10.11.12", ExpectedString: fmt.Sprintf("«%s»(https://docs.bazel.build/versions/3.7.0) 3.7.0", icon), Version: "bazel 3.7.0"},
|
||||
{Case: "", ExpectedString: fmt.Sprintf("%s err parsing info from bazel with", icon), Version: ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
params := &mockedLanguageParams{
|
||||
cmd: "bazel",
|
||||
versionParam: "--version",
|
||||
versionOutput: tc.Version,
|
||||
extension: "*.bazel",
|
||||
}
|
||||
env, props := getMockedLanguageEnv(params)
|
||||
props[Icon] = icon
|
||||
b := &Bazel{}
|
||||
b.Init(props, env)
|
||||
failMsg := fmt.Sprintf("Failed in case: %s", tc.Case)
|
||||
assert.True(t, b.Enabled(), failMsg)
|
||||
assert.Equal(t, tc.ExpectedString, renderTemplate(env, b.Template(), b), failMsg)
|
||||
}
|
||||
}
|
|
@ -609,6 +609,46 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
"type": {
|
||||
"const": "bazel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"title": "Bazel Segment",
|
||||
"description": "https://ohmyposh.dev/docs/segments/bazel",
|
||||
"properties": {
|
||||
"properties": {
|
||||
"properties": {
|
||||
"home_enabled": {
|
||||
"$ref": "#/definitions/home_enabled"
|
||||
},
|
||||
"fetch_version": {
|
||||
"$ref": "#/definitions/fetch_version"
|
||||
},
|
||||
"display_mode": {
|
||||
"$ref": "#/definitions/display_mode"
|
||||
},
|
||||
"missing_command_text": {
|
||||
"$ref": "#/definitions/missing_command_text"
|
||||
},
|
||||
"version_url_template": {
|
||||
"$ref": "#/definitions/version_url_template"
|
||||
},
|
||||
"icon": {
|
||||
"type": "string",
|
||||
"title": "Icon",
|
||||
"description": "The icon representing Bazel's logo",
|
||||
"default": "\ue63a"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
|
|
58
website/docs/segments/bazel.mdx
Normal file
58
website/docs/segments/bazel.mdx
Normal file
|
@ -0,0 +1,58 @@
|
|||
---
|
||||
id: bazel
|
||||
title: Bazel
|
||||
sidebar_label: Bazel
|
||||
---
|
||||
|
||||
## What
|
||||
|
||||
Display the currently active [Bazel][bazel-github] version.
|
||||
|
||||
## Sample Configuration
|
||||
|
||||
import Config from "@site/src/components/Config.js";
|
||||
|
||||
<Config data={{
|
||||
"type": "bazel",
|
||||
"style": "powerline",
|
||||
"powerline_symbol": "\uE0B0",
|
||||
"foreground": "#ffffff",
|
||||
"background": "#43a047",
|
||||
}}/>
|
||||
|
||||
## Properties
|
||||
|
||||
| Name | Type | Description |
|
||||
| ---------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `home_enabled` | `boolean` | display the segment in the HOME folder or not - defaults to `false` |
|
||||
| `fetch_version` | `boolean` | display the Bazel version - defaults to `true` |
|
||||
| `missing_command_text` | `string` | text to display when the command is missing - defaults to empty |
|
||||
| `display_mode` | `string` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when `*.bazel`, `*.bzl`, `.bazelrc`, `.bazelversion`, `BUILD` or `WORKSPACE` files or any of Bazel's output folders are present (**default**)</li></ul> |
|
||||
| `version_url_template` | `string` | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info documentation |
|
||||
| `icon` | `string` | the icon for the segment - defaults to `"\ue63a"` |
|
||||
|
||||
## Template ([info][templates])
|
||||
|
||||
:::note default template
|
||||
|
||||
```template
|
||||
{{ if .Error }}{{ .Icon }} {{ .Error }}{{ else }}{{ url .Icon .URL }} {{ .Full }}{{ end }}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
### Properties
|
||||
|
||||
| Name | Type | Description |
|
||||
| -------- | -------- | -------------------------------------------------- |
|
||||
| `.Full` | `string` | the full version |
|
||||
| `.Major` | `string` | major number |
|
||||
| `.Minor` | `string` | minor number |
|
||||
| `.Patch` | `string` | patch number |
|
||||
| `.URL` | `string` | URL of the version info / release notes |
|
||||
| `.Error` | `string` | error encountered when fetching the version string |
|
||||
| `.Icon` | `string` | the icon representing Bazel's logo |
|
||||
|
||||
[bazel-github]: https://github.com/bazelbuild/bazel
|
||||
[go-text-template]: https://golang.org/pkg/text/template/
|
||||
[templates]: /docs/configuration/templates
|
|
@ -57,6 +57,7 @@ module.exports = {
|
|||
"segments/az",
|
||||
"segments/azfunc",
|
||||
"segments/battery",
|
||||
"segments/bazel",
|
||||
"segments/brewfather",
|
||||
"segments/buf",
|
||||
"segments/carbonintensity",
|
||||
|
|
Loading…
Reference in a new issue