refactor: rename prompt writer

This commit is contained in:
Jan De Dobbeleer 2021-11-02 15:39:51 +02:00 committed by Jan De Dobbeleer
parent 915d2796fe
commit aa94042bd5
5 changed files with 24 additions and 20 deletions

View file

@ -34,7 +34,7 @@ type Block struct {
Newline bool `config:"newline"` Newline bool `config:"newline"`
env environmentInfo env environmentInfo
writer colorWriter writer promptWriter
ansi *ansiUtils ansi *ansiUtils
activeSegment *Segment activeSegment *Segment
previousActiveSegment *Segment previousActiveSegment *Segment
@ -42,7 +42,7 @@ type Block struct {
activeForeground string activeForeground string
} }
func (b *Block) init(env environmentInfo, writer colorWriter, ansi *ansiUtils) { func (b *Block) init(env environmentInfo, writer promptWriter, ansi *ansiUtils) {
b.env = env b.env = env
b.writer = writer b.writer = writer
b.ansi = ansi b.ansi = ansi
@ -51,7 +51,7 @@ func (b *Block) init(env environmentInfo, writer colorWriter, ansi *ansiUtils) {
func (b *Block) initPlain(env environmentInfo, config *Config) { func (b *Block) initPlain(env environmentInfo, config *Config) {
b.ansi = &ansiUtils{} b.ansi = &ansiUtils{}
b.ansi.init(plain) b.ansi.init(plain)
b.writer = &AnsiColor{ b.writer = &AnsiWriter{
ansi: b.ansi, ansi: b.ansi,
terminalBackground: getConsoleBackgroundColor(env, config.TerminalBackground), terminalBackground: getConsoleBackgroundColor(env, config.TerminalBackground),
} }

View file

@ -9,7 +9,7 @@ import (
type engine struct { type engine struct {
config *Config config *Config
env environmentInfo env environmentInfo
colorWriter colorWriter colorWriter promptWriter
ansi *ansiUtils ansi *ansiUtils
consoleTitle *consoleTitle consoleTitle *consoleTitle

View file

@ -191,7 +191,7 @@ func main() {
ansi := &ansiUtils{} ansi := &ansiUtils{}
ansi.init(env.getShellName()) ansi.init(env.getShellName())
colorer := &AnsiColor{ colorer := &AnsiWriter{
ansi: ansi, ansi: ansi,
terminalBackground: getConsoleBackgroundColor(env, cfg.TerminalBackground), terminalBackground: getConsoleBackgroundColor(env, cfg.TerminalBackground),
} }

View file

@ -31,6 +31,10 @@ var (
} }
) )
const (
colorRegex = `<(?P<foreground>[^,>]+)?,?(?P<background>[^>]+)?>(?P<content>[^<]*)<\/>`
)
// Returns the color code for a given color name // Returns the color code for a given color name
func getColorFromName(colorName string, isBackground bool) (string, error) { func getColorFromName(colorName string, isBackground bool) (string, error) {
colorMapOffset := 0 colorMapOffset := 0
@ -43,7 +47,7 @@ func getColorFromName(colorName string, isBackground bool) (string, error) {
return "", errors.New("color name does not exist") return "", errors.New("color name does not exist")
} }
type colorWriter interface { type promptWriter interface {
write(background, foreground, text string) write(background, foreground, text string)
string() string string() string
reset() reset()
@ -52,8 +56,8 @@ type colorWriter interface {
clearParentColors() clearParentColors()
} }
// AnsiColor writes colorized strings // AnsiWriter writes colorized strings
type AnsiColor struct { type AnsiWriter struct {
builder strings.Builder builder strings.Builder
ansi *ansiUtils ansi *ansiUtils
terminalBackground string terminalBackground string
@ -77,28 +81,28 @@ const (
Foreground = "foreground" Foreground = "foreground"
) )
func (a *AnsiColor) setColors(background, foreground string) { func (a *AnsiWriter) setColors(background, foreground string) {
a.Colors = &Color{ a.Colors = &Color{
Background: background, Background: background,
Foreground: foreground, Foreground: foreground,
} }
} }
func (a *AnsiColor) setParentColors(background, foreground string) { func (a *AnsiWriter) setParentColors(background, foreground string) {
a.ParentColors = &Color{ a.ParentColors = &Color{
Background: background, Background: background,
Foreground: foreground, Foreground: foreground,
} }
} }
func (a *AnsiColor) clearParentColors() { func (a *AnsiWriter) clearParentColors() {
a.ParentColors = nil a.ParentColors = nil
} }
// Gets the ANSI color code for a given color string. // Gets the ANSI color code for a given color string.
// This can include a valid hex color in the format `#FFFFFF`, // This can include a valid hex color in the format `#FFFFFF`,
// but also a name of one of the first 16 ANSI colors like `lightBlue`. // but also a name of one of the first 16 ANSI colors like `lightBlue`.
func (a *AnsiColor) getAnsiFromColorString(colorString string, isBackground bool) string { func (a *AnsiWriter) getAnsiFromColorString(colorString string, isBackground bool) string {
if colorString == Transparent || len(colorString) == 0 { if colorString == Transparent || len(colorString) == 0 {
return colorString return colorString
} }
@ -113,7 +117,7 @@ func (a *AnsiColor) getAnsiFromColorString(colorString string, isBackground bool
return style.String() return style.String()
} }
func (a *AnsiColor) writeColoredText(background, foreground, text string) { func (a *AnsiWriter) writeColoredText(background, foreground, text string) {
// Avoid emitting empty strings with color codes // Avoid emitting empty strings with color codes
if text == "" || (foreground == Transparent && background == Transparent) { if text == "" || (foreground == Transparent && background == Transparent) {
return return
@ -141,12 +145,12 @@ func (a *AnsiColor) writeColoredText(background, foreground, text string) {
a.builder.WriteString(coloredText) a.builder.WriteString(coloredText)
} }
func (a *AnsiColor) writeAndRemoveText(background, foreground, text, textToRemove, parentText string) string { func (a *AnsiWriter) writeAndRemoveText(background, foreground, text, textToRemove, parentText string) string {
a.writeColoredText(background, foreground, text) a.writeColoredText(background, foreground, text)
return strings.Replace(parentText, textToRemove, "", 1) return strings.Replace(parentText, textToRemove, "", 1)
} }
func (a *AnsiColor) write(background, foreground, text string) { func (a *AnsiWriter) write(background, foreground, text string) {
if len(text) == 0 { if len(text) == 0 {
return return
} }
@ -189,7 +193,7 @@ func (a *AnsiColor) write(background, foreground, text string) {
text = a.ansi.generateHyperlink(text) text = a.ansi.generateHyperlink(text)
// first we match for any potentially valid colors enclosed in <> // first we match for any potentially valid colors enclosed in <>
match := findAllNamedRegexMatch(`<(?P<foreground>[^,>]+)?,?(?P<background>[^>]+)?>(?P<content>[^<]*)<\/>`, text) match := findAllNamedRegexMatch(colorRegex, text)
for i := range match { for i := range match {
fg := match[i]["foreground"] fg := match[i]["foreground"]
bg := match[i]["background"] bg := match[i]["background"]
@ -214,10 +218,10 @@ func (a *AnsiColor) write(background, foreground, text string) {
a.writeColoredText(bgAnsi, fgAnsi, text) a.writeColoredText(bgAnsi, fgAnsi, text)
} }
func (a *AnsiColor) string() string { func (a *AnsiWriter) string() string {
return a.builder.String() return a.builder.String()
} }
func (a *AnsiColor) reset() { func (a *AnsiWriter) reset() {
a.builder.Reset() a.builder.Reset()
} }

View file

@ -22,7 +22,7 @@ func TestGetAnsiFromColorString(t *testing.T) {
{Case: "Base 16 backround", Expected: "101", Color: "lightRed", Background: true}, {Case: "Base 16 backround", Expected: "101", Color: "lightRed", Background: true},
} }
for _, tc := range cases { for _, tc := range cases {
renderer := &AnsiColor{} renderer := &AnsiWriter{}
ansiColor := renderer.getAnsiFromColorString(tc.Color, true) ansiColor := renderer.getAnsiFromColorString(tc.Color, true)
assert.Equal(t, tc.Expected, ansiColor, tc.Case) assert.Equal(t, tc.Expected, ansiColor, tc.Case)
} }
@ -182,7 +182,7 @@ func TestWriteANSIColors(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
ansi := &ansiUtils{} ansi := &ansiUtils{}
ansi.init("pwsh") ansi.init("pwsh")
renderer := &AnsiColor{ renderer := &AnsiWriter{
ansi: ansi, ansi: ansi,
ParentColors: tc.Parent, ParentColors: tc.Parent,
Colors: tc.Colors, Colors: tc.Colors,