Skip to content

Commit

Permalink
refactor(cellbuf): simplify NewCell and NewCellString
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 27, 2025
1 parent b7858a2 commit c997d3d
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions cellbuf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,30 @@ import (
// NewCell returns a new cell. This is a convenience function that initializes a
// new cell with the given content. The cell's width is determined by the
// content using [runewidth.RuneWidth].
func NewCell(r rune, comb ...rune) *Cell {
width := runewidth.StringWidth(string(append([]rune{r}, comb...)))
return &Cell{
Rune: r,
Comb: comb,
Width: width,
}
func NewCell(r rune, comb ...rune) (c *Cell) {
c = new(Cell)
c.Rune = r
c.Comb = comb
c.Width = runewidth.StringWidth(string(append([]rune{r}, comb...)))
return
}

// NewCellString returns a new cell with the given string content. This is a
// convenience function that initializes a new cell with the given content. The
// cell's width is determined by the content using [wcwidth.StringWidth].
// This will only use the first combined rune in the string. If the string is
// empty, it will return an empty cell with a width of 0.
func NewCellString(s string) *Cell {
var r rune
var comb []rune
var w int
for i, c := range s {
func NewCellString(s string) (c *Cell) {
c = new(Cell)
c.Width = runewidth.StringWidth(s)
for i, r := range s {
if i == 0 {
r = c
w = runewidth.RuneWidth(c)
c.Rune = r
continue
}
if runewidth.RuneWidth(c) > 0 {
break
}
comb = append(comb, c)
}

return &Cell{
Rune: r,
Comb: comb,
Width: w,
c.Comb = append(c.Comb, r)
}
return
}

// NewGraphemeCell returns a new cell. This is a convenience function that
Expand Down

0 comments on commit c997d3d

Please sign in to comment.