keybinds.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package config
  2. import (
  3. "github.com/BurntSushi/toml"
  4. "github.com/ayn2op/tview/keybind"
  5. )
  6. type Keybind struct {
  7. keybind.Keybind
  8. }
  9. var _ toml.Unmarshaler = (*Keybind)(nil)
  10. func (k *Keybind) UnmarshalTOML(value any) error {
  11. switch value := value.(type) {
  12. case string:
  13. k.SetKeys(value)
  14. case []any:
  15. keys := make([]string, 0, len(value))
  16. for _, key := range value {
  17. if key, ok := key.(string); ok {
  18. keys = append(keys, key)
  19. }
  20. }
  21. k.SetKeys(keys...)
  22. }
  23. // Keep displayed help key aligned with configured key(s).
  24. if keys := k.Keys(); len(keys) > 0 {
  25. k.SetHelp(keys[0], k.Help().Desc)
  26. }
  27. return nil
  28. }
  29. func newKeybind(key, desc string) Keybind {
  30. return Keybind{
  31. Keybind: keybind.NewKeybind(
  32. keybind.WithKeys(key),
  33. keybind.WithHelp(key, desc),
  34. ),
  35. }
  36. }
  37. type NavigationKeybinds struct {
  38. Up Keybind `toml:"up"`
  39. Down Keybind `toml:"down"`
  40. Top Keybind `toml:"top"`
  41. Bottom Keybind `toml:"bottom"`
  42. }
  43. type ScrollKeybinds struct {
  44. ScrollUp Keybind `toml:"scroll_up"`
  45. ScrollDown Keybind `toml:"scroll_down"`
  46. ScrollTop Keybind `toml:"scroll_top"`
  47. ScrollBottom Keybind `toml:"scroll_bottom"`
  48. }
  49. type SelectionKeybinds struct {
  50. SelectUp Keybind `toml:"select_up"`
  51. SelectDown Keybind `toml:"select_down"`
  52. SelectTop Keybind `toml:"select_top"`
  53. SelectBottom Keybind `toml:"select_bottom"`
  54. }
  55. type PickerKeybinds struct {
  56. NavigationKeybinds
  57. Select Keybind `toml:"select"`
  58. Cancel Keybind `toml:"cancel"`
  59. }
  60. type GuildsTreeKeybinds struct {
  61. NavigationKeybinds
  62. SelectCurrent Keybind `toml:"select_current"`
  63. YankID Keybind `toml:"yank_id"`
  64. CollapseParentNode Keybind `toml:"collapse_parent_node"`
  65. MoveToParentNode Keybind `toml:"move_to_parent_node"`
  66. }
  67. type MessagesListKeybinds struct {
  68. SelectionKeybinds
  69. ScrollKeybinds
  70. SelectReply Keybind `toml:"select_reply"`
  71. Reply Keybind `toml:"reply"`
  72. ReplyMention Keybind `toml:"reply_mention"`
  73. Cancel Keybind `toml:"cancel"`
  74. Edit Keybind `toml:"edit"`
  75. Delete Keybind `toml:"delete"`
  76. DeleteConfirm Keybind `toml:"delete_confirm"`
  77. Open Keybind `toml:"open"`
  78. YankContent Keybind `toml:"yank_content"`
  79. YankURL Keybind `toml:"yank_url"`
  80. YankID Keybind `toml:"yank_id"`
  81. }
  82. type MessageInputKeybinds struct {
  83. Paste Keybind `toml:"paste"`
  84. Send Keybind `toml:"send"`
  85. Cancel Keybind `toml:"cancel"`
  86. TabComplete Keybind `toml:"tab_complete"`
  87. Undo Keybind `toml:"undo"`
  88. OpenEditor Keybind `toml:"open_editor"`
  89. OpenFilePicker Keybind `toml:"open_file_picker"`
  90. }
  91. type MentionsListKeybinds struct {
  92. NavigationKeybinds
  93. }
  94. type Keybinds struct {
  95. ToggleGuildsTree Keybind `toml:"toggle_guilds_tree"`
  96. ToggleChannelsPicker Keybind `toml:"toggle_channels_picker"`
  97. ToggleHelp Keybind `toml:"toggle_help"`
  98. Suspend Keybind `toml:"suspend"`
  99. FocusGuildsTree Keybind `toml:"focus_guilds_tree"`
  100. FocusMessagesList Keybind `toml:"focus_messages_list"`
  101. FocusMessageInput Keybind `toml:"focus_message_input"`
  102. FocusPrevious Keybind `toml:"focus_previous"`
  103. FocusNext Keybind `toml:"focus_next"`
  104. Picker PickerKeybinds `toml:"picker"`
  105. GuildsTree GuildsTreeKeybinds `toml:"guilds_tree"`
  106. MessagesList MessagesListKeybinds `toml:"messages_list"`
  107. MessageInput MessageInputKeybinds `toml:"message_input"`
  108. MentionsList MentionsListKeybinds `toml:"mentions_list"`
  109. Logout Keybind `toml:"logout"`
  110. Quit Keybind `toml:"quit"`
  111. }
  112. func defaultPickerKeybinds() PickerKeybinds {
  113. return PickerKeybinds{
  114. NavigationKeybinds: NavigationKeybinds{
  115. Up: newKeybind("ctrl+p", "up"),
  116. Down: newKeybind("ctrl+n", "down"),
  117. Top: newKeybind("home", "top"),
  118. Bottom: newKeybind("end", "bottom"),
  119. },
  120. Cancel: newKeybind("esc", "cancel"),
  121. Select: newKeybind("enter", "sel"),
  122. }
  123. }
  124. func defaultNavigationKeybinds() NavigationKeybinds {
  125. return NavigationKeybinds{
  126. Up: newKeybind("k", "up"),
  127. Down: newKeybind("j", "down"),
  128. Top: newKeybind("g", "top"),
  129. Bottom: newKeybind("G", "bottom"),
  130. }
  131. }
  132. func defaultGuildsTreeKeybinds() GuildsTreeKeybinds {
  133. return GuildsTreeKeybinds{
  134. NavigationKeybinds: defaultNavigationKeybinds(),
  135. SelectCurrent: newKeybind("enter", "sel"),
  136. YankID: newKeybind("i", "copy id"),
  137. CollapseParentNode: newKeybind("-", "collapse"),
  138. MoveToParentNode: newKeybind("p", "parent"),
  139. }
  140. }
  141. func defaultMessagesListKeybinds() MessagesListKeybinds {
  142. return MessagesListKeybinds{
  143. SelectionKeybinds: SelectionKeybinds{
  144. SelectUp: newKeybind("k", "up"),
  145. SelectDown: newKeybind("j", "down"),
  146. SelectTop: newKeybind("g", "top"),
  147. SelectBottom: newKeybind("G", "bottom"),
  148. },
  149. ScrollKeybinds: ScrollKeybinds{
  150. ScrollUp: newKeybind("K", "scroll up"),
  151. ScrollDown: newKeybind("J", "scroll down"),
  152. ScrollTop: newKeybind("home", "scroll top"),
  153. ScrollBottom: newKeybind("end", "scroll bottom"),
  154. },
  155. SelectReply: newKeybind("s", "sel reply"),
  156. Reply: newKeybind("R", "reply"),
  157. ReplyMention: newKeybind("r", "@reply"),
  158. Cancel: newKeybind("esc", "cancel"),
  159. Edit: newKeybind("e", "edit"),
  160. Delete: newKeybind("D", "force delete"),
  161. DeleteConfirm: newKeybind(
  162. "d",
  163. "delete",
  164. ),
  165. Open: newKeybind("o", "open"),
  166. YankContent: newKeybind("y", "copy text"),
  167. YankURL: newKeybind("u", "copy url"),
  168. YankID: newKeybind("i", "copy id"),
  169. }
  170. }
  171. func defaultMessageInputKeybinds() MessageInputKeybinds {
  172. return MessageInputKeybinds{
  173. Paste: newKeybind("ctrl+v", "paste"),
  174. Send: newKeybind("enter", "send"),
  175. Cancel: newKeybind("esc", "cancel"),
  176. TabComplete: newKeybind("tab", "complete"),
  177. Undo: newKeybind("ctrl+u", "undo"),
  178. OpenEditor: newKeybind("ctrl+e", "editor"),
  179. OpenFilePicker: newKeybind("ctrl+\\", "attach"),
  180. }
  181. }
  182. func defaultMentionsListKeybinds() MentionsListKeybinds {
  183. return MentionsListKeybinds{
  184. NavigationKeybinds: NavigationKeybinds{
  185. Up: newKeybind("ctrl+p", "up"),
  186. Down: newKeybind("ctrl+n", "down"),
  187. Top: newKeybind("home", "top"),
  188. Bottom: newKeybind("end", "bottom"),
  189. },
  190. }
  191. }
  192. func defaultKeybinds() Keybinds {
  193. return Keybinds{
  194. ToggleGuildsTree: newKeybind("ctrl+b", "toggle guilds"),
  195. ToggleChannelsPicker: newKeybind("ctrl+k", "channels picker"),
  196. ToggleHelp: newKeybind("ctrl+.", "help"),
  197. Suspend: newKeybind("ctrl+z", "suspend"),
  198. FocusGuildsTree: newKeybind("ctrl+g", "guilds"),
  199. FocusMessagesList: newKeybind("ctrl+t", "messages"),
  200. FocusMessageInput: newKeybind("ctrl+i", "input"),
  201. FocusPrevious: newKeybind("ctrl+h", "focus prev"),
  202. FocusNext: newKeybind("ctrl+l", "focus next"),
  203. Logout: newKeybind("ctrl+d", "logout"),
  204. Quit: newKeybind("ctrl+c", "quit"),
  205. Picker: defaultPickerKeybinds(),
  206. GuildsTree: defaultGuildsTreeKeybinds(),
  207. MessagesList: defaultMessagesListKeybinds(),
  208. MessageInput: defaultMessageInputKeybinds(),
  209. MentionsList: defaultMentionsListKeybinds(),
  210. }
  211. }