2017-02-16 15:21:11 +03:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-06-24 18:40:18 +03:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
package termui
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
// 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
|
2016-10-28 20:05:01 +03:00
|
|
|
ls.BorderLabel = "List"
|
2015-06-24 18:40:18 +03:00
|
|
|
ls.Height = 7
|
|
|
|
ls.Width = 25
|
|
|
|
ls.Y = 0
|
|
|
|
*/
|
|
|
|
type List struct {
|
|
|
|
Block
|
|
|
|
Items []string
|
|
|
|
Overflow string
|
|
|
|
ItemFgColor Attribute
|
|
|
|
ItemBgColor Attribute
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewList returns a new *List with current theme.
|
|
|
|
func NewList() *List {
|
|
|
|
l := &List{Block: *NewBlock()}
|
|
|
|
l.Overflow = "hidden"
|
2016-02-11 16:16:52 +02:00
|
|
|
l.ItemFgColor = ThemeAttr("list.item.fg")
|
|
|
|
l.ItemBgColor = ThemeAttr("list.item.bg")
|
2015-06-24 18:40:18 +03:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer implements Bufferer interface.
|
2016-02-11 16:16:52 +02:00
|
|
|
func (l *List) Buffer() Buffer {
|
|
|
|
buf := l.Block.Buffer()
|
|
|
|
|
2015-06-24 18:40:18 +03:00
|
|
|
switch l.Overflow {
|
|
|
|
case "wrap":
|
2016-02-11 16:16:52 +02:00
|
|
|
cs := DefaultTxBuilder.Build(strings.Join(l.Items, "\n"), l.ItemFgColor, l.ItemBgColor)
|
2015-06-24 18:40:18 +03:00
|
|
|
i, j, k := 0, 0, 0
|
2016-02-11 16:16:52 +02:00
|
|
|
for i < l.innerArea.Dy() && k < len(cs) {
|
|
|
|
w := cs[k].Width()
|
|
|
|
if cs[k].Ch == '\n' || j+w > l.innerArea.Dx() {
|
2015-06-24 18:40:18 +03:00
|
|
|
i++
|
|
|
|
j = 0
|
2016-02-11 16:16:52 +02:00
|
|
|
if cs[k].Ch == '\n' {
|
2015-06-24 18:40:18 +03:00
|
|
|
k++
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-02-11 16:16:52 +02:00
|
|
|
buf.Set(l.innerArea.Min.X+j, l.innerArea.Min.Y+i, cs[k])
|
2015-06-24 18:40:18 +03:00
|
|
|
|
|
|
|
k++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
case "hidden":
|
|
|
|
trimItems := l.Items
|
2016-02-11 16:16:52 +02:00
|
|
|
if len(trimItems) > l.innerArea.Dy() {
|
|
|
|
trimItems = trimItems[:l.innerArea.Dy()]
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|
|
|
|
for i, v := range trimItems {
|
2016-02-11 16:16:52 +02:00
|
|
|
cs := DTrimTxCls(DefaultTxBuilder.Build(v, l.ItemFgColor, l.ItemBgColor), l.innerArea.Dx())
|
2015-06-24 18:40:18 +03:00
|
|
|
j := 0
|
2016-02-11 16:16:52 +02:00
|
|
|
for _, vv := range cs {
|
|
|
|
w := vv.Width()
|
|
|
|
buf.Set(l.innerArea.Min.X+j, l.innerArea.Min.Y+i, vv)
|
2015-06-24 18:40:18 +03:00
|
|
|
j += w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 16:16:52 +02:00
|
|
|
return buf
|
2015-06-24 18:40:18 +03:00
|
|
|
}
|