keybinds.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. ArrowUp Keybind `toml:"arrow_up"`
  67. ArrowDown Keybind `toml:"arrow_down"`
  68. ArrowLeft Keybind `toml:"arrow_left"`
  69. ArrowRight Keybind `toml:"arrow_right"`
  70. }
  71. type MessagesListKeybinds struct {
  72. SelectionKeybinds
  73. ScrollKeybinds
  74. SelectReply Keybind `toml:"select_reply"`
  75. Reply Keybind `toml:"reply"`
  76. ReplyMention Keybind `toml:"reply_mention"`
  77. Cancel Keybind `toml:"cancel"`
  78. Edit Keybind `toml:"edit"`
  79. Delete Keybind `toml:"delete"`
  80. DeleteConfirm Keybind `toml:"delete_confirm"`
  81. Open Keybind `toml:"open"`
  82. SaveImage Keybind `toml:"save_image"`
  83. UserInfo Keybind `toml:"user_info"`
  84. Search Keybind `toml:"search"`
  85. OpenThread Keybind `toml:"open_thread"`
  86. ToggleTimestamps Keybind `toml:"toggle_timestamps"`
  87. ToggleReplies Keybind `toml:"toggle_replies"`
  88. AddReaction Keybind `toml:"add_reaction"`
  89. ViewReactions Keybind `toml:"view_reactions"`
  90. ArrowUp Keybind `toml:"arrow_up"`
  91. ArrowDown Keybind `toml:"arrow_down"`
  92. ArrowLeft Keybind `toml:"arrow_left"`
  93. ArrowRight Keybind `toml:"arrow_right"`
  94. YankContent Keybind `toml:"yank_content"`
  95. YankURL Keybind `toml:"yank_url"`
  96. YankID Keybind `toml:"yank_id"`
  97. }
  98. type MessageInputKeybinds struct {
  99. Paste Keybind `toml:"paste"`
  100. Send Keybind `toml:"send"`
  101. Cancel Keybind `toml:"cancel"`
  102. TabComplete Keybind `toml:"tab_complete"`
  103. Undo Keybind `toml:"undo"`
  104. OpenEditor Keybind `toml:"open_editor"`
  105. OpenFilePicker Keybind `toml:"open_file_picker"`
  106. }
  107. type MentionsListKeybinds struct {
  108. NavigationKeybinds
  109. }
  110. type Keybinds struct {
  111. ToggleGuildsTree Keybind `toml:"toggle_guilds_tree"`
  112. ToggleChannelsPicker Keybind `toml:"toggle_channels_picker"`
  113. ToggleHelp Keybind `toml:"toggle_help"`
  114. EditConfig Keybind `toml:"edit_config"`
  115. Suspend Keybind `toml:"suspend"`
  116. AttachFile Keybind `toml:"attach_file"`
  117. FocusGuildsTree Keybind `toml:"focus_guilds_tree"`
  118. FocusMessagesList Keybind `toml:"focus_messages_list"`
  119. FocusMessageInput Keybind `toml:"focus_message_input"`
  120. FocusPrevious Keybind `toml:"focus_previous"`
  121. FocusNext Keybind `toml:"focus_next"`
  122. Back Keybind `toml:"back"`
  123. Picker PickerKeybinds `toml:"picker"`
  124. GuildsTree GuildsTreeKeybinds `toml:"guilds_tree"`
  125. MessagesList MessagesListKeybinds `toml:"messages_list"`
  126. MessageInput MessageInputKeybinds `toml:"message_input"`
  127. MentionsList MentionsListKeybinds `toml:"mentions_list"`
  128. CommandMode Keybind `toml:"command_mode"`
  129. Logout Keybind `toml:"logout"`
  130. Quit Keybind `toml:"quit"`
  131. }
  132. func defaultPickerKeybinds() PickerKeybinds {
  133. return PickerKeybinds{
  134. NavigationKeybinds: NavigationKeybinds{
  135. Up: newKeybind("ctrl+p", "up"),
  136. Down: newKeybind("ctrl+n", "down"),
  137. Top: newKeybind("home", "top"),
  138. Bottom: newKeybind("end", "bottom"),
  139. },
  140. Cancel: newKeybind("esc", "cancel"),
  141. Select: newKeybind("enter", "sel"),
  142. }
  143. }
  144. func defaultNavigationKeybinds() NavigationKeybinds {
  145. return NavigationKeybinds{
  146. Up: newKeybind("k", "up"),
  147. Down: newKeybind("j", "down"),
  148. Top: newKeybind("g", "top"),
  149. Bottom: newKeybind("G", "bottom"),
  150. }
  151. }
  152. func defaultGuildsTreeKeybinds() GuildsTreeKeybinds {
  153. return GuildsTreeKeybinds{
  154. NavigationKeybinds: defaultNavigationKeybinds(),
  155. SelectCurrent: newKeybind("enter", "sel"),
  156. YankID: newKeybind("C", "copy id"),
  157. CollapseParentNode: newKeybind("-", "collapse"),
  158. MoveToParentNode: newKeybind("p", "parent"),
  159. ArrowUp: newKeybind("up", "up"),
  160. ArrowDown: newKeybind("down", "down"),
  161. ArrowLeft: newKeybind("left", "left"),
  162. ArrowRight: newKeybind("right", "right"),
  163. }
  164. }
  165. func defaultMessagesListKeybinds() MessagesListKeybinds {
  166. return MessagesListKeybinds{
  167. SelectionKeybinds: SelectionKeybinds{
  168. SelectUp: newKeybind("k", "up"),
  169. SelectDown: newKeybind("j", "down"),
  170. SelectTop: newKeybind("g", "top"),
  171. SelectBottom: newKeybind("G", "bottom"),
  172. },
  173. ScrollKeybinds: ScrollKeybinds{
  174. ScrollUp: newKeybind("K", "scroll up"),
  175. ScrollDown: newKeybind("J", "scroll down"),
  176. ScrollTop: newKeybind("home", "scroll top"),
  177. ScrollBottom: newKeybind("end", "scroll bottom"),
  178. },
  179. SelectReply: newKeybind("s", "sel reply"),
  180. Reply: newKeybind("r", "reply"),
  181. ReplyMention: newKeybind("R", "@reply"),
  182. Cancel: newKeybind("esc", "cancel"),
  183. Edit: newKeybind("e", "edit"),
  184. Delete: newKeybind("D", "force delete"),
  185. DeleteConfirm: newKeybind(
  186. "d",
  187. "delete",
  188. ),
  189. Open: newKeybind("o", "open"),
  190. SaveImage: newKeybind("S", "save image"),
  191. UserInfo: newKeybind("w", "who"),
  192. Search: newKeybind("/", "search"),
  193. OpenThread: newKeybind("T", "thread"),
  194. ToggleTimestamps: newKeybind("t", "timestamps"),
  195. ToggleReplies: newKeybind("Z", "collapse replies"),
  196. AddReaction: newKeybind("e", "react"),
  197. ViewReactions: newKeybind("v", "reactions"),
  198. ArrowUp: newKeybind("up", "up"),
  199. ArrowDown: newKeybind("down", "down"),
  200. ArrowLeft: newKeybind("left", "left"),
  201. ArrowRight: newKeybind("right", "right"),
  202. YankContent: newKeybind("y", "copy text"),
  203. YankURL: newKeybind("u", "copy url"),
  204. YankID: newKeybind("C", "copy id"),
  205. }
  206. }
  207. func defaultMessageInputKeybinds() MessageInputKeybinds {
  208. return MessageInputKeybinds{
  209. Paste: newKeybind("ctrl+v", "paste"),
  210. Send: newKeybind("enter", "send"),
  211. Cancel: newKeybind("esc", "cancel"),
  212. TabComplete: newKeybind("tab", "complete"),
  213. Undo: newKeybind("ctrl+u", "undo"),
  214. OpenEditor: newKeybind("ctrl+e", "editor"),
  215. OpenFilePicker: newKeybind("ctrl+\\", "attach"),
  216. }
  217. }
  218. func defaultMentionsListKeybinds() MentionsListKeybinds {
  219. return MentionsListKeybinds{
  220. NavigationKeybinds: NavigationKeybinds{
  221. Up: newKeybind("ctrl+p", "up"),
  222. Down: newKeybind("ctrl+n", "down"),
  223. Top: newKeybind("home", "top"),
  224. Bottom: newKeybind("end", "bottom"),
  225. },
  226. }
  227. }
  228. func defaultKeybinds() Keybinds {
  229. return Keybinds{
  230. ToggleGuildsTree: newKeybind("H", "toggle guilds"),
  231. ToggleChannelsPicker: newKeybind("c", "channels picker"),
  232. ToggleHelp: newKeybind("?", "help"),
  233. EditConfig: newKeybind("E", "edit config"),
  234. Suspend: newKeybind("ctrl+z", "suspend"),
  235. AttachFile: newKeybind("I", "attach file"),
  236. FocusGuildsTree: newKeybind("ctrl+g", "guilds"),
  237. FocusMessagesList: newKeybind("ctrl+t", "messages"),
  238. FocusMessageInput: newKeybind("i", "input"),
  239. FocusPrevious: newKeybind("h", "focus prev"),
  240. FocusNext: newKeybind("l", "focus next"),
  241. Back: newKeybind("b", "back"),
  242. CommandMode: newKeybind(":", "command"),
  243. Logout: newKeybind("ctrl+d", "logout"),
  244. Quit: newKeybind("ctrl+c", "quit"),
  245. Picker: defaultPickerKeybinds(),
  246. GuildsTree: defaultGuildsTreeKeybinds(),
  247. MessagesList: defaultMessagesListKeybinds(),
  248. MessageInput: defaultMessageInputKeybinds(),
  249. MentionsList: defaultMentionsListKeybinds(),
  250. }
  251. }