Skip to content

Commit

Permalink
refactor(cellbuf): rename Style to CellStyle and Hyperlink to CellLink
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Sep 18, 2024
1 parent 9350707 commit 227168d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cellbuf/buffer_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
// Render returns a string representation of the buffer with ANSI escape
// sequences. Use [ansi.Strip] to remove them.
func (b *Buffer) Render() string {
var pen Style
var link Hyperlink
var pen CellStyle
var link CellLink
var buf bytes.Buffer
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
Expand Down
4 changes: 2 additions & 2 deletions cellbuf/buffer_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func writeFrame[
decodeRune TDecodeRuneFunc,
) (n int, err error) {
var cell Cell
var pen Style
var link Hyperlink
var pen CellStyle
var link CellLink
origX := x

p := ansi.GetParser()
Expand Down
16 changes: 8 additions & 8 deletions cellbuf/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var (
type Cell struct {
// The style of the cell. Nil style means no style. Zero value prints a
// reset sequence.
Style Style
Style CellStyle

// Link is the hyperlink of the cell.
Link Hyperlink
Link CellLink

// Content is the string representation of the cell as a grapheme cluster.
Content string
Expand Down Expand Up @@ -85,30 +85,30 @@ func (c *Cell) Info() string {
return b.String()
}

// Hyperlink represents a hyperlink in the terminal screen.
type Hyperlink struct {
// CellLink represents a hyperlink in the terminal screen.
type CellLink struct {
URL string
URLID string
}

// Reset resets the hyperlink to the default state zero value.
func (h *Hyperlink) Reset() {
func (h *CellLink) Reset() {
h.URL = ""
h.URLID = ""
}

// Equal returns true if the hyperlink is equal to the other hyperlink.
func (h Hyperlink) Equal(o Hyperlink) bool {
func (h CellLink) Equal(o CellLink) bool {
return h.URL == o.URL && h.URLID == o.URLID
}

// IsEmpty returns true if the hyperlink is empty.
func (h Hyperlink) IsEmpty() bool {
func (h CellLink) IsEmpty() bool {
return h.URL == "" && h.URLID == ""
}

// Info returns a string representation of the hyperlink.
func (h Hyperlink) Info() string {
func (h CellLink) Info() string {
if h.URL == "" && h.URLID == "" {
return "Hyperlink{}"
} else if h.URL == "" {
Expand Down
42 changes: 21 additions & 21 deletions cellbuf/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (u UnderlineStyle) String() string {
return underlineStyleNames[u]
}

// Style represents the Style of a cell.
type Style struct {
// CellStyle represents the CellStyle of a cell.
type CellStyle struct {
Fg ansi.Color
Bg ansi.Color
Ul ansi.Color
Expand All @@ -96,7 +96,7 @@ type Style struct {
}

// Sequence returns the ANSI sequence that sets the style.
func (s Style) Sequence() string {
func (s CellStyle) Sequence() string {
if s.IsEmpty() {
return ansi.ResetStyle
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func (s Style) Sequence() string {

// DiffSequence returns the ANSI sequence that sets the style as a diff from
// another style.
func (s Style) DiffSequence(o Style) string {
func (s CellStyle) DiffSequence(o CellStyle) string {
if o.IsEmpty() {
return s.Sequence()
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func (s Style) DiffSequence(o Style) string {
}

// Equal returns true if the style is equal to the other style.
func (s Style) Equal(o Style) bool {
func (s CellStyle) Equal(o CellStyle) bool {
return colorEqual(s.Fg, o.Fg) &&
colorEqual(s.Bg, o.Bg) &&
colorEqual(s.Ul, o.Ul) &&
Expand All @@ -268,7 +268,7 @@ func colorEqual(c, o ansi.Color) bool {
}

// Bold sets the bold attribute.
func (s *Style) Bold(v bool) *Style {
func (s *CellStyle) Bold(v bool) *CellStyle {
if v {
s.Attrs |= BoldAttr
} else {
Expand All @@ -278,7 +278,7 @@ func (s *Style) Bold(v bool) *Style {
}

// Faint sets the faint attribute.
func (s *Style) Faint(v bool) *Style {
func (s *CellStyle) Faint(v bool) *CellStyle {
if v {
s.Attrs |= FaintAttr
} else {
Expand All @@ -288,7 +288,7 @@ func (s *Style) Faint(v bool) *Style {
}

// Italic sets the italic attribute.
func (s *Style) Italic(v bool) *Style {
func (s *CellStyle) Italic(v bool) *CellStyle {
if v {
s.Attrs |= ItalicAttr
} else {
Expand All @@ -298,7 +298,7 @@ func (s *Style) Italic(v bool) *Style {
}

// SlowBlink sets the slow blink attribute.
func (s *Style) SlowBlink(v bool) *Style {
func (s *CellStyle) SlowBlink(v bool) *CellStyle {
if v {
s.Attrs |= SlowBlinkAttr
} else {
Expand All @@ -308,7 +308,7 @@ func (s *Style) SlowBlink(v bool) *Style {
}

// RapidBlink sets the rapid blink attribute.
func (s *Style) RapidBlink(v bool) *Style {
func (s *CellStyle) RapidBlink(v bool) *CellStyle {
if v {
s.Attrs |= RapidBlinkAttr
} else {
Expand All @@ -318,7 +318,7 @@ func (s *Style) RapidBlink(v bool) *Style {
}

// Reverse sets the reverse attribute.
func (s *Style) Reverse(v bool) *Style {
func (s *CellStyle) Reverse(v bool) *CellStyle {
if v {
s.Attrs |= ReverseAttr
} else {
Expand All @@ -328,7 +328,7 @@ func (s *Style) Reverse(v bool) *Style {
}

// Conceal sets the conceal attribute.
func (s *Style) Conceal(v bool) *Style {
func (s *CellStyle) Conceal(v bool) *CellStyle {
if v {
s.Attrs |= ConcealAttr
} else {
Expand All @@ -338,7 +338,7 @@ func (s *Style) Conceal(v bool) *Style {
}

// Strikethrough sets the strikethrough attribute.
func (s *Style) Strikethrough(v bool) *Style {
func (s *CellStyle) Strikethrough(v bool) *CellStyle {
if v {
s.Attrs |= StrikethroughAttr
} else {
Expand All @@ -348,40 +348,40 @@ func (s *Style) Strikethrough(v bool) *Style {
}

// UnderlineStyle sets the underline style.
func (s *Style) UnderlineStyle(style UnderlineStyle) *Style {
func (s *CellStyle) UnderlineStyle(style UnderlineStyle) *CellStyle {
s.UlStyle = style
return s
}

// Underline sets the underline attribute.
// This is a syntactic sugar for [UnderlineStyle].
func (s *Style) Underline(v bool) *Style {
func (s *CellStyle) Underline(v bool) *CellStyle {
if v {
return s.UnderlineStyle(SingleUnderline)
}
return s.UnderlineStyle(NoUnderline)
}

// Foreground sets the foreground color.
func (s *Style) Foreground(c ansi.Color) *Style {
func (s *CellStyle) Foreground(c ansi.Color) *CellStyle {
s.Fg = c
return s
}

// Background sets the background color.
func (s *Style) Background(c ansi.Color) *Style {
func (s *CellStyle) Background(c ansi.Color) *CellStyle {
s.Bg = c
return s
}

// UnderlineColor sets the underline color.
func (s *Style) UnderlineColor(c ansi.Color) *Style {
func (s *CellStyle) UnderlineColor(c ansi.Color) *CellStyle {
s.Ul = c
return s
}

// Reset resets the style to default.
func (s *Style) Reset() *Style {
func (s *CellStyle) Reset() *CellStyle {
s.Fg = nil
s.Bg = nil
s.Ul = nil
Expand All @@ -391,12 +391,12 @@ func (s *Style) Reset() *Style {
}

// IsEmpty returns true if the style is empty.
func (s *Style) IsEmpty() bool {
func (s *CellStyle) IsEmpty() bool {
return s.Fg == nil && s.Bg == nil && s.Ul == nil && s.Attrs == ResetAttr && s.UlStyle == NoUnderline
}

// Info returns a string representation of the style.
func (s *Style) Info() string {
func (s *CellStyle) Info() string {
if s.IsEmpty() {
return "Style{ResetAttr}"
}
Expand Down

0 comments on commit 227168d

Please sign in to comment.