keybinds.go 7.4 KB

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