barely

package module
v0.0.0-...-c4e789d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 6 Imported by: 34

README

barely report

Dead simple but yet extensible status bar for displaying interactive progress for the shell-based tools, written in Go-lang.

barely-example

Example

package main

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

	"github.com/reconquest/barely"
	"github.com/reconquest/loreley"
)

func main() {
	format, err := loreley.CompileWithReset(
		` {bg 2}{fg 15}{bold} {.Mode} `+
			`{bg 253}{fg 0} `+
			`{if .Updated}{fg 70}{end}{.Done}{fg 0}/{.Total} `,
		nil,
	)
	if err != nil {
		panic(err)
	}

	var (
		bar = barely.NewStatusBar(format.Template)
		wg  = sync.WaitGroup{}

		status = &struct {
			Mode  string
			Total int
			Done  int64

			Updated int64
		}{
			Mode:  "PROCESSING",
			Total: 100,
		}
	)

	bar.SetStatus(status)
	bar.Render(os.Stderr)

	for i := 1; i <= status.Total; i++ {
		wg.Add(1)

		go func(i int) {
			sleep := time.Duration(rand.Intn(i)) * time.Millisecond * 300
			time.Sleep(sleep)

			atomic.AddInt64(&status.Done, 1)
			atomic.AddInt64(&status.Updated, 1)

			if i%10 == 0 {
				bar.Clear(os.Stderr)
				fmt.Printf("go-routine %d done (%s sleep)\n", i, sleep)
			}

			bar.Render(os.Stderr)

			wg.Done()

			<-time.After(time.Millisecond * 500)
			atomic.AddInt64(&status.Updated, -1)
			bar.Render(os.Stderr)
		}(i)
	}

	wg.Wait()

	bar.Clear(os.Stderr)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StatusBar

type StatusBar struct {
	sync.Locker
	// contains filtered or unexported fields
}

StatusBar is the implementation of configurable status bar.

func NewStatusBar

func NewStatusBar(format *template.Template) *StatusBar

NewStatusBar returns new StatusBar object, initialized with given template.

Template will be used later on Render calls.

func (*StatusBar) Clear

func (bar *StatusBar) Clear(writer io.Writer)

Clear writes clear sequence in the specified writer, which is represented by terminal erase line sequence.

func (*StatusBar) Lock

func (bar *StatusBar) Lock()

Lock locks StatusBar object if locker object was set with SetLock method to prevent multi-threading race conditions.

StatusBar will be locked in the Set and Render methods.

func (*StatusBar) Render

func (bar *StatusBar) Render(writer io.Writer) error

Render renders specified template and writes it to the specified writer.

Also, render result will be remembered and will be used to generate clear sequence which can be obtained from Clear method call.

func (*StatusBar) SetLock

func (bar *StatusBar) SetLock(lock sync.Locker)

SetLock sets locker object, that will be used for Lock and Unlock methods.

func (*StatusBar) SetStatus

func (bar *StatusBar) SetStatus(data interface{})

SetStatus sets data which will be used in the template execution, which is previously set through NewStatusBar function.

func (*StatusBar) Unlock

func (bar *StatusBar) Unlock()

Unlock unlocks previously locked StatusBar object.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL