keybinds.go 8.1 KB

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