Repository: JanDeDobbeleer/oh-my-posh
Pull request #2492 (2022-07-08 04:17:07)

Add Perl segment
### 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

Added segment to display active perl version.

[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 22d83ea6..a08c7725 100644
--- a/src/engine/segment.go
+++ b/src/engine/segment.go
@@ -140,6 +140,8 @@ const (
 	OWM SegmentType = "owm"
 	// PATH represents the current path segment
 	PATH SegmentType = "path"
+	// PERL writes which perl version is currently active
+	PERL SegmentType = "perl"
 	// PHP writes which php version is currently active
 	PHP SegmentType = "php"
 	// PLASTIC represents the plastic scm status and information
@@ -289,6 +291,7 @@ func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error
 		OS:            &segments.Os{},
 		OWM:           &segments.Owm{},
 		PATH:          &segments.Path{},
+		PERL:          &segments.Perl{},
 		PHP:           &segments.Php{},
 		PLASTIC:       &segments.Plastic{},
 		POSHGIT:       &segments.PoshGit{},
diff --git a/src/segments/perl.go b/src/segments/perl.go
new file mode 100644
index 00000000..1818d3c8
--- /dev/null
+++ b/src/segments/perl.go
@@ -0,0 +1,39 @@
+package segments
+
+import (
+	"oh-my-posh/environment"
+	"oh-my-posh/properties"
+)
+
+type Perl struct {
+	language
+}
+
+func (p *Perl) Template() string {
+	return languageTemplate
+}
+
+func (p *Perl) Init(props properties.Properties, env environment.Environment) {
+	perlRegex := `This is perl.*v(?P<version>(?P<major>[0-9]+)(?:\.(?P<minor>[0-9]+))(?:\.(?P<patch>[0-9]+))?).* built for .+`
+	p.language = language{
+		env:   env,
+		props: props,
+		extensions: []string{
+			".perl-version",
+			"*.pl",
+			"*.pm",
+			"*.t",
+		},
+		commands: []*cmd{
+			{
+				executable: "perl",
+				args:       []string{"-version"},
+				regex:      perlRegex,
+			},
+		},
+	}
+}
+
+func (p *Perl) Enabled() bool {
+	return p.language.Enabled()
+}
diff --git a/themes/schema.json b/themes/schema.json
index 24eb54c1..8f8a1d35 100644
--- a/themes/schema.json
+++ b/themes/schema.json
@@ -226,6 +226,7 @@
             "os",
             "owm",
             "path",
+            "perl",
             "python",
             "poshgit",
             "php",
@@ -943,6 +944,37 @@
             }
           }
         },
+        {
+          "if": {
+            "properties": {
+              "type": {
+                "const": "perl"
+              }
+            }
+          },
+          "then": {
+            "title": "Perl Segment",
+            "description": "https://ohmyposh.dev/docs/segments/perl",
+            "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/perl.mdx b/website/docs/segments/perl.mdx
new file mode 100644
index 00000000..274cbdb1
--- /dev/null
+++ b/website/docs/segments/perl.mdx
@@ -0,0 +1,55 @@
+---
+id: perl
+title: Perl
+sidebar_label: Perl
+---
+
+## What
+
+Display the currently active perl version.
+
+## Sample Configuration
+
+```json
+{
+  "type": "perl",
+  "style": "powerline",
+  "powerline_symbol": "\uE0B0",
+  "foreground": "#ffffff",
+  "background": "#4063D8",
+  "template": " \ue769 {{ .Full }}"
+}
+```
+
+## Properties
+
+- home_enabled: `boolean` - display the segment in the HOME folder or not - defaults to `false`
+- fetch_version: `boolean` - display the perl version - defaults to `true`
+- missing_command_text: `string` - text to display when the perl command is missing - defaults to empty
+- display_mode: `string` - determines when the segment is displayed
+  - `always`: the segment is always displayed
+  - `files`: the segment is only displayed when one of the following files is present:
+    - `.perl-version`
+    - `*.pl`
+    - `*.p,`
+    - `*.t`
+
+## Template ([info][templates])
+
+:::note default template
+
+``` template
+{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}
+```
+
+:::
+
+### Properties
+
+- `.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 f95bfb32..452f18b5 100644
--- a/website/sidebars.js
+++ b/website/sidebars.js
@@ -86,6 +86,7 @@ module.exports = {
         "segments/os",
         "segments/owm",
         "segments/path",
+        "segments/perl",
         "segments/php",
         "segments/plastic",
         "segments/project",


Test patch:
diff --git a/src/segments/perl_test.go b/src/segments/perl_test.go
new file mode 100644
index 00000000..e17af480
--- /dev/null
+++ b/src/segments/perl_test.go
@@ -0,0 +1,46 @@
+package segments
+
+import (
+	"fmt"
+	"oh-my-posh/mock"
+	"oh-my-posh/properties"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestPerl(t *testing.T) {
+	cases := []struct {
+		Case            string
+		ExpectedString  string
+		Version         string
+		PerlHomeVersion string
+		PerlHomeEnabled bool
+	}{
+		{
+			Case:           "v5.12+",
+			ExpectedString: "5.32.1",
+			Version:        "This is perl 5, version 32, subversion 1 (v5.32.1) built for MSWin32-x64-multi-thread",
+		},
+		{
+			Case:           "v5.6 - v5.10",
+			ExpectedString: "5.6.1",
+			Version:        "This is perl, v5.6.1 built for MSWin32-x86-multi-thread",
+		},
+	}
+	for _, tc := range cases {
+		env := new(mock.MockedEnvironment)
+		env.On("HasCommand", "perl").Return(true)
+		env.On("RunCommand", "perl", []string{"-version"}).Return(tc.Version, nil)
+		env.On("HasFiles", ".perl-version").Return(true)
+		env.On("Pwd").Return("/usr/home/project")
+		env.On("Home").Return("/usr/home")
+		props := properties.Map{
+			properties.FetchVersion: true,
+		}
+		p := &Perl{}
+		p.Init(props, env)
+		assert.True(t, p.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
+		assert.Equal(t, tc.ExpectedString, renderTemplate(env, p.Template(), p), fmt.Sprintf("Failed in case: %s", tc.Case))
+	}
+}