1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-27 13:48:49 +08:00

Merge pull request #259 from mum4k/termdashdemo-bugfix

Fix a bug where segment display text appears to be jumping.
This commit is contained in:
Jakub Sobon 2020-11-14 02:49:55 -05:00 committed by GitHub
commit 1b9b13d5dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View File

@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- coveralls again triggers and reports on PRs.
- improving test coverage in some modules.
- changing the Blue color in demos to a more readable shade.
- fixed a bug where segment display text in `termdashdemo` appeared to be
jumping.
## [0.12.2] - 31-Aug-2020

View File

@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
"image"
"log"
"math"
"math/rand"
@ -69,9 +70,9 @@ type widgets struct {
}
// newWidgets creates all widgets used by this demo.
func newWidgets(ctx context.Context, c *container.Container) (*widgets, error) {
func newWidgets(ctx context.Context, t terminalapi.Terminal, c *container.Container) (*widgets, error) {
updateText := make(chan string)
sd, err := newSegmentDisplay(ctx, updateText)
sd, err := newSegmentDisplay(ctx, t, updateText)
if err != nil {
return nil, err
}
@ -504,7 +505,7 @@ func main() {
}
ctx, cancel := context.WithCancel(context.Background())
w, err := newWidgets(ctx, c)
w, err := newWidgets(ctx, t, c)
if err != nil {
panic(err)
}
@ -586,7 +587,7 @@ func newTextInput(updateText chan<- string) (*textinput.TextInput, error) {
// newSegmentDisplay creates a new SegmentDisplay that initially shows the
// Termdash name. Shows any text that is sent over the channel.
func newSegmentDisplay(ctx context.Context, updateText <-chan string) (*segmentdisplay.SegmentDisplay, error) {
func newSegmentDisplay(ctx context.Context, t terminalapi.Terminal, updateText <-chan string) (*segmentdisplay.SegmentDisplay, error) {
sd, err := segmentdisplay.New()
if err != nil {
return nil, err
@ -609,12 +610,33 @@ func newSegmentDisplay(ctx context.Context, updateText <-chan string) (*segmentd
go func() {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
capacity := 0
termSize := t.Size()
for {
select {
case <-ticker.C:
state := textState(text, sd.Capacity(), step)
if capacity == 0 {
// The segment display only knows its capacity after both
// text size and terminal size are known.
capacity = sd.Capacity()
}
if t.Size().Eq(image.ZP) || !t.Size().Eq(termSize) {
// Update the capacity initially the first time the
// terminal reports a non-zero size and then every time the
// terminal resizes.
//
// This is better than updating the capacity on every
// iteration since that leads to edge cases - segment
// display capacity depends on the length of text and here
// we are trying to adjust the text length to the capacity.
termSize = t.Size()
capacity = sd.Capacity()
}
state := textState(text, capacity, step)
var chunks []*segmentdisplay.TextChunk
for i := 0; i < sd.Capacity(); i++ {
for i := 0; i < capacity; i++ {
if i >= len(state) {
break
}