Skip to content

Commit c997d3d

Browse files
committed
refactor(cellbuf): simplify NewCell and NewCellString
1 parent b7858a2 commit c997d3d

1 file changed

Lines changed: 13 additions & 24 deletions

File tree

cellbuf/buffer.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,30 @@ import (
1010
// NewCell returns a new cell. This is a convenience function that initializes a
1111
// new cell with the given content. The cell's width is determined by the
1212
// content using [runewidth.RuneWidth].
13-
func NewCell(r rune, comb ...rune) *Cell {
14-
width := runewidth.StringWidth(string(append([]rune{r}, comb...)))
15-
return &Cell{
16-
Rune: r,
17-
Comb: comb,
18-
Width: width,
19-
}
13+
func NewCell(r rune, comb ...rune) (c *Cell) {
14+
c = new(Cell)
15+
c.Rune = r
16+
c.Comb = comb
17+
c.Width = runewidth.StringWidth(string(append([]rune{r}, comb...)))
18+
return
2019
}
2120

2221
// NewCellString returns a new cell with the given string content. This is a
2322
// convenience function that initializes a new cell with the given content. The
2423
// cell's width is determined by the content using [wcwidth.StringWidth].
2524
// This will only use the first combined rune in the string. If the string is
2625
// empty, it will return an empty cell with a width of 0.
27-
func NewCellString(s string) *Cell {
28-
var r rune
29-
var comb []rune
30-
var w int
31-
for i, c := range s {
26+
func NewCellString(s string) (c *Cell) {
27+
c = new(Cell)
28+
c.Width = runewidth.StringWidth(s)
29+
for i, r := range s {
3230
if i == 0 {
33-
r = c
34-
w = runewidth.RuneWidth(c)
31+
c.Rune = r
3532
continue
3633
}
37-
if runewidth.RuneWidth(c) > 0 {
38-
break
39-
}
40-
comb = append(comb, c)
41-
}
42-
43-
return &Cell{
44-
Rune: r,
45-
Comb: comb,
46-
Width: w,
34+
c.Comb = append(c.Comb, r)
4735
}
36+
return
4837
}
4938

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

0 commit comments

Comments
 (0)