Kaynağa Gözat

More command stuff

Johan Rong 1 ay önce
ebeveyn
işleme
0dd75ead05

+ 1 - 1
internal/buffer/buffer.go

@@ -9,7 +9,7 @@ import (
 type BufferManager struct {
 	Buffers       []Buffer
 	CurrentIdx    int
-	CommandBuffer Buffer
+	PaletteBuffer Buffer
 }
 
 func (bm *BufferManager) Current() *Buffer {

+ 17 - 11
internal/editor/action.go

@@ -8,7 +8,7 @@ type ActionManager = struct {
 	Common  []Action
 	Normal  []Action
 	Insert  []Action
-	Command []Action
+	Palette []Action
 }
 
 type Action = struct {
@@ -66,9 +66,9 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "Rune[q]",
 				Callback: func(m *Model, args []string) {
-					m.Mode = ModeCommand
-					m.BM.CommandBuffer.Clear()
-					m.BM.CommandBuffer.Insert('/')
+					m.Mode = ModePalette
+					m.BM.PaletteBuffer.Clear()
+					m.BM.PaletteBuffer.Insert('/')
 				},
 			},
 		},
@@ -141,17 +141,17 @@ func DefaultActionManager() ActionManager {
 				},
 			},
 		},
-		Command: []Action{
+		Palette: []Action{
 			Action{
 				Binding: "backspace",
 				Callback: func(m *Model, args []string) {
-					m.BM.CommandBuffer.Delete()
+					m.BM.PaletteBuffer.Delete()
 				},
 			},
 			Action{
 				Binding: "enter",
 				Callback: func(m *Model, _ []string) {
-					input := m.BM.CommandBuffer.String()
+					input := m.BM.PaletteBuffer.String()
 					args := strings.Split(input, " ")
 
 					if args[0] == "" {
@@ -159,18 +159,24 @@ func DefaultActionManager() ActionManager {
 						return
 					}
 
+					cmd := string([]rune(args[0])[1:])
 					if strings.HasPrefix(args[0], "/") {
 						for _, action := range append(m.AM.Common, append(m.AM.Normal, m.AM.Insert...)...) {
-							if action.Command != "" && string([]rune(args[0])[1:]) == action.Command {
+							if action.Command != "" && cmd == action.Command {
 								m.Mode = ModeNormal
 								action.Callback(m, args[1:])
 								return
 							}
 						}
-					}
 
-					m.Mode = ModeNormal
-					// TODO: Write an error to the commandbuffer 
+						m.Mode = ModeNormal
+						m.BM.PaletteBuffer.Clear()
+						m.BM.PaletteBuffer.Paste("moose.error:Unknown command \"" + cmd + "\"")
+					} else {
+						m.Mode = ModeNormal
+						m.BM.PaletteBuffer.Clear()
+						m.BM.PaletteBuffer.Paste("moose.error:Unknown palette input \"" + cmd + "\"")
+					}
 				},
 			},
 		},

+ 2 - 2
internal/editor/mode.go

@@ -4,14 +4,14 @@ type Mode int
 const (
 	ModeNormal Mode = iota
 	ModeInsert
-	ModeCommand
+	ModePalette
 )
 
 func (mode Mode) String() string {
 	switch mode {
 	case ModeNormal:  return "normal"
 	case ModeInsert:  return "insert"
-	case ModeCommand: return "command"
+	case ModePalette: return "command"
 	}
 
 	return "unknown"

+ 10 - 8
internal/editor/model.go

@@ -23,7 +23,7 @@ func NewModel(screen tcell.Screen, style tcell.Style) Model {
 		BM:            buffer.BufferManager{
 			Buffers:       []buffer.Buffer{buffer.NewBuffer()},
 			CurrentIdx:    0,
-			CommandBuffer: buffer.NewBuffer(),
+			PaletteBuffer: buffer.NewBuffer(),
 		},
 		AM:       DefaultActionManager(),
 		ShouldQuit:    false,
@@ -36,7 +36,7 @@ func (m *Model) CurrentActionSet() []Action {
 	switch m.Mode {
 	case ModeNormal:  return m.AM.Normal
 	case ModeInsert:  return m.AM.Insert
-	case ModeCommand: return m.AM.Command
+	case ModePalette: return m.AM.Palette
 	}
 
 	return nil
@@ -77,8 +77,10 @@ func (m Model) Render() {
 	modeStr := m.Mode.String()
 	m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
 
-	if m.Mode == ModeCommand {
-		m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.CommandBuffer.String(), m.Style)
+	if m.Mode == ModePalette {
+		m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
+	} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
+		m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(tcell.ColorRed))
 	}
 }
 
@@ -86,8 +88,8 @@ func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
 	for _, action := range append(m.AM.Common, m.CurrentActionSet()...) {
 		if action.Binding != "" && strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
 			if action.Command != "" && action.HasArgs == true {
-				m.Mode = ModeCommand
-				m.BM.CommandBuffer.Paste(action.Command)
+				m.Mode = ModePalette
+				m.BM.PaletteBuffer.Paste(action.Command)
 				return
 			}
 
@@ -103,10 +105,10 @@ func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
 			}
 		}
 	}
-	if m.Mode == ModeCommand {
+	if m.Mode == ModePalette {
 		if ev.Key() == tcell.KeyRune {
 			for _, r := range ev.Str() {
-				m.BM.CommandBuffer.Insert(r)
+				m.BM.PaletteBuffer.Insert(r)
 			}
 		}
 	}