2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-12 23:57:46 -07:00
|
|
|
"sync"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type engine struct {
|
|
|
|
settings *Settings
|
|
|
|
env environmentInfo
|
2020-09-23 00:33:54 -07:00
|
|
|
renderer *Renderer
|
2019-03-13 04:14:30 -07:00
|
|
|
activeBlock *Block
|
|
|
|
activeSegment *Segment
|
|
|
|
previousActiveSegment *Segment
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) getPowerlineColor(foreground bool) string {
|
|
|
|
if e.previousActiveSegment == nil {
|
2020-09-21 06:44:09 -07:00
|
|
|
return Transparent
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
if !foreground && e.activeSegment.Style != Powerline {
|
2020-09-21 06:44:09 -07:00
|
|
|
return Transparent
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
if foreground && e.previousActiveSegment.Style != Powerline {
|
2020-09-21 06:44:09 -07:00
|
|
|
return Transparent
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
return e.previousActiveSegment.Background
|
|
|
|
}
|
|
|
|
|
2020-09-26 04:41:28 -07:00
|
|
|
func (e *engine) writePowerLineSeparator(background string, foreground string, end bool) {
|
|
|
|
symbol := e.activeSegment.PowerlineSymbol
|
|
|
|
if end {
|
|
|
|
symbol = e.previousActiveSegment.PowerlineSymbol
|
|
|
|
}
|
2020-09-27 00:37:50 -07:00
|
|
|
if e.activeSegment.InvertPowerline {
|
2020-09-26 04:41:28 -07:00
|
|
|
e.renderer.write(foreground, background, symbol)
|
2019-03-13 04:14:30 -07:00
|
|
|
return
|
|
|
|
}
|
2020-09-26 04:41:28 -07:00
|
|
|
e.renderer.write(background, foreground, symbol)
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) endPowerline() {
|
|
|
|
if e.activeSegment != nil &&
|
|
|
|
e.activeSegment.Style != Powerline &&
|
|
|
|
e.previousActiveSegment != nil &&
|
|
|
|
e.previousActiveSegment.Style == Powerline {
|
2020-09-26 04:41:28 -07:00
|
|
|
e.writePowerLineSeparator(e.getPowerlineColor(false), e.previousActiveSegment.Background, true)
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) renderPowerLineSegment(text string) {
|
2020-09-26 04:41:28 -07:00
|
|
|
e.writePowerLineSeparator(e.activeSegment.Background, e.getPowerlineColor(true), false)
|
2019-03-13 04:14:30 -07:00
|
|
|
e.renderText(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) renderPlainSegment(text string) {
|
|
|
|
e.renderText(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) renderDiamondSegment(text string) {
|
2020-09-21 06:44:09 -07:00
|
|
|
e.renderer.write(Transparent, e.activeSegment.Background, e.activeSegment.LeadingDiamond)
|
2019-03-13 04:14:30 -07:00
|
|
|
e.renderText(text)
|
2020-09-21 06:44:09 -07:00
|
|
|
e.renderer.write(Transparent, e.activeSegment.Background, e.activeSegment.TrailingDiamond)
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) renderText(text string) {
|
2020-10-02 07:58:25 -07:00
|
|
|
prefix := e.activeSegment.getValue(Prefix, " ")
|
|
|
|
postfix := e.activeSegment.getValue(Postfix, " ")
|
2019-03-13 04:14:30 -07:00
|
|
|
e.renderer.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, postfix))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) renderSegmentText(text string) {
|
|
|
|
switch e.activeSegment.Style {
|
|
|
|
case Plain:
|
|
|
|
e.renderPlainSegment(text)
|
|
|
|
case Diamond:
|
|
|
|
e.renderDiamondSegment(text)
|
|
|
|
default:
|
|
|
|
e.renderPowerLineSegment(text)
|
|
|
|
}
|
|
|
|
e.previousActiveSegment = e.activeSegment
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) renderBlockSegments(block *Block) string {
|
|
|
|
defer e.reset()
|
|
|
|
e.activeBlock = block
|
2020-10-12 23:57:46 -07:00
|
|
|
e.setStringValues(block.Segments)
|
2019-03-13 04:14:30 -07:00
|
|
|
for _, segment := range block.Segments {
|
2020-10-12 23:57:46 -07:00
|
|
|
if !segment.active {
|
2019-03-13 04:14:30 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
e.activeSegment = segment
|
|
|
|
e.endPowerline()
|
|
|
|
text := segment.string()
|
2020-10-12 23:57:46 -07:00
|
|
|
e.activeSegment.Background = segment.props.background
|
|
|
|
e.activeSegment.Foreground = segment.props.foreground
|
2019-03-13 04:14:30 -07:00
|
|
|
e.renderSegmentText(text)
|
|
|
|
}
|
|
|
|
if e.previousActiveSegment != nil && e.previousActiveSegment.Style == Powerline {
|
2020-09-26 04:41:28 -07:00
|
|
|
e.writePowerLineSeparator(Transparent, e.previousActiveSegment.Background, true)
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
return e.renderer.string()
|
|
|
|
}
|
|
|
|
|
2020-10-12 23:57:46 -07:00
|
|
|
func (e *engine) setStringValues(segments []*Segment) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(segments))
|
|
|
|
defer wg.Wait()
|
|
|
|
cwd := e.env.getcwd()
|
|
|
|
for _, segment := range segments {
|
|
|
|
go func(s *Segment) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := s.mapSegmentWithWriter(e.env)
|
|
|
|
if err == nil && !s.hasValue(IgnoreFolders, cwd) && s.enabled() {
|
|
|
|
s.stringValue = s.string()
|
|
|
|
}
|
|
|
|
}(segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:08:28 -07:00
|
|
|
func (e *engine) render() {
|
2019-03-13 04:14:30 -07:00
|
|
|
for _, block := range e.settings.Blocks {
|
|
|
|
// if line break, append a line break
|
|
|
|
if block.Type == LineBreak {
|
2020-09-23 00:33:54 -07:00
|
|
|
fmt.Print(e.renderer.lineBreak())
|
2019-03-13 04:14:30 -07:00
|
|
|
continue
|
|
|
|
}
|
2020-09-25 23:17:30 -07:00
|
|
|
if block.VerticalOffset != 0 {
|
|
|
|
fmt.Print(e.renderer.changeLine(block.VerticalOffset))
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
switch block.Alignment {
|
|
|
|
case Right:
|
2020-09-26 11:25:47 -07:00
|
|
|
fmt.Print(e.renderer.carriageForward())
|
2019-03-13 04:14:30 -07:00
|
|
|
blockText := e.renderBlockSegments(block)
|
2020-09-26 11:25:47 -07:00
|
|
|
cursorMove := e.renderer.setCursorForRightWrite(blockText, block.HorizontalOffset)
|
2020-09-23 00:33:54 -07:00
|
|
|
fmt.Print(cursorMove)
|
2020-09-16 04:08:28 -07:00
|
|
|
fmt.Print(blockText)
|
2019-03-13 04:14:30 -07:00
|
|
|
default:
|
2020-09-16 04:08:28 -07:00
|
|
|
fmt.Print(e.renderBlockSegments(block))
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 00:02:33 -07:00
|
|
|
if e.settings.ConsoleTitle {
|
|
|
|
e.renderer.setConsoleTitle(e.env.getcwd())
|
|
|
|
}
|
2020-10-14 07:25:18 -07:00
|
|
|
e.renderer.creset()
|
2020-09-26 11:00:28 -07:00
|
|
|
if e.settings.FinalSpace {
|
2020-09-16 04:08:28 -07:00
|
|
|
fmt.Print(" ")
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) reset() {
|
|
|
|
e.renderer.reset()
|
|
|
|
e.previousActiveSegment = nil
|
|
|
|
e.activeBlock = nil
|
|
|
|
}
|