bar.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. func (bar *ScreenBar) reset(msg *osc.Message) {
  19. bar.ticks = 0
  20. bar.ticker.Reset(time.Second)
  21. bar.update()
  22. }
  23. func (bar *ScreenBar) tick() {
  24. bar.ticks += 1
  25. bar.update()
  26. }
  27. func (bar *ScreenBar) update() {
  28. fmt.Print("")
  29. fmt.Print("")
  30. s := fmt.Sprintf(" %%%ds %%6d ", 9-bar.columns)
  31. fmt.Printf(s, bar.title, bar.ticks)
  32. fmt.Print("")
  33. // An attempt to display Screenbar using kitty image protocol
  34. 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))
  35. if out, err := cmd.Output(); err != nil {
  36. os.Stdout.Write(out)
  37. log.Print(err)
  38. } else {
  39. os.Stdout.Write(out)
  40. }
  41. }
  42. func NewScreenBar(title string) *ScreenBar {
  43. bar := new(ScreenBar)
  44. bar.title = title
  45. bar.ticks = 0
  46. bar.tickerActive = true
  47. bar.ticker = time.NewTicker(time.Second)
  48. bar.update()
  49. // Goroutine to handle ticker updates
  50. go func() {
  51. for range bar.ticker.C {
  52. bar.ticks += 1
  53. bar.update()
  54. }
  55. }()
  56. return bar
  57. }
  58. func (bar *ScreenBar) setTitle(msg *osc.Message) {
  59. for _, arg := range msg.Arguments {
  60. switch arg.(type) {
  61. case string:
  62. bar.title = arg.(string)
  63. bar.update()
  64. }
  65. }
  66. }