diff --git a/src/block.go b/src/block.go index 20f4d1b5..dcbabc52 100644 --- a/src/block.go +++ b/src/block.go @@ -34,7 +34,7 @@ type Block struct { Newline bool `config:"newline"` env environmentInfo - writer colorWriter + writer promptWriter ansi *ansiUtils activeSegment *Segment previousActiveSegment *Segment @@ -42,7 +42,7 @@ type Block struct { 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.writer = writer 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) { b.ansi = &ansiUtils{} b.ansi.init(plain) - b.writer = &AnsiColor{ + b.writer = &AnsiWriter{ ansi: b.ansi, terminalBackground: getConsoleBackgroundColor(env, config.TerminalBackground), } diff --git a/src/engine.go b/src/engine.go index 53d16edc..9cd059e0 100644 --- a/src/engine.go +++ b/src/engine.go @@ -9,7 +9,7 @@ import ( type engine struct { config *Config env environmentInfo - colorWriter colorWriter + colorWriter promptWriter ansi *ansiUtils consoleTitle *consoleTitle diff --git a/src/main.go b/src/main.go index 268a56b3..56497908 100644 --- a/src/main.go +++ b/src/main.go @@ -191,7 +191,7 @@ func main() { ansi := &ansiUtils{} ansi.init(env.getShellName()) - colorer := &AnsiColor{ + colorer := &AnsiWriter{ ansi: ansi, terminalBackground: getConsoleBackgroundColor(env, cfg.TerminalBackground), } diff --git a/src/ansi_color.go b/src/writer_ansi.go similarity index 87% rename from src/ansi_color.go rename to src/writer_ansi.go index db17134d..f4b4f69a 100644 --- a/src/ansi_color.go +++ b/src/writer_ansi.go @@ -31,6 +31,10 @@ var ( } ) +const ( + colorRegex = `<(?P[^,>]+)?,?(?P[^>]+)?>(?P[^<]*)<\/>` +) + // Returns the color code for a given color name func getColorFromName(colorName string, isBackground bool) (string, error) { colorMapOffset := 0 @@ -43,7 +47,7 @@ func getColorFromName(colorName string, isBackground bool) (string, error) { return "", errors.New("color name does not exist") } -type colorWriter interface { +type promptWriter interface { write(background, foreground, text string) string() string reset() @@ -52,8 +56,8 @@ type colorWriter interface { clearParentColors() } -// AnsiColor writes colorized strings -type AnsiColor struct { +// AnsiWriter writes colorized strings +type AnsiWriter struct { builder strings.Builder ansi *ansiUtils terminalBackground string @@ -77,28 +81,28 @@ const ( Foreground = "foreground" ) -func (a *AnsiColor) setColors(background, foreground string) { +func (a *AnsiWriter) setColors(background, foreground string) { a.Colors = &Color{ Background: background, Foreground: foreground, } } -func (a *AnsiColor) setParentColors(background, foreground string) { +func (a *AnsiWriter) setParentColors(background, foreground string) { a.ParentColors = &Color{ Background: background, Foreground: foreground, } } -func (a *AnsiColor) clearParentColors() { +func (a *AnsiWriter) clearParentColors() { a.ParentColors = nil } // Gets the ANSI color code for a given color string. // 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`. -func (a *AnsiColor) getAnsiFromColorString(colorString string, isBackground bool) string { +func (a *AnsiWriter) getAnsiFromColorString(colorString string, isBackground bool) string { if colorString == Transparent || len(colorString) == 0 { return colorString } @@ -113,7 +117,7 @@ func (a *AnsiColor) getAnsiFromColorString(colorString string, isBackground bool 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 if text == "" || (foreground == Transparent && background == Transparent) { return @@ -141,12 +145,12 @@ func (a *AnsiColor) writeColoredText(background, foreground, text string) { 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) 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 { return } @@ -189,7 +193,7 @@ func (a *AnsiColor) write(background, foreground, text string) { text = a.ansi.generateHyperlink(text) // first we match for any potentially valid colors enclosed in <> - match := findAllNamedRegexMatch(`<(?P[^,>]+)?,?(?P[^>]+)?>(?P[^<]*)<\/>`, text) + match := findAllNamedRegexMatch(colorRegex, text) for i := range match { fg := match[i]["foreground"] bg := match[i]["background"] @@ -214,10 +218,10 @@ func (a *AnsiColor) write(background, foreground, text string) { a.writeColoredText(bgAnsi, fgAnsi, text) } -func (a *AnsiColor) string() string { +func (a *AnsiWriter) string() string { return a.builder.String() } -func (a *AnsiColor) reset() { +func (a *AnsiWriter) reset() { a.builder.Reset() } diff --git a/src/ansi_color_test.go b/src/writer_ansi_test.go similarity index 99% rename from src/ansi_color_test.go rename to src/writer_ansi_test.go index 05a77dac..7d6bbcd4 100644 --- a/src/ansi_color_test.go +++ b/src/writer_ansi_test.go @@ -22,7 +22,7 @@ func TestGetAnsiFromColorString(t *testing.T) { {Case: "Base 16 backround", Expected: "101", Color: "lightRed", Background: true}, } for _, tc := range cases { - renderer := &AnsiColor{} + renderer := &AnsiWriter{} ansiColor := renderer.getAnsiFromColorString(tc.Color, true) assert.Equal(t, tc.Expected, ansiColor, tc.Case) } @@ -182,7 +182,7 @@ func TestWriteANSIColors(t *testing.T) { for _, tc := range cases { ansi := &ansiUtils{} ansi.init("pwsh") - renderer := &AnsiColor{ + renderer := &AnsiWriter{ ansi: ansi, ParentColors: tc.Parent, Colors: tc.Colors,