bar.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. // bar
  6. "golang.org/x/term"
  7. // osc
  8. "github.com/hypebeast/go-osc/osc"
  9. )
  10. // Your basic bar object
  11. type ScreenBar struct {
  12. title string
  13. ticks int
  14. ticker *time.Ticker
  15. tickerActive bool
  16. columns int
  17. }
  18. // Ticker's gonna tick
  19. func (bar *ScreenBar) tick() {
  20. bar.ticks += 1
  21. bar.update()
  22. }
  23. func (bar *ScreenBar) reset(msg *osc.Message) {
  24. bar.ticks = 0
  25. bar.ticker.Reset(time.Second)
  26. bar.update()
  27. }
  28. func (bar *ScreenBar) update() {
  29. fmt.Print("")
  30. fmt.Print("")
  31. s := fmt.Sprintf(" %%%ds %%6d ", 9-bar.columns)
  32. fmt.Printf(s, bar.title, bar.ticks)
  33. fmt.Print("")
  34. // An attempt to display Screenbar using kitty image protocol
  35. 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))
  36. if out, err := cmd.Output(); err != nil {
  37. os.Stdout.Write(out)
  38. log.Print(err)
  39. } else {
  40. os.Stdout.Write(out)
  41. }
  42. }
  43. func NewScreenBar(title string) *ScreenBar {
  44. bar := new(ScreenBar)
  45. bar.title = title
  46. bar.ticks = 0
  47. bar.tickerActive = true
  48. bar.ticker = time.NewTicker(time.Second)
  49. bar.update()
  50. // Goroutine to handle ticker updates
  51. go func() {
  52. for range bar.ticker.C {
  53. bar.ticks += 1
  54. bar.update()
  55. }
  56. }()
  57. return bar
  58. }
  59. func (bar *ScreenBar) setTitle(msg *osc.Message) {
  60. for _, arg := range msg.Arguments {
  61. switch arg.(type) {
  62. case string:
  63. bar.title = arg.(string)
  64. bar.update()
  65. }
  66. }
  67. }