Repository: JanDeDobbeleer/oh-my-posh
Pull request #3385 (2023-01-21 08:31:08)

segments
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added/updated (for bug fixes/features)

### Description

- feat: add buf segment
- fix(git): parse git@ url's correctly

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/engine/segment.go b/src/engine/segment.go
index c11b9e98..b33e8a04 100644
--- a/src/engine/segment.go
+++ b/src/engine/segment.go
@@ -107,6 +107,8 @@ const (
 	BATTERY SegmentType = "battery"
 	// Brewfather segment
 	BREWFATHER SegmentType = "brewfather"
+	// Buf segment writes the active buf version
+	BUF SegmentType = "buf"
 	// cds (SAP CAP) version
 	CDS SegmentType = "cds"
 	// Cloud Foundry segment
@@ -242,6 +244,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
 	AZFUNC:        func() SegmentWriter { return &segments.AzFunc{} },
 	BATTERY:       func() SegmentWriter { return &segments.Battery{} },
 	BREWFATHER:    func() SegmentWriter { return &segments.Brewfather{} },
+	BUF:           func() SegmentWriter { return &segments.Buf{} },
 	CDS:           func() SegmentWriter { return &segments.Cds{} },
 	CF:            func() SegmentWriter { return &segments.Cf{} },
 	CFTARGET:      func() SegmentWriter { return &segments.CfTarget{} },
diff --git a/src/segments/buf.go b/src/segments/buf.go
new file mode 100644
index 00000000..2a6716ac
--- /dev/null
+++ b/src/segments/buf.go
@@ -0,0 +1,34 @@
+package segments
+
+import (
+	"github.com/jandedobbeleer/oh-my-posh/src/platform"
+	"github.com/jandedobbeleer/oh-my-posh/src/properties"
+)
+
+type Buf struct {
+	language
+}
+
+func (b *Buf) Template() string {
+	return languageTemplate
+}
+
+func (b *Buf) Init(props properties.Properties, env platform.Environment) {
+	b.language = language{
+		env:        env,
+		props:      props,
+		extensions: []string{"buf.yaml", "buf.gen.yaml", "buf.work.yaml"},
+		commands: []*cmd{
+			{
+				executable: "buf",
+				args:       []string{"--version"},
+				regex:      `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
+			},
+		},
+		versionURLTemplate: "https://github.com/bufbuild/buf/releases/tag/v{{.Full}}",
+	}
+}
+
+func (b *Buf) Enabled() bool {
+	return b.language.Enabled()
+}
diff --git a/src/segments/git.go b/src/segments/git.go
index adc90302..cb96777a 100644
--- a/src/segments/git.go
+++ b/src/segments/git.go
@@ -386,7 +386,10 @@ func (g *Git) getUpstreamIcon() string {
 		}
 		url = strings.TrimPrefix(url, "ssh://")
 		url = strings.TrimPrefix(url, "git://")
-		url = strings.TrimPrefix(url, "git@")
+		if strings.HasPrefix(url, "git@") {
+			url = strings.TrimPrefix(url, "git@")
+			url = strings.Replace(url, ":", "/", 1)
+		}
 		url = strings.TrimSuffix(url, ".git")
 		return fmt.Sprintf("https://%s", url)
 	}
diff --git a/website/docs/segments/buf.mdx b/website/docs/segments/buf.mdx
new file mode 100644
index 00000000..7a3d8709
--- /dev/null
+++ b/website/docs/segments/buf.mdx
@@ -0,0 +1,54 @@
+---
+id: buf
+title: Buf
+sidebar_label: Buf
+---
+
+## What
+
+Display the currently active [Buf CLI][buf-docs] version.
+
+## Sample Configuration
+
+```json
+{
+  "type": "buf",
+  "style": "plain",
+  "foreground": "#1000D6",
+  "template": " 🐃 {{ .Full }} "
+}
+```
+
+## Properties
+
+| Name                   | Type      | Description                                                                                                                                                                                   |
+| ---------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `home_enabled`         | `boolean` | display the segment in the HOME folder or not - defaults to `false`                                                                                                                           |
+| `fetch_version`        | `boolean` | fetch the active version or not; useful if all you need is an icon indicating `buf`                                                                                                          |
+| `display_mode`         | `string`  | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when `buf.yaml`, `buf.gen.yaml` or `buf.work.yaml` files 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 / release notes                                                                         |
+
+## Template ([info][templates])
+
+:::note default template
+
+```template
+{{ if .Error }}{{ .Error }}{{ else }}{{ .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 |
+
+[go-text-template]: https://golang.org/pkg/text/template/
+[templates]: /docs/configuration/templates
+[buf-docs]: https://buf.build/
diff --git a/website/sidebars.js b/website/sidebars.js
index 040ef253..66925590 100644
--- a/website/sidebars.js
+++ b/website/sidebars.js
@@ -57,6 +57,7 @@ module.exports = {
         "segments/azfunc",
         "segments/battery",
         "segments/brewfather",
+        "segments/buf",
         "segments/cds",
         "segments/cf",
         "segments/cftarget",


Test patch:
diff --git a/src/segments/buf_test.go b/src/segments/buf_test.go
new file mode 100644
index 00000000..e5109424
--- /dev/null
+++ b/src/segments/buf_test.go
@@ -0,0 +1,31 @@
+package segments
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestBuf(t *testing.T) {
+	cases := []struct {
+		Case           string
+		ExpectedString string
+		Version        string
+	}{
+		{Case: "Buf 1.12.0", ExpectedString: "1.12.0", Version: "1.12.0"},
+	}
+	for _, tc := range cases {
+		params := &mockedLanguageParams{
+			cmd:           "buf",
+			versionParam:  "--version",
+			versionOutput: tc.Version,
+			extension:     "buf.yaml",
+		}
+		env, props := getMockedLanguageEnv(params)
+		b := &Buf{}
+		b.Init(props, env)
+		assert.True(t, b.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
+		assert.Equal(t, tc.ExpectedString, renderTemplate(env, b.Template(), b), fmt.Sprintf("Failed in case: %s", tc.Case))
+	}
+}