1
0

action.go 5.5 KB

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