Selaa lähdekoodia

Change keybinds etc, and terms

Johan Rong 1 kuukausi sitten
vanhempi
sitoutus
76eb0bf3dc

+ 29 - 39
internal/editor/action.go

@@ -2,6 +2,7 @@ package editor
 
 import(
 	"strings"
+	"strconv"
 	"os"
 )
 
@@ -13,10 +14,10 @@ type ActionManager = struct {
 }
 
 type Action = struct {
-	Binding  string
-	Command  string
-	NeedsArgs  bool
-	Callback func(*Model, []string)
+	Binding   string
+	Command   string
+	AskArgs bool
+	Callback  func(*Model, []string)
 }
 
 func DefaultActionManager() ActionManager {
@@ -43,7 +44,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "shift+ctrl+Rune[v]",
 				Command: "paste",
-				NeedsArgs: false,
+				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					m.Screen.GetClipboard()
 				},
@@ -51,22 +52,22 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "ctrl+q",
 				Command: "q",
-				NeedsArgs: true,
+				AskArgs: true,
 				Callback: func(m *Model, args []string) {
 					m.Quit()
 				},
 			},
 			Action{
-				Binding: "ctrl+w",
-				Command: "w",
-				NeedsArgs: false,
+				Binding: "ctrl+s",
+				Command: "s",
+				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					path := ""
 					if len(args) < 1 || args[0] == "" {
 						if m.BM.Current().Path == "" {
 							m.Mode = ModeNormal
 							m.BM.PaletteBuffer.Clear()
-							m.BM.PaletteBuffer.Paste("moose.error:Missing filename argument for write")
+							m.BM.PaletteBuffer.Paste("moose.error:Missing filename argument for save")
 							return
 						}
 					}
@@ -82,39 +83,26 @@ func DefaultActionManager() ActionManager {
 					if err != nil {
 						m.Mode = ModeNormal
 						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Could not write to file \"" + path + "\": " + err.Error())
+						m.BM.PaletteBuffer.Paste("moose.error:Could not save to file \"" + path + "\": " + err.Error())
 						return
 					}
+
+					m.BM.PaletteBuffer.Clear()
+					m.BM.PaletteBuffer.Paste("moose.info:Wrote " + strconv.Itoa(len(data)) + " bytes to \"" + path + "\"")
+					return
 				},
 			},
 			Action{
-				Binding: "shift+ctrl+Rune[w]",
-				Command: "wp",
-				NeedsArgs: true,
-				Callback: func(m *Model, args []string) {
-					if len(args) < 1 || args[0] == "" {
-						m.Mode = ModeNormal
-						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Missing filename argument for write")
-						return
-					}
-
-					data := []byte(m.BM.Current().String())
-					err := os.WriteFile(args[0], data, 0644)
-					if err != nil {
-						m.Mode = ModeNormal
-						m.BM.PaletteBuffer.Clear()
-						m.BM.PaletteBuffer.Paste("moose.error:Could not write to file \"" + args[0] + "\": " + err.Error())
-						return
-					}
-				},
+				Binding: "shift+ctrl+Rune[s]",
+				Command: "s",
+				AskArgs: true,
 			},
 		},
 		Normal:  []Action{
 			Action{
-				Binding: "Rune[i]",
+				Binding: "Rune[w]",
 				Callback: func(m *Model, args []string) {
-					m.Mode = ModeInsert
+					m.Mode = ModeWrite
 				},
 			},
 			Action{
@@ -142,7 +130,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "shift+up",
 				Command: "TODO:",
-				NeedsArgs: false,
+				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					m.BM.Current().AddCursorVert(-1)
 				},
@@ -150,7 +138,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "shift+down",
 				Command: "TODO:",
-				NeedsArgs: false,
+				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					m.BM.Current().AddCursorVert(1)
 				},
@@ -158,7 +146,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "esc",
 				Command: "TODO:",
-				NeedsArgs: false,
+				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					m.BM.Current().ClearCursors()
 				},
@@ -166,7 +154,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "tab",
 				Command: "TODO:",
-				NeedsArgs: false,
+				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					for _ = range 4 {
 						m.BM.Current().Insert(' ')
@@ -176,7 +164,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "shift+tab",
 				Command: "TODO:",
-				NeedsArgs: false,
+				AskArgs: false,
 				Callback: func(m *Model, args []string) {
 					// TODO: implement m.BM.Current().remove...
 					
@@ -205,7 +193,7 @@ func DefaultActionManager() ActionManager {
 			Action{
 				Binding: "left",
 				Callback: func(m *Model, args []string) {
-					m.BM.PaletteBuffer.MoveHoriz(-1)	
+					m.BM.PaletteBuffer.MoveHoriz(-1)
 				},
 			},
 			Action{
@@ -228,6 +216,8 @@ func DefaultActionManager() ActionManager {
 					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.Callback == nil { continue }
+
 							if action.Command != "" && cmd == action.Command {
 								m.Mode = ModeNormal
 								action.Callback(m, args[1:])

+ 2 - 2
internal/editor/keyinput.go

@@ -8,7 +8,7 @@ import (
 func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
 	for _, action := range append(m.CurrentActionSet(), m.AM.Common...) {
 		if action.Binding != "" && strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
-			if action.Command != "" && action.NeedsArgs == true {
+			if action.Command != "" && action.AskArgs == true {
 				m.Mode = ModePalette
 				m.BM.PaletteBuffer.Clear()
 				m.BM.PaletteBuffer.Paste("/" + action.Command)
@@ -20,7 +20,7 @@ func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
 		}
 	}
 
-	if m.Mode == ModeInsert {
+	if m.Mode == ModeWrite {
 		if ev.Key() == tcell.KeyRune {
 			for _, r := range ev.Str() {
 				m.BM.Current().Insert(r)

+ 2 - 2
internal/editor/mode.go

@@ -3,14 +3,14 @@ package editor
 type Mode int
 const (
 	ModeNormal Mode = iota
-	ModeInsert
+	ModeWrite
 	ModePalette
 )
 
 func (mode Mode) String() string {
 	switch mode {
 	case ModeNormal:  return "normal"
-	case ModeInsert:  return "insert"
+	case ModeWrite:  return "write"
 	case ModePalette: return "palette"
 	}
 

+ 1 - 1
internal/editor/model.go

@@ -34,7 +34,7 @@ func NewModel(screen tcell.Screen, style tcell.Style) Model {
 func (m *Model) CurrentActionSet() []Action {
 	switch m.Mode {
 	case ModeNormal:  return m.AM.Normal
-	case ModeInsert:  return m.AM.Insert
+	case ModeWrite:  return m.AM.Insert
 	case ModePalette: return m.AM.Palette
 	}
 

+ 24 - 10
internal/editor/render.go

@@ -3,7 +3,7 @@ package editor
 import (
 	"strings"
 	"moose/internal/buffer"
-	"github.com/gdamore/tcell/v3"
+	"github.com/gdamore/tcell/v3/color"
 )
 
 func (m Model) Render() {
@@ -27,15 +27,17 @@ func (m Model) Render() {
 			m.Screen.PutStrStyled(bufWidth*i, j, string(r), m.Style)
 		}
 
-		for _, cur := range buf.CM.Cursors {
-			line, col := buffer.LineCol(buf.Rope, cur.Offset)
-			if line + 1 > maxHeight { continue }
-			if col + 1 > bufWidth { continue }
+		if i == m.BM.CurrentIdx || m.Mode == ModePalette {
+			for _, cur := range buf.CM.Cursors {
+				line, col := buffer.LineCol(buf.Rope, cur.Offset)
+				if line + 1 > maxHeight { continue }
+				if col + 1 > bufWidth { continue }
 
-			if i == m.BM.CurrentIdx && m.Mode != ModePalette {
-				m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))
-			} else {
-				m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true).Foreground(tcell.ColorGray))				
+				if m.Mode == ModeWrite {
+					m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))				
+				} else {
+					m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true).Foreground(color.Gray))	
+				}
 			}
 		}
 	}
@@ -45,6 +47,14 @@ func (m Model) Render() {
 	}
 
 	modeStr := m.Mode.String()
+	if m.Mode == ModePalette {
+		if strings.HasPrefix(m.BM.PaletteBuffer.String(), "/") {
+			modeStr += " (cmd)"
+		} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "?") {
+			modeStr += " (find)"
+		}
+	}
+
 	m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
 
 	if m.Mode == ModePalette {
@@ -57,7 +67,11 @@ func (m Model) Render() {
 
 			m.Screen.SetContent(col, maxHeight + 1, ' ', nil, m.Style.Reverse(true))
 		}
+	} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.info:") {
+		m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Style.Foreground(color.LightGray))
+	} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.warn:") {
+		m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Style.Foreground(color.Orange))
 	} 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))
+		m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(color.Red))
 	}
 }

+ 2 - 1
main.go

@@ -5,6 +5,7 @@ import (
 	"os"
 	"moose/internal/editor"
 	"github.com/gdamore/tcell/v3"
+	"github.com/gdamore/tcell/v3/color"
 )
 
 func main() {
@@ -18,7 +19,7 @@ func main() {
 		os.Exit(1)
 	}
 
-	style := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
+	style := tcell.StyleDefault.Background(color.Reset).Foreground(color.Reset)
 	s.SetStyle(style)
 
 	m := editor.NewModel(s, style)