mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor(segment): cleaner logic
This commit is contained in:
parent
55ab413d6e
commit
a5162b0f19
|
@ -80,14 +80,14 @@ func (b *Block) enabled() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) setStringValues() {
|
func (b *Block) renderSegmentsText() {
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(len(b.Segments))
|
wg.Add(len(b.Segments))
|
||||||
defer wg.Wait()
|
defer wg.Wait()
|
||||||
for _, segment := range b.Segments {
|
for _, segment := range b.Segments {
|
||||||
go func(s *Segment) {
|
go func(s *Segment) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
s.setStringValue(b.env)
|
s.renderText(b.env)
|
||||||
}(segment)
|
}(segment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,10 +110,10 @@ func (b *Block) renderSegment(segment *Segment) {
|
||||||
b.writePowerline(false)
|
b.writePowerline(false)
|
||||||
switch b.activeSegment.Style {
|
switch b.activeSegment.Style {
|
||||||
case Plain, Powerline:
|
case Plain, Powerline:
|
||||||
b.writer.Write(b.activeBackground, b.activeForeground, segment.stringValue)
|
b.writer.Write(b.activeBackground, b.activeForeground, segment.text)
|
||||||
case Diamond:
|
case Diamond:
|
||||||
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
|
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
|
||||||
b.writer.Write(b.activeBackground, b.activeForeground, segment.stringValue)
|
b.writer.Write(b.activeBackground, b.activeForeground, segment.text)
|
||||||
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
|
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
|
||||||
}
|
}
|
||||||
b.previousActiveSegment = b.activeSegment
|
b.previousActiveSegment = b.activeSegment
|
||||||
|
@ -168,30 +168,22 @@ func (b *Block) debug() (int, []*SegmentTiming) {
|
||||||
var segmentTimings []*SegmentTiming
|
var segmentTimings []*SegmentTiming
|
||||||
largestSegmentNameLength := 0
|
largestSegmentNameLength := 0
|
||||||
for _, segment := range b.Segments {
|
for _, segment := range b.Segments {
|
||||||
err := segment.mapSegmentWithWriter(b.env)
|
|
||||||
if err != nil || !segment.shouldIncludeFolder() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var segmentTiming SegmentTiming
|
var segmentTiming SegmentTiming
|
||||||
segmentTiming.name = string(segment.Type)
|
segmentTiming.name = string(segment.Type)
|
||||||
segmentTiming.nameLength = len(segmentTiming.name)
|
segmentTiming.nameLength = len(segmentTiming.name)
|
||||||
if segmentTiming.nameLength > largestSegmentNameLength {
|
if segmentTiming.nameLength > largestSegmentNameLength {
|
||||||
largestSegmentNameLength = segmentTiming.nameLength
|
largestSegmentNameLength = segmentTiming.nameLength
|
||||||
}
|
}
|
||||||
// enabled() timing
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
segmentTiming.enabled = segment.enabled()
|
segment.renderText(b.env)
|
||||||
segmentTiming.enabledDuration = time.Since(start)
|
segmentTiming.active = segment.active
|
||||||
// string() timing
|
segmentTiming.text = segment.text
|
||||||
if segmentTiming.enabled {
|
if segmentTiming.active {
|
||||||
start = time.Now()
|
|
||||||
segment.stringValue = segment.string()
|
|
||||||
segmentTiming.stringDuration = time.Since(start)
|
|
||||||
b.renderSegment(segment)
|
b.renderSegment(segment)
|
||||||
b.writePowerline(true)
|
segmentTiming.text = b.writer.String()
|
||||||
segmentTiming.stringValue = b.writer.String()
|
|
||||||
b.writer.Reset()
|
b.writer.Reset()
|
||||||
}
|
}
|
||||||
|
segmentTiming.duration = time.Since(start)
|
||||||
segmentTimings = append(segmentTimings, &segmentTiming)
|
segmentTimings = append(segmentTimings, &segmentTiming)
|
||||||
}
|
}
|
||||||
return largestSegmentNameLength, segmentTimings
|
return largestSegmentNameLength, segmentTimings
|
||||||
|
|
|
@ -83,7 +83,7 @@ func (e *Engine) renderBlock(block *Block) {
|
||||||
} else {
|
} else {
|
||||||
block.init(e.Env, e.Writer, e.Ansi)
|
block.init(e.Env, e.Writer, e.Ansi)
|
||||||
}
|
}
|
||||||
block.setStringValues()
|
block.renderSegmentsText()
|
||||||
if !block.enabled() {
|
if !block.enabled() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -138,10 +138,9 @@ func (e *Engine) Debug(version string) string {
|
||||||
segmentTiming := &SegmentTiming{
|
segmentTiming := &SegmentTiming{
|
||||||
name: "ConsoleTitle",
|
name: "ConsoleTitle",
|
||||||
nameLength: 12,
|
nameLength: 12,
|
||||||
enabled: e.Config.ConsoleTitle,
|
active: e.Config.ConsoleTitle,
|
||||||
stringValue: consoleTitle,
|
text: consoleTitle,
|
||||||
enabledDuration: 0,
|
duration: duration,
|
||||||
stringDuration: duration,
|
|
||||||
}
|
}
|
||||||
segmentTimings = append(segmentTimings, segmentTiming)
|
segmentTimings = append(segmentTimings, segmentTiming)
|
||||||
// loop each segments of each blocks
|
// loop each segments of each blocks
|
||||||
|
@ -157,12 +156,9 @@ func (e *Engine) Debug(version string) string {
|
||||||
// pad the output so the tabs render correctly
|
// pad the output so the tabs render correctly
|
||||||
largestSegmentNameLength += 7
|
largestSegmentNameLength += 7
|
||||||
for _, segment := range segmentTimings {
|
for _, segment := range segmentTimings {
|
||||||
duration := segment.enabledDuration.Milliseconds()
|
duration := segment.duration.Milliseconds()
|
||||||
if segment.enabled {
|
segmentName := fmt.Sprintf("%s(%t)", segment.name, segment.active)
|
||||||
duration += segment.stringDuration.Milliseconds()
|
e.write(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.text))
|
||||||
}
|
|
||||||
segmentName := fmt.Sprintf("%s(%t)", segment.name, segment.enabled)
|
|
||||||
e.write(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.stringValue))
|
|
||||||
}
|
}
|
||||||
e.write(fmt.Sprintf("\n\x1b[1mRun duration:\x1b[0m %s\n", time.Since(start)))
|
e.write(fmt.Sprintf("\n\x1b[1mRun duration:\x1b[0m %s\n", time.Since(start)))
|
||||||
e.write(fmt.Sprintf("\n\x1b[1mCache path:\x1b[0m %s\n", e.Env.CachePath()))
|
e.write(fmt.Sprintf("\n\x1b[1mCache path:\x1b[0m %s\n", e.Env.CachePath()))
|
||||||
|
@ -209,10 +205,10 @@ func (e *Engine) RenderTooltip(tip string) string {
|
||||||
if err := tooltip.mapSegmentWithWriter(e.Env); err != nil {
|
if err := tooltip.mapSegmentWithWriter(e.Env); err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if !tooltip.enabled() {
|
if !tooltip.writer.Enabled() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
tooltip.stringValue = tooltip.string()
|
tooltip.text = tooltip.string()
|
||||||
// little hack to reuse the current logic
|
// little hack to reuse the current logic
|
||||||
block := &Block{
|
block := &Block{
|
||||||
Alignment: Right,
|
Alignment: Right,
|
||||||
|
@ -278,7 +274,7 @@ func (e *Engine) RenderRPrompt() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
block.init(e.Env, e.Writer, e.Ansi)
|
block.init(e.Env, e.Writer, e.Ansi)
|
||||||
block.setStringValues()
|
block.renderSegmentsText()
|
||||||
if !block.enabled() {
|
if !block.enabled() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ type Segment struct {
|
||||||
TrailingDiamond string `json:"trailing_diamond,omitempty"`
|
TrailingDiamond string `json:"trailing_diamond,omitempty"`
|
||||||
Properties properties.Map `json:"properties,omitempty"`
|
Properties properties.Map `json:"properties,omitempty"`
|
||||||
writer SegmentWriter
|
writer SegmentWriter
|
||||||
stringValue string
|
text string
|
||||||
active bool
|
active bool
|
||||||
env environment.Environment
|
env environment.Environment
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,9 @@ type Segment struct {
|
||||||
type SegmentTiming struct {
|
type SegmentTiming struct {
|
||||||
name string
|
name string
|
||||||
nameLength int
|
nameLength int
|
||||||
enabled bool
|
active bool
|
||||||
stringValue string
|
text string
|
||||||
enabledDuration time.Duration
|
duration time.Duration
|
||||||
stringDuration time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SegmentWriter is the interface used to define what and if to write to the prompt
|
// SegmentWriter is the interface used to define what and if to write to the prompt
|
||||||
|
@ -150,26 +149,6 @@ const (
|
||||||
IPIFY SegmentType = "ipify"
|
IPIFY SegmentType = "ipify"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (segment *Segment) string() string {
|
|
||||||
segmentTemplate := segment.Properties.GetString(properties.SegmentTemplate, segment.writer.Template())
|
|
||||||
tmpl := &template.Text{
|
|
||||||
Template: segmentTemplate,
|
|
||||||
Context: segment.writer,
|
|
||||||
Env: segment.env,
|
|
||||||
}
|
|
||||||
text, err := tmpl.Render()
|
|
||||||
if err != nil {
|
|
||||||
return err.Error()
|
|
||||||
}
|
|
||||||
segment.active = len(strings.TrimSpace(text)) > 0
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
|
|
||||||
func (segment *Segment) enabled() bool {
|
|
||||||
segment.active = segment.writer.Enabled()
|
|
||||||
return segment.active
|
|
||||||
}
|
|
||||||
|
|
||||||
func (segment *Segment) shouldIncludeFolder() bool {
|
func (segment *Segment) shouldIncludeFolder() bool {
|
||||||
cwdIncluded := segment.cwdIncluded()
|
cwdIncluded := segment.cwdIncluded()
|
||||||
cwdExcluded := segment.cwdExcluded()
|
cwdExcluded := segment.cwdExcluded()
|
||||||
|
@ -296,7 +275,21 @@ func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error
|
||||||
return errors.New("unable to map writer")
|
return errors.New("unable to map writer")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (segment *Segment) setStringValue(env environment.Environment) {
|
func (segment *Segment) string() string {
|
||||||
|
segmentTemplate := segment.Properties.GetString(properties.SegmentTemplate, segment.writer.Template())
|
||||||
|
tmpl := &template.Text{
|
||||||
|
Template: segmentTemplate,
|
||||||
|
Context: segment.writer,
|
||||||
|
Env: segment.env,
|
||||||
|
}
|
||||||
|
text, err := tmpl.Render()
|
||||||
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
func (segment *Segment) renderText(env environment.Environment) {
|
||||||
defer func() {
|
defer func() {
|
||||||
err := recover()
|
err := recover()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -305,14 +298,15 @@ func (segment *Segment) setStringValue(env environment.Environment) {
|
||||||
// display a message explaining omp failed(with the err)
|
// display a message explaining omp failed(with the err)
|
||||||
message := fmt.Sprintf("\noh-my-posh fatal error rendering %s segment:%s\n\n%s\n", segment.Type, err, debug.Stack())
|
message := fmt.Sprintf("\noh-my-posh fatal error rendering %s segment:%s\n\n%s\n", segment.Type, err, debug.Stack())
|
||||||
fmt.Println(message)
|
fmt.Println(message)
|
||||||
segment.stringValue = "error"
|
segment.text = "error"
|
||||||
segment.active = true
|
segment.active = true
|
||||||
}()
|
}()
|
||||||
err := segment.mapSegmentWithWriter(env)
|
err := segment.mapSegmentWithWriter(env)
|
||||||
if err != nil || !segment.shouldIncludeFolder() {
|
if err != nil || !segment.shouldIncludeFolder() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if segment.enabled() {
|
if segment.writer.Enabled() {
|
||||||
segment.stringValue = segment.string()
|
segment.text = segment.string()
|
||||||
|
segment.active = len(strings.TrimSpace(segment.text)) > 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue