Repository: JanDeDobbeleer/oh-my-posh
Pull request #3047 (2022-11-05 12:26:50)

feat(segment): add xmake
### 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

I added a segment for [xmake](https://xmake.io/)

[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 37b00b1a..a7e933ff 100644
--- a/src/engine/segment.go
+++ b/src/engine/segment.go
@@ -198,6 +198,8 @@ const (
 	WINREG SegmentType = "winreg"
 	// WITHINGS queries the Withings API.
 	WITHINGS SegmentType = "withings"
+	// XMAKE write the xmake version if xmake.lua is present
+	XMAKE SegmentType = "xmake"
 	// YTM writes YouTube Music information and status
 	YTM SegmentType = "ytm"
 )
@@ -329,6 +331,7 @@ func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error
 		WAKATIME:      &segments.Wakatime{},
 		WINREG:        &segments.WindowsRegistry{},
 		WITHINGS:      &segments.Withings{},
+		XMAKE:         &segments.XMake{},
 		YTM:           &segments.Ytm{},
 	}
 	if segment.Properties == nil {
diff --git a/src/segments/xmake.go b/src/segments/xmake.go
new file mode 100644
index 00000000..87773336
--- /dev/null
+++ b/src/segments/xmake.go
@@ -0,0 +1,33 @@
+package segments
+
+import (
+	"oh-my-posh/environment"
+	"oh-my-posh/properties"
+)
+
+type XMake struct {
+	language
+}
+
+func (x *XMake) Template() string {
+	return languageTemplate
+}
+
+func (x *XMake) Init(props properties.Properties, env environment.Environment) {
+	x.language = language{
+		env:        env,
+		props:      props,
+		extensions: []string{"xmake.lua"},
+		commands: []*cmd{
+			{
+				executable: "xmake",
+				args:       []string{"--version"},
+				regex:      `xmake v(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
+			},
+		},
+	}
+}
+
+func (x *XMake) Enabled() bool {
+	return x.language.Enabled()
+}
diff --git a/themes/schema.json b/themes/schema.json
index 29df8fae..d8796432 100644
--- a/themes/schema.json
+++ b/themes/schema.json
@@ -277,6 +277,7 @@
             "wakatime",
             "winreg",
             "withings",
+            "xmake",
             "ytm"
           ]
         },
@@ -1261,6 +1262,37 @@
             }
           }
         },
+        {
+          "if": {
+            "properties": {
+              "type": {
+                "const": "xmake"
+              }
+            }
+          },
+          "then": {
+            "title": "XMake Segment",
+            "description": "https://ohmyposh.dev/docs/segments/xmake",
+            "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"
+                  }
+                }
+              }
+            }
+          }
+        },
         {
           "if": {
             "properties": {
diff --git a/website/docs/segments/xmake.mdx b/website/docs/segments/xmake.mdx
new file mode 100644
index 00000000..cff4c683
--- /dev/null
+++ b/website/docs/segments/xmake.mdx
@@ -0,0 +1,53 @@
+---
+id: xmake
+title: XMake
+sidebar_label: XMake
+---
+
+## What
+
+Display the currently active xmake version.
+
+## Sample Configuration
+
+```json
+{
+  "type": "xmake",
+  "style": "powerline",
+  "powerline_symbol": "\uE0B0",
+  "foreground": "#e0f2f1",
+  "background": "#22a079",
+  "template": " xmake v{{ .Full }} "
+},
+```
+
+## Properties
+
+| Name                   | Type      | Description                                                                                                                                                 |
+| ---------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `home_enabled`         | `boolean` | display the segment in the HOME folder or not - defaults to `false`                                                                                         |
+| `fetch_version`        | `boolean` | display the xmake version (`xmake --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 a `xmake.lua` file is present (**default**)</li></ul> |
+
+## Template ([into][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                                       |
+| `.Error` | `string` | error encountered when fetching the version string |
+
+[templates]: /docs/configuration/templates
diff --git a/website/sidebars.js b/website/sidebars.js
index 6cf179c8..b84a42d2 100644
--- a/website/sidebars.js
+++ b/website/sidebars.js
@@ -113,6 +113,7 @@ module.exports = {
         "segments/wakatime",
         "segments/withings",
         "segments/winreg",
+        "segments/xmake",
         "segments/ytm",
       ],
     },


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