Skip to content

Commit

Permalink
Improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Dec 20, 2023
1 parent b103f0c commit af014a3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
74 changes: 37 additions & 37 deletions m/ansiTokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func cellsFromString(s string, lineNumberOneBased *int) cellsWithTrailer {
var cells []twin.Cell

// Specs: https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
styleUnprintable := twin.StyleDefault.Background(twin.NewColor16(1)).Foreground(twin.NewColor16(7))
styleUnprintable := twin.StyleDefault.WithBackground(twin.NewColor16(1)).WithForeground(twin.NewColor16(7))

trailer := styledStringsFromString(s, lineNumberOneBased, func(str string, style twin.Style) {
for _, token := range tokensFromStyledString(_StyledString{String: str, Style: style}) {
Expand Down Expand Up @@ -485,59 +485,59 @@ func rawUpdateStyle(style twin.Style, escapeSequenceWithoutHeader string, number

// Foreground colors, https://pkg.go.dev/github.com/gdamore/tcell#Color
case 30:
style = style.Foreground(twin.NewColor16(0))
style = style.WithForeground(twin.NewColor16(0))
case 31:
style = style.Foreground(twin.NewColor16(1))
style = style.WithForeground(twin.NewColor16(1))
case 32:
style = style.Foreground(twin.NewColor16(2))
style = style.WithForeground(twin.NewColor16(2))
case 33:
style = style.Foreground(twin.NewColor16(3))
style = style.WithForeground(twin.NewColor16(3))
case 34:
style = style.Foreground(twin.NewColor16(4))
style = style.WithForeground(twin.NewColor16(4))
case 35:
style = style.Foreground(twin.NewColor16(5))
style = style.WithForeground(twin.NewColor16(5))
case 36:
style = style.Foreground(twin.NewColor16(6))
style = style.WithForeground(twin.NewColor16(6))
case 37:
style = style.Foreground(twin.NewColor16(7))
style = style.WithForeground(twin.NewColor16(7))
case 38:
var err error
var color *twin.Color
index, color, err = consumeCompositeColor(numbersBuffer, index-1)
if err != nil {
return style, numbersBuffer, fmt.Errorf("Foreground: %w", err)
}
style = style.Foreground(*color)
style = style.WithForeground(*color)
case 39:
style = style.Foreground(twin.ColorDefault)
style = style.WithForeground(twin.ColorDefault)

// Background colors, see https://pkg.go.dev/github.com/gdamore/Color
case 40:
style = style.Background(twin.NewColor16(0))
style = style.WithBackground(twin.NewColor16(0))
case 41:
style = style.Background(twin.NewColor16(1))
style = style.WithBackground(twin.NewColor16(1))
case 42:
style = style.Background(twin.NewColor16(2))
style = style.WithBackground(twin.NewColor16(2))
case 43:
style = style.Background(twin.NewColor16(3))
style = style.WithBackground(twin.NewColor16(3))
case 44:
style = style.Background(twin.NewColor16(4))
style = style.WithBackground(twin.NewColor16(4))
case 45:
style = style.Background(twin.NewColor16(5))
style = style.WithBackground(twin.NewColor16(5))
case 46:
style = style.Background(twin.NewColor16(6))
style = style.WithBackground(twin.NewColor16(6))
case 47:
style = style.Background(twin.NewColor16(7))
style = style.WithBackground(twin.NewColor16(7))
case 48:
var err error
var color *twin.Color
index, color, err = consumeCompositeColor(numbersBuffer, index-1)
if err != nil {
return style, numbersBuffer, fmt.Errorf("Background: %w", err)
}
style = style.Background(*color)
style = style.WithBackground(*color)
case 49:
style = style.Background(twin.ColorDefault)
style = style.WithBackground(twin.ColorDefault)

// Bright foreground colors: see https://pkg.go.dev/github.com/gdamore/Color
//
Expand All @@ -546,38 +546,38 @@ func rawUpdateStyle(style twin.Style, escapeSequenceWithoutHeader string, number
// * TERM=xterm-256color
// * TERM=screen-256color
case 90:
style = style.Foreground(twin.NewColor16(8))
style = style.WithForeground(twin.NewColor16(8))
case 91:
style = style.Foreground(twin.NewColor16(9))
style = style.WithForeground(twin.NewColor16(9))
case 92:
style = style.Foreground(twin.NewColor16(10))
style = style.WithForeground(twin.NewColor16(10))
case 93:
style = style.Foreground(twin.NewColor16(11))
style = style.WithForeground(twin.NewColor16(11))
case 94:
style = style.Foreground(twin.NewColor16(12))
style = style.WithForeground(twin.NewColor16(12))
case 95:
style = style.Foreground(twin.NewColor16(13))
style = style.WithForeground(twin.NewColor16(13))
case 96:
style = style.Foreground(twin.NewColor16(14))
style = style.WithForeground(twin.NewColor16(14))
case 97:
style = style.Foreground(twin.NewColor16(15))
style = style.WithForeground(twin.NewColor16(15))

case 100:
style = style.Background(twin.NewColor16(8))
style = style.WithBackground(twin.NewColor16(8))
case 101:
style = style.Background(twin.NewColor16(9))
style = style.WithBackground(twin.NewColor16(9))
case 102:
style = style.Background(twin.NewColor16(10))
style = style.WithBackground(twin.NewColor16(10))
case 103:
style = style.Background(twin.NewColor16(11))
style = style.WithBackground(twin.NewColor16(11))
case 104:
style = style.Background(twin.NewColor16(12))
style = style.WithBackground(twin.NewColor16(12))
case 105:
style = style.Background(twin.NewColor16(13))
style = style.WithBackground(twin.NewColor16(13))
case 106:
style = style.Background(twin.NewColor16(14))
style = style.WithBackground(twin.NewColor16(14))
case 107:
style = style.Background(twin.NewColor16(15))
style = style.WithBackground(twin.NewColor16(15))

default:
return style, numbersBuffer, fmt.Errorf("Unrecognized ANSI SGR code <%d>", number)
Expand Down
2 changes: 1 addition & 1 deletion m/ansiTokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestConsumeCompositeColorIncomplete24Bit(t *testing.T) {
func TestRawUpdateStyle(t *testing.T) {
numberColored, _, err := rawUpdateStyle(twin.StyleDefault, "33m", make([]uint, 0))
assert.NilError(t, err)
assert.Equal(t, numberColored, twin.StyleDefault.Foreground(twin.NewColor16(3)))
assert.Equal(t, numberColored, twin.StyleDefault.WithForeground(twin.NewColor16(3)))
}

// Test with the recommended terminator ESC-backslash.
Expand Down
30 changes: 15 additions & 15 deletions m/pager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func TestFgColorRendering(t *testing.T) {
"\x1b[30ma\x1b[31mb\x1b[32mc\x1b[33md\x1b[34me\x1b[35mf\x1b[36mg\x1b[37mh\x1b[0mi"))

var answers = []twin.Cell{
twin.NewCell('a', twin.StyleDefault.Foreground(twin.NewColor16(0))),
twin.NewCell('b', twin.StyleDefault.Foreground(twin.NewColor16(1))),
twin.NewCell('c', twin.StyleDefault.Foreground(twin.NewColor16(2))),
twin.NewCell('d', twin.StyleDefault.Foreground(twin.NewColor16(3))),
twin.NewCell('e', twin.StyleDefault.Foreground(twin.NewColor16(4))),
twin.NewCell('f', twin.StyleDefault.Foreground(twin.NewColor16(5))),
twin.NewCell('g', twin.StyleDefault.Foreground(twin.NewColor16(6))),
twin.NewCell('h', twin.StyleDefault.Foreground(twin.NewColor16(7))),
twin.NewCell('a', twin.StyleDefault.WithForeground(twin.NewColor16(0))),
twin.NewCell('b', twin.StyleDefault.WithForeground(twin.NewColor16(1))),
twin.NewCell('c', twin.StyleDefault.WithForeground(twin.NewColor16(2))),
twin.NewCell('d', twin.StyleDefault.WithForeground(twin.NewColor16(3))),
twin.NewCell('e', twin.StyleDefault.WithForeground(twin.NewColor16(4))),
twin.NewCell('f', twin.StyleDefault.WithForeground(twin.NewColor16(5))),
twin.NewCell('g', twin.StyleDefault.WithForeground(twin.NewColor16(6))),
twin.NewCell('h', twin.StyleDefault.WithForeground(twin.NewColor16(7))),
twin.NewCell('i', twin.StyleDefault),
}

Expand All @@ -80,7 +80,7 @@ func TestBrokenUtf8(t *testing.T) {
twin.NewCell('a', twin.StyleDefault),
twin.NewCell('b', twin.StyleDefault),
twin.NewCell('c', twin.StyleDefault),
twin.NewCell('?', twin.StyleDefault.Foreground(twin.NewColor16(7)).Background(twin.NewColor16(1))),
twin.NewCell('?', twin.StyleDefault.WithForeground(twin.NewColor16(7)).WithBackground(twin.NewColor16(1))),
twin.NewCell('d', twin.StyleDefault),
twin.NewCell('e', twin.StyleDefault),
twin.NewCell('f', twin.StyleDefault),
Expand Down Expand Up @@ -168,8 +168,8 @@ func TestCodeHighlighting(t *testing.T) {
panic(err)
}

packageKeywordStyle := twin.StyleDefault.WithAttr(twin.AttrBold).Foreground(twin.NewColorHex(0x6AB825))
packageNameStyle := twin.StyleDefault.Foreground(twin.NewColorHex(0xD0D0D0))
packageKeywordStyle := twin.StyleDefault.WithAttr(twin.AttrBold).WithForeground(twin.NewColorHex(0x6AB825))
packageNameStyle := twin.StyleDefault.WithForeground(twin.NewColorHex(0xD0D0D0))
var answers = []twin.Cell{
twin.NewCell('p', packageKeywordStyle),
twin.NewCell('a', packageKeywordStyle),
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestManPageFormatting(t *testing.T) {
testManPageFormatting(t, "_\x08x", twin.NewCell('x', twin.StyleDefault.WithAttr(twin.AttrUnderline)))

// Corner cases
testManPageFormatting(t, "\x08", twin.NewCell('<', twin.StyleDefault.Foreground(twin.NewColor16(7)).Background(twin.NewColor16(1))))
testManPageFormatting(t, "\x08", twin.NewCell('<', twin.StyleDefault.WithForeground(twin.NewColor16(7)).WithBackground(twin.NewColor16(1))))

// FIXME: Test two consecutive backspaces

Expand Down Expand Up @@ -527,7 +527,7 @@ func TestClearToEndOfLine_ClearFromStart(t *testing.T) {
var expected []twin.Cell
for len(expected) < screenWidth {
expected = append(expected,
twin.NewCell(' ', twin.StyleDefault.Background(twin.NewColor16(4))),
twin.NewCell(' ', twin.StyleDefault.WithBackground(twin.NewColor16(4))),
)
}

Expand All @@ -545,7 +545,7 @@ func TestClearToEndOfLine_ClearFromNotStart(t *testing.T) {
}
for len(expected) < screenWidth {
expected = append(expected,
twin.NewCell(' ', twin.StyleDefault.Background(twin.NewColor16(4))),
twin.NewCell(' ', twin.StyleDefault.WithBackground(twin.NewColor16(4))),
)
}

Expand Down Expand Up @@ -575,7 +575,7 @@ func TestClearToEndOfLine_ClearFromStartScrolledRight(t *testing.T) {
var expected []twin.Cell
for len(expected) < screenWidth {
expected = append(expected,
twin.NewCell(' ', twin.StyleDefault.Background(twin.NewColor16(4))),
twin.NewCell(' ', twin.StyleDefault.WithBackground(twin.NewColor16(4))),
)
}

Expand Down
2 changes: 1 addition & 1 deletion m/styledStringSplitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ func TestColonColors(t *testing.T) {
assert.Equal(t, twin.StyleDefault, trailer)
assert.Equal(t, 1, len(styledStrings))
assert.Equal(t, "hello", styledStrings[0].String)
assert.Equal(t, twin.StyleDefault.Foreground(twin.NewColor256(238)), styledStrings[0].Style)
assert.Equal(t, twin.StyleDefault.WithForeground(twin.NewColor256(238)), styledStrings[0].Style)
}
4 changes: 2 additions & 2 deletions twin/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (attr AttrMask) has(attrs AttrMask) bool {
return attr&attrs != 0
}

func (style Style) Background(color Color) Style {
func (style Style) WithBackground(color Color) Style {
return Style{
fg: style.fg,
bg: color,
Expand All @@ -126,7 +126,7 @@ func (style Style) Background(color Color) Style {
}
}

func (style Style) Foreground(color Color) Style {
func (style Style) WithForeground(color Color) Style {
return Style{
fg: color,
bg: style.bg,
Expand Down

0 comments on commit af014a3

Please sign in to comment.