application.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package cmd
  2. import (
  3. "log/slog"
  4. "strings"
  5. "fmt"
  6. "github.com/ayn2op/discordo/internal/config"
  7. "github.com/ayn2op/discordo/internal/keyring"
  8. "github.com/ayn2op/discordo/internal/login"
  9. "github.com/ayn2op/discordo/internal/ui"
  10. "github.com/ayn2op/tview"
  11. "github.com/gdamore/tcell/v2"
  12. "golang.design/x/clipboard"
  13. )
  14. const (
  15. flexPageName = "flex"
  16. mentionsListPageName = "mentionsList"
  17. attachmentsListPageName = "attachmentsList"
  18. modalPageName = "modalPageName"
  19. )
  20. type application struct {
  21. cfg *config.Config
  22. *tview.Application
  23. pages *tview.Pages
  24. flex *tview.Flex
  25. guildsTree *guildsTree
  26. messagesList *messagesList
  27. messageInput *messageInput
  28. }
  29. func newApplication(cfg *config.Config) *application {
  30. app := &application{
  31. cfg: cfg,
  32. Application: tview.NewApplication(),
  33. pages: tview.NewPages(),
  34. flex: tview.NewFlex(),
  35. guildsTree: newGuildsTree(cfg),
  36. messagesList: newMessagesList(cfg),
  37. messageInput: newMessageInput(cfg),
  38. }
  39. app.pages.SetInputCapture(app.onPagesInputCapture)
  40. app.
  41. EnableMouse(cfg.Mouse).
  42. SetInputCapture(app.onInputCapture).
  43. EnablePaste(true)
  44. return app
  45. }
  46. func (a *application) run(token string) error {
  47. if token == "" {
  48. loginForm := login.NewForm(a.Application, a.cfg, func(token string) {
  49. if err := a.run(token); err != nil {
  50. slog.Error("failed to run application", "err", err)
  51. }
  52. })
  53. return a.SetRoot(loginForm, true).Run()
  54. }
  55. if err := openState(token); err != nil {
  56. return err
  57. }
  58. a.init()
  59. return a.SetRoot(a.pages, true).Run()
  60. }
  61. func (a *application) quit() {
  62. if discordState != nil {
  63. if err := discordState.Close(); err != nil {
  64. slog.Error("failed to close the session", "err", err)
  65. }
  66. }
  67. a.Stop()
  68. }
  69. func (a *application) init() {
  70. a.pages.Clear()
  71. a.flex.Clear()
  72. right := tview.NewFlex().
  73. SetDirection(tview.FlexRow).
  74. AddItem(a.messagesList, 0, 1, false).
  75. AddItem(a.messageInput, 3, 1, false)
  76. // The guilds tree is always focused first at start-up.
  77. a.flex.
  78. AddItem(a.guildsTree, 0, 1, true).
  79. AddItem(right, 0, 4, false)
  80. a.pages.AddAndSwitchToPage(flexPageName, a.flex, true)
  81. }
  82. func (a *application) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  83. switch event.Name() {
  84. case a.cfg.Keys.Quit:
  85. a.quit()
  86. return nil
  87. case "Ctrl+C":
  88. // https://github.com/ayn2op/tview/blob/a64fc48d7654432f71922c8b908280cdb525805c/application.go#L153
  89. return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
  90. }
  91. return event
  92. }
  93. func (a *application) onPagesInputCapture(event *tcell.EventKey) *tcell.EventKey {
  94. switch event.Name() {
  95. case a.cfg.Keys.FocusGuildsTree:
  96. a.messageInput.removeMentionsList()
  97. a.focusGuildsTree()
  98. return nil
  99. case a.cfg.Keys.FocusMessagesList:
  100. a.messageInput.removeMentionsList()
  101. a.SetFocus(a.messagesList)
  102. return nil
  103. case a.cfg.Keys.FocusMessageInput:
  104. a.focusMessageInput()
  105. return nil
  106. case a.cfg.Keys.FocusPrevious:
  107. a.focusPrevious()
  108. return nil
  109. case a.cfg.Keys.FocusNext:
  110. a.focusNext()
  111. return nil
  112. case a.cfg.Keys.Logout:
  113. a.quit()
  114. if err := keyring.DeleteToken(); err != nil {
  115. a.onError("Failed to delete token from keyring", err)
  116. return nil
  117. }
  118. return nil
  119. case a.cfg.Keys.ToggleGuildsTree:
  120. a.toggleGuildsTree()
  121. return nil
  122. }
  123. return event
  124. }
  125. func (a *application) toggleGuildsTree() {
  126. // The guilds tree is visible if the number of items is two.
  127. if a.flex.GetItemCount() == 2 {
  128. a.flex.RemoveItem(a.guildsTree)
  129. if a.guildsTree.HasFocus() {
  130. a.SetFocus(a.flex)
  131. }
  132. } else {
  133. a.init()
  134. a.SetFocus(a.guildsTree)
  135. }
  136. }
  137. func (a *application) focusGuildsTree() bool {
  138. // The guilds tree is not hidden if the number of items is two.
  139. if a.flex.GetItemCount() == 2 {
  140. a.SetFocus(a.guildsTree)
  141. return true
  142. }
  143. return false
  144. }
  145. func (a *application) focusMessageInput() bool {
  146. if !a.messageInput.GetDisabled() {
  147. a.SetFocus(a.messageInput)
  148. return true
  149. }
  150. return false
  151. }
  152. func (a *application) focusPrevious() {
  153. switch a.GetFocus() {
  154. case a.guildsTree:
  155. a.SetFocus(a.messageInput)
  156. case a.messagesList: // Handle both a.messagesList and a.flex as well as other edge cases (if there is).
  157. if ok := a.focusGuildsTree(); !ok {
  158. a.SetFocus(a.messageInput)
  159. }
  160. case a.messageInput:
  161. a.SetFocus(a.messagesList)
  162. }
  163. }
  164. func (a *application) focusNext() {
  165. switch a.GetFocus() {
  166. case a.guildsTree:
  167. a.SetFocus(a.messagesList)
  168. case a.messagesList:
  169. a.SetFocus(a.messageInput)
  170. case a.messageInput: // Handle both a.messageInput and a.flex as well as other edge cases (if there is).
  171. if ok := a.focusGuildsTree(); !ok {
  172. a.SetFocus(a.messagesList)
  173. }
  174. }
  175. }
  176. func (a *application) onError(msg string, err error, info ...any) {
  177. slog.Error(msg, append(info, "err", err)...)
  178. a.showErrorModal(msg, err.Error(), info...)
  179. }
  180. func (a *application) showModal(title, prompt string, buttons []string, onDone func(label string)) {
  181. previousFocus := a.GetFocus()
  182. modal := tview.NewModal()
  183. modal.Box = ui.ConfigureBox(modal.Box, &a.cfg.Theme)
  184. modal.
  185. SetText(prompt).
  186. AddButtons(buttons).
  187. SetDoneFunc(func(_ int, buttonLabel string) {
  188. a.pages.
  189. RemovePage(modalPageName).
  190. SwitchToPage(flexPageName)
  191. a.SetFocus(previousFocus)
  192. if onDone != nil {
  193. onDone(buttonLabel)
  194. }
  195. }).
  196. SetTitle(title).
  197. SetTitleAlignment(tview.AlignmentCenter)
  198. a.pages.
  199. AddAndSwitchToPage(modalPageName, ui.Centered(modal, 0, 0), true).
  200. ShowPage(flexPageName)
  201. a.SetFocus(modal)
  202. }
  203. func (a *application) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
  204. a.showModal("", prompt, buttons, onDone)
  205. }
  206. func (a *application) showErrorModal(msg, err string, info ...any) {
  207. a.showModal("[ ERROR ]", msg + "\nReason: " + err, []string{"Copy", "OK"}, func(label string) {
  208. if label != "Copy" {
  209. return
  210. }
  211. res := &strings.Builder{}
  212. fmt.Fprintf(res, "%s\nReason: %s", msg, err)
  213. for i := 0; i < len(info)-1; i += 2 {
  214. fmt.Fprintf(res, "\n%v: %#v", info[i], info[i+1])
  215. }
  216. // Do as log/slog does. "!BADKEY" for last odd argument.
  217. if (len(info) % 2) == 1 {
  218. fmt.Fprintf(res, "\n!BADKEY: %#v", info[len(info)-1])
  219. }
  220. go clipboard.Write(clipboard.FmtText, []byte(res.String()))
  221. })
  222. }
  223. func (a *application) showInfoModal(text string) {
  224. a.showModal("[ INFO ]", text, []string{"OK"}, nil)
  225. }