keybinds.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. return nil
  24. }
  25. func newKeybind(key, desc string) Keybind {
  26. return Keybind{
  27. Keybind: keybind.NewKeybind(
  28. keybind.WithKeys(key),
  29. keybind.WithHelp(key, desc),
  30. ),
  31. }
  32. }
  33. type NavigationKeybinds struct {
  34. Up Keybind `toml:"up"`
  35. Down Keybind `toml:"down"`
  36. Top Keybind `toml:"top"`
  37. Bottom Keybind `toml:"bottom"`
  38. }
  39. type ScrollKeybinds struct {
  40. ScrollUp Keybind `toml:"scroll_up"`
  41. ScrollDown Keybind `toml:"scroll_down"`
  42. ScrollTop Keybind `toml:"scroll_top"`
  43. ScrollBottom Keybind `toml:"scroll_bottom"`
  44. }
  45. type SelectionKeybinds struct {
  46. SelectUp Keybind `toml:"select_up"`
  47. SelectDown Keybind `toml:"select_down"`
  48. SelectTop Keybind `toml:"select_top"`
  49. SelectBottom Keybind `toml:"select_bottom"`
  50. }
  51. type PickerKeybinds struct {
  52. NavigationKeybinds
  53. Select Keybind `toml:"select"`
  54. Cancel Keybind `toml:"cancel"`
  55. }
  56. type GuildsTreeKeybinds struct {
  57. NavigationKeybinds
  58. SelectCurrent Keybind `toml:"select_current"`
  59. YankID Keybind `toml:"yank_id"`
  60. CollapseParentNode Keybind `toml:"collapse_parent_node"`
  61. MoveToParentNode Keybind `toml:"move_to_parent_node"`
  62. }
  63. type MessagesListKeybinds struct {
  64. SelectionKeybinds
  65. ScrollKeybinds
  66. SelectReply Keybind `toml:"select_reply"`
  67. Reply Keybind `toml:"reply"`
  68. ReplyMention Keybind `toml:"reply_mention"`
  69. Cancel Keybind `toml:"cancel"`
  70. Edit Keybind `toml:"edit"`
  71. Delete Keybind `toml:"delete"`
  72. DeleteConfirm Keybind `toml:"delete_confirm"`
  73. Open Keybind `toml:"open"`
  74. YankContent Keybind `toml:"yank_content"`
  75. YankURL Keybind `toml:"yank_url"`
  76. YankID Keybind `toml:"yank_id"`
  77. }
  78. type MessageInputKeybinds struct {
  79. Paste Keybind `toml:"paste"`
  80. Send Keybind `toml:"send"`
  81. Cancel Keybind `toml:"cancel"`
  82. TabComplete Keybind `toml:"tab_complete"`
  83. Undo Keybind `toml:"undo"`
  84. OpenEditor Keybind `toml:"open_editor"`
  85. OpenFilePicker Keybind `toml:"open_file_picker"`
  86. }
  87. type MentionsListKeybinds struct {
  88. NavigationKeybinds
  89. }
  90. type Keybinds struct {
  91. ToggleGuildsTree Keybind `toml:"toggle_guilds_tree"`
  92. ToggleChannelsPicker Keybind `toml:"toggle_channels_picker"`
  93. ToggleHelp Keybind `toml:"toggle_help"`
  94. Suspend Keybind `toml:"suspend"`
  95. FocusGuildsTree Keybind `toml:"focus_guilds_tree"`
  96. FocusMessagesList Keybind `toml:"focus_messages_list"`
  97. FocusMessageInput Keybind `toml:"focus_message_input"`
  98. FocusPrevious Keybind `toml:"focus_previous"`
  99. FocusNext Keybind `toml:"focus_next"`
  100. Picker PickerKeybinds `toml:"picker"`
  101. GuildsTree GuildsTreeKeybinds `toml:"guilds_tree"`
  102. MessagesList MessagesListKeybinds `toml:"messages_list"`
  103. MessageInput MessageInputKeybinds `toml:"message_input"`
  104. MentionsList MentionsListKeybinds `toml:"mentions_list"`
  105. Logout Keybind `toml:"logout"`
  106. Quit Keybind `toml:"quit"`
  107. }
  108. func defaultKeybinds() Keybinds {
  109. return Keybinds{
  110. ToggleGuildsTree: newKeybind("ctrl+b", "toggle guilds"),
  111. ToggleChannelsPicker: newKeybind("ctrl+k", "channels picker"),
  112. ToggleHelp: newKeybind("ctrl+.", "help"),
  113. Suspend: newKeybind("ctrl+z", "suspend"),
  114. FocusGuildsTree: newKeybind("ctrl+g", "guilds"),
  115. FocusMessagesList: newKeybind("ctrl+t", "messages"),
  116. FocusMessageInput: newKeybind("ctrl+i", "input"),
  117. FocusPrevious: newKeybind("ctrl+h", "focus prev"),
  118. FocusNext: newKeybind("ctrl+l", "focus next"),
  119. Logout: newKeybind("ctrl+d", "logout"),
  120. Quit: newKeybind("ctrl+c", "quit"),
  121. Picker: PickerKeybinds{
  122. NavigationKeybinds: NavigationKeybinds{
  123. Up: newKeybind("ctrl+p", "up"),
  124. Down: newKeybind("ctrl+n", "down"),
  125. Top: newKeybind("home", "top"),
  126. Bottom: newKeybind("end", "bottom"),
  127. },
  128. Cancel: newKeybind("esc", "cancel"),
  129. Select: newKeybind("enter", "sel"),
  130. },
  131. GuildsTree: GuildsTreeKeybinds{
  132. NavigationKeybinds: NavigationKeybinds{
  133. Up: newKeybind("k", "up"),
  134. Down: newKeybind("j", "down"),
  135. Top: newKeybind("g", "top"),
  136. Bottom: newKeybind("G", "bottom"),
  137. },
  138. SelectCurrent: newKeybind("enter", "sel"),
  139. YankID: newKeybind("i", "copy id"),
  140. CollapseParentNode: newKeybind("-", "collapse"),
  141. MoveToParentNode: newKeybind("p", "parent"),
  142. },
  143. MessagesList: MessagesListKeybinds{
  144. SelectionKeybinds: SelectionKeybinds{
  145. SelectUp: newKeybind("k", "up"),
  146. SelectDown: newKeybind("j", "down"),
  147. SelectTop: newKeybind("g", "top"),
  148. SelectBottom: newKeybind("G", "bottom"),
  149. },
  150. ScrollKeybinds: ScrollKeybinds{
  151. ScrollUp: newKeybind("K", "scroll up"),
  152. ScrollDown: newKeybind("J", "scroll down"),
  153. ScrollTop: newKeybind("home", "scroll top"),
  154. ScrollBottom: newKeybind("end", "scroll bottom"),
  155. },
  156. SelectReply: newKeybind("s", "sel reply"),
  157. Reply: newKeybind("R", "reply"),
  158. ReplyMention: newKeybind("r", "@reply"),
  159. Cancel: newKeybind("esc", "cancel"),
  160. Edit: newKeybind("e", "edit"),
  161. Delete: newKeybind("D", "force delete"),
  162. DeleteConfirm: newKeybind(
  163. "d",
  164. "delete",
  165. ),
  166. Open: newKeybind("o", "open"),
  167. YankContent: newKeybind("y", "copy text"),
  168. YankURL: newKeybind("u", "copy url"),
  169. YankID: newKeybind("i", "copy id"),
  170. },
  171. MessageInput: MessageInputKeybinds{
  172. Paste: newKeybind("ctrl+v", "paste"),
  173. Send: newKeybind("enter", "send"),
  174. Cancel: newKeybind("esc", "cancel"),
  175. TabComplete: newKeybind("tab", "complete"),
  176. Undo: newKeybind("ctrl+u", "undo"),
  177. OpenEditor: newKeybind("ctrl+e", "editor"),
  178. OpenFilePicker: newKeybind("ctrl+\\", "attach"),
  179. },
  180. MentionsList: MentionsListKeybinds{
  181. NavigationKeybinds: NavigationKeybinds{
  182. Up: newKeybind("ctrl+p", "up"),
  183. Down: newKeybind("ctrl+n", "down"),
  184. Top: newKeybind("home", "top"),
  185. Bottom: newKeybind("end", "bottom"),
  186. },
  187. },
  188. }
  189. }