Compare commits

..

1 Commits

Author SHA1 Message Date
Ian C 28827c1146 add compact flag
Add an option to render the list compactly (without blank lines between `ListItem`s). This flag will be ignored if `showSecondaryText` is set to true.
2021-12-18 11:39:51 -08:00
8 changed files with 50 additions and 132 deletions

View File

@ -1,12 +1,6 @@
v1.5.9 (2022-02-02)
- Fix unlocking application mutex when failing to initialize the screen
- Fix DropDown.SetCurrentOption Unlock of unlocked RWMutex panic
v1.5.8 (2022-08-01)
v1.5.8 (WIP)
- Add TabbedPanels.SetChangedFunc
- Add DropDown.SetDropDownSelectedSymbolRune (PR by gdamore)
- Add List.SetIndicators (PR by gdamore)
- Fix some missing ANSI translations
- Fix some missing ANSI translations
v1.5.7 (2021-09-01)
- Add Application.HandlePanic

View File

@ -1,7 +1,6 @@
# cview - Terminal-based user interface toolkit
[![GoDoc](https://code.rocketnine.space/tslocum/godoc-static/raw/branch/master/badge.svg)](https://docs.rocketnine.space/code.rocketnine.space/tslocum/cview)
[![Donate via LiberaPay](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
[![Donate via Patreon](https://img.shields.io/badge/dynamic/json?color=%23e85b46&label=Patreon&query=data.attributes.patron_count&suffix=%20patrons&url=https%3A%2F%2Fwww.patreon.com%2Fapi%2Fcampaigns%2F5252223)](https://www.patreon.com/rocketnine)
[![Donate](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
This package is a fork of [tview](https://github.com/rivo/tview).
See [FORK.md](https://code.rocketnine.space/tslocum/cview/src/branch/master/FORK.md) for more information.

View File

@ -25,9 +25,9 @@ const (
// The following command displays a primitive p on the screen until Ctrl-C is
// pressed:
//
// if err := cview.NewApplication().SetRoot(p, true).Run(); err != nil {
// panic(err)
// }
// if err := cview.NewApplication().SetRoot(p, true).Run(); err != nil {
// panic(err)
// }
type Application struct {
// The application's screen. Apart from Run(), this variable should never be
// set directly. Always use the screenReplacement channel after calling
@ -251,9 +251,11 @@ func (a *Application) init() error {
var err error
a.screen, err = tcell.NewScreen()
if err != nil {
a.Unlock()
return err
}
if err = a.screen.Init(); err != nil {
a.Unlock()
return err
}
a.width, a.height = a.screen.Size()
@ -302,7 +304,6 @@ func (a *Application) Run() error {
// Initialize screen
err := a.init()
if err != nil {
a.Unlock()
return err
}

View File

@ -140,9 +140,6 @@ type DropDown struct {
// The symbol to draw at the end of the field when opened.
dropDownOpenSymbol rune
// The symbol used to draw the selected item when opened.
dropDownSelectedSymbol rune
// A flag that determines whether the drop down symbol is always drawn.
alwaysDrawDropDownSymbol bool
@ -169,16 +166,12 @@ func NewDropDown() *DropDown {
prefixTextColor: Styles.ContrastSecondaryTextColor,
dropDownSymbol: Styles.DropDownSymbol,
dropDownOpenSymbol: Styles.DropDownOpenSymbol,
dropDownSelectedSymbol: Styles.DropDownSelectedSymbol,
abbreviationChars: Styles.DropDownAbbreviationChars,
labelColorFocused: ColorUnset,
fieldBackgroundColorFocused: ColorUnset,
fieldTextColorFocused: ColorUnset,
}
if sym := d.dropDownSelectedSymbol; sym != 0 {
list.SetIndicators(" "+string(sym)+" ", "", " ", "")
}
d.focus = d
return d
@ -198,21 +191,6 @@ func (d *DropDown) SetDropDownOpenSymbolRune(symbol rune) {
d.Lock()
defer d.Unlock()
d.dropDownOpenSymbol = symbol
if symbol != 0 {
d.list.SetIndicators(" "+string(symbol)+" ", "", " ", "")
} else {
d.list.SetIndicators("", "", "", "")
}
}
// SetDropDownSelectedSymbolRune sets the rune to be drawn at the start of the
// selected list item.
func (d *DropDown) SetDropDownSelectedSymbolRune(symbol rune) {
d.Lock()
defer d.Unlock()
d.dropDownSelectedSymbol = symbol
}
// SetAlwaysDrawDropDownSymbol sets a flad that determines whether the drop
@ -228,6 +206,8 @@ func (d *DropDown) SetAlwaysDrawDropDownSymbol(alwaysDraw bool) {
// this function will also trigger the "selected" callback (if there is one).
func (d *DropDown) SetCurrentOption(index int) {
d.Lock()
defer d.Unlock()
if index >= 0 && index < len(d.options) {
d.currentOption = index
d.list.SetCurrentItem(index)
@ -250,7 +230,6 @@ func (d *DropDown) SetCurrentOption(index int) {
d.Lock()
}
}
d.Unlock()
}
// GetCurrentOption returns the index of the currently selected option as well
@ -436,8 +415,7 @@ func (d *DropDown) getFieldWidth() int {
}
fieldWidth += len(d.optionPrefix) + len(d.optionSuffix)
fieldWidth += len(d.currentOptionPrefix) + len(d.currentOptionSuffix)
// space <text> space + dropDownSymbol + space
fieldWidth += 4
fieldWidth += 3 // space + dropDownSymbol + space
return fieldWidth
}

9
go.mod
View File

@ -4,9 +4,10 @@ go 1.12
require (
code.rocketnine.space/tslocum/cbind v0.1.5
github.com/gdamore/tcell/v2 v2.7.0
github.com/gdamore/tcell/v2 v2.4.1-0.20211027054709-8fc0c2be8f7d
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-runewidth v0.0.15
github.com/rivo/uniseg v0.4.6
golang.org/x/term v0.16.0 // indirect
github.com/mattn/go-runewidth v0.0.14-0.20210830053702-dc8fe66265af
github.com/rivo/uniseg v0.2.0
golang.org/x/sys v0.0.0-20211112193437-faf0a1b62c6b // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
)

49
go.sum
View File

@ -3,58 +3,29 @@ code.rocketnine.space/tslocum/cbind v0.1.5/go.mod h1:LtfqJTzM7qhg88nAvNhx+VnTjZ0
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.2.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/gdamore/tcell/v2 v2.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA=
github.com/gdamore/tcell/v2 v2.7.0/go.mod h1:hl/KtAANGBecfIPxk+FzKvThTqI84oplgbPEmVX60b8=
github.com/gdamore/tcell/v2 v2.4.1-0.20211027054709-8fc0c2be8f7d h1:4xJ3ZUnK8TJg4O/zuC7rVvEWPpz2p+3mPMr3eLn7BQg=
github.com/gdamore/tcell/v2 v2.4.1-0.20211027054709-8fc0c2be8f7d/go.mod h1:ZPwXnysybtQqdqKcWMWXux9aGdtMHe+kr+cwEZEe+A4=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14-0.20210830053702-dc8fe66265af h1:Vz2tUCLqu+a1igkarhrqNG+OFBs0uJqB3ADWFsQA9jM=
github.com/mattn/go-runewidth v0.0.14-0.20210830053702-dc8fe66265af/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309040221-94ec62e08169/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/sys v0.0.0-20211112193437-faf0a1b62c6b h1:uo+9AuR+gDt/gdj+1BaLhdOHsaGI6YU6585IiDcLrFE=
golang.org/x/sys v0.0.0-20211112193437-faf0a1b62c6b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

72
list.go
View File

@ -194,14 +194,9 @@ type List struct {
// The height of the list the last time it was drawn.
height int
// Prefix and suffix strings drawn for unselected elements.
unselectedPrefix, unselectedSuffix []byte
// Prefix and suffix strings drawn for selected elements.
selectedPrefix, selectedSuffix []byte
// Maximum prefix and suffix width.
prefixWidth, suffixWidth int
// If true, will render each ListItem without blank lines in between. This flag
// is ignored if `showSecondaryText` is true.
compact bool
sync.RWMutex
}
@ -218,6 +213,7 @@ func NewList() *List {
selectedTextColor: Styles.PrimitiveBackgroundColor,
scrollBarColor: Styles.ScrollBarColor,
selectedBackgroundColor: Styles.PrimaryTextColor,
compact: false,
}
l.ContextMenu = NewContextMenu(l)
@ -528,6 +524,15 @@ func (l *List) SetDoneFunc(handler func()) {
l.done = handler
}
// SetCompactList sets the flag that determines whether a blank line is drawn between
// each ListItem. This flag will be ignored if `showSecondaryText` is true.
func (l *List) SetCompactList(compact bool) {
l.Lock()
defer l.Unlock()
l.compact = compact
}
// AddItem calls InsertItem() with an index of -1.
func (l *List) AddItem(item *ListItem) {
l.InsertItem(-1, item)
@ -636,24 +641,6 @@ func (l *List) SetItemEnabled(index int, enabled bool) {
item.disabled = !enabled
}
// SetIndicators is used to set prefix and suffix indicators for selected and unselected items.
func (l *List) SetIndicators(selectedPrefix, selectedSuffix, unselectedPrefix, unselectedSuffix string) {
l.Lock()
defer l.Unlock()
l.selectedPrefix = []byte(selectedPrefix)
l.selectedSuffix = []byte(selectedSuffix)
l.unselectedPrefix = []byte(unselectedPrefix)
l.unselectedSuffix = []byte(unselectedSuffix)
l.prefixWidth = len(selectedPrefix)
if len(unselectedPrefix) > l.prefixWidth {
l.prefixWidth = len(unselectedPrefix)
}
l.suffixWidth = len(selectedSuffix)
if len(unselectedSuffix) > l.suffixWidth {
l.suffixWidth = len(unselectedSuffix)
}
}
// FindItems searches the main and secondary texts for the given strings and
// returns a list of item indices in which those strings are found. One of the
// two search strings may be empty, it will then be ignored. Indices are always
@ -826,7 +813,11 @@ func (l *List) updateOffset() {
}
} else {
if l.currentItem-l.itemOffset >= h {
l.itemOffset = l.currentItem + 1 - h
if l.compact {
l.itemOffset = l.currentItem - h
} else {
l.itemOffset = l.currentItem + 1 - h
}
}
}
@ -893,7 +884,7 @@ func (l *List) Draw(screen tcell.Screen) {
// Determine the dimensions.
x, y, width, height := l.GetInnerRect()
leftEdge := x
fullWidth := width + l.paddingLeft + l.paddingRight + l.prefixWidth + l.suffixWidth
fullWidth := width + l.paddingLeft + l.paddingRight
bottomLimit := y + height
l.height = height
@ -961,25 +952,7 @@ func (l *List) Draw(screen tcell.Screen) {
RenderScrollBar(screen, l.scrollBarVisibility, scrollBarX, y, scrollBarHeight, len(l.items), scrollBarCursor, index-l.itemOffset, l.hasFocus, l.scrollBarColor)
y++
continue
}
if index == l.currentItem {
if len(l.selectedPrefix) > 0 {
mainText = append(l.selectedPrefix, mainText...)
}
if len(l.selectedSuffix) > 0 {
mainText = append(mainText, l.selectedSuffix...)
}
} else {
if len(l.unselectedPrefix) > 0 {
mainText = append(l.unselectedPrefix, mainText...)
}
if len(l.unselectedSuffix) > 0 {
mainText = append(mainText, l.unselectedSuffix...)
}
}
if item.disabled {
} else if item.disabled {
// Shortcuts.
if showShortcuts && item.shortcut != 0 {
Print(screen, []byte(fmt.Sprintf("(%c)", item.shortcut)), x-5, y, 4, AlignRight, tcell.ColorDarkSlateGray.TrueColor())
@ -1023,7 +996,10 @@ func (l *List) Draw(screen tcell.Screen) {
RenderScrollBar(screen, l.scrollBarVisibility, scrollBarX, y, scrollBarHeight, len(l.items), scrollBarCursor, index-l.itemOffset, l.hasFocus, l.scrollBarColor)
y++
// Compact List
if !l.compact {
y++
}
if y >= bottomLimit {
break

View File

@ -39,7 +39,6 @@ type Theme struct {
DropDownAbbreviationChars string // The chars to show when the option's text gets shortened.
DropDownSymbol rune // The symbol to draw at the end of the field when closed.
DropDownOpenSymbol rune // The symbol to draw at the end of the field when opened.
DropDownSelectedSymbol rune // The symbol to draw to indicate the selected list item.
// Scroll bar
ScrollBarColor tcell.Color
@ -81,7 +80,6 @@ var Styles = Theme{
DropDownAbbreviationChars: "...",
DropDownSymbol: '◀',
DropDownOpenSymbol: '▼',
DropDownSelectedSymbol: '▶',
ScrollBarColor: tcell.ColorWhite.TrueColor(),