keybinds.go 8.5 KB

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