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
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
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
|
|
|
|
Items []string
|
|
|
|
Overflow string
|
|
|
|
ItemFgColor Attribute
|
|
|
|
ItemBgColor Attribute
|
|
|
|
}
|
|
|
|
|
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-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":
|
|
|
|
rs := str2runes(strings.Join(l.Items, "\n"))
|
|
|
|
i, j, k := 0, 0, 0
|
|
|
|
for i < l.innerHeight && k < len(rs) {
|
2015-03-25 16:04:15 -06:00
|
|
|
w := charWidth(rs[k])
|
|
|
|
if rs[k] == '\n' || j+w > l.innerWidth {
|
2015-02-03 18:56:49 -07:00
|
|
|
i++
|
|
|
|
j = 0
|
|
|
|
if rs[k] == '\n' {
|
|
|
|
k++
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pi := Point{}
|
|
|
|
pi.X = l.innerX + j
|
|
|
|
pi.Y = l.innerY + i
|
|
|
|
|
2015-03-03 11:28:09 -07:00
|
|
|
pi.Ch = rs[k]
|
|
|
|
pi.Bg = l.ItemBgColor
|
|
|
|
pi.Fg = l.ItemFgColor
|
2015-02-03 18:56:49 -07:00
|
|
|
|
|
|
|
ps = append(ps, pi)
|
|
|
|
k++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
case "hidden":
|
|
|
|
trimItems := l.Items
|
|
|
|
if len(trimItems) > l.innerHeight {
|
|
|
|
trimItems = trimItems[:l.innerHeight]
|
|
|
|
}
|
|
|
|
for i, v := range trimItems {
|
|
|
|
rs := trimStr2Runes(v, l.innerWidth)
|
|
|
|
|
|
|
|
j := 0
|
|
|
|
for _, vv := range rs {
|
2015-03-25 16:04:15 -06:00
|
|
|
w := charWidth(vv)
|
2015-02-03 18:56:49 -07:00
|
|
|
p := Point{}
|
|
|
|
p.X = l.innerX + j
|
|
|
|
p.Y = l.innerY + i
|
|
|
|
|
2015-03-03 11:28:09 -07:00
|
|
|
p.Ch = vv
|
|
|
|
p.Bg = l.ItemBgColor
|
|
|
|
p.Fg = l.ItemFgColor
|
2015-02-03 18:56:49 -07:00
|
|
|
|
|
|
|
ps = append(ps, p)
|
2015-03-25 16:04:15 -06:00
|
|
|
j += w
|
2015-02-03 18:56:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|