keybinds.go 8.5 KB

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