Skip to content

Commit

Permalink
feat: expose segment output text
Browse files Browse the repository at this point in the history
resolves #5750
  • Loading branch information
JanDeDobbeleer committed Oct 28, 2024
1 parent 8b5e032 commit f49e325
Show file tree
Hide file tree
Showing 145 changed files with 1,198 additions and 1,826 deletions.
9 changes: 9 additions & 0 deletions src/config/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestMigratePropertyKey(t *testing.T) {

type MockedWriter struct {
template string
text string
}

func (m *MockedWriter) Enabled() bool {
Expand All @@ -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) {
Expand Down
41 changes: 16 additions & 25 deletions src/config/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

func (segment *Segment) textCacheKey() string {
return segment.cacheKey("segment_cache_%s")
}

func (segment *Segment) writerCacheKey() string {
return segment.cacheKey("segment_cache_writer_%s")
segment.env.Session().Set(segment.cacheKey(), string(data), segment.Cache.Duration)
}

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())
Expand Down
21 changes: 12 additions & 9 deletions src/config/segment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions src/prompt/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/prompt/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
23 changes: 7 additions & 16 deletions src/segments/angular.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
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,
},
versionURLTemplate: "https://github.com/angular/angular/releases/tag/{{.Full}}",
}
}
a.versionURLTemplate = "https://github.com/angular/angular/releases/tag/{{.Full}}"

func (a *Angular) Enabled() bool {
return a.language.Enabled()
}

Expand Down
10 changes: 1 addition & 9 deletions src/segments/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -33,8 +31,7 @@ type ArgocdConfig struct {
}

type Argocd struct {
props properties.Properties
env runtime.Environment
base

ArgocdContext
}
Expand All @@ -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()
Expand Down
24 changes: 14 additions & 10 deletions src/segments/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 1 addition & 8 deletions src/segments/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions src/segments/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 2 additions & 8 deletions src/segments/az.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit f49e325

Please sign in to comment.