main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "log"
  4. "strings"
  5. "github.com/gdamore/tcell/v3"
  6. "github.com/gdamore/tcell/v3/color"
  7. )
  8. func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
  9. row := y1
  10. col := x1
  11. var width int
  12. for text != "" {
  13. text, width = s.Put(col, row, text, style)
  14. col += width
  15. if col >= x2 {
  16. row++
  17. col = x1
  18. }
  19. if row > y2 {
  20. break
  21. }
  22. if width == 0 {
  23. // incomplete grapheme at end of string
  24. break
  25. }
  26. }
  27. }
  28. func main() {
  29. var title string = `
  30. ⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀
  31. ⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀
  32. ⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀
  33. ⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⣿⣿⡀⠀⠀⠀⠀⢀⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀
  34. ⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
  35. ⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣠⣤⣤⣼⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀
  36. ⠀⠀⠀⢀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀
  37. ⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀
  38. ⠀⠀⠀⠘⣿⣿⣿⣿⠟⠁⠀⠀⠀⠹⣿⣿⣿⣿⣿⠟⠁⠀⠀⠹⣿⣿⡿⠀⠀⠀⠀⠀
  39. ⠀⠀⠀⠀⣿⣿⣿⡇⠀⠀⠀⢼⣿⠀⢿⣿⣿⣿⣿⠀⣾⣷⠀⠀⢿⣿⣷⠀⠀⠀⠀⠀
  40. ⠀⠀⠀⢠⣿⣿⣿⣷⡀⠀⠀⠈⠋⢀⣿⣿⣿⣿⣿⡀⠙⠋⠀⢀⣾⣿⣿⠀⠀⠀⠀⠀
  41. ⢀⣀⣀⣀⣿⣿⣿⣿⣿⣶⣶⣶⣶⣿⣿⣿⣿⣾⣿⣷⣦⣤⣴⣿⣿⣿⣿⣤⠤⢤⣤⡄
  42. ⠈⠉⠉⢉⣙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⣀⣀⣀⡀⠀
  43. ⠐⠚⠋⠉⢀⣬⡿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣥⣀⡀⠈⠀⠈⠛
  44. ⠀⠀⠴⠚⠉⠀⠀⠀⠉⠛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⠋⠁⠀⠀⠀⠉⠛⠢⠀⠀
  45. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ /$$ /$$ /$$ /$$ /$$
  46. ⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀| $$ | $$ |__/| $$| $$
  47. ⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀| $$ | $$ /$$$$$$ /$$$$$$$ /$$| $$| $$ /$$$$$$
  48. ⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀| $$ / $$/|____ $$| $$__ $$| $$| $$| $$ |____ $$
  49. ⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀ \ $$ $$/ /$$$$$$$| $$ \ $$| $$| $$| $$ /$$$$$$$
  50. ⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀ \ $$$/ /$$__ $$| $$ | $$| $$| $$| $$ /$$__ $$
  51. ⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀ \ $/ | $$$$$$$| $$ | $$| $$| $$| $$| $$$$$$$
  52. ⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀ \_/ \_______/|__/ |__/|__/|__/|__/ \_______/
  53. /$$$$$$ /$$ /$$
  54. /$$__ $$ | $$ | $$
  55. | $$ \ $$ /$$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ /$$$$$$ /$$$$$$$
  56. | $$$$$$$$ /$$__ $$| $$ /$$//$$__ $$| $$__ $$|_ $$_/ | $$ | $$ /$$__ $$ /$$__ $$ /$$_____/
  57. | $$__ $$| $$ | $$ \ $$/$$/| $$$$$$$$| $$ \ $$ | $$ | $$ | $$| $$ \__/| $$$$$$$$| $$$$$$
  58. | $$ | $$| $$ | $$ \ $$$/ | $$_____/| $$ | $$ | $$ /$$| $$ | $$| $$ | $$_____/ \____ $$
  59. | $$ | $$| $$$$$$$ \ $/ | $$$$$$$| $$ | $$ | $$$$/| $$$$$$/| $$ | $$$$$$$ /$$$$$$$/
  60. |__/ |__/ \_______/ \_/ \_______/|__/ |__/ \___/ \______/ |__/ \_______/|_______/
  61. `
  62. lines := strings.Split(title, "\n")
  63. defStyle := tcell.StyleDefault.Background(color.Reset).Foreground(color.NewHexColor(0xf3e5ab))
  64. // Initialize screen
  65. s, err := tcell.NewScreen()
  66. if err != nil {
  67. log.Fatalf("%+v", err)
  68. }
  69. if err := s.Init(); err != nil {
  70. log.Fatalf("%+v", err)
  71. }
  72. s.SetStyle(defStyle)
  73. s.Clear()
  74. quit := func() {
  75. // You have to catch panics in a defer, clean up, and
  76. // re-raise them - otherwise your application can
  77. // die without leaving any diagnostic trace.
  78. maybePanic := recover()
  79. s.Fini()
  80. if maybePanic != nil {
  81. panic(maybePanic)
  82. }
  83. }
  84. defer quit()
  85. // Here's how to get the screen size when you need it.
  86. // xmax, ymax := s.Size()
  87. // Here's an example of how to inject a keystroke where it will
  88. // be picked up by a future read of the event queue. Note that
  89. // care should be used to avoid blocking writes to the queue if
  90. // this is done from the same thread that is responsible for reading
  91. // the queue, or else a single-party deadlock might occur.
  92. // s.EventQ() <- tcell.NewEventKey(tcell.KeyRune, rune('a'), 0)
  93. // Event loop
  94. for {
  95. // Update screen
  96. s.Show()
  97. // Poll event (this can be in a select statement as well)
  98. ev := <-s.EventQ()
  99. xmax, ymax := s.Size()
  100. for l:= range lines{
  101. drawText(s,10, l, xmax, ymax, defStyle, lines[l])
  102. }
  103. // Process event
  104. switch ev := ev.(type) {
  105. case *tcell.EventResize:
  106. s.Sync()
  107. case *tcell.EventKey:
  108. if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
  109. return
  110. } else if ev.Key() == tcell.KeyCtrlL {
  111. s.Sync()
  112. }
  113. }
  114. }
  115. }