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", "print",
"primary", "primary",
"--shell=pwsh", "--shell=pwsh",
"--terminal-width=200", "--terminal-width=200"
] ]
}, },
{ {

View file

@ -2,9 +2,9 @@ package ansi
type Cycle []*Colors type Cycle []*Colors
func PopColors(array []*Colors) (*Colors, Cycle) { func (c Cycle) Loop() (*Colors, Cycle) {
if len(array) == 0 { if len(c) == 0 {
return nil, array 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 writer *ansi.Writer
activeSegment *Segment activeSegment *Segment
previousActiveSegment *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.env = env
b.writer = writer b.writer = writer
b.cycle = cycle
b.executeSegmentLogic() b.executeSegmentLogic()
} }
@ -132,9 +130,8 @@ func (b *Block) RenderSegments() (string, int) {
if !segment.Enabled && segment.style() != Accordion { if !segment.Enabled && segment.style() != Accordion {
continue continue
} }
if b.cycle != nil && len(*b.cycle) > 0 { if colors, newCycle := cycle.Loop(); colors != nil {
colors, cycle := ansi.PopColors(*b.cycle) cycle = &newCycle
b.cycle = &cycle
segment.colors = colors segment.colors = colors
} }
b.setActiveSegment(segment) b.setActiveSegment(segment)

View file

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

View file

@ -239,9 +239,8 @@ for any palette color reference in templates/colors.
## Cycle ## Cycle
When you want to display the same **sequence of colors** (background and foreground) regardless of which segments are active, you can 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 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
anymore and your prompt would still render additional segments, the segment's colors are being used. A defined cycle always gets cycle always gets precendence over everything else.
precendence over everything else.
```json ```json
... ...