Initial commit

This commit is contained in:
nxshock 2021-06-30 18:35:00 +05:00
commit f3bfb03738
4 changed files with 153 additions and 0 deletions

2
.gitattributes vendored Normal file
View file

@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

12
.gitignore vendored Normal file
View file

@ -0,0 +1,12 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 nxshock
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

118
progressmessage.go Normal file
View file

@ -0,0 +1,118 @@
package progressmessage
import (
"fmt"
"io"
"os"
"sync"
"time"
)
var (
defaultWriter = os.Stderr
defaultUpdatePeriod = time.Second
defaultMessageDelimiter = "\r"
)
type ProgressMessage struct {
// message format
format string
// list of message params
params []interface{}
// ticker for updating message
ticker *time.Ticker
// chan for stopping updates
stopChan chan struct{}
// previous message to prevent output of same message more than one time
prevMessage string
writer io.Writer
isStarted bool
// message update period
updatePeriod time.Duration
mu sync.RWMutex
}
func New(format string) *ProgressMessage {
pm := &ProgressMessage{
format: format,
stopChan: make(chan struct{}),
writer: defaultWriter,
updatePeriod: defaultUpdatePeriod}
return pm
}
func (pm *ProgressMessage) ChangeWriter(newWriter io.Writer) {
pm.mu.Lock()
defer pm.mu.Unlock()
pm.writer = newWriter
}
func (pm *ProgressMessage) ChangeFormat(newFormat string) {
pm.mu.Lock()
defer pm.mu.Unlock()
pm.format = newFormat
}
func (pm *ProgressMessage) Update(params ...interface{}) {
// TODO: use mutex?
pm.params = params
}
func (pm *ProgressMessage) ChangeUpdatePeriod(newUpdatePeriod time.Duration) {
pm.mu.Lock()
defer pm.mu.Unlock()
if pm.isStarted {
defer func() {
pm.Stop()
pm.Start()
}()
}
pm.updatePeriod = newUpdatePeriod
}
func (pm *ProgressMessage) Start() {
pm.isStarted = true
go func() {
pm.ticker = time.NewTicker(pm.updatePeriod)
for {
select {
case <-pm.ticker.C:
if pm.params == nil {
continue
}
m := fmt.Sprintf(pm.format, pm.params...)
if m == pm.prevMessage {
continue
}
pm.writer.Write([]byte(defaultMessageDelimiter + m))
pm.prevMessage = m
case <-pm.stopChan:
pm.ticker.Stop()
break
}
}
}()
}
func (pm *ProgressMessage) Stop() {
pm.stopChan <- struct{}{}
pm.isStarted = false
}