bar.go 1.7 KB

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