Skip to content

Commit 9fec0f7

Browse files
committed
Initial commit of wait
Add wait repository which is based on github.com/homeport/gonvenience and now is its own standalone Go package.
0 parents  commit 9fec0f7

File tree

8 files changed

+457
-0
lines changed

8 files changed

+457
-0
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
language: go
3+
go:
4+
- 1.12.x
5+
6+
install:
7+
- curl --silent --location https://goo.gl/g1CpPX | bash -s v1.0.7
8+
9+
script:
10+
- export GO111MODULE=on
11+
- go mod download
12+
- go mod verify
13+
- ginkgo -r -nodes 4 -randomizeAllSpecs -randomizeSuites -race -trace
14+
- staticcheck ./...
15+
- golint ./...

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 The Homeport Team
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# wait
2+
3+
[![License](https://img.shields.io/github/license/gonvenience/wait.svg)](https://github.com/gonvenience/wait/blob/master/LICENSE)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/gonvenience/wait)](https://goreportcard.com/report/github.com/gonvenience/wait)
5+
[![Build Status](https://travis-ci.org/gonvenience/wait.svg?branch=master)](https://travis-ci.org/gonvenience/wait)
6+
[![GoDoc](https://godoc.org/github.com/gonvenience/wait/pkg?status.svg)](https://godoc.org/github.com/gonvenience/wait/pkg)
7+
[![Release](https://img.shields.io/github/release/gonvenience/wait.svg)](https://github.com/gonvenience/wait/releases/latest)
8+
9+
Golang package provides convenience functions to create a progress indicator for CLI applications (also know as a spinners)

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/gonvenience/wait
2+
3+
go 1.12
4+
5+
require (
6+
github.com/gonvenience/bunt v1.0.0
7+
github.com/gonvenience/term v1.0.0
8+
github.com/gonvenience/text v1.0.0
9+
github.com/lucasb-eyer/go-colorful v1.0.2
10+
github.com/onsi/ginkgo v1.8.0
11+
github.com/onsi/gomega v1.5.0
12+
)

go.sum

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
2+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
4+
github.com/gonvenience/bunt v1.0.0 h1:+SozbZgVunILmyX/EXbUNz/LTulUZTo8vXlvHiWZNFM=
5+
github.com/gonvenience/bunt v1.0.0/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
6+
github.com/gonvenience/term v1.0.0 h1:joCB/j0Ngmdakd3muuLgAGPMf7DNKdoe708c1I6RiBs=
7+
github.com/gonvenience/term v1.0.0/go.mod h1:wohD4Iqso9Eol7qc2VnNhSFFhZxok5PvO7pZhdrAn4E=
8+
github.com/gonvenience/text v1.0.0 h1:ayqzSeBE2nlKLtipVrJH63hJcVVpDLBH76muEkq2SSU=
9+
github.com/gonvenience/text v1.0.0/go.mod h1:jj9PRxju4cKHIzMal+QtCwPtOcmLeX5nHfe8KuSm6Kg=
10+
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
11+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
12+
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
13+
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
14+
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 h1:BXxTozrOU8zgC5dkpn3J6NTRdoP+hjok/e+ACr4Hibk=
15+
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3/go.mod h1:x1uk6vxTiVuNt6S5R2UYgdhpj3oKojXvOXauHZ7dEnI=
16+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
17+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
18+
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
19+
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
20+
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
21+
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
22+
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
23+
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
24+
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
25+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
26+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
27+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
28+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
29+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
30+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
31+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
32+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
33+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
34+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
35+
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
36+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
37+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
38+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
39+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
40+
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
41+
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
42+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
43+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
44+
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
45+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

wait.go

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
// Copyright © 2019 The Homeport Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
/*
22+
Package wait contains convenience functions to create a progress indicator for
23+
CLI applications, also know as a spinner. It is basically a text and a rapidly
24+
changing symbol that provides feedback to the user that something is still
25+
running even though there is no information how long it will continue to run.
26+
27+
Example:
28+
package main
29+
30+
import (
31+
"time"
32+
33+
"github.com/gonvenience/wait"
34+
)
35+
36+
func main() {
37+
pi := wait.NewProgressIndicator("operation in progress")
38+
39+
pi.SetTimeout(10 * time.Second)
40+
pi.Start()
41+
42+
time.Sleep(5 * time.Second)
43+
44+
pi.SetText("operation *still* in progress")
45+
46+
time.Sleep(5 * time.Second)
47+
48+
pi.Done("Ok, done")
49+
}
50+
*/
51+
package wait
52+
53+
import (
54+
"fmt"
55+
"io"
56+
"math"
57+
"os"
58+
"strings"
59+
"sync/atomic"
60+
"time"
61+
62+
"github.com/gonvenience/bunt"
63+
"github.com/gonvenience/term"
64+
"github.com/gonvenience/text"
65+
"github.com/lucasb-eyer/go-colorful"
66+
)
67+
68+
const resetLine = "\r\x1b[K"
69+
const refreshIntervalInMs = 250
70+
71+
var symbols = []rune(`⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏`)
72+
73+
var defaultElapsedTimeColor = bunt.DimGray
74+
75+
// ProgressIndicator is a handle to a progress indicator (spinner).
76+
type ProgressIndicator struct {
77+
out io.Writer
78+
format string
79+
args []interface{}
80+
81+
spin bool
82+
83+
start time.Time
84+
running uint64
85+
counter uint64
86+
87+
timeout time.Duration
88+
89+
timeInfoText func(time.Duration) (string, colorful.Color)
90+
}
91+
92+
// NewProgressIndicator creates a new progress indicator handle. The provided
93+
// text is shown as long as the progress indicator runs, or if new text is
94+
// supplied during runtime.
95+
func NewProgressIndicator(format string, args ...interface{}) *ProgressIndicator {
96+
return &ProgressIndicator{
97+
out: os.Stderr,
98+
format: format,
99+
args: args,
100+
spin: !term.IsDumbTerminal(),
101+
timeout: 0 * time.Second,
102+
timeInfoText: TimeInfoText,
103+
}
104+
}
105+
106+
// Start starts the progress indicator. If it is already started, the this
107+
// function returns immediately.
108+
func (pi *ProgressIndicator) Start() *ProgressIndicator {
109+
// No-op, in case it is already running
110+
if atomic.LoadUint64(&pi.running) > 0 {
111+
return pi
112+
}
113+
114+
pi.start = time.Now()
115+
atomic.StoreUint64(&pi.running, 1)
116+
117+
if pi.spin {
118+
term.HideCursor()
119+
120+
go func() {
121+
for atomic.LoadUint64(&pi.running) > 0 {
122+
elapsedTime := time.Since(pi.start)
123+
124+
// Timeout reached, stopping the progress indicator
125+
if pi.timeout > time.Nanosecond && elapsedTime > pi.timeout {
126+
pi.Done("timeout occurred")
127+
break
128+
}
129+
130+
mainContentText := removeLineFeeds(bunt.Sprintf(pi.format, pi.args...))
131+
elapsedTimeText, elapsedTimeColor := pi.timeInfoText(elapsedTime)
132+
133+
availableSpace := term.GetTerminalWidth() - bunt.PlainTextLength(elapsedTimeText) - 4
134+
135+
// In case a timeout is set, smoothly blend the time info text color from
136+
// the provided color into red depending on how much time is left
137+
if pi.timeout > time.Nanosecond {
138+
// Use smooth curved gradient: http://fooplot.com/?lang=en#W3sidHlwZSI6MCwiZXEiOiIoMS1jb3MoeF4yKjMuMTQxNSkpLzIiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyIwIiwiMSIsIjAiLCIxIl19XQ--
139+
blendFactor := 0.5 * (1.0 - math.Cos(math.Pow(elapsedTime.Seconds()/pi.timeout.Seconds(), 2)*math.Pi))
140+
elapsedTimeColor = elapsedTimeColor.BlendLab(bunt.Red, blendFactor)
141+
}
142+
143+
bunt.Fprint(pi.out,
144+
resetLine, " ", pi.nextSymbol(), " ",
145+
text.FixedLength(mainContentText, availableSpace), " ",
146+
bunt.Style(elapsedTimeText, bunt.Foreground(elapsedTimeColor)),
147+
)
148+
149+
time.Sleep(refreshIntervalInMs * time.Millisecond)
150+
}
151+
}()
152+
153+
} else {
154+
bunt.Fprintf(pi.out, pi.format, pi.args...)
155+
bunt.Fprintln(pi.out)
156+
}
157+
158+
return pi
159+
}
160+
161+
// Stop stops the progress indicator by clearing the line one last time
162+
func (pi *ProgressIndicator) Stop() bool {
163+
if x := atomic.SwapUint64(&pi.running, 0); x > 0 {
164+
if pi.spin {
165+
term.ShowCursor()
166+
bunt.Fprint(pi.out, resetLine)
167+
}
168+
169+
return true
170+
}
171+
172+
return false
173+
}
174+
175+
// SetText updates the waiting text.
176+
func (pi *ProgressIndicator) SetText(format string, args ...interface{}) {
177+
if bunt.Sprintf(format, args...) != bunt.Sprintf(pi.format, pi.args...) {
178+
pi.format = format
179+
pi.args = args
180+
181+
if !pi.spin {
182+
bunt.Fprintf(pi.out, pi.format, pi.args...)
183+
bunt.Fprintln(pi.out)
184+
}
185+
}
186+
}
187+
188+
// SetOutputWriter sets the output writer to used to print the progress
189+
// indicator texts to, e.g. `os.Stderr` or `os.Stdout`.
190+
func (pi *ProgressIndicator) SetOutputWriter(out io.Writer) {
191+
pi.out = out
192+
}
193+
194+
// SetTimeout specifies that the progress indicator will timeout after the
195+
// provided duration. A timeout duration lower than one nanosecond means that
196+
// there is no timeout.
197+
func (pi *ProgressIndicator) SetTimeout(timeout time.Duration) {
198+
pi.timeout = timeout
199+
}
200+
201+
// SetTimeInfoTextFunc sets a custom time info text function that is called to
202+
// create the string and the color to be used on the far right side of the
203+
// progress indicator.
204+
func (pi *ProgressIndicator) SetTimeInfoTextFunc(f func(time.Duration) (string, colorful.Color)) {
205+
pi.timeInfoText = f
206+
}
207+
208+
// Done stops the progress indicator.
209+
func (pi *ProgressIndicator) Done(format string, args ...interface{}) bool {
210+
defer func() {
211+
bunt.Fprintf(pi.out, format, args...)
212+
bunt.Fprintln(pi.out)
213+
}()
214+
215+
return pi.Stop()
216+
}
217+
218+
func (pi *ProgressIndicator) nextSymbol() string {
219+
pi.counter++
220+
return string(symbols[pi.counter%uint64(len(symbols))])
221+
}
222+
223+
// TimeInfoText is the default implementation for the time information text on
224+
// the far right side of the progress indicator line.
225+
func TimeInfoText(elapsedTime time.Duration) (string, colorful.Color) {
226+
return humanReadableDuration(elapsedTime), defaultElapsedTimeColor
227+
}
228+
229+
func humanReadableDuration(duration time.Duration) string {
230+
if duration < time.Second {
231+
return "less than a second"
232+
}
233+
234+
seconds := int(duration.Seconds())
235+
minutes := 0
236+
hours := 0
237+
238+
if seconds >= 60 {
239+
minutes = seconds / 60
240+
seconds = seconds % 60
241+
242+
if minutes >= 60 {
243+
hours = minutes / 60
244+
minutes = minutes % 60
245+
}
246+
}
247+
248+
parts := []string{}
249+
if hours > 0 {
250+
parts = append(parts, fmt.Sprintf("%d h", hours))
251+
}
252+
253+
if minutes > 0 {
254+
parts = append(parts, fmt.Sprintf("%d min", minutes))
255+
}
256+
257+
if seconds > 0 {
258+
parts = append(parts, fmt.Sprintf("%d sec", seconds))
259+
}
260+
261+
return strings.Join(parts, " ")
262+
}
263+
264+
func removeLineFeeds(input string) string {
265+
return strings.Replace(input, "\n", " ", -1)
266+
}

0 commit comments

Comments
 (0)