1
0

action.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. },
  109. Insert: []Action{
  110. Action{
  111. Binding: "up",
  112. Callback: func(m *Model, args []string) {
  113. m.BM.Current().MoveVert(-1)
  114. },
  115. },
  116. Action{
  117. Binding: "down",
  118. Callback: func(m *Model, args []string) {
  119. m.BM.Current().MoveVert(1)
  120. },
  121. },
  122. Action{
  123. Binding: "shift+up",
  124. Command: "TODO:",
  125. AskArgs: false,
  126. Callback: func(m *Model, args []string) {
  127. m.BM.Current().AddCursorVert(-1)
  128. },
  129. },
  130. Action{
  131. Binding: "shift+down",
  132. Command: "TODO:",
  133. AskArgs: false,
  134. Callback: func(m *Model, args []string) {
  135. m.BM.Current().AddCursorVert(1)
  136. },
  137. },
  138. Action{
  139. Binding: "esc",
  140. Command: "TODO:",
  141. AskArgs: false,
  142. Callback: func(m *Model, args []string) {
  143. m.BM.Current().ClearCursors()
  144. },
  145. },
  146. Action{
  147. Binding: "tab",
  148. Command: "TODO:",
  149. AskArgs: false,
  150. Callback: func(m *Model, args []string) {
  151. for _ = range 4 {
  152. m.BM.Current().Insert(' ')
  153. }
  154. },
  155. },
  156. Action{
  157. Binding: "shift+tab",
  158. Command: "TODO:",
  159. AskArgs: false,
  160. Callback: func(m *Model, args []string) {
  161. // TODO: implement m.BM.Current().remove...
  162. },
  163. },
  164. Action{
  165. Binding: "backspace",
  166. Callback: func(m *Model, args []string) {
  167. m.BM.Current().Delete()
  168. },
  169. },
  170. Action{
  171. Binding: "enter",
  172. Callback: func(m *Model, args []string) {
  173. m.BM.Current().Insert('\n')
  174. },
  175. },
  176. },
  177. Palette: []Action{
  178. Action{
  179. Binding: "backspace",
  180. Callback: func(m *Model, args []string) {
  181. m.BM.PaletteBuffer.Delete()
  182. },
  183. },
  184. Action{
  185. Binding: "left",
  186. Callback: func(m *Model, args []string) {
  187. m.BM.PaletteBuffer.MoveHoriz(-1)
  188. },
  189. },
  190. Action{
  191. Binding: "right",
  192. Callback: func(m *Model, args []string) {
  193. m.BM.PaletteBuffer.MoveHoriz(1)
  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.Callback == nil { continue }
  209. if action.Command != "" && cmd == action.Command {
  210. m.Mode = ModeNormal
  211. action.Callback(m, args[1:])
  212. return
  213. }
  214. }
  215. m.Mode = ModeNormal
  216. m.BM.PaletteBuffer.Clear()
  217. m.BM.PaletteBuffer.Paste("moose.error:Unknown command \"" + cmd + "\"")
  218. } else {
  219. m.Mode = ModeNormal
  220. m.BM.PaletteBuffer.Clear()
  221. m.BM.PaletteBuffer.Paste("moose.error:Unknown palette input \"" + cmd + "\"")
  222. }
  223. },
  224. },
  225. },
  226. }
  227. }