diff --git a/demos/presentation/progressbar.go b/demos/presentation/progressbar.go index 7771411..a3b4bb5 100644 --- a/demos/presentation/progressbar.go +++ b/demos/presentation/progressbar.go @@ -13,7 +13,7 @@ func ProgressBar(nextSlide func()) (title string, content cview.Primitive) { verticalProgressBar := cview.NewProgressBar() verticalProgressBar.SetBorder(true) - verticalProgressBar.Vertical = true + verticalProgressBar.SetVertical(true) horizontalProgressBar := cview.NewProgressBar() horizontalProgressBar.SetBorder(true) diff --git a/demos/progressbar/main.go b/demos/progressbar/main.go index 2e15f93..ef8cc9b 100644 --- a/demos/progressbar/main.go +++ b/demos/progressbar/main.go @@ -15,7 +15,7 @@ func main() { verticalProgressBar := cview.NewProgressBar() verticalProgressBar.SetBorder(true) - verticalProgressBar.Vertical = true + verticalProgressBar.SetVertical(true) horizontalProgressBar := cview.NewProgressBar() horizontalProgressBar.SetBorder(true) diff --git a/go.mod b/go.mod index 4d06fd8..ef99982 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.12 require ( github.com/gdamore/tcell v1.3.0 github.com/lucasb-eyer/go-colorful v1.0.3 - github.com/mattn/go-runewidth v0.0.8 + github.com/mattn/go-runewidth v0.0.9 github.com/rivo/uniseg v0.1.0 - golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect + golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775 // indirect golang.org/x/text v0.3.2 // indirect ) diff --git a/go.sum b/go.sum index d135f9b..8de7444 100644 --- a/go.sum +++ b/go.sum @@ -10,14 +10,14 @@ github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tW github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0= -github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10= golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 h1:sfkvUWPNGwSV+8/fNqctR5lS2AqCSqYwXdrjCxp/dXo= -golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775 h1:TC0v2RSO1u2kn1ZugjrFXkRZAEaqMN/RW+OTZkBzmLE= +golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= diff --git a/progressbar.go b/progressbar.go index c92e7a8..f55cd51 100644 --- a/progressbar.go +++ b/progressbar.go @@ -12,24 +12,27 @@ type ProgressBar struct { *Box // Rune to use when rendering the empty area of the progress bar. - EmptyRune rune + emptyRune rune // Color of the empty area of the progress bar. - EmptyColor tcell.Color + emptyColor tcell.Color // Rune to use when rendering the filled area of the progress bar. - FilledRune rune + filledRune rune // Color of the filled area of the progress bar. - FilledColor tcell.Color + filledColor tcell.Color // If set to true, instead of filling from left to right, the bar is filled // from bottom to top. - Vertical bool + vertical bool - max int + // Current progress. progress int + // Progress required to fill the bar. + max int + sync.Mutex } @@ -37,14 +40,54 @@ type ProgressBar struct { func NewProgressBar() *ProgressBar { return &ProgressBar{ Box: NewBox().SetBackgroundColor(Styles.PrimitiveBackgroundColor), - EmptyRune: ' ', - EmptyColor: Styles.PrimitiveBackgroundColor, - FilledRune: tcell.RuneBlock, - FilledColor: Styles.PrimaryTextColor, + emptyRune: ' ', + emptyColor: Styles.PrimitiveBackgroundColor, + filledRune: tcell.RuneBlock, + filledColor: Styles.PrimaryTextColor, max: 100, } } +// SetEmptyRune sets the rune used for the empty area of the progress bar. +func (p *ProgressBar) SetEmptyRune(empty rune) { + p.Lock() + defer p.Unlock() + + p.emptyRune = empty +} + +// SetEmptyColor sets the color of the empty area of the progress bar. +func (p *ProgressBar) SetEmptyColor(empty tcell.Color) { + p.Lock() + defer p.Unlock() + + p.emptyColor = empty +} + +// SetFilledRune sets the rune used for the filled area of the progress bar. +func (p *ProgressBar) SetFilledRune(filled rune) { + p.Lock() + defer p.Unlock() + + p.filledRune = filled +} + +// SetFilledColor sets the color of the filled area of the progress bar. +func (p *ProgressBar) SetFilledColor(filled tcell.Color) { + p.Lock() + defer p.Unlock() + + p.filledColor = filled +} + +// SetVertical sets the direction of the progress bar. +func (p *ProgressBar) SetVertical(vertical bool) { + p.Lock() + defer p.Unlock() + + p.vertical = vertical +} + // SetMax sets the progress required to fill the bar. func (p *ProgressBar) SetMax(max int) { p.Lock() @@ -104,7 +147,7 @@ func (p *ProgressBar) Draw(screen tcell.Screen) { barSize := height maxLength := width - if p.Vertical { + if p.vertical { barSize = width maxLength = height } @@ -116,17 +159,17 @@ func (p *ProgressBar) Draw(screen tcell.Screen) { for i := 0; i < barSize; i++ { for j := 0; j < barLength; j++ { - if p.Vertical { - screen.SetContent(x+i, y+(height-1-j), p.FilledRune, nil, tcell.StyleDefault.Foreground(p.FilledColor).Background(p.backgroundColor)) + if p.vertical { + screen.SetContent(x+i, y+(height-1-j), p.filledRune, nil, tcell.StyleDefault.Foreground(p.filledColor).Background(p.backgroundColor)) } else { - screen.SetContent(x+j, y+i, p.FilledRune, nil, tcell.StyleDefault.Foreground(p.FilledColor).Background(p.backgroundColor)) + screen.SetContent(x+j, y+i, p.filledRune, nil, tcell.StyleDefault.Foreground(p.filledColor).Background(p.backgroundColor)) } } for j := barLength; j < maxLength; j++ { - if p.Vertical { - screen.SetContent(x+i, y+(height-1-j), p.EmptyRune, nil, tcell.StyleDefault.Foreground(p.EmptyColor).Background(p.backgroundColor)) + if p.vertical { + screen.SetContent(x+i, y+(height-1-j), p.emptyRune, nil, tcell.StyleDefault.Foreground(p.emptyColor).Background(p.backgroundColor)) } else { - screen.SetContent(x+j, y+i, p.EmptyRune, nil, tcell.StyleDefault.Foreground(p.EmptyColor).Background(p.backgroundColor)) + screen.SetContent(x+j, y+i, p.emptyRune, nil, tcell.StyleDefault.Foreground(p.emptyColor).Background(p.backgroundColor)) } } } diff --git a/textview.go b/textview.go index d2236b5..ae8e1cb 100644 --- a/textview.go +++ b/textview.go @@ -2,7 +2,6 @@ package cview import ( "bytes" - "fmt" "regexp" "strings" "sync" @@ -90,7 +89,6 @@ type textViewIndex struct { // // See https://gitlab.com/tslocum/cview/wiki/TextView for an example. type TextView struct { - sync.RWMutex *Box // The text buffer. @@ -170,6 +168,8 @@ type TextView struct { // An optional function which is called when the user presses one of the // following keys: Escape, Enter, Tab, Backtab. done func(tcell.Key) + + sync.RWMutex } // NewTextView returns a new text view. @@ -258,8 +258,11 @@ func (t *TextView) SetTextColor(color tcell.Color) *TextView { // SetText sets the text of this text view to the provided string. Previously // contained text will be removed. func (t *TextView) SetText(text string) *TextView { - t.Clear() - fmt.Fprint(t, text) + t.Lock() + defer t.Unlock() + + t.clear() + t.write([]byte(text)) return t } @@ -413,6 +416,10 @@ func (t *TextView) Clear() *TextView { t.Lock() defer t.Unlock() + return t.clear() +} + +func (t *TextView) clear() *TextView { t.buffer = nil t.recentBytes = nil t.index = nil @@ -580,6 +587,10 @@ func (t *TextView) Write(p []byte) (n int, err error) { } defer t.Unlock() + return t.write(p) +} + +func (t *TextView) write(p []byte) (n int, err error) { // Copy data over. newBytes := append(t.recentBytes, p...) t.recentBytes = nil