keybinds.go 7.1 KB

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