refactor: remove powerline bg color

This commit is contained in:
Jan De Dobbeleer 2020-09-21 15:44:09 +02:00 committed by Jan De Dobbeleer
parent bbeba938f6
commit f410c32f6e
10 changed files with 33 additions and 22 deletions

View file

@ -72,6 +72,5 @@
]
}
],
"console_background_color": "#193549",
"end_space_enabled": true
}

View file

@ -9,7 +9,6 @@
"type": "time",
"style": "plain",
"foreground": "#007ACC",
"background": "#193549",
"properties": {
"time_format": "15:04:05"
}
@ -65,6 +64,5 @@
]
}
],
"console_background_color": "#193549",
"end_space_enabled": true
}

View file

@ -57,6 +57,5 @@
]
}
],
"console_background_color": "#193549",
"end_space_enabled": true
}

View file

@ -98,6 +98,5 @@
]
}
],
"console_background_color": "#193549",
"end_space_enabled": true
}

View file

@ -90,6 +90,5 @@
]
}
],
"console_background_color": "#193549",
"end_space_enabled": true
}

View file

@ -67,6 +67,5 @@
]
}
],
"console_background_color": "#193549",
"end_space_enabled": true
}

View file

@ -2,6 +2,7 @@ package main
import (
"bytes"
"fmt"
"regexp"
"strings"
@ -13,13 +14,23 @@ type ColorWriter struct {
Buffer *bytes.Buffer
}
const (
//Transparent implies a transparent color
Transparent string = "transparent"
)
func (w *ColorWriter) writeColoredText(background string, foreground string, text string) {
var coloredText string
if background != "" {
style := color.HEXStyle(foreground, background)
if foreground == Transparent {
style := color.HEX(background, false)
colorCodes := style.Code()
// this takes the colors and inverts them so the foreground becomes transparent
coloredText = fmt.Sprintf("\x1b[%s;49m\x1b[7m%s\x1b[m\x1b[0m", colorCodes, text)
} else if background == "" || background == Transparent {
style := color.HEX(foreground)
coloredText = style.Sprint(text)
} else {
style := color.HEX(foreground)
style := color.HEXStyle(foreground, background)
coloredText = style.Sprint(text)
}
w.Buffer.WriteString(coloredText)

View file

@ -35,3 +35,12 @@ func TestWriteColorOverride(t *testing.T) {
writer.write("#193549", "#ff5733", text)
assert.NotContains(t, writer.string(), "<#ff5733>")
}
func TestWriteColorTransparent(t *testing.T) {
writer := &ColorWriter{
Buffer: new(bytes.Buffer),
}
text := "This is white"
writer.writeColoredText("#193549", Transparent, text)
t.Log(writer.string())
}

View file

@ -18,13 +18,13 @@ type engine struct {
func (e *engine) getPowerlineColor(foreground bool) string {
if e.previousActiveSegment == nil {
return e.settings.ConsoleBackgroundColor
return Transparent
}
if !foreground && e.activeSegment.Style != Powerline {
return e.settings.ConsoleBackgroundColor
return Transparent
}
if foreground && e.previousActiveSegment.Style != Powerline {
return e.settings.ConsoleBackgroundColor
return Transparent
}
return e.previousActiveSegment.Background
}
@ -56,9 +56,9 @@ func (e *engine) renderPlainSegment(text string) {
}
func (e *engine) renderDiamondSegment(text string) {
e.renderer.write(e.settings.ConsoleBackgroundColor, e.activeSegment.Background, e.activeSegment.LeadingDiamond)
e.renderer.write(Transparent, e.activeSegment.Background, e.activeSegment.LeadingDiamond)
e.renderText(text)
e.renderer.write(e.settings.ConsoleBackgroundColor, e.activeSegment.Background, e.activeSegment.TrailingDiamond)
e.renderer.write(Transparent, e.activeSegment.Background, e.activeSegment.TrailingDiamond)
}
func (e *engine) getStringProperty(property Property, defaultValue string) string {
@ -102,7 +102,7 @@ func (e *engine) renderBlockSegments(block *Block) string {
e.renderSegmentText(text)
}
if e.previousActiveSegment != nil && e.previousActiveSegment.Style == Powerline {
e.writePowerLineSeparator(e.settings.ConsoleBackgroundColor, e.previousActiveSegment.Background)
e.writePowerLineSeparator(Transparent, e.previousActiveSegment.Background)
}
return e.renderer.string()
}

View file

@ -8,10 +8,9 @@ import (
//Settings holds all the theme for rendering the prompt
type Settings struct {
ConsoleBackgroundColor string `json:"console_background_color"`
RightSegmentOffset int `json:"right_segment_offset"`
EndSpaceEnabled bool `json:"end_space_enabled"`
Blocks []*Block `json:"blocks"`
RightSegmentOffset int `json:"right_segment_offset"`
EndSpaceEnabled bool `json:"end_space_enabled"`
Blocks []*Block `json:"blocks"`
}
//BlockType type of block
@ -70,8 +69,7 @@ func loadUserConfiguration(env environmentInfo) (*Settings, error) {
func getDefaultSettings() *Settings {
settings := &Settings{
EndSpaceEnabled: true,
ConsoleBackgroundColor: "#193549",
EndSpaceEnabled: true,
Blocks: []*Block{
{
Type: Prompt,