feat: perl segment

This commit is contained in:
Michael Carman 2022-07-08 05:21:08 -05:00 committed by GitHub
parent e8cbf7199e
commit 43fee03fcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 176 additions and 0 deletions

View file

@ -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{},

39
src/segments/perl.go Normal file
View file

@ -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()
}

46
src/segments/perl_test.go Normal file
View file

@ -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))
}
}

View file

@ -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": {

View file

@ -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

View file

@ -86,6 +86,7 @@ module.exports = {
"segments/os",
"segments/owm",
"segments/path",
"segments/perl",
"segments/php",
"segments/plastic",
"segments/project",