Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Marquee decorator for long message scrolling #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions _examples/marqueeDecorator/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/vbauerster/mpb/_examples/marqueeDecorator

go 1.17

require github.com/vbauerster/mpb/v8 v8.3.0

require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sys v0.6.0 // indirect
)
55 changes: 55 additions & 0 deletions _examples/marqueeDecorator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"math/rand"
"sync"
"time"

"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)

func main() {
var wg sync.WaitGroup
// passed wg will be accounted at p.Wait() call
p := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithWidth(64))
total, numBars := 100, 3
wg.Add(numBars)

fileToDownload := []string{
"One Hundred Years of Solitude by Gabriel Garcia Marquez",
"The Brothers Karamazov by Fyodor Dostoyevsky",
"Alice's Adventures in Wonderland by Lewis Carroll",
}

for i := 0; i < numBars; i++ {
name := fmt.Sprintf("Bar#%d:", i)
bar := p.AddBar(int64(total),
mpb.PrependDecorators(
// simple name decorator
decor.Name(name),
decor.OnComplete(
// Marquee decorator with default style
Marquee(fileToDownload[i], 20, decor.WCSyncSpace), "done",
),
),
mpb.AppendDecorators(
// decor.DSyncWidth bit enables column width synchronization
decor.Percentage(decor.WCSyncWidth),
),
)
// simulating some work
go func() {
defer wg.Done()
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
max := 100 * time.Millisecond
for i := 0; i < total; i++ {
time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
bar.Increment()
}
}()
}
// wait for passed wg and for all bars to complete and flush
p.Wait()
}
25 changes: 25 additions & 0 deletions decor/marquee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package decor

// Marquee returns marquee decorator that will scroll text right to left.
//
// `text` is the scrolling message
//
// `ws` controls the showing window size
func Marquee(text string, ws uint, wcc ...WC) Decorator {
bytes := []byte(text)
buf := make([]byte, ws)
var count uint
f := func(s Statistics) string {
start := count % uint(len(bytes))
var i uint = 0
for ; i < ws && start+i < uint(len(bytes)); i++ {
buf[i] = bytes[start+i]
}
for ; i < ws; i++ {
buf[i] = ' '
}
count++
return string(buf)
}
return Any(f, wcc...)
}