util.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package chat
  2. import (
  3. "strings"
  4. "github.com/ayn2op/discordo/internal/config"
  5. "github.com/ayn2op/discordo/internal/ui"
  6. "github.com/ayn2op/tview"
  7. "github.com/ayn2op/tview/list"
  8. "github.com/ayn2op/tview/picker"
  9. )
  10. func ConfigurePicker(model *picker.Model, cfg *config.Config, title string) {
  11. model.Box = ui.ConfigureBox(tview.NewBox(), &cfg.Theme)
  12. // When a child of the parent flex is focused, the parent layout itself is not reported as focused.
  13. // Instead, the focused child (picker) is considered focused.
  14. // Therefore, we manually set the active border style on the picker to ensure it displays the correct focused appearance.
  15. model.
  16. SetBlurFunc(nil).
  17. SetFocusFunc(nil).
  18. SetBorderSet(cfg.Theme.Border.ActiveSet.BorderSet).
  19. SetBorderStyle(cfg.Theme.Border.ActiveStyle.Style).
  20. SetTitleStyle(cfg.Theme.Title.ActiveStyle.Style).
  21. SetFooterStyle(cfg.Theme.Footer.ActiveStyle.Style)
  22. model.SetTitle(title)
  23. model.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
  24. model.SetScrollBar(tview.NewScrollBar().
  25. SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
  26. SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
  27. SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
  28. model.SetKeybinds(picker.Keybinds{
  29. Cancel: cfg.Keybinds.Picker.Cancel.Keybind,
  30. Keybinds: list.Keybinds{
  31. SelectUp: cfg.Keybinds.Picker.Up.Keybind,
  32. SelectDown: cfg.Keybinds.Picker.Down.Keybind,
  33. SelectTop: cfg.Keybinds.Picker.Top.Keybind,
  34. SelectBottom: cfg.Keybinds.Picker.Bottom.Keybind,
  35. },
  36. Select: cfg.Keybinds.Picker.Select.Keybind,
  37. })
  38. }
  39. func humanJoin(items []string) string {
  40. count := len(items)
  41. switch count {
  42. case 0:
  43. return ""
  44. case 1:
  45. return items[0]
  46. case 2:
  47. return items[0] + " and " + items[1]
  48. default:
  49. return strings.Join(items[:count-1], ", ") + ", and " + items[count-1]
  50. }
  51. }