action.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package editor
  2. import(
  3. "strings"
  4. "os"
  5. )
  6. type ActionManager = struct {
  7. Common []Action
  8. Normal []Action
  9. Insert []Action
  10. Palette []Action
  11. }
  12. type Action = struct {
  13. Binding string
  14. Command string
  15. NeedsArgs bool
  16. Callback func(*Model, []string)
  17. }
  18. func DefaultActionManager() ActionManager {
  19. return ActionManager{
  20. Common: []Action{
  21. Action{
  22. Binding: "f12",
  23. Callback: func(m *Model, args []string) {
  24. m.Mode = ModeNormal
  25. },
  26. },
  27. Action{
  28. Binding: "left",
  29. Callback: func(m *Model, args []string) {
  30. m.BM.Current().MoveHoriz(-1)
  31. },
  32. },
  33. Action{
  34. Binding: "right",
  35. Callback: func(m *Model, args []string) {
  36. m.BM.Current().MoveHoriz(1)
  37. },
  38. },
  39. Action{
  40. Binding: "shift+ctrl+Rune[v]",
  41. Command: "paste",
  42. NeedsArgs: false,
  43. Callback: func(m *Model, args []string) {
  44. m.Screen.GetClipboard()
  45. },
  46. },
  47. Action{
  48. Binding: "ctrl+q",
  49. Command: "q",
  50. NeedsArgs: true,
  51. Callback: func(m *Model, args []string) {
  52. m.Quit()
  53. },
  54. },
  55. Action{
  56. Binding: "ctrl+w",
  57. Command: "w",
  58. NeedsArgs: false,
  59. Callback: func(m *Model, args []string) {
  60. path := ""
  61. if len(args) < 1 || args[0] == "" {
  62. if m.BM.Current().Path == "" {
  63. m.Mode = ModeNormal
  64. m.BM.PaletteBuffer.Clear()
  65. m.BM.PaletteBuffer.Paste("moose.error:Missing filename argument for write")
  66. return
  67. }
  68. }
  69. if len(args) > 0 && args[0] != "" {
  70. m.BM.Current().Path = args[0]
  71. }
  72. path = m.BM.Current().Path
  73. data := []byte(m.BM.Current().String())
  74. err := os.WriteFile(path, data, 0644)
  75. if err != nil {
  76. m.Mode = ModeNormal
  77. m.BM.PaletteBuffer.Clear()
  78. m.BM.PaletteBuffer.Paste("moose.error:Could not write to file \"" + path + "\": " + err.Error())
  79. return
  80. }
  81. },
  82. },
  83. Action{
  84. Binding: "shift+ctrl+Rune[w]",
  85. Command: "wp",
  86. NeedsArgs: true,
  87. Callback: func(m *Model, args []string) {
  88. if len(args) < 1 || args[0] == "" {
  89. m.Mode = ModeNormal
  90. m.BM.PaletteBuffer.Clear()
  91. m.BM.PaletteBuffer.Paste("moose.error:Missing filename argument for write")
  92. return
  93. }
  94. data := []byte(m.BM.Current().String())
  95. err := os.WriteFile(args[0], data, 0644)
  96. if err != nil {
  97. m.Mode = ModeNormal
  98. m.BM.PaletteBuffer.Clear()
  99. m.BM.PaletteBuffer.Paste("moose.error:Could not write to file \"" + args[0] + "\": " + err.Error())
  100. return
  101. }
  102. },
  103. },
  104. },
  105. Normal: []Action{
  106. Action{
  107. Binding: "Rune[i]",
  108. Callback: func(m *Model, args []string) {
  109. m.Mode = ModeInsert
  110. },
  111. },
  112. Action{
  113. Binding: "Rune[q]",
  114. Callback: func(m *Model, args []string) {
  115. m.Mode = ModePalette
  116. m.BM.PaletteBuffer.Clear()
  117. m.BM.PaletteBuffer.Insert('/')
  118. },
  119. },
  120. },
  121. Insert: []Action{
  122. Action{
  123. Binding: "up",
  124. Callback: func(m *Model, args []string) {
  125. m.BM.Current().MoveVert(-1)
  126. },
  127. },
  128. Action{
  129. Binding: "down",
  130. Callback: func(m *Model, args []string) {
  131. m.BM.Current().MoveVert(1)
  132. },
  133. },
  134. Action{
  135. Binding: "shift+up",
  136. Command: "TODO:",
  137. NeedsArgs: false,
  138. Callback: func(m *Model, args []string) {
  139. m.BM.Current().AddCursorVert(-1)
  140. },
  141. },
  142. Action{
  143. Binding: "shift+down",
  144. Command: "TODO:",
  145. NeedsArgs: false,
  146. Callback: func(m *Model, args []string) {
  147. m.BM.Current().AddCursorVert(1)
  148. },
  149. },
  150. Action{
  151. Binding: "esc",
  152. Command: "TODO:",
  153. NeedsArgs: false,
  154. Callback: func(m *Model, args []string) {
  155. m.BM.Current().ClearCursors()
  156. },
  157. },
  158. Action{
  159. Binding: "tab",
  160. Command: "TODO:",
  161. NeedsArgs: false,
  162. Callback: func(m *Model, args []string) {
  163. for _ = range 4 {
  164. m.BM.Current().Insert(' ')
  165. }
  166. },
  167. },
  168. Action{
  169. Binding: "shift+tab",
  170. Command: "TODO:",
  171. NeedsArgs: false,
  172. Callback: func(m *Model, args []string) {
  173. // TODO: implement m.BM.Current().remove...
  174. },
  175. },
  176. Action{
  177. Binding: "backspace",
  178. Callback: func(m *Model, args []string) {
  179. m.BM.Current().Delete()
  180. },
  181. },
  182. Action{
  183. Binding: "enter",
  184. Callback: func(m *Model, args []string) {
  185. m.BM.Current().Insert('\n')
  186. },
  187. },
  188. },
  189. Palette: []Action{
  190. Action{
  191. Binding: "backspace",
  192. Callback: func(m *Model, args []string) {
  193. m.BM.PaletteBuffer.Delete()
  194. },
  195. },
  196. Action{
  197. Binding: "enter",
  198. Callback: func(m *Model, _ []string) {
  199. input := m.BM.PaletteBuffer.String()
  200. args := strings.Split(input, " ")
  201. if args[0] == "" {
  202. m.Mode = ModeNormal
  203. return
  204. }
  205. cmd := string([]rune(args[0])[1:])
  206. if strings.HasPrefix(args[0], "/") {
  207. for _, action := range append(m.AM.Common, append(m.AM.Normal, m.AM.Insert...)...) {
  208. if action.Command != "" && cmd == action.Command {
  209. m.Mode = ModeNormal
  210. action.Callback(m, args[1:])
  211. return
  212. }
  213. }
  214. m.Mode = ModeNormal
  215. m.BM.PaletteBuffer.Clear()
  216. m.BM.PaletteBuffer.Paste("moose.error:Unknown command \"" + cmd + "\"")
  217. } else {
  218. m.Mode = ModeNormal
  219. m.BM.PaletteBuffer.Clear()
  220. m.BM.PaletteBuffer.Paste("moose.error:Unknown palette input \"" + cmd + "\"")
  221. }
  222. },
  223. },
  224. },
  225. }
  226. }