bar.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // Paint a screen by drawing bar
  29. func (bar *ScreenBar) update() {
  30. // Config size if in terminal
  31. if term.IsTerminal(0) {
  32. bar.columns, _, _ = term.GetSize(0)
  33. }
  34. // Go home and draw (type) a bar
  35. fmt.Print("")
  36. fmt.Print("")
  37. s := fmt.Sprintf(" %%%ds %%6d ", 9-bar.columns)
  38. fmt.Printf(s, bar.title, bar.ticks)
  39. fmt.Print("")
  40. // An attempt to display Screenbar using kitty image protocol
  41. 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))
  42. if out, err := cmd.Output(); err != nil {
  43. os.Stdout.Write(out)
  44. log.Print(err)
  45. } else {
  46. os.Stdout.Write(out)
  47. }
  48. }
  49. func NewScreenBar(title string) *ScreenBar {
  50. bar := new(ScreenBar)
  51. bar.title = title
  52. bar.ticks = 0
  53. bar.tickerActive = true
  54. bar.ticker = time.NewTicker(time.Second)
  55. bar.update()
  56. // Goroutine to handle ticker updates
  57. go func() {
  58. for range bar.ticker.C {
  59. bar.ticks += 1
  60. bar.update()
  61. }
  62. }()
  63. return bar
  64. }
  65. func (bar *ScreenBar) setTitle(msg *osc.Message) {
  66. for _, arg := range msg.Arguments {
  67. switch arg.(type) {
  68. case string:
  69. bar.title = arg.(string)
  70. bar.update()
  71. }
  72. }
  73. }