fix(cycle): cycle in a loop

This commit is contained in:
Jan De Dobbeleer 2023-01-11 13:36:25 +01:00 committed by Jan De Dobbeleer
parent 86c0daaa68
commit 7dbc3c96ea
5 changed files with 20 additions and 21 deletions

2
.vscode/launch.json vendored
View file

@ -12,7 +12,7 @@
"print",
"primary",
"--shell=pwsh",
"--terminal-width=200",
"--terminal-width=200"
]
},
{

View file

@ -2,9 +2,9 @@ package ansi
type Cycle []*Colors
func PopColors(array []*Colors) (*Colors, Cycle) {
if len(array) == 0 {
return nil, array
func (c Cycle) Loop() (*Colors, Cycle) {
if len(c) == 0 {
return nil, c
}
return array[0], array[1:]
return c[0], append(c[1:], c[0])
}

View file

@ -55,13 +55,11 @@ type Block struct {
writer *ansi.Writer
activeSegment *Segment
previousActiveSegment *Segment
cycle *ansi.Cycle
}
func (b *Block) Init(env platform.Environment, writer *ansi.Writer, cycle *ansi.Cycle) {
func (b *Block) Init(env platform.Environment, writer *ansi.Writer) {
b.env = env
b.writer = writer
b.cycle = cycle
b.executeSegmentLogic()
}
@ -132,9 +130,8 @@ func (b *Block) RenderSegments() (string, int) {
if !segment.Enabled && segment.style() != Accordion {
continue
}
if b.cycle != nil && len(*b.cycle) > 0 {
colors, cycle := ansi.PopColors(*b.cycle)
b.cycle = &cycle
if colors, newCycle := cycle.Loop(); colors != nil {
cycle = &newCycle
segment.colors = colors
}
b.setActiveSegment(segment)

View file

@ -11,6 +11,10 @@ import (
"github.com/jandedobbeleer/oh-my-posh/src/template"
)
var (
cycle *ansi.Cycle
)
type Engine struct {
Config *Config
Env platform.Environment
@ -21,7 +25,6 @@ type Engine struct {
currentLineLength int
rprompt string
rpromptLength int
cycle *ansi.Cycle
}
func (e *Engine) write(text string) {
@ -59,7 +62,7 @@ func (e *Engine) canWriteRightBlock(rprompt bool) bool {
func (e *Engine) PrintPrimary() string {
// cache a pointer to the color cycle
e.cycle = &e.Config.Cycle
cycle = &e.Config.Cycle
for _, block := range e.Config.Blocks {
e.renderBlock(block)
}
@ -159,7 +162,7 @@ func (e *Engine) renderBlock(block *Block) {
if e.Env.Shell() == shell.BASH && (block.Type == RPrompt || block.Alignment == Right) {
block.InitPlain(e.Env, e.Config)
} else {
block.Init(e.Env, e.Writer, e.cycle)
block.Init(e.Env, e.Writer)
}
if !block.Enabled() {
return
@ -247,10 +250,10 @@ func (e *Engine) PrintDebug(startTime time.Time, version string) string {
}
segmentTimings = append(segmentTimings, segmentTiming)
// cache a pointer to the color cycle
e.cycle = &e.Config.Cycle
cycle = &e.Config.Cycle
// loop each segments of each blocks
for _, block := range e.Config.Blocks {
block.Init(e.Env, e.Writer, e.cycle)
block.Init(e.Env, e.Writer)
longestSegmentName, timings := block.Debug()
segmentTimings = append(segmentTimings, timings...)
if longestSegmentName > largestSegmentNameLength {
@ -349,7 +352,7 @@ func (e *Engine) PrintTooltip(tip string) string {
}
switch e.Env.Shell() {
case shell.ZSH, shell.CMD, shell.FISH, shell.GENERIC:
block.Init(e.Env, e.Writer, nil)
block.Init(e.Env, e.Writer)
if !block.Enabled() {
return ""
}
@ -458,7 +461,7 @@ func (e *Engine) PrintRPrompt() string {
if block == nil {
return ""
}
block.Init(e.Env, e.Writer, e.cycle)
block.Init(e.Env, e.Writer)
if !block.Enabled() {
return ""
}

View file

@ -239,9 +239,8 @@ for any palette color reference in templates/colors.
## Cycle
When you want to display the same **sequence of colors** (background and foreground) regardless of which segments are active, you can
make use of the cycle property. This property is a list of colors which are used one after the other. If there's nothing in the list
anymore and your prompt would still render additional segments, the segment's colors are being used. A defined cycle always gets
precendence over everything else.
make use of the cycle property. This property is a list of colors which are used one after the other in a continuous loop. A defined
cycle always gets precendence over everything else.
```json
...