Bugfixes and refactoring
Bugfixes: - Fixes a bug which placed the tree dots (…) for overflown list on the wrong position. Refactoring - Renamed `TextRender` to `TextRenderer` - Renamed `NoopRenderer` to `PlainRenderer` - Renamed `NoopRendererFactory` to `PlainRendererFactory`
This commit is contained in:
parent
769ce01ae8
commit
3c08053c57
@ -19,8 +19,8 @@ func commonList() *termui.List {
|
|||||||
|
|
||||||
list := termui.NewList()
|
list := termui.NewList()
|
||||||
list.Items = strs
|
list.Items = strs
|
||||||
list.Height = 20
|
list.Height = 15
|
||||||
list.Width = 25
|
list.Width = 26
|
||||||
list.RendererFactory = termui.MarkdownTextRendererFactory{}
|
list.RendererFactory = termui.MarkdownTextRendererFactory{}
|
||||||
|
|
||||||
return list
|
return list
|
||||||
|
|||||||
63
list.go
63
list.go
@ -42,49 +42,54 @@ func NewList() *List {
|
|||||||
l.Overflow = "hidden"
|
l.Overflow = "hidden"
|
||||||
l.ItemFgColor = theme.ListItemFg
|
l.ItemFgColor = theme.ListItemFg
|
||||||
l.ItemBgColor = theme.ListItemBg
|
l.ItemBgColor = theme.ListItemBg
|
||||||
l.RendererFactory = NoopRendererFactory{}
|
l.RendererFactory = PlainRendererFactory{}
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buffer implements Bufferer interface.
|
// Buffer implements Bufferer interface.
|
||||||
func (l *List) Buffer() []Point {
|
func (l *List) Buffer() []Point {
|
||||||
ps := l.Block.Buffer()
|
buffer := l.Block.Buffer()
|
||||||
switch l.Overflow {
|
|
||||||
case "wrap":
|
|
||||||
y := 0
|
|
||||||
for _, item := range l.Items {
|
|
||||||
x := 0
|
|
||||||
|
|
||||||
renderer := l.RendererFactory.TextRenderer(item)
|
breakLoop := func(y int) bool {
|
||||||
sequence := renderer.Render(l.ItemFgColor, l.ItemBgColor)
|
return y+1 > l.innerHeight
|
||||||
for n := range []rune(sequence.NormalizedText) {
|
}
|
||||||
point, width := sequence.PointAt(n, x+l.innerX, y+l.innerY)
|
y := 0
|
||||||
|
|
||||||
if width+x <= l.innerWidth {
|
MainLoop:
|
||||||
ps = append(ps, point)
|
for _, item := range l.Items {
|
||||||
x += width
|
x := 0
|
||||||
} else {
|
bg, fg := l.ItemFgColor, l.ItemBgColor
|
||||||
|
renderer := l.RendererFactory.TextRenderer(item)
|
||||||
|
sequence := renderer.Render(bg, fg)
|
||||||
|
|
||||||
|
for n := range []rune(sequence.NormalizedText) {
|
||||||
|
point, width := sequence.PointAt(n, x+l.innerX, y+l.innerY)
|
||||||
|
|
||||||
|
if width+x <= l.innerWidth {
|
||||||
|
buffer = append(buffer, point)
|
||||||
|
x += width
|
||||||
|
} else {
|
||||||
|
if l.Overflow == "wrap" {
|
||||||
y++
|
y++
|
||||||
|
if breakLoop(y) {
|
||||||
|
break MainLoop
|
||||||
|
}
|
||||||
x = 0
|
x = 0
|
||||||
|
} else {
|
||||||
|
dotR := []rune(dot)[0]
|
||||||
|
dotX := l.innerWidth + l.innerX - charWidth(dotR)
|
||||||
|
p := newPointWithAttrs(dotR, dotX, y+l.innerY, bg, fg)
|
||||||
|
buffer = append(buffer, p)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
y++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "hidden":
|
y++
|
||||||
trimItems := l.Items
|
if breakLoop(y) {
|
||||||
if len(trimItems) > l.innerHeight {
|
break MainLoop
|
||||||
trimItems = trimItems[:l.innerHeight]
|
|
||||||
}
|
|
||||||
|
|
||||||
for y, item := range trimItems {
|
|
||||||
text := TrimStrIfAppropriate(item, l.innerWidth)
|
|
||||||
render := l.RendererFactory.TextRenderer(text)
|
|
||||||
sequence := render.RenderSequence(0, -1, l.ItemFgColor, l.ItemBgColor)
|
|
||||||
t, _ := sequence.Buffer(l.innerX, y+l.innerY)
|
|
||||||
ps = append(ps, t...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return l.Block.chopOverflow(ps)
|
return l.Block.chopOverflow(buffer)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TextRender adds common methods for rendering a text on screeen.
|
// TextRenderer adds common methods for rendering a text on screeen.
|
||||||
type TextRender interface {
|
type TextRenderer interface {
|
||||||
NormalizedText() string
|
NormalizedText() string
|
||||||
Render(lastColor, background Attribute) RenderedSequence
|
Render(lastColor, background Attribute) RenderedSequence
|
||||||
RenderSequence(start, end int, lastColor, background Attribute) RenderedSequence
|
RenderSequence(start, end int, lastColor, background Attribute) RenderedSequence
|
||||||