feat: expose segment output text

resolves #5750
This commit is contained in:
Jan De Dobbeleer 2024-10-22 12:22:40 +02:00 committed by Jan De Dobbeleer
parent d4054b04d6
commit 9f576507b1
145 changed files with 1245 additions and 1873 deletions

View file

@ -81,6 +81,7 @@ func TestMigratePropertyKey(t *testing.T) {
type MockedWriter struct {
template string
text string
}
func (m *MockedWriter) Enabled() bool {
@ -91,6 +92,14 @@ func (m *MockedWriter) Template() string {
return m.template
}
func (m *MockedWriter) Text() string {
return m.text
}
func (m *MockedWriter) SetText(text string) {
m.text = text
}
func (m *MockedWriter) Init(_ properties.Properties, _ runtime.Environment) {}
func TestIconOverride(t *testing.T) {

View file

@ -43,7 +43,6 @@ type Segment struct {
Cache *cache.Config `json:"cache,omitempty" toml:"cache,omitempty"`
Filler string `json:"filler,omitempty" toml:"filler,omitempty"`
styleCache SegmentStyle
Text string `json:"-" toml:"-"`
name string
LeadingDiamond string `json:"leading_diamond,omitempty" toml:"leading_diamond,omitempty"`
TrailingDiamond string `json:"trailing_diamond,omitempty" toml:"trailing_diamond,omitempty"`
@ -124,17 +123,26 @@ func (segment *Segment) Render() {
return
}
segment.Text = segment.string()
segment.Enabled = len(strings.ReplaceAll(segment.Text, " ", "")) > 0
text := segment.string()
segment.Enabled = len(strings.ReplaceAll(text, " ", "")) > 0
if !segment.Enabled {
segment.env.TemplateCache().RemoveSegmentData(segment.Name())
return
}
segment.writer.SetText(text)
segment.setCache()
}
func (segment *Segment) Text() string {
return segment.writer.Text()
}
func (segment *Segment) SetText(text string) {
segment.writer.SetText(text)
}
func (segment *Segment) ResolveForeground() color.Ansi {
if len(segment.ForegroundTemplates) != 0 {
match := segment.ForegroundTemplates.FirstMatch(segment.writer, segment.Foreground.String())
@ -202,25 +210,17 @@ func (segment *Segment) restoreCache() bool {
return false
}
text, OK := segment.env.Session().Get(segment.textCacheKey())
data, OK := segment.env.Session().Get(segment.cacheKey())
if !OK {
return false
}
segment.env.DebugF("restored %s segment from cache", segment.Name())
segment.Text = text
segment.Enabled = true
data, OK := segment.env.Session().Get(segment.writerCacheKey())
if !OK {
return true
}
err := json.Unmarshal([]byte(data), &segment.writer)
if err != nil {
segment.env.Error(err)
}
segment.Enabled = true
segment.env.TemplateCache().AddSegmentData(segment.Name(), segment.writer)
return true
@ -231,26 +231,17 @@ func (segment *Segment) setCache() {
return
}
segment.env.Session().Set(segment.textCacheKey(), segment.Text, segment.Cache.Duration)
data, err := json.Marshal(segment.writer)
if err != nil {
segment.env.Error(err)
return
}
segment.env.Session().Set(segment.writerCacheKey(), string(data), segment.Cache.Duration)
segment.env.Session().Set(segment.cacheKey(), string(data), segment.Cache.Duration)
}
func (segment *Segment) textCacheKey() string {
return segment.cacheKey("segment_cache_%s")
}
func (segment *Segment) writerCacheKey() string {
return segment.cacheKey("segment_cache_writer_%s")
}
func (segment *Segment) cacheKey(format string) string {
func (segment *Segment) cacheKey() string {
format := "segment_cache_%s"
switch segment.Cache.Strategy {
case cache.Session:
return fmt.Sprintf(format, segment.Name())

View file

@ -15,6 +15,8 @@ type SegmentType string
type SegmentWriter interface {
Enabled() bool
Template() string
SetText(text string)
Text() string
Init(props properties.Properties, env runtime.Environment)
}
@ -320,15 +322,16 @@ func (segment *Segment) MapSegmentWithWriter(env runtime.Environment) error {
segment.Properties = make(properties.Map)
}
if f, ok := Segments[segment.Type]; ok {
writer := f()
wrapper := &properties.Wrapper{
Properties: segment.Properties,
}
writer.Init(wrapper, env)
segment.writer = writer
return nil
f, ok := Segments[segment.Type]
if !ok {
return errors.New("unable to map writer")
}
return errors.New("unable to map writer")
writer := f()
wrapper := &properties.Wrapper{
Properties: segment.Properties,
}
writer.Init(wrapper, env)
segment.writer = writer
return nil
}

View file

@ -21,14 +21,16 @@ func (e *Engine) PrintDebug(startTime time.Time, version string) string {
// console title timing
titleStartTime := time.Now()
e.Env.Debug("segment: Title")
title := e.getTitleTemplateText()
consoleTitle := &config.Segment{
Alias: "ConsoleTitle",
NameLength: 12,
Enabled: len(e.Config.ConsoleTitleTemplate) > 0,
Text: title,
Duration: time.Since(titleStartTime),
Type: config.TEXT,
}
_ = consoleTitle.MapSegmentWithWriter(e.Env)
consoleTitle.SetText(e.getTitleTemplateText())
largestSegmentNameLength := consoleTitle.NameLength
// render prompt

View file

@ -267,7 +267,7 @@ func (e *Engine) renderActiveSegment() {
switch e.activeSegment.ResolveStyle() {
case config.Plain, config.Powerline:
terminal.Write(color.Background, color.Foreground, e.activeSegment.Text)
terminal.Write(color.Background, color.Foreground, e.activeSegment.Text())
case config.Diamond:
background := color.Transparent
@ -276,10 +276,10 @@ func (e *Engine) renderActiveSegment() {
}
terminal.Write(background, color.Background, e.activeSegment.LeadingDiamond)
terminal.Write(color.Background, color.Foreground, e.activeSegment.Text)
terminal.Write(color.Background, color.Foreground, e.activeSegment.Text())
case config.Accordion:
if e.activeSegment.Enabled {
terminal.Write(color.Background, color.Foreground, e.activeSegment.Text)
terminal.Write(color.Background, color.Foreground, e.activeSegment.Text())
}
}

View file

@ -2,9 +2,6 @@ package segments
import (
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Angular struct {
@ -15,22 +12,16 @@ func (a *Angular) Template() string {
return languageTemplate
}
func (a *Angular) Init(props properties.Properties, env runtime.Environment) {
a.language = language{
env: env,
props: props,
extensions: []string{"angular.json"},
commands: []*cmd{
{
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
getVersion: a.getVersion,
},
},
versionURLTemplate: "https://github.com/angular/angular/releases/tag/{{.Full}}",
}
}
func (a *Angular) Enabled() bool {
a.extensions = []string{"angular.json"}
a.commands = []*cmd{
{
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
getVersion: a.getVersion,
},
}
a.versionURLTemplate = "https://github.com/angular/angular/releases/tag/{{.Full}}"
return a.language.Enabled()
}

View file

@ -6,8 +6,6 @@ import (
"path"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)
@ -33,8 +31,7 @@ type ArgocdConfig struct {
}
type Argocd struct {
props properties.Properties
env runtime.Environment
base
ArgocdContext
}
@ -43,11 +40,6 @@ func (a *Argocd) Template() string {
return NameTemplate
}
func (a *Argocd) Init(props properties.Properties, env runtime.Environment) {
a.props = props
a.env = env
}
func (a *Argocd) Enabled() bool {
// always parse config instead of using cli to save time
configPath := a.getConfigPath()

View file

@ -37,8 +37,10 @@ func TestArgocdGetConfigFromOpts(t *testing.T) {
env.On("Getenv", argocdOptsEnv).Return(tc.Opts)
argocd := &Argocd{
env: env,
props: properties.Map{},
base: base{
env: env,
props: properties.Map{},
},
}
config := argocd.getConfigFromOpts()
assert.Equal(t, tc.Expected, config, tc.Case)
@ -63,8 +65,10 @@ func TestArgocdGetConfigPath(t *testing.T) {
env.On("Getenv", argocdOptsEnv).Return(tc.Opts)
argocd := &Argocd{
env: env,
props: properties.Map{},
base: base{
env: env,
props: properties.Map{},
},
}
assert.Equal(t, tc.Expected, argocd.getConfigPath())
}
@ -159,8 +163,10 @@ users:
env.On("Error", testify_.Anything).Return()
argocd := &Argocd{
env: env,
props: properties.Map{},
base: base{
env: env,
props: properties.Map{},
},
}
if len(tc.ExpectedError) > 0 {
_, err := argocd.parseConfig(configFile)
@ -254,10 +260,8 @@ servers:
env.On("Error", testify_.Anything).Return()
env.On("Flags").Return(&runtime.Flags{})
argocd := &Argocd{
env: env,
props: properties.Map{},
}
argocd := &Argocd{}
argocd.Init(properties.Map{}, env)
assert.Equal(t, tc.ExpectedEnabled, argocd.Enabled(), tc.Case)

View file

@ -5,12 +5,10 @@ import (
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Aws struct {
props properties.Properties
env runtime.Environment
base
Profile string
Region string
@ -24,11 +22,6 @@ func (a *Aws) Template() string {
return " {{ .Profile }}{{ if .Region }}@{{ .Region }}{{ end }} "
}
func (a *Aws) Init(props properties.Properties, env runtime.Environment) {
a.props = props
a.env = env
}
func (a *Aws) Enabled() bool {
getEnvFirstMatch := func(envs ...string) string {
for _, env := range envs {

View file

@ -66,10 +66,8 @@ func TestAWSSegment(t *testing.T) {
}
env.On("Flags").Return(&runtime.Flags{})
aws := &Aws{
env: env,
props: props,
}
aws := &Aws{}
aws.Init(props, env)
if tc.Template == "" {
tc.Template = aws.Template()

View file

@ -7,12 +7,11 @@ import (
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Az struct {
props properties.Properties
env runtime.Environment
base
Origin string
AzureSubscription
}
@ -75,11 +74,6 @@ func (a *Az) Template() string {
return NameTemplate
}
func (a *Az) Init(props properties.Properties, env runtime.Environment) {
a.props = props
a.env = env
}
func (a *Az) Enabled() bool {
source := a.props.GetString(Source, FirstMatch)
switch source {

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type AzFunc struct {
language
}
@ -13,21 +8,16 @@ func (az *AzFunc) Template() string {
return languageTemplate
}
func (az *AzFunc) Init(props properties.Properties, env runtime.Environment) {
az.language = language{
env: env,
props: props,
extensions: []string{"host.json", "local.settings.json", "function.json"},
commands: []*cmd{
{
executable: "func",
args: []string{"--version"},
regex: `(?P<version>[0-9.]+)`,
},
func (az *AzFunc) Enabled() bool {
az.extensions = []string{"host.json", "local.settings.json", "function.json"}
az.commands = []*cmd{
{
executable: "func",
args: []string{"--version"},
regex: `(?P<version>[0-9.]+)`,
},
}
}
func (az *AzFunc) Enabled() bool {
return az.language.Enabled()
}

View file

@ -140,12 +140,9 @@ func TestAzSegment(t *testing.T) {
tc.Source = FirstMatch
}
az := &Az{
env: env,
props: properties.Map{
Source: tc.Source,
},
}
az := &Az{}
az.Init(properties.Map{}, env)
assert.Equal(t, tc.ExpectedEnabled, az.Enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, az), tc.Case)
}

View file

@ -4,14 +4,10 @@ import (
"encoding/json"
"path/filepath"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Azd struct {
props properties.Properties
env runtime.Environment
base
azdConfig
}
@ -25,11 +21,6 @@ func (t *Azd) Template() string {
return " \uebd8 {{ .DefaultEnvironment }} "
}
func (t *Azd) Init(props properties.Properties, env runtime.Environment) {
t.props = props
t.env = env
}
func (t *Azd) Enabled() bool {
var parentFilePath string

View file

@ -62,10 +62,8 @@ func TestAzdSegment(t *testing.T) {
env.On("HasParentFilePath", ".azure", false).Return(&runtime.FileInfo{}, errors.New("no such file or directory"))
}
azd := Azd{
env: env,
props: properties.Map{},
}
azd := Azd{}
azd.Init(properties.Map{}, env)
assert.Equal(t, tc.ExpectedEnabled, azd.Enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, azd), tc.Case)

26
src/segments/base.go Normal file
View file

@ -0,0 +1,26 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type base struct {
props properties.Properties
env runtime.Environment
Output string `json:"Text"`
}
func (s *base) Text() string {
return s.Output
}
func (s *base) SetText(text string) {
s.Output = text
}
func (s *base) Init(props properties.Properties, env runtime.Environment) {
s.props = props
s.env = env
}

View file

@ -2,13 +2,11 @@ package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/battery"
)
type Battery struct {
props properties.Properties
env runtime.Environment
base
*battery.Info
Error string
@ -83,8 +81,3 @@ func (b *Battery) enabledWhileError(err error) bool {
b.State = battery.Full
return true
}
func (b *Battery) Init(props properties.Properties, env runtime.Environment) {
b.props = props
b.env = env
}

View file

@ -2,7 +2,6 @@ package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Bazel struct {
@ -15,29 +14,24 @@ const (
Icon properties.Property = "icon"
)
func (c *Bazel) Template() string {
func (b *Bazel) Template() string {
return " {{ if .Error }}{{ .Icon }} {{ .Error }}{{ else }}{{ url .Icon .URL }} {{ .Full }}{{ end }} "
}
func (c *Bazel) Init(props properties.Properties, env runtime.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]+)))`,
},
func (b *Bazel) Enabled() bool {
b.extensions = []string{"*.bazel", "*.bzl", "BUILD", "WORKSPACE", ".bazelrc", ".bazelversion"}
b.folders = []string{"bazel-bin", "bazel-out", "bazel-testlogs"}
b.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 >5.4.1, since they do not have the docs subdomain.
versionURLTemplate: "https://{{ if lt .Major 6 }}docs.{{ end }}bazel.build/versions/{{ .Major }}.{{ .Minor }}.{{ .Patch }}",
}
c.Icon = props.GetString(Icon, "\ue63a")
}
// Use the correct URL for Bazel >5.4.1, since they do not have the docs subdomain.
b.versionURLTemplate = "https://{{ if lt .Major 6 }}docs.{{ end }}bazel.build/versions/{{ .Major }}.{{ .Minor }}.{{ .Patch }}"
func (c *Bazel) Enabled() bool {
return c.language.Enabled()
b.Icon = b.props.GetString(Icon, "\ue63a")
return b.language.Enabled()
}

View file

@ -11,13 +11,12 @@ import (
"time"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
// segment struct, makes templating easier
type Brewfather struct {
props properties.Properties
env runtime.Environment
base
DaysBottledOrFermented *uint
TemperatureTrendIcon string
StatusIcon string
@ -276,8 +275,3 @@ func (bf *Brewfather) SGToPlato(sg float64) float64 {
// from https://en.wikipedia.org/wiki/Brix#Specific_gravity_2
return math.Round(100*((135.997*sg*sg*sg)-(630.272*sg*sg)+(1111.14*sg)-616.868)) / 100 // 2 decimal places
}
func (bf *Brewfather) Init(props properties.Properties, env runtime.Environment) {
bf.props = props
bf.env = env
}

View file

@ -156,20 +156,18 @@ func TestBrewfatherSegment(t *testing.T) {
env.On("HTTPRequest", BFBatchReadingsURL).Return([]byte(tc.BatchReadingsJSONResponse), tc.Error)
env.On("Flags").Return(&runtime.Flags{})
ns := &Brewfather{
props: props,
env: env,
}
brew := &Brewfather{}
brew.Init(props, env)
enabled := ns.Enabled()
enabled := brew.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if !enabled {
continue
}
if tc.Template == "" {
tc.Template = ns.Template()
tc.Template = brew.Template()
}
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, ns), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, brew), tc.Case)
}
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Buf struct {
language
}
@ -13,22 +8,16 @@ func (b *Buf) Template() string {
return languageTemplate
}
func (b *Buf) Init(props properties.Properties, env runtime.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 {
b.extensions = []string{"buf.yaml", "buf.gen.yaml", "buf.work.yaml"}
b.commands = []*cmd{
{
executable: "buf",
args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
b.versionURLTemplate = "https://github.com/bufbuild/buf/releases/tag/v{{.Full}}"
return b.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Bun struct {
language
}
@ -13,22 +8,16 @@ func (b *Bun) Template() string {
return languageTemplate
}
func (b *Bun) Init(props properties.Properties, env runtime.Environment) {
b.language = language{
env: env,
props: props,
extensions: []string{"bun.lockb"},
commands: []*cmd{
{
executable: "bun",
args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
versionURLTemplate: "https://github.com/oven-sh/bun/releases/tag/bun-v{{.Full}}",
}
}
func (b *Bun) Enabled() bool {
b.extensions = []string{"bun.lockb"}
b.commands = []*cmd{
{
executable: "bun",
args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
b.versionURLTemplate = "https://github.com/oven-sh/bun/releases/tag/bun-v{{.Full}}"
return b.language.Enabled()
}

View file

@ -5,12 +5,10 @@ import (
"fmt"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type CarbonIntensity struct {
props properties.Properties
env runtime.Environment
base
TrendIcon string
@ -126,8 +124,3 @@ func (d *CarbonIntensity) setStatus() error {
return nil
}
func (d *CarbonIntensity) Init(props properties.Properties, env runtime.Environment) {
d.props = props
d.env = env
}

View file

@ -229,10 +229,8 @@ func TestCarbonIntensitySegmentSingle(t *testing.T) {
env.On("Error", testify_.Anything)
env.On("Flags").Return(&runtime.Flags{})
d := &CarbonIntensity{
props: props,
env: env,
}
d := &CarbonIntensity{}
d.Init(props, env)
enabled := d.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)

View file

@ -2,9 +2,6 @@ package segments
import (
"encoding/json"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Cds struct {
@ -16,25 +13,20 @@ func (c *Cds) Template() string {
return languageTemplate
}
func (c *Cds) Init(props properties.Properties, env runtime.Environment) {
c.language = language{
env: env,
props: props,
extensions: []string{".cdsrc.json", ".cdsrc-private.json", "*.cds"},
commands: []*cmd{
{
executable: "cds",
args: []string{"--version"},
regex: `@sap/cds: (?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
loadContext: c.loadContext,
inContext: c.inContext,
displayMode: props.GetString(DisplayMode, DisplayModeContext),
}
}
func (c *Cds) Enabled() bool {
c.extensions = []string{".cdsrc.json", ".cdsrc-private.json", "*.cds"}
c.commands = []*cmd{
{
executable: "cds",
args: []string{"--version"},
regex: `@sap/cds: (?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
//TODO: is this necessary?
c.language.loadContext = c.loadContext
c.language.inContext = c.inContext
c.displayMode = c.props.GetString(DisplayMode, DisplayModeContext)
return c.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Cf struct {
language
}
@ -13,23 +8,17 @@ func (c *Cf) Template() string {
return languageTemplate
}
func (c *Cf) Init(props properties.Properties, env runtime.Environment) {
c.language = language{
env: env,
props: props,
extensions: []string{"manifest.yml", "mta.yaml"},
commands: []*cmd{
{
executable: "cf",
args: []string{"version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
displayMode: props.GetString(DisplayMode, DisplayModeFiles),
versionURLTemplate: "https://github.com/cloudfoundry/cli/releases/tag/v{{ .Full }}",
}
}
func (c *Cf) Enabled() bool {
c.extensions = []string{"manifest.yml", "mta.yaml"}
c.commands = []*cmd{
{
executable: "cf",
args: []string{"version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
c.displayMode = c.props.GetString(DisplayMode, DisplayModeFiles)
c.versionURLTemplate = "https://github.com/cloudfoundry/cli/releases/tag/v{{ .Full }}"
return c.language.Enabled()
}

View file

@ -5,12 +5,10 @@ import (
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type CfTarget struct {
props properties.Properties
env runtime.Environment
base
CfTargetDetails
}
@ -26,11 +24,6 @@ func (c *CfTarget) Template() string {
return "{{if .Org }}{{ .Org }}{{ end }}{{if .Space }}/{{ .Space }}{{ end }}"
}
func (c *CfTarget) Init(props properties.Properties, env runtime.Environment) {
c.props = props
c.env = env
}
func (c *CfTarget) Enabled() bool {
if !c.env.HasCommand("cf") {
return false

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Cmake struct {
language
}
@ -13,22 +8,16 @@ func (c *Cmake) Template() string {
return languageTemplate
}
func (c *Cmake) Init(props properties.Properties, env runtime.Environment) {
c.language = language{
env: env,
props: props,
extensions: []string{"*.cmake", "CMakeLists.txt"},
commands: []*cmd{
{
executable: "cmake",
args: []string{"--version"},
regex: `cmake version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://cmake.org/cmake/help/v{{ .Major }}.{{ .Minor }}",
}
}
func (c *Cmake) Enabled() bool {
c.extensions = []string{"*.cmake", "CMakeLists.txt"}
c.commands = []*cmd{
{
executable: "cmake",
args: []string{"--version"},
regex: `cmake version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
c.versionURLTemplate = "https://cmake.org/cmake/help/v{{ .Major }}.{{ .Minor }}"
return c.language.Enabled()
}

View file

@ -4,12 +4,10 @@ import (
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Cmd struct {
props properties.Properties
env runtime.Environment
base
Output string
}
@ -85,8 +83,3 @@ func (c *Cmd) runScript(shell, script string) bool {
c.Output = c.env.RunShellCommand(shell, script)
return len(c.Output) != 0
}
func (c *Cmd) Init(props properties.Properties, env runtime.Environment) {
c.props = props
c.env = env
}

View file

@ -16,10 +16,10 @@ func TestExecuteCommand(t *testing.T) {
props := properties.Map{
Command: "echo hello",
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.True(t, enabled)
assert.Equal(t, "hello", renderTemplate(env, c.Template(), c))
@ -34,10 +34,10 @@ func TestExecuteMultipleCommandsOrFirst(t *testing.T) {
props := properties.Map{
Command: "exit 1 || echo hello",
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.True(t, enabled)
assert.Equal(t, "hello", renderTemplate(env, c.Template(), c))
@ -51,10 +51,10 @@ func TestExecuteMultipleCommandsOrSecond(t *testing.T) {
props := properties.Map{
Command: "echo hello || echo world",
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.True(t, enabled)
assert.Equal(t, "hello", renderTemplate(env, c.Template(), c))
@ -68,10 +68,10 @@ func TestExecuteMultipleCommandsAnd(t *testing.T) {
props := properties.Map{
Command: "echo hello && echo world",
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.True(t, enabled)
assert.Equal(t, "helloworld", renderTemplate(env, c.Template(), c))
@ -84,10 +84,10 @@ func TestExecuteSingleCommandEmpty(t *testing.T) {
props := properties.Map{
Command: "",
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.False(t, enabled)
}
@ -97,10 +97,10 @@ func TestExecuteSingleCommandNoCommandProperty(t *testing.T) {
env.On("HasCommand", "bash").Return(true)
env.On("RunShellCommand", "bash", "").Return("")
var props properties.Map
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.False(t, enabled)
}
@ -112,10 +112,10 @@ func TestExecuteMultipleCommandsAndDisabled(t *testing.T) {
props := properties.Map{
Command: "echo && echo",
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.False(t, enabled)
}
@ -128,10 +128,10 @@ func TestExecuteMultipleCommandsOrDisabled(t *testing.T) {
props := properties.Map{
Command: "echo|| echo",
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.False(t, enabled)
}
@ -144,10 +144,10 @@ func TestExecuteNonInterpretedCommand(t *testing.T) {
Command: "echo hello && echo world",
Interpret: false,
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.True(t, enabled)
assert.Equal(t, "hello world", renderTemplate(env, c.Template(), c))
@ -180,10 +180,10 @@ func TestExecuteScript(t *testing.T) {
props := properties.Map{
Script: script,
}
c := &Cmd{
props: props,
env: env,
}
c := &Cmd{}
c.Init(props, env)
enabled := c.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if tc.ExpectedEnabled {

View file

@ -8,8 +8,7 @@ import (
)
type Connection struct {
props properties.Properties
env runtime.Environment
base
runtime.Connection
}
@ -35,8 +34,3 @@ func (c *Connection) Enabled() bool {
}
return false
}
func (c *Connection) Init(props properties.Properties, env runtime.Environment) {
c.props = props
c.env = env
}

View file

@ -92,12 +92,14 @@ func TestConnection(t *testing.T) {
for _, con := range tc.Connections {
env.On("Connection", con.Connection.Type).Return(con.Connection, con.Error)
}
c := &Connection{
env: env,
props: &properties.Map{
Type: tc.ConnectionType,
},
props := &properties.Map{
Type: tc.ConnectionType,
}
c := &Connection{}
c.Init(props, env)
assert.Equal(t, tc.ExpectedEnabled, c.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, c.Template(), c), fmt.Sprintf("Failed in case: %s", tc.Case))

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Crystal struct {
language
}
@ -13,22 +8,16 @@ func (c *Crystal) Template() string {
return languageTemplate
}
func (c *Crystal) Init(props properties.Properties, env runtime.Environment) {
c.language = language{
env: env,
props: props,
extensions: []string{"*.cr", "shard.yml"},
commands: []*cmd{
{
executable: "crystal",
args: []string{"--version"},
regex: `Crystal (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/crystal-lang/crystal/releases/tag/{{ .Full }}",
}
}
func (c *Crystal) Enabled() bool {
c.extensions = []string{"*.cr", "shard.yml"}
c.commands = []*cmd{
{
executable: "crystal",
args: []string{"--version"},
regex: `Crystal (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
c.versionURLTemplate = "https://github.com/crystal-lang/crystal/releases/tag/{{ .Full }}"
return c.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
var (
dartExtensions = []string{"*.dart", "pubspec.yaml", "pubspec.yml", "pubspec.lock"}
dartFolders = []string{".dart_tool"}
@ -18,23 +13,17 @@ func (d *Dart) Template() string {
return languageTemplate
}
func (d *Dart) Init(props properties.Properties, env runtime.Environment) {
d.language = language{
env: env,
props: props,
extensions: dartExtensions,
folders: dartFolders,
commands: []*cmd{
{
executable: "dart",
args: []string{"--version"},
regex: `Dart SDK version: (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://dart.dev/guides/language/evolution#dart-{{ .Major }}{{ .Minor }}",
}
}
func (d *Dart) Enabled() bool {
d.extensions = dartExtensions
d.folders = dartFolders
d.commands = []*cmd{
{
executable: "dart",
args: []string{"--version"},
regex: `Dart SDK version: (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
d.versionURLTemplate = "https://dart.dev/guides/language/evolution#dart-{{ .Major }}{{ .Minor }}"
return d.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Deno struct {
language
}
@ -13,22 +8,16 @@ func (d *Deno) Template() string {
return languageTemplate
}
func (d *Deno) Init(props properties.Properties, env runtime.Environment) {
d.language = language{
env: env,
props: props,
extensions: []string{"*.js", "*.ts", "deno.json"},
commands: []*cmd{
{
executable: "deno",
args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
versionURLTemplate: "https://github.com/denoland/deno/releases/tag/v{{.Full}}",
}
}
func (d *Deno) Enabled() bool {
d.extensions = []string{"*.js", "*.ts", "deno.json"}
d.commands = []*cmd{
{
executable: "deno",
args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
d.versionURLTemplate = "https://github.com/denoland/deno/releases/tag/v{{.Full}}"
return d.language.Enabled()
}

View file

@ -3,9 +3,6 @@ package segments
import (
"encoding/json"
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type DockerConfig struct {
@ -13,8 +10,7 @@ type DockerConfig struct {
}
type Docker struct {
props properties.Properties
env runtime.Environment
base
Context string
}
@ -23,11 +19,6 @@ func (d *Docker) Template() string {
return " \uf308 {{ .Context }} "
}
func (d *Docker) Init(props properties.Properties, env runtime.Environment) {
d.props = props
d.env = env
}
func (d *Docker) envVars() []string {
return []string{"DOCKER_MACHINE_NAME", "DOCKER_HOST", "DOCKER_CONTEXT"}
}

View file

@ -2,8 +2,6 @@ package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/constants"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Dotnet struct {
@ -16,24 +14,30 @@ func (d *Dotnet) Template() string {
return " {{ if .Unsupported }}\uf071{{ else }}{{ .Full }}{{ end }} "
}
func (d *Dotnet) Init(props properties.Properties, env runtime.Environment) {
d.language = language{
env: env,
props: props,
extensions: []string{"*.cs", "*.csx", "*.vb", "*.sln", "*.slnf", "*.csproj", "*.vbproj", "*.fs", "*.fsx", "*.fsproj", "global.json"},
commands: []*cmd{
{
executable: "dotnet",
args: []string{"--version"},
regex: `(?P<version>((?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)` +
`(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?))`,
},
},
versionURLTemplate: "https://github.com/dotnet/core/blob/master/release-notes/{{ .Major }}.{{ .Minor }}/{{ .Major }}.{{ .Minor }}.{{ substr 0 1 .Patch }}/{{ .Major }}.{{ .Minor }}.{{ substr 0 1 .Patch }}.md", //nolint: lll
}
}
func (d *Dotnet) Enabled() bool {
d.extensions = []string{
"*.cs",
"*.csx",
"*.vb",
"*.sln",
"*.slnf",
"*.csproj",
"*.vbproj",
"*.fs",
"*.fsx",
"*.fsproj",
"global.json",
}
d.commands = []*cmd{
{
executable: "dotnet",
args: []string{"--version"},
regex: `(?P<version>((?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)` +
`(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?))`,
},
}
d.versionURLTemplate = "https://github.com/dotnet/core/blob/master/release-notes/{{ .Major }}.{{ .Minor }}/{{ .Major }}.{{ .Minor }}.{{ substr 0 1 .Patch }}/{{ .Major }}.{{ .Minor }}.{{ substr 0 1 .Patch }}.md" //nolint: lll
enabled := d.language.Enabled()
if !enabled {
return false

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Elixir struct {
language
}
@ -13,27 +8,21 @@ func (e *Elixir) Template() string {
return languageTemplate
}
func (e *Elixir) Init(props properties.Properties, env runtime.Environment) {
e.language = language{
env: env,
props: props,
extensions: []string{"*.ex", "*.exs"},
commands: []*cmd{
{
executable: "asdf",
args: []string{"current", "elixir"},
regex: `elixir\s+(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))[^\s]*\s+`,
},
{
executable: "elixir",
args: []string{"--version"},
regex: `Elixir (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/elixir-lang/elixir/releases/tag/v{{ .Full }}",
}
}
func (e *Elixir) Enabled() bool {
e.extensions = []string{"*.ex", "*.exs"}
e.commands = []*cmd{
{
executable: "asdf",
args: []string{"current", "elixir"},
regex: `elixir\s+(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))[^\s]*\s+`,
},
{
executable: "elixir",
args: []string{"--version"},
regex: `Elixir (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
e.versionURLTemplate = "https://github.com/elixir-lang/elixir/releases/tag/v{{ .Full }}"
return e.language.Enabled()
}

View file

@ -5,15 +5,13 @@ import (
"strconv"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
lang "golang.org/x/text/language"
"golang.org/x/text/message"
)
type Executiontime struct {
props properties.Properties
env runtime.Environment
base
FormattedMs string
Ms int64
@ -70,11 +68,6 @@ func (t *Executiontime) Template() string {
return " {{ .FormattedMs }} "
}
func (t *Executiontime) Init(props properties.Properties, env runtime.Environment) {
t.props = props
t.env = env
}
func (t *Executiontime) formatDuration(style DurationStyle) string {
switch style {
case Austin:

View file

@ -14,20 +14,20 @@ import (
func TestExecutionTimeWriterDefaultThresholdEnabled(t *testing.T) {
env := new(mock.Environment)
env.On("ExecutionTime").Return(1337)
executionTime := &Executiontime{
env: env,
props: properties.Map{},
}
executionTime := &Executiontime{}
executionTime.Init(properties.Map{}, env)
assert.True(t, executionTime.Enabled())
}
func TestExecutionTimeWriterDefaultThresholdDisabled(t *testing.T) {
env := new(mock.Environment)
env.On("ExecutionTime").Return(1)
executionTime := &Executiontime{
env: env,
props: properties.Map{},
}
executionTime := &Executiontime{}
executionTime.Init(properties.Map{}, env)
assert.False(t, executionTime.Enabled())
}
@ -37,10 +37,10 @@ func TestExecutionTimeWriterCustomThresholdEnabled(t *testing.T) {
props := properties.Map{
ThresholdProperty: float64(10),
}
executionTime := &Executiontime{
env: env,
props: props,
}
executionTime := &Executiontime{}
executionTime.Init(props, env)
assert.True(t, executionTime.Enabled())
}
@ -50,10 +50,10 @@ func TestExecutionTimeWriterCustomThresholdDisabled(t *testing.T) {
props := properties.Map{
ThresholdProperty: float64(100),
}
executionTime := &Executiontime{
env: env,
props: props,
}
executionTime := &Executiontime{}
executionTime.Init(props, env)
assert.False(t, executionTime.Enabled())
}
@ -62,10 +62,10 @@ func TestExecutionTimeWriterDuration(t *testing.T) {
expected := "1.337s"
env := new(mock.Environment)
env.On("ExecutionTime").Return(input)
executionTime := &Executiontime{
env: env,
props: properties.Map{},
}
executionTime := &Executiontime{}
executionTime.Init(properties.Map{}, env)
executionTime.Enabled()
assert.Equal(t, expected, executionTime.FormattedMs)
}
@ -75,10 +75,10 @@ func TestExecutionTimeWriterDuration2(t *testing.T) {
expected := "3h 42m 51.337s"
env := new(mock.Environment)
env.On("ExecutionTime").Return(input)
executionTime := &Executiontime{
env: env,
props: properties.Map{},
}
executionTime := &Executiontime{}
executionTime.Init(properties.Map{}, env)
executionTime.Enabled()
assert.Equal(t, expected, executionTime.FormattedMs)
}

View file

@ -5,9 +5,6 @@ import (
"errors"
"path/filepath"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
const (
@ -15,8 +12,7 @@ const (
)
type Firebase struct {
props properties.Properties
env runtime.Environment
base
Project string
}
@ -29,11 +25,6 @@ func (f *Firebase) Template() string {
return " {{ .Project}} "
}
func (f *Firebase) Init(props properties.Properties, env runtime.Environment) {
f.props = props
f.env = env
}
func (f *Firebase) Enabled() bool {
cfgDir := filepath.Join(f.env.Home(), ".config", "configstore")
configFile, err := f.getActiveConfig(cfgDir)

View file

@ -4,6 +4,7 @@ import (
"path/filepath"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/stretchr/testify/assert"
testify_ "github.com/stretchr/testify/mock"
@ -59,10 +60,12 @@ func TestFirebaseSegment(t *testing.T) {
fcPath := filepath.Join("home", ".config", "configstore", "firebase-tools.json")
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
env.On("Error", testify_.Anything).Return()
f := Firebase{
env: env,
}
f := &Firebase{}
f.Init(properties.Map{}, env)
f.Enabled()
assert.Equal(t, tc.ExpectedEnabled, f.Enabled())
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, f.Template(), f), tc.Case)
@ -102,9 +105,10 @@ func TestGetFirebaseActiveConfig(t *testing.T) {
contentPath := filepath.Join(configPath, "firebase-tools.json")
env.On("FileContent", contentPath).Return(tc.ActiveConfig)
env.On("Error", testify_.Anything).Return()
f := Firebase{
env: env,
}
f := &Firebase{}
f.Init(properties.Map{}, env)
got, err := f.getActiveConfig(configPath)
assert.Equal(t, tc.ExpectedString, got, tc.Case)
if len(tc.ExpectedError) > 0 {

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Flutter struct {
language
}
@ -13,23 +8,17 @@ func (f *Flutter) Template() string {
return languageTemplate
}
func (f *Flutter) Init(props properties.Properties, env runtime.Environment) {
f.language = language{
env: env,
props: props,
extensions: dartExtensions,
folders: dartFolders,
commands: []*cmd{
{
executable: "flutter",
args: []string{"--version"},
regex: `Flutter (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/flutter/flutter/releases/tag/{{ .Major }}.{{ .Minor }}.{{ .Patch }}",
}
}
func (f *Flutter) Enabled() bool {
f.extensions = dartExtensions
f.folders = dartFolders
f.commands = []*cmd{
{
executable: "flutter",
args: []string{"--version"},
regex: `Flutter (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
f.versionURLTemplate = "https://github.com/flutter/flutter/releases/tag/{{ .Major }}.{{ .Minor }}.{{ .Patch }}"
return f.language.Enabled()
}

View file

@ -67,13 +67,12 @@ func TestFossilStatus(t *testing.T) {
env.On("InWSLSharedDrive").Return(false)
env.On("HasCommand", FOSSILCOMMAND).Return(tc.HasCommand)
env.On("RunCommand", FOSSILCOMMAND, []string{"status"}).Return(strings.ReplaceAll(tc.Output, "\t", ""), tc.OutputError)
f := &Fossil{
scm: scm{
env: env,
props: properties.Map{},
},
}
f := &Fossil{}
f.Init(properties.Map{}, env)
got := f.Enabled()
assert.Equal(t, !tc.ExpectedDisabled, got, tc.Case)
if tc.ExpectedDisabled {
continue

View file

@ -4,7 +4,6 @@ import (
"errors"
"path"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"gopkg.in/ini.v1"
@ -15,8 +14,7 @@ const (
)
type Gcp struct {
props properties.Properties
env runtime.Environment
base
Account string
Project string
@ -27,11 +25,6 @@ func (g *Gcp) Template() string {
return " {{ .Project }} "
}
func (g *Gcp) Init(props properties.Properties, env runtime.Environment) {
g.props = props
g.env = env
}
func (g *Gcp) Enabled() bool {
cfgDir := g.getConfigDirectory()
configFile, err := g.getActiveConfig(cfgDir)

View file

@ -4,6 +4,7 @@ import (
"path"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
@ -58,9 +59,10 @@ func TestGcpSegment(t *testing.T) {
cfgpath := path.Join("config", "configurations", "config_production")
env.On("FileContent", cfgpath).Return(tc.CfgData)
env.On("Error", testify_.Anything).Return()
g := &Gcp{
env: env,
}
g := &Gcp{}
g.Init(properties.Map{}, env)
assert.Equal(t, tc.ExpectedEnabled, g.Enabled(), tc.Case)
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, "{{.Project}} :: {{.Region}} :: {{.Account}}", g), tc.Case)
@ -101,9 +103,10 @@ func TestGetConfigDirectory(t *testing.T) {
env.On("Getenv", "APPDATA").Return(tc.AppData)
env.On("Home").Return(tc.Home)
env.On("GOOS").Return(tc.GOOS)
g := &Gcp{
env: env,
}
g := &Gcp{}
g.Init(properties.Map{}, env)
assert.Equal(t, tc.Expected, g.getConfigDirectory(), tc.Case)
}
}
@ -129,9 +132,10 @@ func TestGetActiveConfig(t *testing.T) {
for _, tc := range cases {
env := new(mock.Environment)
env.On("FileContent", "active_config").Return(tc.ActiveConfig)
g := &Gcp{
env: env,
}
g := &Gcp{}
g.Init(properties.Map{}, env)
got, err := g.getActiveConfig("")
assert.Equal(t, tc.ExpectedString, got, tc.Case)
if len(tc.ExpectedError) > 0 {

View file

@ -29,12 +29,10 @@ func TestEnabledGitNotFound(t *testing.T) {
env.On("HasCommand", "git").Return(false)
env.On("GOOS").Return("")
env.On("IsWsl").Return(false)
g := &Git{
scm: scm{
env: env,
props: properties.Map{},
},
}
g := &Git{}
g.Init(properties.Map{}, env)
assert.False(t, g.Enabled())
}
@ -56,12 +54,10 @@ func TestEnabledInWorkingDirectory(t *testing.T) {
env.On("Home").Return(poshHome)
env.On("Getenv", poshGitEnv).Return("")
env.On("DirMatchesOneOf", testify_.Anything, testify_.Anything).Return(false)
g := &Git{
scm: scm{
env: env,
props: properties.Map{},
},
}
g := &Git{}
g.Init(properties.Map{}, env)
assert.True(t, g.Enabled())
assert.Equal(t, fileInfo.Path, g.workingDir)
}
@ -138,12 +134,10 @@ func TestEnabledInWorktree(t *testing.T) {
env.On("HasFilesInDir", tc.WorkingFolder, tc.WorkingFolderAddon).Return(true)
env.On("HasFilesInDir", tc.WorkingFolder, "HEAD").Return(true)
env.On("PathSeparator").Return(string(os.PathSeparator))
g := &Git{
scm: scm{
env: env,
props: properties.Map{},
},
}
g := &Git{}
g.Init(properties.Map{}, env)
assert.Equal(t, tc.ExpectedEnabled, g.hasWorktree(fileInfo), tc.Case)
assert.Equal(t, tc.ExpectedWorkingFolder, g.workingDir, tc.Case)
assert.Equal(t, tc.ExpectedRealFolder, g.realDir, tc.Case)
@ -198,15 +192,15 @@ func TestEnabledInBareRepo(t *testing.T) {
env.On("FileContent", "/home/user/bare.git/HEAD").Return(tc.HEAD)
env.MockGitCommand(pwd, tc.Remote, "remote")
env.MockGitCommand(pwd, tc.RemoteURL, "remote", "get-url", tc.Remote)
g := &Git{
scm: scm{
env: env,
props: properties.Map{
FetchBareInfo: true,
FetchUpstreamIcon: tc.FetchRemote,
},
},
props := properties.Map{
FetchBareInfo: true,
FetchUpstreamIcon: tc.FetchRemote,
}
g := &Git{}
g.Init(props, env)
assert.Equal(t, g.Enabled(), tc.ExpectedEnabled, tc.Case)
assert.Equal(t, g.Ref, tc.ExpectedHEAD, tc.Case)
assert.Equal(t, g.UpstreamIcon, tc.ExpectedRemote, tc.Case)
@ -221,13 +215,14 @@ func TestGetGitOutputForCommand(t *testing.T) {
env.On("IsWsl").Return(false)
env.On("RunCommand", "git", append(args, commandArgs...)).Return(want, nil)
env.On("GOOS").Return("unix")
g := &Git{
scm: scm{
env: env,
props: properties.Map{},
command: GITCOMMAND,
},
}
g.Init(properties.Map{}, env)
got := g.getGitCommandOutput(commandArgs...)
assert.Equal(t, want, got)
}
@ -366,23 +361,25 @@ func TestSetGitHEADContextClean(t *testing.T) {
env.On("HasFilesInDir", "", "sequencer/todo").Return(tc.Sequencer)
env.On("FileContent", "/sequencer/todo").Return(tc.Theirs)
props := properties.Map{
BranchIcon: "branch ",
CommitIcon: "commit ",
RebaseIcon: "rebase ",
MergeIcon: "merge ",
CherryPickIcon: "pick ",
TagIcon: "tag ",
RevertIcon: "revert ",
}
g := &Git{
scm: scm{
env: env,
props: properties.Map{
BranchIcon: "branch ",
CommitIcon: "commit ",
RebaseIcon: "rebase ",
MergeIcon: "merge ",
CherryPickIcon: "pick ",
TagIcon: "tag ",
RevertIcon: "revert ",
},
command: GITCOMMAND,
},
ShortHash: "1234567",
Ref: tc.Ref,
}
g.Init(props, env)
g.setGitHEADContext()
assert.Equal(t, tc.Expected, g.HEAD, tc.Case)
}
@ -409,18 +406,21 @@ func TestSetPrettyHEADName(t *testing.T) {
env.On("GOOS").Return("unix")
env.On("IsWsl").Return(false)
env.MockGitCommand("", tc.Tag, "describe", "--tags", "--exact-match")
props := properties.Map{
BranchIcon: "branch ",
CommitIcon: "commit ",
TagIcon: "tag ",
}
g := &Git{
scm: scm{
env: env,
props: properties.Map{
BranchIcon: "branch ",
CommitIcon: "commit ",
TagIcon: "tag ",
},
command: GITCOMMAND,
},
ShortHash: tc.ShortHash,
}
g.Init(props, env)
g.setPrettyHEADName()
assert.Equal(t, tc.Expected, g.HEAD, tc.Case)
}
@ -579,13 +579,14 @@ func TestSetGitStatus(t *testing.T) {
env.On("GOOS").Return("unix")
env.On("IsWsl").Return(false)
env.MockGitCommand("", strings.ReplaceAll(tc.Output, "\t", ""), "status", "-unormal", "--branch", "--porcelain=2")
g := &Git{
scm: scm{
env: env,
props: properties.Map{},
command: GITCOMMAND,
},
}
g.Init(properties.Map{}, env)
if tc.ExpectedWorking == nil {
tc.ExpectedWorking = &GitStatus{}
}
@ -620,12 +621,14 @@ func TestGetStashContextZeroEntries(t *testing.T) {
for _, tc := range cases {
env := new(mock.Environment)
env.On("FileContent", "/logs/refs/stash").Return(tc.StashContent)
g := &Git{
scm: scm{
env: env,
workingDir: "",
},
}
g.Init(properties.Map{}, env)
got := g.StashCount()
assert.Equal(t, tc.Expected, got)
}
@ -697,14 +700,15 @@ func TestGitUpstream(t *testing.T) {
"src.example.com": "EX",
},
}
g := &Git{
scm: scm{
env: env,
props: props,
command: GITCOMMAND,
},
Upstream: "origin/main",
}
g.Init(props, env)
upstreamIcon := g.getUpstreamIcon()
assert.Equal(t, tc.Expected, upstreamIcon, tc.Case)
}
@ -735,15 +739,15 @@ func TestGetBranchStatus(t *testing.T) {
BranchIdenticalIcon: "equal",
BranchGoneIcon: "gone",
}
g := &Git{
scm: scm{
props: props,
},
Ahead: tc.Ahead,
Behind: tc.Behind,
Upstream: tc.Upstream,
UpstreamGone: tc.UpstreamGone,
}
g.Init(props, new(mock.Environment))
g.setBranchStatus()
assert.Equal(t, tc.Expected, g.BranchStatus, tc.Case)
}
@ -919,14 +923,17 @@ func TestGitUntrackedMode(t *testing.T) {
}
for _, tc := range cases {
props := properties.Map{
UntrackedModes: tc.UntrackedModes,
}
g := &Git{
scm: scm{
props: properties.Map{
UntrackedModes: tc.UntrackedModes,
},
realDir: "foo",
},
}
g.Init(props, new(mock.Environment))
got := g.getUntrackedFilesMode()
assert.Equal(t, tc.Expected, got, tc.Case)
}
@ -961,14 +968,17 @@ func TestGitIgnoreSubmodules(t *testing.T) {
}
for _, tc := range cases {
props := properties.Map{
IgnoreSubmodules: tc.IgnoreSubmodules,
}
g := &Git{
scm: scm{
props: properties.Map{
IgnoreSubmodules: tc.IgnoreSubmodules,
},
realDir: "foo",
},
}
g.Init(props, new(mock.Environment))
got := g.getIgnoreSubmodulesMode()
assert.Equal(t, tc.Expected, got, tc.Case)
}
@ -1093,12 +1103,14 @@ func TestGitCommit(t *testing.T) {
for _, tc := range cases {
env := new(mock.Environment)
env.MockGitCommand("", tc.Output, "log", "-1", "--pretty=format:an:%an%nae:%ae%ncn:%cn%nce:%ce%nat:%at%nsu:%s%nha:%H%nrf:%D", "--decorate=full")
g := &Git{
scm: scm{
env: env,
command: "git",
command: GITCOMMAND,
},
}
g.Init(properties.Map{}, env)
got := g.Commit()
assert.Equal(t, tc.Expected, got, tc.Case)
}
@ -1148,11 +1160,10 @@ func TestGitRemotes(t *testing.T) {
g := &Git{
scm: scm{
props: properties.Map{},
realDir: "foo",
env: env,
},
}
g.Init(properties.Map{}, env)
got := g.Remotes()
assert.Equal(t, tc.Expected, len(got), tc.Case)
@ -1194,13 +1205,12 @@ func TestGitRepoName(t *testing.T) {
g := &Git{
scm: scm{
props: properties.Map{},
env: env,
realDir: tc.RealDir,
workingDir: tc.WorkingDir,
},
IsWorkTree: tc.IsWorkTree,
}
g.Init(properties.Map{}, env)
got := g.repoName()
assert.Equal(t, tc.Expected, got, tc.Case)

View file

@ -2,9 +2,6 @@ package segments
import (
"encoding/json"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type gitVersion struct {
@ -44,8 +41,7 @@ type gitVersion struct {
}
type GitVersion struct {
props properties.Properties
env runtime.Environment
base
gitVersion
}
@ -70,8 +66,3 @@ func (n *GitVersion) Enabled() bool {
return err == nil
}
func (n *GitVersion) Init(props properties.Properties, env runtime.Environment) {
n.props = props
n.env = env
}

View file

@ -47,10 +47,9 @@ func TestGitversion(t *testing.T) {
env.On("Pwd").Return("test-dir")
env.On("RunCommand", "gitversion", []string{"-output", "json"}).Return(tc.Response, tc.CommandError)
gitversion := &GitVersion{
env: env,
props: properties.Map{},
}
gitversion := &GitVersion{}
gitversion.Init(properties.Map{}, env)
if len(tc.Template) == 0 {
tc.Template = gitversion.Template()
}

View file

@ -2,7 +2,6 @@ package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"golang.org/x/mod/modfile"
)
@ -19,26 +18,6 @@ func (g *Golang) Template() string {
return languageTemplate
}
func (g *Golang) Init(props properties.Properties, env runtime.Environment) {
g.language = language{
env: env,
props: props,
extensions: []string{"*.go", "go.mod"},
commands: []*cmd{
{
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?))`,
getVersion: g.getVersion,
},
{
executable: "go",
args: []string{"version"},
regex: `(?:go(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?)))`,
},
},
versionURLTemplate: "https://golang.org/doc/go{{ .Major }}.{{ .Minor }}",
}
}
func (g *Golang) getVersion() (string, error) {
if !g.props.GetBool(ParseModFile, false) {
return "", nil
@ -56,5 +35,19 @@ func (g *Golang) getVersion() (string, error) {
}
func (g *Golang) Enabled() bool {
g.extensions = []string{"*.go", "go.mod"}
g.commands = []*cmd{
{
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?))`,
getVersion: g.getVersion,
},
{
executable: "go",
args: []string{"version"},
regex: `(?:go(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?)))`,
},
}
g.versionURLTemplate = "https://golang.org/doc/go{{ .Major }}.{{ .Minor }}"
return g.language.Enabled()
}

View file

@ -2,7 +2,6 @@ package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Haskell struct {
@ -19,7 +18,7 @@ func (h *Haskell) Template() string {
return languageTemplate
}
func (h *Haskell) Init(props properties.Properties, env runtime.Environment) {
func (h *Haskell) Enabled() bool {
ghcRegex := `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`
ghcCmd := &cmd{
executable: "ghc",
@ -33,13 +32,9 @@ func (h *Haskell) Init(props properties.Properties, env runtime.Environment) {
regex: ghcRegex,
}
h.language = language{
env: env,
props: props,
extensions: []string{"*.hs", "*.lhs", "stack.yaml", "package.yaml", "*.cabal", "cabal.project"},
commands: []*cmd{ghcCmd},
versionURLTemplate: "https://www.haskell.org/ghc/download_ghc_{{ .Major }}_{{ .Minor }}_{{ .Patch }}.html",
}
h.extensions = []string{"*.hs", "*.lhs", "stack.yaml", "package.yaml", "*.cabal", "cabal.project"}
h.commands = []*cmd{ghcCmd}
h.versionURLTemplate = "https://www.haskell.org/ghc/download_ghc_{{ .Major }}_{{ .Minor }}_{{ .Patch }}.html"
switch h.props.GetString(StackGhcMode, "never") {
case "always":
@ -52,8 +47,6 @@ func (h *Haskell) Init(props properties.Properties, env runtime.Environment) {
h.StackGhc = true
}
}
}
func (h *Haskell) Enabled() bool {
return h.language.Enabled()
}

View file

@ -1,13 +1,7 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Helm struct {
props properties.Properties
env runtime.Environment
base
Version string
}
@ -34,11 +28,6 @@ func (h *Helm) Template() string {
return " Helm {{.Version}}"
}
func (h *Helm) Init(props properties.Properties, env runtime.Environment) {
h.props = props
h.env = env
}
func (h *Helm) getVersion() bool {
cmd := "helm"
if !h.env.HasCommand(cmd) {

View file

@ -93,10 +93,9 @@ func TestHelmSegment(t *testing.T) {
DisplayMode: tc.DisplayMode,
}
h := &Helm{
env: env,
props: props,
}
h := &Helm{}
h.Init(props, env)
assert.Equal(t, tc.ExpectedEnabled, h.Enabled(), tc.Case)
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, h.Template(), h), tc.Case)

View file

@ -4,7 +4,6 @@ import (
"net"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
)
@ -26,6 +25,8 @@ func (i *ipAPI) Get() (*ipData, error) {
}
type IPify struct {
base
api IPAPI
IP string
}
@ -41,6 +42,8 @@ func (i *IPify) Template() string {
}
func (i *IPify) Enabled() bool {
i.initAPI()
ip, err := i.getResult()
if err != nil {
return false
@ -55,16 +58,22 @@ func (i *IPify) getResult() (string, error) {
if dnsErr, OK := err.(*net.DNSError); OK && dnsErr.IsNotFound {
return OFFLINE, nil
}
if err != nil {
return "", err
}
return data.IP, err
}
func (i *IPify) Init(props properties.Properties, env runtime.Environment) {
func (i *IPify) initAPI() {
if i.api != nil {
return
}
request := &http.Request{
Env: env,
HTTPTimeout: props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout),
Env: i.env,
HTTPTimeout: i.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout),
}
i.api = &ipAPI{

View file

@ -11,10 +11,6 @@ import (
testify_ "github.com/stretchr/testify/mock"
)
const (
IPIFYAPIURL = "https://api.ipify.org"
)
type mockedipAPI struct {
testify_.Mock
}

View file

@ -2,9 +2,6 @@ package segments
import (
"fmt"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Java struct {
@ -15,36 +12,40 @@ func (j *Java) Template() string {
return languageTemplate
}
func (j *Java) Init(props properties.Properties, env runtime.Environment) {
func (j *Java) Enabled() bool {
j.init()
return j.language.Enabled()
}
func (j *Java) init() {
javaRegex := `(?: JRE)(?: \(.*\))? \((?P<version>(?P<major>[0-9]+)(?:\.(?P<minor>[0-9]+))?(?:\.(?P<patch>[0-9]+))?).*\),`
javaCmd := &cmd{
executable: "java",
args: []string{"-Xinternalversion"},
regex: javaRegex,
}
j.language = language{
env: env,
props: props,
extensions: []string{
"pom.xml",
"build.gradle.kts",
"build.sbt",
".java-version",
".deps.edn",
"project.clj",
"build.boot",
"*.java",
"*.class",
"*.gradle",
"*.jar",
"*.clj",
"*.cljc",
},
j.extensions = []string{
"pom.xml",
"build.gradle.kts",
"build.sbt",
".java-version",
".deps.edn",
"project.clj",
"build.boot",
"*.java",
"*.class",
"*.gradle",
"*.jar",
"*.clj",
"*.cljc",
}
javaHome := j.language.env.Getenv("JAVA_HOME")
javaHome := j.env.Getenv("JAVA_HOME")
if len(javaHome) > 0 {
java := fmt.Sprintf("%s/bin/java", javaHome)
j.language.commands = []*cmd{
j.commands = []*cmd{
{
executable: java,
args: []string{"-Xinternalversion"},
@ -54,9 +55,6 @@ func (j *Java) Init(props properties.Properties, env runtime.Environment) {
}
return
}
j.language.commands = []*cmd{javaCmd}
}
func (j *Java) Enabled() bool {
return j.language.Enabled()
j.commands = []*cmd{javaCmd}
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Julia struct {
language
}
@ -13,22 +8,16 @@ func (j *Julia) Template() string {
return languageTemplate
}
func (j *Julia) Init(props properties.Properties, env runtime.Environment) {
j.language = language{
env: env,
props: props,
extensions: []string{"*.jl"},
commands: []*cmd{
{
executable: "julia",
args: []string{"--version"},
regex: `julia version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/JuliaLang/julia/releases/tag/v{{ .Full }}",
}
}
func (j *Julia) Enabled() bool {
j.extensions = []string{"*.jl"}
j.commands = []*cmd{
{
executable: "julia",
args: []string{"--version"},
regex: `julia version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
j.versionURLTemplate = "https://github.com/JuliaLang/julia/releases/tag/v{{ .Full }}"
return j.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Kotlin struct {
language
}
@ -13,22 +8,16 @@ func (k *Kotlin) Template() string {
return languageTemplate
}
func (k *Kotlin) Init(props properties.Properties, env runtime.Environment) {
k.language = language{
env: env,
props: props,
extensions: []string{"*.kt", "*.kts", "*.ktm"},
commands: []*cmd{
{
executable: "kotlin",
args: []string{"-version"},
regex: `Kotlin version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/JetBrains/kotlin/releases/tag/v{{ .Full }}",
}
}
func (k *Kotlin) Enabled() bool {
k.extensions = []string{"*.kt", "*.kts", "*.ktm"}
k.commands = []*cmd{
{
executable: "kotlin",
args: []string{"-version"},
regex: `Kotlin version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
k.versionURLTemplate = "https://github.com/JetBrains/kotlin/releases/tag/v{{ .Full }}"
return k.language.Enabled()
}

View file

@ -4,7 +4,6 @@ import (
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"gopkg.in/yaml.v3"
)
@ -17,8 +16,8 @@ const (
)
type Kubectl struct {
props properties.Properties
env runtime.Environment
base
KubeContext
Context string
dirty bool
@ -42,11 +41,6 @@ func (k *Kubectl) Template() string {
return " {{ .Context }}{{ if .Namespace }} :: {{ .Namespace }}{{ end }} "
}
func (k *Kubectl) Init(props properties.Properties, env runtime.Environment) {
k.props = props
k.env = env
}
func (k *Kubectl) Enabled() bool {
parseKubeConfig := k.props.GetBool(ParseKubeConfig, true)

View file

@ -158,15 +158,15 @@ func TestKubectlSegment(t *testing.T) {
env.On("Home").Return("testhome")
k := &Kubectl{
env: env,
props: properties.Map{
properties.DisplayError: tc.DisplayError,
ParseKubeConfig: tc.ParseKubeConfig,
ContextAliases: tc.ContextAliases,
},
props := properties.Map{
properties.DisplayError: tc.DisplayError,
ParseKubeConfig: tc.ParseKubeConfig,
ContextAliases: tc.ContextAliases,
}
k := &Kubectl{}
k.Init(props, env)
assert.Equal(t, tc.ExpectedEnabled, k.Enabled(), tc.Case)
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, k), tc.Case)

View file

@ -60,8 +60,8 @@ func (c *cmd) parse(versionInfo string) (*version, error) {
}
type language struct {
env runtime.Environment
props properties.Properties
base
projectRoot *runtime.FileInfo
loadContext loadContext
inContext inContext

View file

@ -64,13 +64,12 @@ func bootStrapLanguageTest(args *languageArgs) *language {
}
l := &language{
props: args.properties,
env: env,
extensions: args.extensions,
commands: args.commands,
versionURLTemplate: args.versionURLTemplate,
matchesVersionFile: args.matchesVersionFile,
}
l.Init(args.properties, env)
return l
}

View file

@ -6,12 +6,10 @@ import (
"fmt"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type LastFM struct {
props properties.Properties
env runtime.Environment
base
Artist string
Track string
@ -120,8 +118,3 @@ func (d *LastFM) setStatus() error {
return nil
}
func (d *LastFM) Init(props properties.Properties, env runtime.Environment) {
d.props = props
d.env = env
}

View file

@ -63,20 +63,18 @@ func TestLFMSegmentSingle(t *testing.T) {
env.On("HTTPRequest", LFMAPIURL).Return([]byte(tc.APIJSONResponse), tc.Error)
env.On("Error", testify_.Anything)
o := &LastFM{
props: props,
env: env,
}
lfm := &LastFM{}
lfm.Init(props, env)
enabled := o.Enabled()
enabled := lfm.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if !enabled {
continue
}
if tc.Template == "" {
tc.Template = o.Template()
tc.Template = lfm.Template()
}
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, o), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, lfm), tc.Case)
}
}

View file

@ -2,7 +2,6 @@ package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Lua struct {
@ -17,32 +16,27 @@ func (l *Lua) Template() string {
return languageTemplate
}
func (l *Lua) Init(props properties.Properties, env runtime.Environment) {
l.language = language{
env: env,
props: props,
extensions: []string{"*.lua", "*.rockspec"},
folders: []string{"lua"},
commands: []*cmd{
{
executable: "lua",
args: []string{"-v"},
regex: `Lua (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?))`,
versionURLTemplate: "https://www.lua.org/manual/{{ .Major }}.{{ .Minor }}/readme.html#changes",
},
{
executable: "luajit",
args: []string{"-v"},
regex: `LuaJIT (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?))`,
versionURLTemplate: "https://github.com/LuaJIT/LuaJIT/tree/v{{ .Major}}.{{ .Minor}}",
},
func (l *Lua) Enabled() bool {
l.extensions = []string{"*.lua", "*.rockspec"}
l.folders = []string{"lua"}
l.commands = []*cmd{
{
executable: "lua",
args: []string{"-v"},
regex: `Lua (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?))`,
versionURLTemplate: "https://www.lua.org/manual/{{ .Major }}.{{ .Minor }}/readme.html#changes",
},
{
executable: "luajit",
args: []string{"-v"},
regex: `LuaJIT (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+)(.(?P<patch>[0-9]+))?))`,
versionURLTemplate: "https://github.com/LuaJIT/LuaJIT/tree/v{{ .Major}}.{{ .Minor}}",
},
}
if l.props.GetString(PreferredExecutable, "lua") == "luajit" {
l.commands = []*cmd{l.commands[1], l.commands[0]}
}
}
func (l *Lua) Enabled() bool {
return l.language.Enabled()
}

View file

@ -16,12 +16,8 @@ func TestMercurialEnabledToolNotFound(t *testing.T) {
env.On("GOOS").Return("")
env.On("IsWsl").Return(false)
hg := &Mercurial{
scm: scm{
env: env,
props: properties.Map{},
},
}
hg := &Mercurial{}
hg.Init(properties.Map{}, env)
assert.False(t, hg.Enabled())
}
@ -42,12 +38,8 @@ func TestMercurialEnabledInWorkingDirectory(t *testing.T) {
env.On("Home").Return(poshHome)
env.On("Getenv", poshGitEnv).Return("")
hg := &Mercurial{
scm: scm{
env: env,
props: properties.Map{},
},
}
hg := &Mercurial{}
hg.Init(properties.Map{}, env)
assert.True(t, hg.Enabled())
assert.Equal(t, fileInfo.Path, hg.workingDir)
@ -142,6 +134,7 @@ A Added.File
ParentFolder: "/dir",
IsDir: true,
}
props := properties.Map{
FetchStatus: true,
}
@ -158,12 +151,8 @@ A Added.File
env.MockHgCommand(fileInfo.Path, tc.LogOutput, "log", "-r", ".", "--template", hgLogTemplate)
env.MockHgCommand(fileInfo.Path, tc.StatusOutput, "status")
hg := &Mercurial{
scm: scm{
env: env,
props: props,
},
}
hg := &Mercurial{}
hg.Init(props, env)
if tc.ExpectedWorking != nil {
tc.ExpectedWorking.Formats = map[string]string{}

View file

@ -1,39 +1,28 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Mvn struct {
language
}
func (m *Mvn) Enabled() bool {
m.extensions = []string{"pom.xml"}
m.commands = []*cmd{
{
executable: "mvn",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)(?:-(?P<prerelease>[a-z]+-[0-9]+))?))`,
},
}
m.versionURLTemplate = "https://github.com/apache/maven/releases/tag/maven-{{ .Full }}"
mvnw, err := m.env.HasParentFilePath("mvnw", false)
if err == nil {
m.commands[0].executable = mvnw.Path
}
return m.language.Enabled()
}
func (m *Mvn) Template() string {
return languageTemplate
}
func (m *Mvn) Init(props properties.Properties, env runtime.Environment) {
m.language = language{
env: env,
props: props,
extensions: []string{"pom.xml"},
commands: []*cmd{
{
executable: "mvn",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)(?:-(?P<prerelease>[a-z]+-[0-9]+))?))`,
},
},
versionURLTemplate: "https://github.com/apache/maven/releases/tag/maven-{{ .Full }}",
}
mvnw, err := m.language.env.HasParentFilePath("mvnw", false)
if err == nil {
m.language.commands[0].executable = mvnw.Path
}
}

View file

@ -7,13 +7,11 @@ import (
"time"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
// segment struct, makes templating easier
type Nba struct {
props properties.Properties
env runtime.Environment
base
NBAData
}
@ -310,8 +308,3 @@ func (nba *Nba) getResult() (*NBAData, error) {
return data, nil
}
func (nba *Nba) Init(props properties.Properties, env runtime.Environment) {
nba.props = props
nba.env = env
}

View file

@ -94,10 +94,8 @@ func TestNBASegment(t *testing.T) {
env.On("HTTPRequest", scheduleURLEndpoint).Return([]byte(tc.JSONResponse), tc.Error)
}
nba := &Nba{
props: props,
env: env,
}
nba := &Nba{}
nba.Init(props, env)
enabled := nba.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)

View file

@ -2,14 +2,10 @@ package segments
import (
"encoding/json"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Nbgv struct {
props properties.Properties
env runtime.Environment
base
VersionInfo
}
@ -45,8 +41,3 @@ func (n *Nbgv) Enabled() bool {
}
return n.VersionInfo.VersionFileFound
}
func (n *Nbgv) Init(props properties.Properties, env runtime.Environment) {
n.props = props
n.env = env
}

View file

@ -63,11 +63,12 @@ func TestNbgv(t *testing.T) {
env := new(mock.Environment)
env.On("HasCommand", "nbgv").Return(tc.HasNbgv)
env.On("RunCommand", "nbgv", []string{"get-version", "--format=json"}).Return(tc.Response, tc.Error)
nbgv := &Nbgv{
env: env,
props: properties.Map{},
}
nbgv := &Nbgv{}
nbgv.Init(properties.Map{}, env)
enabled := nbgv.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if tc.Template == "" {
tc.Template = nbgv.Template()

View file

@ -7,13 +7,12 @@ import (
"time"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
// segment struct, makes templating easier
type Nightscout struct {
props properties.Properties
env runtime.Environment
base
TrendIcon string
NightscoutData
}
@ -124,8 +123,3 @@ func (ns *Nightscout) getResult() (*NightscoutData, error) {
return data, nil
}
func (ns *Nightscout) Init(props properties.Properties, env runtime.Environment) {
ns.props = props
ns.env = env
}

View file

@ -120,10 +120,8 @@ func TestNSSegment(t *testing.T) {
env.On("HTTPRequest", FAKEAPIURL).Return([]byte(tc.JSONResponse), tc.Error)
ns := &Nightscout{
props: props,
env: env,
}
ns := &Nightscout{}
ns.Init(props, env)
enabled := ns.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)

View file

@ -3,9 +3,6 @@ package segments
import (
"path/filepath"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
const (
@ -13,8 +10,7 @@ const (
)
type NixShell struct {
props properties.Properties
env runtime.Environment
base
Type string
}
@ -53,11 +49,6 @@ func (n *NixShell) InNewNixShell() bool {
return false
}
func (n *NixShell) Init(props properties.Properties, env runtime.Environment) {
n.props = props
n.env = env
}
func (n *NixShell) Enabled() bool {
n.Type = n.DetectType()

View file

@ -6,7 +6,6 @@ import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/regex"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Node struct {
@ -29,25 +28,19 @@ func (n *Node) Template() string {
return " {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} "
}
func (n *Node) Init(props properties.Properties, env runtime.Environment) {
n.language = language{
env: env,
props: props,
extensions: []string{"*.js", "*.ts", "package.json", ".nvmrc", "pnpm-workspace.yaml", ".pnpmfile.cjs", ".vue"},
commands: []*cmd{
{
executable: "node",
args: []string{"--version"},
regex: `(?:v(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
versionURLTemplate: "https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V{{ .Major }}.md#{{ .Full }}",
matchesVersionFile: n.matchesVersionFile,
loadContext: n.loadContext,
}
}
func (n *Node) Enabled() bool {
n.extensions = []string{"*.js", "*.ts", "package.json", ".nvmrc", "pnpm-workspace.yaml", ".pnpmfile.cjs", ".vue"}
n.commands = []*cmd{
{
executable: "node",
args: []string{"--version"},
regex: `(?:v(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
n.versionURLTemplate = "https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V{{ .Major }}.md#{{ .Full }}"
n.language.matchesVersionFile = n.matchesVersionFile
n.language.loadContext = n.loadContext
return n.language.Enabled()
}

View file

@ -42,10 +42,11 @@ func TestNodeMatchesVersionFile(t *testing.T) {
node := &Node{
language: language{
env: env,
version: nodeVersion,
},
}
node.Init(properties.Map{}, env)
version, match := node.matchesVersionFile()
assert.Equal(t, tc.Expected, match, tc.Case)
assert.Equal(t, tc.ExpectedVersion, version, tc.Case)
@ -80,17 +81,17 @@ func TestNodeInContext(t *testing.T) {
env.On("HasFiles", "yarn.lock").Return(tc.hasYarn)
env.On("HasFiles", "package-lock.json").Return(tc.hasNPM)
env.On("HasFiles", "package.json").Return(tc.hasDefault)
node := &Node{
language: language{
env: env,
props: properties.Map{
PnpmIcon: "pnpm",
YarnIcon: "yarn",
NPMIcon: "npm",
FetchPackageManager: tc.PkgMgrEnabled,
},
},
props := properties.Map{
PnpmIcon: "pnpm",
YarnIcon: "yarn",
NPMIcon: "npm",
FetchPackageManager: tc.PkgMgrEnabled,
}
node := &Node{}
node.Init(props, env)
node.loadContext()
assert.Equal(t, tc.ExpectedString, node.PackageManagerIcon, tc.Case)
}

View file

@ -1,34 +1,23 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Npm struct {
language
}
func (n *Npm) Enabled() bool {
n.extensions = []string{"package.json", "package-lock.json"}
n.commands = []*cmd{
{
executable: "npm",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
n.versionURLTemplate = "https://github.com/npm/cli/releases/tag/v{{ .Full }}"
return n.language.Enabled()
}
func (n *Npm) Template() string {
return " \ue71e {{.Full}} "
}
func (n *Npm) Init(props properties.Properties, env runtime.Environment) {
n.language = language{
env: env,
props: props,
extensions: []string{"package.json", "package-lock.json"},
commands: []*cmd{
{
executable: "npm",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/npm/cli/releases/tag/v{{ .Full }}",
}
}

View file

@ -5,7 +5,6 @@ import (
"fmt"
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
@ -17,22 +16,16 @@ func (a *Nx) Template() string {
return languageTemplate
}
func (a *Nx) Init(props properties.Properties, env runtime.Environment) {
a.language = language{
env: env,
props: props,
extensions: []string{"workspace.json", "nx.json"},
commands: []*cmd{
{
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
getVersion: a.getVersion,
},
},
versionURLTemplate: "https://github.com/nrwl/nx/releases/tag/{{.Full}}",
}
}
func (a *Nx) Enabled() bool {
a.extensions = []string{"workspace.json", "nx.json"}
a.commands = []*cmd{
{
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
getVersion: a.getVersion,
},
}
a.versionURLTemplate = "https://github.com/nrwl/nx/releases/tag/{{.Full}}"
return a.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type OCaml struct {
language
}
@ -13,21 +8,15 @@ func (o *OCaml) Template() string {
return languageTemplate
}
func (o *OCaml) Init(props properties.Properties, env runtime.Environment) {
o.language = language{
env: env,
props: props,
extensions: []string{"*.ml", "*.mli", "dune", "dune-project", "dune-workspace"},
commands: []*cmd{
{
executable: "ocaml",
args: []string{"-version"},
regex: `The OCaml toplevel, version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))(-(?P<prerelease>[a-z]+))?)`,
},
func (o *OCaml) Enabled() bool {
o.extensions = []string{"*.ml", "*.mli", "dune", "dune-project", "dune-workspace"}
o.commands = []*cmd{
{
executable: "ocaml",
args: []string{"-version"},
regex: `The OCaml toplevel, version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))(-(?P<prerelease>[a-z]+))?)`,
},
}
}
func (o *OCaml) Enabled() bool {
return o.language.Enabled()
}

View file

@ -6,8 +6,7 @@ import (
)
type Os struct {
props properties.Properties
env runtime.Environment
base
Icon string
}
@ -91,8 +90,3 @@ func (oi *Os) getDistroIcon(distro string) string {
return oi.props.GetString(Linux, "\uF17C")
}
func (oi *Os) Init(props properties.Properties, env runtime.Environment) {
oi.props = props
oi.env = env
}

View file

@ -103,10 +103,8 @@ func TestOSInfo(t *testing.T) {
props[properties.Property(tc.Platform)] = tc.Icon
}
osInfo := &Os{
env: env,
props: props,
}
osInfo := &Os{}
osInfo.Init(props, env)
_ = osInfo.Enabled()
assert.Equal(t, tc.ExpectedString, renderTemplate(env, osInfo.Template(), osInfo), tc.Case)

View file

@ -8,12 +8,11 @@ import (
"net/url"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Owm struct {
props properties.Properties
env runtime.Environment
base
Weather string
URL string
units string
@ -167,8 +166,3 @@ func (d *Owm) setStatus() error {
}
return nil
}
func (d *Owm) Init(props properties.Properties, env runtime.Environment) {
d.props = props
d.env = env
}

View file

@ -84,10 +84,8 @@ func TestOWMSegmentSingle(t *testing.T) {
env.On("HTTPRequest", testURL).Return([]byte(tc.WeatherJSONResponse), tc.Error)
env.On("Error", testify_.Anything)
o := &Owm{
props: props,
env: env,
}
o := &Owm{}
o.Init(props, env)
enabled := o.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
@ -212,15 +210,15 @@ func TestOWMSegmentIcons(t *testing.T) {
env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil)
o := &Owm{
props: properties.Map{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
},
env: env,
props := properties.Map{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
}
o := &Owm{}
o.Init(props, env)
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, renderTemplate(env, o.Template(), o), tc.Case)
}
@ -234,15 +232,15 @@ func TestOWMSegmentIcons(t *testing.T) {
env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil)
o := &Owm{
props: properties.Map{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
},
env: env,
props := properties.Map{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
}
o := &Owm{}
o.Init(props, env)
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, renderTemplate(env, "«{{.Weather}} ({{.Temperature}}{{.UnitIcon}})»({{.URL}})", o), tc.Case)
}

View file

@ -32,8 +32,8 @@ func (f Folders) List() []string {
}
type Path struct {
props properties.Properties
env runtime.Environment
base
mappedLocations map[string]string
root string
relative string
@ -193,11 +193,6 @@ func (pt *Path) Parent() string {
return sb.String()
}
func (pt *Path) Init(props properties.Properties, env runtime.Environment) {
pt.props = props
pt.env = env
}
func (pt *Path) setStyle() {
if len(pt.relative) == 0 {
root := pt.root

View file

@ -155,13 +155,14 @@ func TestParent(t *testing.T) {
env.On("Shell").Return(shell.GENERIC)
env.On("PathSeparator").Return(tc.PathSeparator)
env.On("GOOS").Return(tc.GOOS)
path := &Path{
env: env,
props: properties.Map{
FolderSeparatorIcon: tc.FolderSeparatorIcon,
},
props := properties.Map{
FolderSeparatorIcon: tc.FolderSeparatorIcon,
}
path := &Path{}
path.Init(props, env)
path.setPaths()
got := path.Parent()
@ -795,18 +796,18 @@ func TestAgnosterPathStyles(t *testing.T) {
env.On("RunCommand", "cygpath", testify_.Anything).Return("brrrr", nil)
}
path := &Path{
env: env,
props: properties.Map{
FolderSeparatorIcon: tc.FolderSeparatorIcon,
properties.Style: tc.Style,
MaxDepth: tc.MaxDepth,
MaxWidth: tc.MaxWidth,
HideRootLocation: tc.HideRootLocation,
DisplayCygpath: displayCygpath,
},
props := properties.Map{
FolderSeparatorIcon: tc.FolderSeparatorIcon,
properties.Style: tc.Style,
MaxDepth: tc.MaxDepth,
MaxWidth: tc.MaxWidth,
HideRootLocation: tc.HideRootLocation,
DisplayCygpath: displayCygpath,
}
path := &Path{}
path.Init(props, env)
path.setPaths()
path.setStyle()
got := renderTemplateNoTrimSpace(env, "{{ .Path }}", path)
@ -933,11 +934,12 @@ func TestFullAndFolderPath(t *testing.T) {
if tc.DisableMappedLocations {
props[MappedLocationsEnabled] = false
}
path := &Path{
env: env,
props: props,
StackCount: env.StackCount(),
}
path.Init(props, env)
path.setPaths()
path.setStyle()
got := renderTemplateNoTrimSpace(env, tc.Template, path)
@ -992,15 +994,15 @@ func TestFullPathCustomMappedLocations(t *testing.T) {
template.Init(env)
path := &Path{
env: env,
props: properties.Map{
properties.Style: Full,
MappedLocationsEnabled: false,
MappedLocations: tc.MappedLocations,
},
props := properties.Map{
properties.Style: Full,
MappedLocationsEnabled: false,
MappedLocations: tc.MappedLocations,
}
path := &Path{}
path.Init(props, env)
path.setPaths()
path.setStyle()
@ -1026,16 +1028,16 @@ func TestFolderPathCustomMappedLocations(t *testing.T) {
template.Init(env)
path := &Path{
env: env,
props: properties.Map{
properties.Style: FolderType,
MappedLocations: map[string]string{
abcd: "#",
},
props := properties.Map{
properties.Style: FolderType,
MappedLocations: map[string]string{
abcd: "#",
},
}
path := &Path{}
path.Init(props, env)
path.setPaths()
path.setStyle()
@ -1207,17 +1209,19 @@ func TestAgnosterPath(t *testing.T) {
}
env.On("Flags").Return(args)
env.On("Shell").Return(shell.PWSH)
path := &Path{
env: env,
props: properties.Map{
properties.Style: Agnoster,
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
Cycle: tc.Cycle,
CycleFolderSeparator: tc.ColorSeparator,
},
props := properties.Map{
properties.Style: Agnoster,
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
Cycle: tc.Cycle,
CycleFolderSeparator: tc.ColorSeparator,
}
path := &Path{}
path.Init(props, env)
path.setPaths()
path.setStyle()
got := renderTemplateNoTrimSpace(env, "{{ .Path }}", path)
@ -1363,15 +1367,17 @@ func TestAgnosterLeftPath(t *testing.T) {
}
env.On("Flags").Return(args)
env.On("Shell").Return(shell.PWSH)
path := &Path{
env: env,
props: properties.Map{
properties.Style: AgnosterLeft,
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
},
props := properties.Map{
properties.Style: AgnosterLeft,
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
}
path := &Path{}
path.Init(props, env)
path.setPaths()
path.setStyle()
got := renderTemplateNoTrimSpace(env, "{{ .Path }}", path)
@ -1421,16 +1427,16 @@ func TestGetPwd(t *testing.T) {
template.Init(env)
path := &Path{
env: env,
props: properties.Map{
MappedLocationsEnabled: tc.MappedLocationsEnabled,
MappedLocations: map[string]string{
abcd: "#",
},
props := properties.Map{
MappedLocationsEnabled: tc.MappedLocationsEnabled,
MappedLocations: map[string]string{
abcd: "#",
},
}
path := &Path{}
path.Init(props, env)
path.setPaths()
assert.Equal(t, tc.Expected, path.pwd)
}
@ -1457,14 +1463,11 @@ func TestGetFolderSeparator(t *testing.T) {
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
env.On("Shell").Return(shell.GENERIC)
env.On("Trace", testify_.Anything, testify_.Anything).Return(nil)
env.On("TemplateCache").Return(&cache.Template{
Shell: "bash",
})
template.Init(env)
path := &Path{
env: env,
pathSeparator: "/",
}
props := properties.Map{}
if len(tc.FolderSeparatorTemplate) > 0 {
@ -1475,11 +1478,11 @@ func TestGetFolderSeparator(t *testing.T) {
props[FolderSeparatorIcon] = tc.FolderSeparatorIcon
}
env.On("TemplateCache").Return(&cache.Template{
Shell: "bash",
})
path := &Path{
pathSeparator: "/",
}
path.Init(props, env)
path.props = props
got := path.getFolderSeparator()
assert.Equal(t, tc.Expected, got)
}
@ -1614,7 +1617,10 @@ func TestNormalizePath(t *testing.T) {
}
env.On("PathSeparator").Return(tc.PathSeparator)
pt := &Path{env: env}
pt := &Path{}
pt.Init(properties.Map{}, env)
got := pt.normalize(tc.Input)
assert.Equal(t, tc.Expected, got, tc.Case)
}
@ -1650,20 +1656,20 @@ func TestReplaceMappedLocations(t *testing.T) {
template.Init(env)
path := &Path{
env: env,
props: properties.Map{
MappedLocationsEnabled: tc.MappedLocationsEnabled,
MappedLocations: map[string]string{
abcd: "#",
"/f/g/h/*": "^",
"/c/l/k/*": "",
"~": "@",
"~/j/*": "",
},
props := properties.Map{
MappedLocationsEnabled: tc.MappedLocationsEnabled,
MappedLocations: map[string]string{
abcd: "#",
"/f/g/h/*": "^",
"/c/l/k/*": "",
"~": "@",
"~/j/*": "",
},
}
path := &Path{}
path.Init(props, env)
path.setPaths()
assert.Equal(t, tc.Expected, path.pwd)
}
@ -1725,16 +1731,17 @@ func TestSplitPath(t *testing.T) {
env.On("HasParentFilePath", ".git", false).Return(tc.GitDir, nil)
env.On("GOOS").Return(tc.GOOS)
props := properties.Map{
GitDirFormat: tc.GitDirFormat,
}
path := &Path{
env: env,
props: properties.Map{
GitDirFormat: tc.GitDirFormat,
},
root: tc.Root,
relative: tc.Relative,
pathSeparator: "/",
windowsPath: tc.GOOS == runtime.WINDOWS,
}
path.Init(props, env)
got := path.splitPath()
assert.Equal(t, tc.Expected, got, tc.Case)
@ -1779,13 +1786,13 @@ func TestGetMaxWidth(t *testing.T) {
template.Init(env)
path := &Path{
env: env,
props: properties.Map{
MaxWidth: tc.MaxWidth,
},
props := properties.Map{
MaxWidth: tc.MaxWidth,
}
path := &Path{}
path.Init(props, env)
got := path.getMaxWidth()
assert.Equal(t, tc.Expected, got, tc.Case)
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Perl struct {
language
}
@ -13,27 +8,21 @@ func (p *Perl) Template() string {
return languageTemplate
}
func (p *Perl) Init(props properties.Properties, env runtime.Environment) {
func (p *Perl) Enabled() bool {
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,
},
p.extensions = []string{
".perl-version",
"*.pl",
"*.pm",
"*.t",
}
p.commands = []*cmd{
{
executable: "perl",
args: []string{"-version"},
regex: perlRegex,
},
}
}
func (p *Perl) Enabled() bool {
return p.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Php struct {
language
}
@ -13,22 +8,16 @@ func (p *Php) Template() string {
return languageTemplate
}
func (p *Php) Init(props properties.Properties, env runtime.Environment) {
p.language = language{
env: env,
props: props,
extensions: []string{"*.php", "composer.json", "composer.lock", ".php-version", "blade.php"},
commands: []*cmd{
{
executable: "php",
args: []string{"--version"},
regex: `(?:PHP (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
versionURLTemplate: "https://www.php.net/ChangeLog-{{ .Major }}.php#PHP_{{ .Major }}_{{ .Minor }}",
}
}
func (p *Php) Enabled() bool {
p.extensions = []string{"*.php", "composer.json", "composer.lock", ".php-version", "blade.php"}
p.commands = []*cmd{
{
executable: "php",
args: []string{"--version"},
regex: `(?:PHP (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
p.versionURLTemplate = "https://www.php.net/ChangeLog-{{ .Major }}.php#PHP_{{ .Major }}_{{ .Minor }}"
return p.language.Enabled()
}

View file

@ -15,12 +15,10 @@ func TestPlasticEnabledNotFound(t *testing.T) {
env.On("HasCommand", "cm").Return(false)
env.On("GOOS").Return("")
env.On("IsWsl").Return(false)
p := &Plastic{
scm: scm{
env: env,
props: properties.Map{},
},
}
p := &Plastic{}
p.Init(properties.Map{}, env)
assert.False(t, p.Enabled())
}
@ -36,12 +34,10 @@ func TestPlasticEnabledInWorkspaceDirectory(t *testing.T) {
IsDir: true,
}
env.On("HasParentFilePath", ".plastic", false).Return(fileInfo, nil)
p := &Plastic{
scm: scm{
env: env,
props: properties.Map{},
},
}
p := &Plastic{}
p.Init(properties.Map{}, env)
assert.True(t, p.Enabled())
assert.Equal(t, fileInfo.ParentFolder, p.plasticWorkspaceFolder)
}
@ -50,12 +46,10 @@ func setupCmStatusEnv(status, headStatus string) *Plastic {
env := new(mock.Environment)
env.On("RunCommand", "cm", []string{"status", "--all", "--machinereadable"}).Return(status, nil)
env.On("RunCommand", "cm", []string{"status", "--head", "--machinereadable"}).Return(headStatus, nil)
p := &Plastic{
scm: scm{
env: env,
props: properties.Map{},
},
}
p := &Plastic{}
p.Init(properties.Map{}, env)
return p
}

View file

@ -1,34 +1,23 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Pnpm struct {
language
}
func (n *Pnpm) Enabled() bool {
n.extensions = []string{"package.json", "pnpm-lock.yaml"}
n.commands = []*cmd{
{
executable: "pnpm",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
n.versionURLTemplate = "https://github.com/pnpm/pnpm/releases/tag/v{{ .Full }}"
return n.language.Enabled()
}
func (n *Pnpm) Template() string {
return " \U000F02C1 {{.Full}} "
}
func (n *Pnpm) Init(props properties.Properties, env runtime.Environment) {
n.language = language{
env: env,
props: props,
extensions: []string{"package.json", "pnpm-lock.yaml"},
commands: []*cmd{
{
executable: "pnpm",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/pnpm/pnpm/releases/tag/v{{ .Full }}",
}
}

View file

@ -191,15 +191,16 @@ func TestPoshGitSegment(t *testing.T) {
env.On("RunCommand", "git", []string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false",
"-c", "color.status=false", "remote", "get-url", "origin"}).Return("github.com/cli", nil)
props := &properties.Map{
FetchUpstreamIcon: tc.FetchUpstreamIcon,
}
g := &Git{
scm: scm{
env: env,
props: &properties.Map{
FetchUpstreamIcon: tc.FetchUpstreamIcon,
},
command: GITCOMMAND,
},
}
g.Init(props, env)
if len(tc.Template) == 0 {
tc.Template = g.Template()
@ -236,11 +237,9 @@ func TestParsePoshGitHEAD(t *testing.T) {
}
for _, tc := range cases {
g := &Git{
scm: scm{
props: &properties.Map{},
},
}
g := &Git{}
g.Init(&properties.Map{}, new(mock.Environment))
assert.Equal(t, tc.ExpectedString, g.parsePoshGitHEAD(tc.HEAD), tc.Case)
}
}

View file

@ -9,7 +9,6 @@ import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/regex"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"golang.org/x/exp/slices"
toml "github.com/pelletier/go-toml/v2"
@ -52,36 +51,14 @@ type NuSpec struct {
}
type Project struct {
props properties.Properties
env runtime.Environment
base
ProjectData
Error string
projects []*ProjectItem
}
func (n *Project) Enabled() bool {
for _, item := range n.projects {
if n.hasProjectFile(item) {
data := item.Fetcher(*item)
if data == nil {
continue
}
n.ProjectData = *data
n.ProjectData.Type = item.Name
return true
}
}
return n.props.GetBool(properties.AlwaysEnabled, false)
}
func (n *Project) Template() string {
return " {{ if .Error }}{{ .Error }}{{ else }}{{ if .Version }}\uf487 {{.Version}} {{ end }}{{ if .Name }}{{ .Name }} {{ end }}{{ if .Target }}\uf4de {{.Target}} {{ end }}{{ end }}" //nolint:lll
}
func (n *Project) Init(props properties.Properties, env runtime.Environment) {
n.props = props
n.env = env
n.projects = []*ProjectItem{
{
Name: "node",
@ -124,6 +101,23 @@ func (n *Project) Init(props properties.Properties, env runtime.Environment) {
Fetcher: n.getPowerShellModuleData,
},
}
for _, item := range n.projects {
if n.hasProjectFile(item) {
data := item.Fetcher(*item)
if data == nil {
continue
}
n.ProjectData = *data
n.ProjectData.Type = item.Name
return true
}
}
return n.props.GetBool(properties.AlwaysEnabled, false)
}
func (n *Project) Template() string {
return " {{ if .Error }}{{ .Error }}{{ else }}{{ if .Version }}\uf487 {{.Version}} {{ end }}{{ if .Name }}{{ .Name }} {{ end }}{{ if .Target }}\uf4de {{.Target}} {{ end }}{{ end }}" //nolint:lll
}
func (n *Project) hasProjectFile(p *ProjectItem) bool {

View file

@ -8,7 +8,6 @@ import (
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"gopkg.in/yaml.v3"
)
@ -24,8 +23,7 @@ const (
)
type Pulumi struct {
props properties.Properties
env runtime.Environment
base
Stack string
Name string
@ -52,11 +50,6 @@ func (p *Pulumi) Template() string {
return "\U000f0d46 {{ .Stack }}{{if .User }} :: {{ .User }}@{{ end }}{{ if .URL }}{{ .URL }}{{ end }}"
}
func (p *Pulumi) Init(props properties.Properties, env runtime.Environment) {
p.props = props
p.env = env
}
func (p *Pulumi) Enabled() bool {
if !p.env.HasCommand("pulumi") {
return false

View file

@ -180,14 +180,14 @@ description: A Console App
env.On("HasFilesInDir", filepath.Clean("/home/foobar/.pulumi/workspaces"), workspaceFile).Return(len(tc.WorkSpaceFile) > 0)
env.On("FileContent", filepath.Clean("/home/foobar/.pulumi/workspaces/"+workspaceFile)).Return(tc.WorkSpaceFile, nil)
pulumi := &Pulumi{
env: env,
props: properties.Map{
FetchStack: tc.FetchStack,
FetchAbout: tc.FetchAbout,
},
props := properties.Map{
FetchStack: tc.FetchStack,
FetchAbout: tc.FetchAbout,
}
pulumi := &Pulumi{}
pulumi.Init(props, env)
assert.Equal(t, tc.ExpectedEnabled, pulumi.Enabled(), tc.Case)
if !tc.ExpectedEnabled {

View file

@ -28,41 +28,35 @@ func (p *Python) Template() string {
return " {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ end }} "
}
func (p *Python) Init(props properties.Properties, env runtime.Environment) {
p.language = language{
env: env,
props: props,
extensions: []string{"*.py", "*.ipynb", "pyproject.toml", "venv.bak"},
folders: []string{".venv", "venv", "virtualenv", "venv-win", "pyenv-win"},
loadContext: p.loadContext,
inContext: p.inContext,
commands: []*cmd{
{
getVersion: p.pyenvVersion,
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
{
executable: "python",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
{
executable: "python3",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
{
executable: "py",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
versionURLTemplate: "https://docs.python.org/release/{{ .Major }}.{{ .Minor }}.{{ .Patch }}/whatsnew/changelog.html#python-{{ .Major }}-{{ .Minor }}-{{ .Patch }}",
displayMode: props.GetString(DisplayMode, DisplayModeEnvironment),
}
}
func (p *Python) Enabled() bool {
p.extensions = []string{"*.py", "*.ipynb", "pyproject.toml", "venv.bak"}
p.folders = []string{".venv", "venv", "virtualenv", "venv-win", "pyenv-win"}
p.commands = []*cmd{
{
getVersion: p.pyenvVersion,
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
{
executable: "python",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
{
executable: "python3",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
{
executable: "py",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
p.versionURLTemplate = "https://docs.python.org/release/{{ .Major }}.{{ .Minor }}.{{ .Patch }}/whatsnew/changelog.html#python-{{ .Major }}-{{ .Minor }}-{{ .Patch }}"
p.displayMode = p.props.GetString(DisplayMode, DisplayModeEnvironment)
p.language.loadContext = p.loadContext
p.language.inContext = p.inContext
return p.language.Enabled()
}

View file

@ -5,7 +5,6 @@ import (
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
const (
@ -25,11 +24,21 @@ type Quasar struct {
}
func (q *Quasar) Enabled() bool {
q.projectFiles = []string{"quasar.config", "quasar.config.js"}
q.commands = []*cmd{
{
executable: "quasar",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
}
q.versionURLTemplate = "https://github.com/quasarframework/quasar/releases/tag/quasar-v{{ .Full }}"
if !q.language.Enabled() {
return false
}
if q.language.props.GetBool(FetchDependencies, false) {
if q.props.GetBool(FetchDependencies, false) {
q.fetchDependencies()
}
@ -40,22 +49,6 @@ func (q *Quasar) Template() string {
return " \uea6a {{.Full}}{{ if .HasVite }} \ueb29 {{ .Vite.Version }}{{ end }} "
}
func (q *Quasar) Init(props properties.Properties, env runtime.Environment) {
q.language = language{
env: env,
props: props,
projectFiles: []string{"quasar.config", "quasar.config.js"},
commands: []*cmd{
{
executable: "quasar",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/quasarframework/quasar/releases/tag/quasar-v{{ .Full }}",
}
}
func (q *Quasar) fetchDependencies() {
if !q.language.env.HasFilesInDir(q.projectRoot.ParentFolder, "package-lock.json") {
return

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type R struct {
language
}
@ -13,33 +8,27 @@ func (r *R) Template() string {
return languageTemplate
}
func (r *R) Init(props properties.Properties, env runtime.Environment) {
rRegex := `version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`
r.language = language{
env: env,
props: props,
extensions: []string{"*.R", "*.Rmd", "*.Rsx", "*.Rda", "*.Rd", "*.Rproj", ".Rproj.user"},
commands: []*cmd{
{
executable: "Rscript",
args: []string{"--version"},
regex: rRegex,
},
{
executable: "R",
args: []string{"--version"},
regex: rRegex,
},
{
executable: "R.exe",
args: []string{"--version"},
regex: rRegex,
},
},
versionURLTemplate: "https://www.r-project.org/",
}
}
func (r *R) Enabled() bool {
rRegex := `version (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`
r.extensions = []string{"*.R", "*.Rmd", "*.Rsx", "*.Rda", "*.Rd", "*.Rproj", ".Rproj.user"}
r.commands = []*cmd{
{
executable: "Rscript",
args: []string{"--version"},
regex: rRegex,
},
{
executable: "R",
args: []string{"--version"},
regex: rRegex,
},
{
executable: "R.exe",
args: []string{"--version"},
regex: rRegex,
},
}
r.versionURLTemplate = "https://www.r-project.org/"
return r.language.Enabled()
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type React struct {
language
}
@ -13,22 +8,16 @@ func (r *React) Template() string {
return languageTemplate
}
func (r *React) Init(props properties.Properties, env runtime.Environment) {
r.language = language{
env: env,
props: props,
extensions: []string{"package.json"},
commands: []*cmd{
{
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
getVersion: r.getVersion,
},
},
versionURLTemplate: "https://github.com/facebook/react/releases/tag/v{{.Full}}",
}
}
func (r *React) Enabled() bool {
r.extensions = []string{"package.json"}
r.commands = []*cmd{
{
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
getVersion: r.getVersion,
},
}
r.versionURLTemplate = "https://github.com/facebook/react/releases/tag/v{{.Full}}"
return r.language.Enabled()
}

View file

@ -1,13 +1,7 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Root struct {
props properties.Properties
env runtime.Environment
base
}
func (rt *Root) Template() string {
@ -17,8 +11,3 @@ func (rt *Root) Template() string {
func (rt *Root) Enabled() bool {
return rt.env.Root()
}
func (rt *Root) Init(props properties.Properties, env runtime.Environment) {
rt.props = props
rt.env = env
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Ruby struct {
language
}
@ -13,46 +8,42 @@ func (r *Ruby) Template() string {
return languageTemplate
}
func (r *Ruby) Init(props properties.Properties, env runtime.Environment) {
r.language = language{
env: env,
props: props,
extensions: []string{"*.rb", "Rakefile", "Gemfile"},
commands: []*cmd{
{
executable: "rbenv",
args: []string{"version-name"},
regex: `(?P<version>.+)`,
},
{
executable: "rvm-prompt",
args: []string{"i", "v", "g"},
regex: `(?P<version>.+)`,
},
{
executable: "chruby",
args: []string(nil),
regex: `\* (?P<version>.+)\n`,
},
{
executable: "asdf",
args: []string{"current", "ruby"},
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
},
{
executable: "ruby",
args: []string{"--version"},
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
},
func (r *Ruby) Enabled() bool {
r.extensions = []string{"*.rb", "Rakefile", "Gemfile"}
r.commands = []*cmd{
{
executable: "rbenv",
args: []string{"version-name"},
regex: `(?P<version>.+)`,
},
{
executable: "rvm-prompt",
args: []string{"i", "v", "g"},
regex: `(?P<version>.+)`,
},
{
executable: "chruby",
args: []string(nil),
regex: `\* (?P<version>.+)\n`,
},
{
executable: "asdf",
args: []string{"current", "ruby"},
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
},
{
executable: "ruby",
args: []string{"--version"},
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
},
}
}
func (r *Ruby) Enabled() bool {
enabled := r.language.Enabled()
// this happens when no version is set
if r.Full == "______" {
r.Full = ""
}
return enabled
}

View file

@ -1,10 +1,5 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Rust struct {
language
}
@ -13,21 +8,15 @@ func (r *Rust) Template() string {
return languageTemplate
}
func (r *Rust) Init(props properties.Properties, env runtime.Environment) {
r.language = language{
env: env,
props: props,
extensions: []string{"*.rs", "Cargo.toml", "Cargo.lock"},
commands: []*cmd{
{
executable: "rustc",
args: []string{"--version"},
regex: `rustc (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))(-(?P<prerelease>[a-z]+))?)(( \((?P<buildmetadata>[0-9a-f]+ [0-9]+-[0-9]+-[0-9]+)\))?)`,
},
func (r *Rust) Enabled() bool {
r.extensions = []string{"*.rs", "Cargo.toml", "Cargo.lock"}
r.commands = []*cmd{
{
executable: "rustc",
args: []string{"--version"},
regex: `rustc (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))(-(?P<prerelease>[a-z]+))?)(( \((?P<buildmetadata>[0-9a-f]+ [0-9]+-[0-9]+-[0-9]+)\))?)`,
},
}
}
func (r *Rust) Enabled() bool {
return r.language.Enabled()
}

Some files were not shown because too many files have changed in this diff Show more