util.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package chat
  2. import (
  3. "encoding/json"
  4. "log/slog"
  5. "os"
  6. "strings"
  7. "github.com/ayn2op/discordo/internal/config"
  8. "github.com/ayn2op/discordo/internal/ui"
  9. "github.com/ayn2op/tview"
  10. "github.com/ayn2op/tview/keybind"
  11. "github.com/ayn2op/tview/list"
  12. "github.com/ayn2op/tview/picker"
  13. "github.com/gdamore/tcell/v3"
  14. )
  15. func ConfigurePicker(model *picker.Model, cfg *config.Config, title string) {
  16. model.Box = ui.ConfigureBox(tview.NewBox(), &cfg.Theme)
  17. // When a child of the parent flex is focused, the parent layout itself is not reported as focused.
  18. // Instead, the focused child (picker) is considered focused.
  19. // Therefore, we manually set the active border style on the picker to ensure it displays the correct focused appearance.
  20. model.
  21. SetBlurFunc(nil).
  22. SetFocusFunc(nil).
  23. SetBorderSet(cfg.Theme.Border.ActiveSet.BorderSet).
  24. SetBorderStyle(cfg.Theme.Border.ActiveStyle.Style).
  25. SetTitleStyle(cfg.Theme.Title.ActiveStyle.Style).
  26. SetFooterStyle(cfg.Theme.Footer.ActiveStyle.Style)
  27. model.SetTitle(title)
  28. model.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
  29. model.SetScrollBar(tview.NewScrollBar().
  30. SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
  31. SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
  32. SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
  33. model.SetKeybinds(picker.Keybinds{
  34. Cancel: cfg.Keybinds.Picker.Cancel.Keybind,
  35. Keybinds: list.Keybinds{
  36. SelectUp: cfg.Keybinds.Picker.Up.Keybind,
  37. SelectDown: cfg.Keybinds.Picker.Down.Keybind,
  38. SelectTop: cfg.Keybinds.Picker.Top.Keybind,
  39. SelectBottom: cfg.Keybinds.Picker.Bottom.Keybind,
  40. },
  41. Select: cfg.Keybinds.Picker.Select.Keybind,
  42. })
  43. }
  44. // browseKeyHandler is an optional callback for picker-specific keys in browse
  45. // mode. Return (cmd, true) if handled, (nil, false) to fall through.
  46. type browseKeyHandler func(event *tview.KeyEvent) (tview.Command, bool)
  47. // pickerBrowseHandleKey implements a two-phase ESC for overlay pickers.
  48. // First ESC enters browse mode (j/k navigate, i returns to input).
  49. // Second ESC calls closeFn to close the picker.
  50. // Returns (command, handled). If handled is false, the caller should
  51. // fall through to the normal picker event handling.
  52. // The optional extra handlers are checked before the default swallow-all,
  53. // allowing pickers to add custom browse-mode keys (e.g. favorite toggle).
  54. func pickerBrowseHandleKey(event *tview.KeyEvent, browseMode *bool, model *picker.Model, closeFn func(), extra ...browseKeyHandler) (tview.Command, bool) {
  55. if !*browseMode {
  56. if event.Key() == tcell.KeyEsc {
  57. *browseMode = true
  58. return nil, true
  59. }
  60. return nil, false
  61. }
  62. // Browse mode: intercept keys before they reach the input field.
  63. switch {
  64. case event.Key() == tcell.KeyEsc:
  65. *browseMode = false
  66. closeFn()
  67. return nil, true
  68. case event.Key() == tcell.KeyRune && event.Str() == "i":
  69. *browseMode = false
  70. return nil, true
  71. case event.Key() == tcell.KeyRune && event.Str() == "j":
  72. return model.HandleEvent(tcell.NewEventKey(tcell.KeyCtrlN, "", tcell.ModCtrl)), true
  73. case event.Key() == tcell.KeyRune && event.Str() == "k":
  74. return model.HandleEvent(tcell.NewEventKey(tcell.KeyCtrlP, "", tcell.ModCtrl)), true
  75. case event.Key() == tcell.KeyRune && event.Str() == "g":
  76. return model.HandleEvent(tcell.NewEventKey(tcell.KeyHome, "", tcell.ModNone)), true
  77. case event.Key() == tcell.KeyRune && event.Str() == "G":
  78. return model.HandleEvent(tcell.NewEventKey(tcell.KeyEnd, "", tcell.ModNone)), true
  79. case event.Key() == tcell.KeyEnter:
  80. return model.HandleEvent(event), true
  81. }
  82. // Check extra handlers before swallowing.
  83. for _, handler := range extra {
  84. if cmd, handled := handler(event); handled {
  85. return cmd, true
  86. }
  87. }
  88. // Swallow other keys in browse mode so they don't reach the input.
  89. return nil, true
  90. }
  91. // pickerShortHelp returns the standard short help for any picker overlay.
  92. func pickerShortHelp(cfg config.PickerKeybinds) []keybind.Keybind {
  93. return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
  94. }
  95. // pickerFullHelp returns the standard full help for any picker overlay.
  96. func pickerFullHelp(cfg config.PickerKeybinds) [][]keybind.Keybind {
  97. return [][]keybind.Keybind{
  98. {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
  99. {cfg.Select.Keybind, cfg.Cancel.Keybind},
  100. }
  101. }
  102. // atomicSaveJSON marshals v as JSON and atomically writes it to path
  103. // via a tmp+rename pattern with 0600 permissions.
  104. func atomicSaveJSON(path string, v any) {
  105. data, err := json.Marshal(v)
  106. if err != nil {
  107. slog.Error("failed to marshal JSON", "path", path, "err", err)
  108. return
  109. }
  110. tmpPath := path + ".tmp"
  111. if err := os.WriteFile(tmpPath, data, 0600); err != nil {
  112. slog.Error("failed to write JSON", "path", path, "err", err)
  113. return
  114. }
  115. if err := os.Rename(tmpPath, path); err != nil {
  116. slog.Error("failed to rename JSON file", "path", path, "err", err)
  117. }
  118. }
  119. func humanJoin(items []string) string {
  120. count := len(items)
  121. switch count {
  122. case 0:
  123. return ""
  124. case 1:
  125. return items[0]
  126. case 2:
  127. return items[0] + " and " + items[1]
  128. default:
  129. return strings.Join(items[:count-1], ", ") + ", and " + items[count-1]
  130. }
  131. }