keybinds.go 6.1 KB

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