model.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package root
  2. import (
  3. "log/slog"
  4. "os"
  5. "github.com/ayn2op/discordo/internal/config"
  6. "github.com/ayn2op/discordo/internal/consts"
  7. "github.com/ayn2op/discordo/internal/ui/chat"
  8. "github.com/ayn2op/discordo/internal/ui/login"
  9. "github.com/ayn2op/discordo/internal/ui/login/qr"
  10. "github.com/ayn2op/discordo/internal/ui/login/token"
  11. "github.com/ayn2op/tview"
  12. "github.com/ayn2op/tview/flex"
  13. "github.com/ayn2op/tview/help"
  14. "github.com/ayn2op/tview/keybind"
  15. "github.com/gdamore/tcell/v3"
  16. )
  17. const tokenEnvVarKey = "DISCORDO_TOKEN"
  18. type Model struct {
  19. app *tview.Application
  20. rootFlex *flex.Model // inner + help
  21. inner tview.Model
  22. help *help.Help
  23. cfg *config.Config
  24. }
  25. func NewModel(cfg *config.Config, app *tview.Application) *Model {
  26. m := &Model{
  27. app: app,
  28. rootFlex: flex.NewModel(),
  29. help: help.New(),
  30. cfg: cfg,
  31. }
  32. m.rootFlex.SetDirection(flex.DirectionRow)
  33. styles := help.DefaultStyles()
  34. styles.ShortKeyStyle = cfg.Theme.Help.ShortKeyStyle.Style
  35. styles.ShortDescStyle = cfg.Theme.Help.ShortDescStyle.Style
  36. styles.FullKeyStyle = cfg.Theme.Help.FullKeyStyle.Style
  37. styles.FullDescStyle = cfg.Theme.Help.FullDescStyle.Style
  38. m.help.SetStyles(styles)
  39. m.help.SetKeyMap(m)
  40. m.help.SetCompactModifiers(cfg.Help.CompactModifiers)
  41. m.help.SetShortSeparator(cfg.Help.Separator)
  42. m.help.SetBorderPadding(0, 0, cfg.Help.Padding[0], cfg.Help.Padding[1])
  43. m.buildLayout()
  44. return m
  45. }
  46. func (m *Model) showLogin() tview.Command {
  47. m.inner = login.NewModel(m.cfg)
  48. m.buildLayout()
  49. return tview.Batch(m.inner.HandleEvent(&tview.InitEvent{}), tview.SetFocus(m))
  50. }
  51. func (m *Model) showChat(token string) tview.Command {
  52. m.inner = chat.NewModel(m.app, m.cfg, token)
  53. m.buildLayout()
  54. return tview.Batch(m.inner.HandleEvent(&tview.InitEvent{}), tview.SetFocus(m))
  55. }
  56. func (m *Model) buildLayout() {
  57. m.rootFlex.Clear()
  58. if m.inner != nil {
  59. m.rootFlex.AddItem(m.inner, 0, 1, true)
  60. }
  61. m.rootFlex.AddItem(m.help, 1, 0, false)
  62. m.updateHelpHeight()
  63. }
  64. var _ tview.Model = (*Model)(nil)
  65. func (m *Model) Draw(screen tcell.Screen) {
  66. m.rootFlex.Draw(screen)
  67. }
  68. func (m *Model) HandleEvent(event tview.Event) tview.Command {
  69. switch event := event.(type) {
  70. case *tview.InitEvent:
  71. var cmd tview.Command
  72. if token := os.Getenv(tokenEnvVarKey); token != "" {
  73. slog.Warn("Using DISCORDO_TOKEN from environment. Environment variables are visible to all processes running as the same user. Consider using the keyring instead.")
  74. cmd = tokenCommand(token)
  75. } else {
  76. cmd = getToken()
  77. }
  78. return tview.Batch(
  79. tview.SetTitle(consts.Name),
  80. initClipboard(),
  81. cmd,
  82. )
  83. case *loginEvent:
  84. return m.showLogin()
  85. case *tokenEvent:
  86. return m.showChat(event.token)
  87. case *token.TokenEvent:
  88. return tview.Batch(m.showChat(event.Token), setToken(event.Token))
  89. case *qr.TokenEvent:
  90. return tview.Batch(m.showChat(event.Token), setToken(event.Token))
  91. case *chat.LogoutEvent:
  92. return tview.Batch(
  93. m.showLogin(),
  94. deleteToken(),
  95. )
  96. case *tview.KeyEvent:
  97. // Skip single-char keybinds when the chat's message input is focused.
  98. type inputChecker interface {
  99. InputActive() bool
  100. }
  101. inputActive := false
  102. if ic, ok := m.inner.(inputChecker); ok {
  103. inputActive = ic.InputActive()
  104. }
  105. switch {
  106. case !inputActive && keybind.Matches(event, m.cfg.Keybinds.ToggleHelp.Keybind):
  107. m.help.SetShowAll(!m.help.ShowAll())
  108. m.updateHelpHeight()
  109. return nil
  110. case !inputActive && m.help.ShowAll() && keybind.Matches(event, m.cfg.Keybinds.EditConfig.Keybind):
  111. m.editConfig()
  112. return nil
  113. case keybind.Matches(event, m.cfg.Keybinds.Suspend.Keybind):
  114. m.suspend()
  115. return nil
  116. case keybind.Matches(event, m.cfg.Keybinds.Quit.Keybind):
  117. var innerCmd tview.Command
  118. if m.inner != nil {
  119. innerCmd = m.inner.HandleEvent(&chat.QuitEvent{})
  120. }
  121. return tview.Batch(innerCmd, tview.Quit())
  122. }
  123. }
  124. if m.inner != nil {
  125. return m.inner.HandleEvent(event)
  126. }
  127. return nil
  128. }
  129. func (m *Model) editConfig() {
  130. cmd := m.cfg.CreateEditorCommand(m.cfg.Path)
  131. if cmd == nil {
  132. return
  133. }
  134. cmd.Stdin = os.Stdin
  135. cmd.Stdout = os.Stdout
  136. cmd.Stderr = os.Stderr
  137. m.app.Suspend(func() {
  138. if err := cmd.Run(); err != nil {
  139. slog.Error("failed to run editor", "args", cmd.Args, "err", err)
  140. }
  141. })
  142. }
  143. func (m *Model) updateHelpHeight() {
  144. height := 1
  145. if m.help.ShowAll() {
  146. height = max(len(m.help.FullHelpLines(m.FullHelp(), 0)), 1)
  147. }
  148. m.rootFlex.ResizeItem(m.help, height, 0)
  149. }
  150. func (m *Model) Rect() (int, int, int, int) {
  151. return m.rootFlex.Rect()
  152. }
  153. func (m *Model) SetRect(x int, y int, width int, height int) {
  154. m.rootFlex.SetRect(x, y, width, height)
  155. }
  156. func (m *Model) Focus(delegate func(tview.Model)) {
  157. if m.inner != nil {
  158. delegate(m.inner)
  159. }
  160. }
  161. func (m *Model) HasFocus() bool {
  162. if m.inner != nil {
  163. return m.inner.HasFocus()
  164. }
  165. return true
  166. }
  167. func (m *Model) Blur() {
  168. if m.inner != nil {
  169. m.inner.Blur()
  170. }
  171. }