123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package bar
- import (
- "fmt"
- "log"
- "os"
- "os/exec"
- "time"
- // bar
- "golang.org/x/term"
- // osc
- "github.com/hypebeast/go-osc/osc"
- )
- // Your basic screenbar
- type ScreenBar struct {
- title string
- ticks int
- ticker *time.Ticker
- tickerActive bool
- columns int
- }
- // ScreenBar construction
- func NewScreenBar(title string) *ScreenBar {
- bar := new(ScreenBar)
- bar.title = title
- bar.ticks = 0
- bar.tickerActive = true
- bar.ticker = time.NewTicker(time.Second)
- bar.update()
- // Goroutine to handle ticker updates
- go func() {
- for range bar.ticker.C {
- bar.ticks += 1
- bar.update()
- }
- }()
- return bar
- }
- // (Re)paint a screen by drawing bar
- func (bar *ScreenBar) update() {
- // Config size if in terminal
- if term.IsTerminal(0) {
- bar.columns, _, _ = term.GetSize(0)
- }
- // Go home and draw (type) a bar
- fmt.Print("[H[2J[3J")
- fmt.Print("[0;37;40m")
- s := fmt.Sprintf(" %%%ds %%6d ", 10-bar.columns)
- fmt.Printf(s, bar.title, bar.ticks)
- fmt.Print("[0m")
- // An attempt to display Screenbar button using kitty image protocol
- cmd := exec.Command("sh", "-c", fmt.Sprintf("kitten icat -z -1 -n --place 2x1@%dx0 --stdin no images/bar_button.png < /dev/null > /dev/tty", bar.columns))
- if out, err := cmd.Output(); err != nil {
- os.Stdout.Write(out)
- log.Print(err)
- } else {
- os.Stdout.Write(out)
- }
- }
- // Set title visible on the bar
- func (bar *ScreenBar) setTitle(msg *osc.Message) {
- for _, arg := range msg.Arguments {
- switch arg.(type) {
- case string:
- bar.title = arg.(string)
- bar.update()
- }
- }
- }
- // Ticker's gonna tick
- func (bar *ScreenBar) tick() {
- bar.ticks += 1
- bar.update()
- }
- // Ticker's gonna need to be reset sometimes
- func (bar *ScreenBar) reset(msg *osc.Message) {
- bar.ticks = 0
- bar.ticker.Reset(time.Second)
- bar.update()
- }
|