Compare commits

...

23 Commits

Author SHA1 Message Date
Trevor Slocum c2285ed778 Fix unlocking application mutex when failing to initialize the screen
Thanks to omni for reporting and sharing a fix for this.
2024-02-02 10:32:23 -08:00
Trevor Slocum d5de9a7b0a Fix DropDown.SetCurrentOption Unlock of unlocked RWMutex panic 2023-09-11 19:50:26 -07:00
Trevor Slocum 51da14062c Update CHANGELOG 2022-08-01 13:23:40 -07:00
Trevor Slocum c1ab34fb2d Update README 2022-08-01 13:21:03 -07:00
tslocum bf436cfdb0 Merge pull request 'Update to tcell v2.5.2' (#90) from gdamore/cview:tcell252 into master
Reviewed-on: https://code.rocketnine.space/tslocum/cview/pulls/90
2022-08-01 13:11:20 -07:00
tslocum c7cb05912a Merge pull request 'Improved drop down list usability.' (#89) from gdamore/cview:dropdown into master
Reviewed-on: https://code.rocketnine.space/tslocum/cview/pulls/89
2022-08-01 13:09:56 -07:00
Garrett D'Amore 07da478daf Update to tcell v2.5.2 2022-07-30 18:08:42 -07:00
Garrett D'Amore 7fbbefd986 Improved drop down list usability. 2022-07-29 19:10:30 -07:00
Trevor Slocum c2f9074643 Add TabbedPanels.SetChangedFunc
Resolves #80.
2021-11-12 15:18:03 -08:00
Trevor Slocum b19f5402ae Document ANSI translation fix 2021-10-04 20:50:50 -07:00
tslocum 10ad6ee7dd Merge pull request 'Add missing ansi translations' (#78) from Seanstoppable/cview:addmissingansitranslations into master
Reviewed-on: https://code.rocketnine.space/tslocum/cview/pulls/78
2021-10-04 20:04:42 -07:00
Sean Smith b2661d1e10 Add missing ansi translations
Without these, the styles are not properly applied
2021-10-02 23:45:30 -04:00
Trevor Slocum be22f3b33c Release v1.5.7 2021-09-01 09:17:54 -07:00
Trevor Slocum a366ccc260 runewidth: Create lookup table
See the following pull request for more information.

https://github.com/mattn/go-runewidth/pull/54
2021-08-31 08:47:28 -07:00
Trevor Slocum be5c1fcafd Draw application after updating root primitive via Application.SetRoot
Resolves #77.
2021-08-08 17:30:29 -07:00
Trevor Slocum 4616c03bbe Fix Application.QueueEvent
Resolves #76.
2021-08-08 17:16:10 -07:00
Trevor Slocum 492ab21896 Add Application.HandlePanic
Resolves #74.
2021-08-03 13:20:44 -07:00
Trevor Slocum 6758d2a787 Document WordWrap bug 2021-08-02 12:59:40 -07:00
Trevor Slocum b4beaaeff9 Document TextView region tag fix 2021-08-02 11:27:18 -07:00
Oliver 6f90b4422c Fixed missed tag at EOL in TextView
This fix was cherry-picked and adapted from tview commit
ba9f3a6fa19d4eb2cebed6c256523ff90faa0bbb

Resolves #73.
2021-08-02 11:22:20 -07:00
Trevor Slocum 605d0123c1 Add Modal.SetButtonsAlign and Modal.SetTextAlign
Also, do not customize Modal window styling by default. Use GetForm and GetFrame to customize.

Resolves #71.
2021-07-17 12:52:49 -07:00
Trevor Slocum 3b49c99661 Add Application.Draw change to FORK.md 2021-07-17 11:43:54 -07:00
Trevor Slocum f794cceb95 Fix TextView.GetRegionText error when text contains color tags
Resolves #69.
2021-07-12 13:21:32 -07:00
40 changed files with 314 additions and 59 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.idea/
dist/
tmp/
vendor/
*.sh
cview.test

View File

@ -1,4 +1,23 @@
v1.5.6 (2020-07-08)
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)
- Add TabbedPanels.SetChangedFunc
- Add DropDown.SetDropDownSelectedSymbolRune (PR by gdamore)
- Add List.SetIndicators (PR by gdamore)
- Fix some missing ANSI translations
v1.5.7 (2021-09-01)
- Add Application.HandlePanic
- Add Modal.SetButtonsAlign and Modal.SetTextAlign
- Fix Application.QueueEvent
- Fix TextView.GetRegionText error when text contains color tags
- Fix TextView region tags when placed at the end of a line
- Do not customize Modal window styling by default (use GetForm and GetFrame to customize)
- Draw application after updating root primitive via Application.SetRoot
v1.5.6 (2021-07-08)
- Add TrueColorTags option and do not use TrueColor tag values by default
- Add TextView.SetHighlightForegroundColor and TextView.SetHighlightBackgroundColor
- Add TextView.SetVerticalAlign
@ -18,14 +37,14 @@ primitives to draw instead of the whole screen
- When resetting color with "-" tag, set background color to primitive
background color, rather than the terminal background color
v1.5.5 (2020-05-24)
v1.5.5 (2021-05-24)
- Fix Application.Suspend by restructuring event loop (queued updates will now only run between draws)
v1.5.4 (2020-04-03)
v1.5.4 (2021-04-03)
- Add TextView.GetBufferSize
- Fix strikethrough support
v1.5.3 (2020-01-14)
v1.5.3 (2021-01-14)
- Document how to prevent screen artifacts when using SetBackgroundTransparent
- Fix highlighting focused Form element
- Fix incorrect TabbedPanels colors

View File

@ -93,6 +93,13 @@ This is for consistency with `TabbedPanels`, which is built on top of `Panels`.
tview moves the updated panel to the end.
## `Application.Draw` may be used to draw the entire screen or a set of primitives
When calling `Application.Draw` without providing any primitives, the entire
screen is drawn. This behavior matches tview.
When using cview, you may provide one or more primitives to limit drawing.
## `Application.ForceDraw` has been removed
Because cview is [thread-safe](https://docs.rocketnine.space/code.rocketnine.space/tslocum/cview/#hdr-Concurrency),

View File

@ -39,6 +39,10 @@ Bugfixes in ANSI escape sequence handling as well as TextView
Sun Jul 12 13:34:19 2020 +0200
TextView.GetText() and .GetRegionText() didn't filter out empty colour tags. Fixes #453
d689098cf1ab89315db61d7020efa5bf4c0c1b44 (HEAD -> master)
Tue Apr 27 09:08:36 2021 +0200
Fixed missed tag at EOL in TextView
The cview license applies to all other commits.
----------

View File

@ -1,6 +1,7 @@
# 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](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
[![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)
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.

12
ansi.go
View File

@ -135,6 +135,10 @@ func (a *ansi) Write(text []byte) (int, error) {
if strings.IndexRune(a.attributes, 'd') < 0 {
a.attributes += "d"
}
case "3", "03":
if strings.IndexRune(a.attributes, 'i') < 0 {
a.attributes += "i"
}
case "4", "04":
if strings.IndexRune(a.attributes, 'u') < 0 {
a.attributes += "u"
@ -143,6 +147,14 @@ func (a *ansi) Write(text []byte) (int, error) {
if strings.IndexRune(a.attributes, 'l') < 0 {
a.attributes += "l"
}
case "7", "07":
if strings.IndexRune(a.attributes, 'r') < 0 {
a.attributes += "r"
}
case "9", "09":
if strings.IndexRune(a.attributes, 's') < 0 {
a.attributes += "s"
}
case "22":
if i := strings.IndexRune(a.attributes, 'b'); i >= 0 {
a.attributes = a.attributes[:i] + a.attributes[i+1:]

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
@ -125,6 +125,23 @@ func NewApplication() *Application {
}
}
// HandlePanic (when deferred at the start of a goroutine) handles panics
// gracefully. The terminal is returned to its original state before the panic
// message is printed.
//
// Panics may only be handled by the panicking goroutine. Because of this,
// HandlePanic must be deferred at the start of each goroutine (including main).
func (a *Application) HandlePanic() {
p := recover()
if p == nil {
return
}
a.finalizeScreen()
panic(p)
}
// SetInputCapture sets a function which captures all key events before they are
// forwarded to the key event handler of the primitive which currently has
// focus. This function can then choose to forward that key event (or a
@ -234,11 +251,9 @@ 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()
@ -287,18 +302,11 @@ func (a *Application) Run() error {
// Initialize screen
err := a.init()
if err != nil {
a.Unlock()
return err
}
// We catch panics to clean up because they mess up the terminal.
defer func() {
if p := recover(); p != nil {
if a.screen != nil {
a.screen.Fini()
}
panic(p)
}
}()
defer a.HandlePanic()
// Draw the screen for the first time.
a.Unlock()
@ -308,6 +316,8 @@ func (a *Application) Run() error {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer a.HandlePanic()
defer wg.Done()
for {
a.RLock()
@ -431,6 +441,8 @@ func (a *Application) Run() error {
semaphore := &sync.Mutex{}
go func() {
defer a.HandlePanic()
for update := range a.updates {
semaphore.Lock()
update()
@ -438,6 +450,16 @@ func (a *Application) Run() error {
}
}()
go func() {
defer a.HandlePanic()
for event := range a.events {
semaphore.Lock()
handle(event)
semaphore.Unlock()
}
}()
// Start screen event loop.
for {
a.Lock()
@ -570,13 +592,18 @@ func (a *Application) Stop() {
a.Lock()
defer a.Unlock()
a.finalizeScreen()
a.screenReplacement <- nil
}
func (a *Application) finalizeScreen() {
screen := a.screen
if screen == nil {
return
}
a.screen = nil
screen.Fini()
a.screenReplacement <- nil
}
// Suspend temporarily suspends the application by exiting terminal UI mode and
@ -734,7 +761,7 @@ func (a *Application) GetAfterDrawFunc() func(screen tcell.Screen) {
// This function must be called at least once or nothing will be displayed when
// the application starts.
//
// It also calls SetFocus() on the primitive.
// It also calls SetFocus() on the primitive and draws the application.
func (a *Application) SetRoot(root Primitive, fullscreen bool) {
a.Lock()
a.root = root
@ -745,6 +772,8 @@ func (a *Application) SetRoot(root Primitive, fullscreen bool) {
a.Unlock()
a.SetFocus(root)
a.Draw()
}
// ResizeToFullScreen resizes the given primitive such that it fills the entire

View File

@ -8,6 +8,7 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
box := cview.NewBox()
box.SetBorder(true)

View File

@ -5,6 +5,8 @@ import "code.rocketnine.space/tslocum/cview"
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
button := cview.NewButton("Hit Enter to close")

View File

@ -7,6 +7,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
checkbox := cview.NewCheckBox()

View File

@ -5,6 +5,8 @@ import "code.rocketnine.space/tslocum/cview"
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
dropdown := cview.NewDropDown()

View File

@ -14,6 +14,8 @@ func demoBox(title string) *cview.Box {
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
subFlex := cview.NewFlex()

View File

@ -18,6 +18,8 @@ func wrap(f func()) func(ev *tcell.EventKey) *tcell.EventKey {
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
input1 := cview.NewInputField()

View File

@ -7,6 +7,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
form := cview.NewForm()

View File

@ -8,6 +8,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
box := cview.NewBox()

View File

@ -7,6 +7,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
newPrimitive := func(text string) cview.Primitive {

View File

@ -12,6 +12,7 @@ const wordList = "ability,able,about,above,accept,according,account,across,act,a
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
words := strings.Split(wordList, ",")

View File

@ -17,6 +17,8 @@ type company struct {
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
inputField := cview.NewInputField()
inputField.SetLabel("Enter a company name: ")
inputField.SetFieldWidth(30)

View File

@ -8,6 +8,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
inputField := cview.NewInputField()

View File

@ -9,6 +9,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
list := cview.NewList()

View File

@ -7,6 +7,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
modal := cview.NewModal()

View File

@ -11,6 +11,8 @@ const panelCount = 5
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
panels := cview.NewPanels()

View File

@ -43,6 +43,8 @@ var app = cview.NewApplication()
// Starting point for the presentation.
func main() {
defer app.HandlePanic()
var debugPort int
flag.IntVar(&debugPort, "debug", 0, "port to serve debug info")
flag.Parse()

View File

@ -61,6 +61,7 @@ func (r *RadioButtons) InputHandler() func(event *tcell.EventKey, setFocus func(
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
radioButtons := NewRadioButtons([]string{"Lions", "Elephants", "Giraffes"})
radioButtons.SetBorder(true)

View File

@ -9,6 +9,7 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
grid := cview.NewGrid()
grid.SetColumns(-1, 6, 4, 30, -1)

View File

@ -11,6 +11,8 @@ const panelCount = 5
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
panels := cview.NewTabbedPanels()

View File

@ -12,6 +12,8 @@ const loremIpsumText = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
table := cview.NewTable()

View File

@ -21,6 +21,8 @@ Capitalize on low hanging fruit to identify a ballpark value added activity to b
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
textView := cview.NewTextView()

View File

@ -12,6 +12,8 @@ import (
// Show a navigable tree view of the current directory.
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
rootDir := "."

View File

@ -9,6 +9,8 @@ import (
func main() {
app := cview.NewApplication()
defer app.HandlePanic()
panels := cview.NewPanels()
form := cview.NewForm()

View File

@ -10,6 +10,8 @@ import (
func ExampleNewApplication() {
// Initialize application.
app := NewApplication()
// Handle panics gracefully.
defer app.HandlePanic()
// Create shared TextView.
sharedTextView := NewTextView()
@ -71,6 +73,8 @@ func ExampleNewApplication() {
func ExampleApplication_EnableMouse() {
// Initialize application.
app := NewApplication()
// Handle panics gracefully.
defer app.HandlePanic()
// Enable mouse support.
app.EnableMouse(true)

View File

@ -140,6 +140,9 @@ 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
@ -166,12 +169,16 @@ 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
@ -191,6 +198,21 @@ 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
@ -206,8 +228,6 @@ 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)
@ -230,6 +250,7 @@ func (d *DropDown) SetCurrentOption(index int) {
d.Lock()
}
}
d.Unlock()
}
// GetCurrentOption returns the index of the currently selected option as well
@ -415,7 +436,8 @@ func (d *DropDown) getFieldWidth() int {
}
fieldWidth += len(d.optionPrefix) + len(d.optionSuffix)
fieldWidth += len(d.currentOptionPrefix) + len(d.currentOptionSuffix)
fieldWidth += 3 // space + dropDownSymbol + space
// space <text> space + dropDownSymbol + space
fieldWidth += 4
return fieldWidth
}

10
go.mod
View File

@ -4,11 +4,9 @@ go 1.12
require (
code.rocketnine.space/tslocum/cbind v0.1.5
github.com/gdamore/tcell/v2 v2.3.11
github.com/gdamore/tcell/v2 v2.7.0
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-runewidth v0.0.13
github.com/rivo/uniseg v0.2.0
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.6 // indirect
github.com/mattn/go-runewidth v0.0.15
github.com/rivo/uniseg v0.4.6
golang.org/x/term v0.16.0 // indirect
)

52
go.sum
View File

@ -3,28 +3,58 @@ 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.3.11 h1:ECO6WqHGbKZ3HrSL7bG/zArMCmLaNr5vcjjMVnLHpzc=
github.com/gdamore/tcell/v2 v2.3.11/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/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.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
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/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-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/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/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-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
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.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
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=

49
list.go
View File

@ -194,6 +194,15 @@ 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
sync.RWMutex
}
@ -627,6 +636,24 @@ 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
@ -866,7 +893,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
fullWidth := width + l.paddingLeft + l.paddingRight + l.prefixWidth + l.suffixWidth
bottomLimit := y + height
l.height = height
@ -934,7 +961,25 @@ 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
} else if item.disabled {
}
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 {
// Shortcuts.
if showShortcuts && item.shortcut != 0 {
Print(screen, []byte(fmt.Sprintf("(%c)", item.shortcut)), x-5, y, 4, AlignRight, tcell.ColorDarkSlateGray.TrueColor())

View File

@ -27,6 +27,9 @@ type Modal struct {
// The text color.
textColor tcell.Color
// The text alignment.
textAlign int
// The optional callback for when the user clicked one of the buttons. It
// receives the index of the clicked button and the button's label.
done func(buttonIndex int, buttonLabel string)
@ -39,13 +42,11 @@ func NewModal() *Modal {
m := &Modal{
Box: NewBox(),
textColor: Styles.PrimaryTextColor,
textAlign: AlignCenter,
}
m.form = NewForm()
m.form.SetButtonsAlign(AlignCenter)
m.form.SetButtonBackgroundColor(Styles.PrimitiveBackgroundColor)
m.form.SetButtonTextColor(Styles.PrimaryTextColor)
m.form.SetBackgroundColor(Styles.ContrastBackgroundColor)
m.form.SetPadding(0, 0, 0, 0)
m.form.SetCancelFunc(func() {
if m.done != nil {
@ -56,7 +57,6 @@ func NewModal() *Modal {
m.frame = NewFrame(m.form)
m.frame.SetBorder(true)
m.frame.SetBorders(0, 0, 1, 0, 0, 0)
m.frame.SetBackgroundColor(Styles.ContrastBackgroundColor)
m.frame.SetPadding(1, 1, 1, 1)
m.focus = m
@ -96,6 +96,15 @@ func (m *Modal) SetButtonTextColor(color tcell.Color) {
m.form.SetButtonTextColor(color)
}
// SetButtonsAlign sets the horizontal alignment of the buttons. This must be
// either AlignLeft, AlignCenter (the default), or AlignRight.
func (m *Modal) SetButtonsAlign(align int) {
m.Lock()
defer m.Unlock()
m.form.SetButtonsAlign(align)
}
// SetDoneFunc sets a handler which is called when one of the buttons was
// pressed. It receives the index of the button as well as its label text. The
// handler is also called when the user presses the Escape key. The index will
@ -117,6 +126,15 @@ func (m *Modal) SetText(text string) {
m.text = text
}
// SetTextAlign sets the horizontal alignment of the text. This must be either
// AlignLeft, AlignCenter (the default), or AlignRight.
func (m *Modal) SetTextAlign(align int) {
m.Lock()
defer m.Unlock()
m.textAlign = align
}
// GetForm returns the Form embedded in the window. The returned Form may be
// modified to include additional elements (e.g. AddInputField, AddFormItem).
func (m *Modal) GetForm() *Form {
@ -215,7 +233,7 @@ func (m *Modal) Draw(screen tcell.Screen) {
m.frame.Clear()
lines := WordWrap(m.text, width)
for _, line := range lines {
m.frame.AddText(line, true, AlignCenter, m.textColor)
m.frame.AddText(line, true, m.textAlign, m.textColor)
}
// Set the Modal's position and size.

View File

@ -39,6 +39,7 @@ 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
@ -80,6 +81,7 @@ var Styles = Theme{
DropDownAbbreviationChars: "...",
DropDownSymbol: '◀',
DropDownOpenSymbol: '▼',
DropDownSelectedSymbol: '▶',
ScrollBarColor: tcell.ColorWhite.TrueColor(),

View File

@ -69,6 +69,12 @@ func NewTabbedPanels() *TabbedPanels {
return t
}
// SetChangedFunc sets a handler which is called whenever a tab is added,
// selected, reordered or removed.
func (t *TabbedPanels) SetChangedFunc(handler func()) {
t.panels.SetChangedFunc(handler)
}
// AddTab adds a new tab. Tab names should consist only of letters, numbers
// and spaces.
func (t *TabbedPanels) AddTab(name, label string, item Primitive) {

View File

@ -697,6 +697,9 @@ func (t *TextView) GetRegionText(regionID string) string {
if currentTag < len(colorTagIndices) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
if pos == colorTagIndices[currentTag][1]-1 {
currentTag++
if currentTag == len(colorTagIndices) {
continue
}
}
if colorTagIndices[currentTag][1]-colorTagIndices[currentTag][0] > 2 {
continue
@ -1168,14 +1171,22 @@ func (t *TextView) Draw(screen tcell.Screen) {
backgroundColor := index.BackgroundColor
attributes := index.Attributes
regionID := index.Region
if t.regions && len(regionID) > 0 && (len(t.regionInfos) == 0 || !bytes.Equal(t.regionInfos[len(t.regionInfos)-1].ID, regionID)) {
t.regionInfos = append(t.regionInfos, &textViewRegion{
ID: regionID,
FromX: x,
FromY: y + line - t.lineOffset,
ToX: -1,
ToY: -1,
})
if t.regions {
if len(t.regionInfos) > 0 && !bytes.Equal(t.regionInfos[len(t.regionInfos)-1].ID, regionID) {
// End last region.
t.regionInfos[len(t.regionInfos)-1].ToX = x
t.regionInfos[len(t.regionInfos)-1].ToY = y + line - t.lineOffset
}
if len(regionID) > 0 && (len(t.regionInfos) == 0 || !bytes.Equal(t.regionInfos[len(t.regionInfos)-1].ID, regionID)) {
// Start a new region.
t.regionInfos = append(t.regionInfos, &textViewRegion{
ID: regionID,
FromX: x,
FromY: y + line - t.lineOffset,
ToX: -1,
ToY: -1,
})
}
}
// Process tags.

10
util.go
View File

@ -86,6 +86,7 @@ const (
// Package initialization.
func init() {
runewidth.EastAsianWidth = true
runewidth.CreateLUT() // Create lookup table
// Initialize the predefined input field handlers.
InputFieldInteger = func(text string, ch rune) bool {
@ -528,7 +529,10 @@ func TaggedStringWidth(text string) int {
//
// Text is always split at newline characters ('\n').
//
// Text must not be escaped.
// BUG(tslocum) Text containing square brackets is not escaped properly.
// Use TextView.SetWrapWidth where possible.
//
// Issue: https://code.rocketnine.space/tslocum/cview/issues/27
func WordWrap(text string, width int) (lines []string) {
colorTagIndices, _, _, _, escapeIndices, strippedText, _ := decomposeText([]byte(text), true, false)
@ -550,7 +554,7 @@ func WordWrap(text string, width int) (lines []string) {
for index := escapePos; index >= 0; index-- {
if index < len(escapeIndices) && startIndex > escapeIndices[index][0] && startIndex < escapeIndices[index][1]-1 {
pos := escapeIndices[index][1] - 2 - startIndex
if pos < 0 || pos > len(substr) {
if pos < 0 || pos > len(substr) { // Workaround for issue #27
return substr
}
return substr[:pos] + substr[pos+1:]
@ -575,7 +579,7 @@ func WordWrap(text string, width int) (lines []string) {
}
// Is this a breakpoint?
if breakpointPos < len(breakpoints) && textPos+tagOffset == breakpoints[breakpointPos][0]+1 {
if breakpointPos < len(breakpoints) && textPos+tagOffset == breakpoints[breakpointPos][0] {
// Yes, it is. Set up breakpoint infos depending on its type.
lastBreakpoint = breakpoints[breakpointPos][0] + tagOffset
lastContinuation = breakpoints[breakpointPos][1] + tagOffset