2015-03-20 14:21:50 -06:00
|
|
|
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-02-03 18:56:49 -07:00
|
|
|
package termui
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// List displays []string as its items,
|
|
|
|
// it has a Overflow option (default is "hidden"), when set to "hidden",
|
|
|
|
// the item exceeding List's width is truncated, but when set to "wrap",
|
|
|
|
// the overflowed text breaks into next line.
|
|
|
|
/*
|
|
|
|
strs := []string{
|
|
|
|
"[0] github.com/gizak/termui",
|
|
|
|
"[1] editbox.go",
|
|
|
|
"[2] iterrupt.go",
|
|
|
|
"[3] keyboard.go",
|
|
|
|
"[4] output.go",
|
|
|
|
"[5] random_out.go",
|
|
|
|
"[6] dashboard.go",
|
|
|
|
"[7] nsf/termbox-go"}
|
|
|
|
|
|
|
|
ls := termui.NewList()
|
|
|
|
ls.Items = strs
|
|
|
|
ls.ItemFgColor = termui.ColorYellow
|
|
|
|
ls.Border.Label = "List"
|
|
|
|
ls.Height = 7
|
|
|
|
ls.Width = 25
|
|
|
|
ls.Y = 0
|
|
|
|
*/
|
2015-02-03 18:56:49 -07:00
|
|
|
type List struct {
|
|
|
|
Block
|
2015-04-05 13:55:29 -06:00
|
|
|
Items []string
|
|
|
|
Overflow string
|
|
|
|
ItemFgColor Attribute
|
|
|
|
ItemBgColor Attribute
|
|
|
|
RendererFactory TextRendererFactory
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// NewList returns a new *List with current theme.
|
2015-02-03 18:56:49 -07:00
|
|
|
func NewList() *List {
|
|
|
|
l := &List{Block: *NewBlock()}
|
|
|
|
l.Overflow = "hidden"
|
2015-03-11 14:15:59 -06:00
|
|
|
l.ItemFgColor = theme.ListItemFg
|
|
|
|
l.ItemBgColor = theme.ListItemBg
|
2015-04-05 13:55:29 -06:00
|
|
|
l.RendererFactory = NoopRendererFactory{}
|
2015-02-03 18:56:49 -07:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Buffer implements Bufferer interface.
|
2015-02-03 18:56:49 -07:00
|
|
|
func (l *List) Buffer() []Point {
|
|
|
|
ps := l.Block.Buffer()
|
|
|
|
switch l.Overflow {
|
|
|
|
case "wrap":
|
2015-04-05 13:55:29 -06:00
|
|
|
y := 0
|
|
|
|
for _, item := range l.Items {
|
|
|
|
x := 0
|
2015-02-03 18:56:49 -07:00
|
|
|
|
2015-04-05 13:55:29 -06:00
|
|
|
renderer := l.RendererFactory.TextRenderer(item)
|
|
|
|
sequence := renderer.Render(l.ItemFgColor, l.ItemBgColor)
|
|
|
|
for n := range []rune(sequence.NormalizedText) {
|
|
|
|
point, width := sequence.PointAt(n, x+l.innerX, y+l.innerY)
|
2015-02-03 18:56:49 -07:00
|
|
|
|
2015-04-05 13:55:29 -06:00
|
|
|
if width+x <= l.innerWidth {
|
|
|
|
ps = append(ps, point)
|
|
|
|
x += width
|
|
|
|
} else {
|
|
|
|
y++
|
|
|
|
x = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
y++
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
case "hidden":
|
|
|
|
trimItems := l.Items
|
|
|
|
if len(trimItems) > l.innerHeight {
|
|
|
|
trimItems = trimItems[:l.innerHeight]
|
|
|
|
}
|
|
|
|
|
2015-04-05 13:55:29 -06:00
|
|
|
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...)
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
|
|
|
}
|
2015-04-05 13:55:29 -06:00
|
|
|
|
2015-04-09 14:40:49 -06:00
|
|
|
return l.Block.chopOverflow(ps)
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|