mirror of
https://github.com/EnumeratedDev/typer.git
synced 2026-09-21 07:36:11 +00:00
Compare commits
4
Commits
b564e0ec2e
...
2f1978e6da
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f1978e6da
|
||
|
|
fcde25c28a
|
||
|
|
0e18083929
|
||
|
|
c2eacac313
|
+20
-11
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -118,12 +117,24 @@ func (buffer *Buffer) Load() error {
|
||||
buffer.filename = filepath.Join(homedir, buffer.filename[2:])
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(buffer.filename)
|
||||
contentBytes, err := os.ReadFile(buffer.filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content := runestring.RuneString(string(contentBytes))
|
||||
|
||||
if len(content) != 0 {
|
||||
buffer.Contents = runestring.Split(content, '\n')
|
||||
|
||||
// Add empty line at end of buffer for last newline
|
||||
if content[len(content)-1] == '\n' {
|
||||
buffer.Contents = append(buffer.Contents, make(runestring.RuneString, 0))
|
||||
}
|
||||
|
||||
buffer.CursorPos.Y = len(buffer.Contents) - 1
|
||||
buffer.CursorPos.X = len(buffer.Contents[buffer.CursorPos.Y])
|
||||
}
|
||||
|
||||
buffer.Contents = runestring.Split(bytes.Runes(content), '\n')
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -160,9 +171,7 @@ func (buffer *Buffer) Save() error {
|
||||
func (buffer *Buffer) GetContentsAsString() runestring.RuneString {
|
||||
finalText := make(runestring.RuneString, 0)
|
||||
for i, line := range buffer.Contents {
|
||||
for _, rune := range line {
|
||||
finalText = append(finalText, rune)
|
||||
}
|
||||
finalText = append(finalText, line...)
|
||||
|
||||
if i != len(buffer.Contents)-1 {
|
||||
finalText = append(finalText, '\n')
|
||||
@@ -277,6 +286,8 @@ func (buffer *Buffer) CutText(window *Window) (runestring.RuneString, int) {
|
||||
// Remove selected text
|
||||
_, edge2 := buffer.GetSelectionEdges()
|
||||
buffer.CursorPos = edge2
|
||||
buffer.MoveRight(1)
|
||||
|
||||
buffer.Delete(len(cutText))
|
||||
|
||||
// Remove selection
|
||||
@@ -311,7 +322,7 @@ func (buffer *Buffer) PasteText(window *Window, text runestring.RuneString) {
|
||||
absEdge2 = len(buffer.Contents) - 1
|
||||
}
|
||||
|
||||
contents = append(contents[:absEdge1], contents[absEdge2+1:]...)
|
||||
contents = slices.Delete(contents, absEdge1, absEdge2+1)
|
||||
buffer.Contents = runestring.Split(contents, '\n')
|
||||
buffer.CursorPos = buffer.AbsolutePositionToPosition(absEdge1)
|
||||
buffer.Selection = nil
|
||||
@@ -353,8 +364,7 @@ func (buffer *Buffer) FindAndReplaceSubstring(substring, replacement runestring.
|
||||
}
|
||||
|
||||
// Replace substring with replacement string
|
||||
contents = append(contents[:index], replacement...)
|
||||
contents = append(contents, contents[index+len(substring):]...)
|
||||
contents = slices.Insert(contents, index, replacement...)
|
||||
|
||||
buffer.Contents = runestring.Split(contents, '\n')
|
||||
|
||||
@@ -495,8 +505,7 @@ func (buffer *Buffer) Delete(i int) bool {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
line := buffer.Contents[buffer.CursorPos.Y]
|
||||
buffer.Contents[buffer.CursorPos.Y] = append(line[:buffer.CursorPos.X], line[buffer.CursorPos.X+1:]...)
|
||||
buffer.Contents[buffer.CursorPos.Y] = slices.Delete(buffer.Contents[buffer.CursorPos.Y], buffer.CursorPos.X, buffer.CursorPos.X+1)
|
||||
}
|
||||
|
||||
remainingSteps--
|
||||
|
||||
+37
-37
@@ -29,7 +29,7 @@ func initCommands() {
|
||||
selectionEnd: Position{len(window.CurrentBuffer.Contents) - 1, len(lastLine) - 1},
|
||||
}
|
||||
|
||||
PrintMessage(window, "Selected all text.")
|
||||
window.PrintMessage("Selected all text.")
|
||||
},
|
||||
}
|
||||
|
||||
@@ -44,9 +44,9 @@ func initCommands() {
|
||||
|
||||
// Send appropriate message and remove text depending on copying method
|
||||
if copyingMethod == 0 {
|
||||
PrintMessage(window, "Copied line to clipboard.")
|
||||
window.PrintMessage("Copied line to clipboard.")
|
||||
} else {
|
||||
PrintMessage(window, "Copied selection to clipboard.")
|
||||
window.PrintMessage("Copied selection to clipboard.")
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -62,9 +62,9 @@ func initCommands() {
|
||||
|
||||
// Send appropriate message depending on copying method
|
||||
if copyingMethod == 0 {
|
||||
PrintMessage(window, "Copied line to clipboard.")
|
||||
window.PrintMessage("Copied line to clipboard.")
|
||||
} else {
|
||||
PrintMessage(window, "Copied selection to clipboard. ")
|
||||
window.PrintMessage("Copied selection to clipboard. ")
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -74,7 +74,7 @@ func initCommands() {
|
||||
run: func(window *Window, args ...string) {
|
||||
if len(window.Clipboard) != 0 {
|
||||
window.CurrentBuffer.PasteText(window, window.Clipboard)
|
||||
PrintMessage(window, "Pasted text to buffer. ")
|
||||
window.PrintMessage("Pasted text to buffer. ")
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func initCommands() {
|
||||
cmd: "save",
|
||||
run: func(window *Window, args ...string) {
|
||||
if !window.CurrentBuffer.canSave {
|
||||
PrintMessage(window, "Cannot save buffer!")
|
||||
window.PrintMessage("Cannot save buffer!")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func initCommands() {
|
||||
input = <-inputChannel
|
||||
|
||||
if strings.TrimSpace(input) == "" {
|
||||
PrintMessage(window, "No save location was given!")
|
||||
window.PrintMessage("No save location was given!")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -108,12 +108,12 @@ func initCommands() {
|
||||
err := window.CurrentBuffer.Save()
|
||||
if err != nil {
|
||||
|
||||
PrintMessage(window, fmt.Sprintf("Could not save file: %s", err))
|
||||
window.PrintMessage(fmt.Sprintf("Could not save file: %s", err))
|
||||
window.CurrentBuffer.filename = ""
|
||||
return
|
||||
}
|
||||
|
||||
PrintMessage(window, "File saved.")
|
||||
window.PrintMessage("File saved.")
|
||||
}()
|
||||
},
|
||||
autocomplete: func(window *Window, args ...string) []string {
|
||||
@@ -133,16 +133,16 @@ func initCommands() {
|
||||
}
|
||||
|
||||
if openBuffer := GetOpenFileBuffer(input); openBuffer != nil {
|
||||
PrintMessage(window, fmt.Sprintf("File already open! Switching to buffer: %s", openBuffer.Name))
|
||||
window.PrintMessage(fmt.Sprintf("File already open! Switching to buffer: %s", openBuffer.Name))
|
||||
window.CurrentBuffer = openBuffer
|
||||
} else {
|
||||
newBuffer, err := CreateFileBuffer(input, false)
|
||||
if err != nil {
|
||||
PrintMessage(window, fmt.Sprintf("Could not open file: %s", err.Error()))
|
||||
window.PrintMessage(fmt.Sprintf("Could not open file: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
PrintMessage(window, fmt.Sprintf("Opening file at: %s", newBuffer.filename))
|
||||
window.PrintMessage(fmt.Sprintf("Opening file at: %s", newBuffer.filename))
|
||||
window.CurrentBuffer = newBuffer
|
||||
}
|
||||
}()
|
||||
@@ -157,7 +157,7 @@ func initCommands() {
|
||||
log.Fatalf("Could not reload buffer: %s", err)
|
||||
}
|
||||
|
||||
PrintMessage(window, "Buffer reloaded.")
|
||||
window.PrintMessage("Buffer reloaded.")
|
||||
},
|
||||
}
|
||||
|
||||
@@ -174,9 +174,9 @@ func initCommands() {
|
||||
pos := window.CurrentBuffer.FindSubstring(input, window.CurrentBuffer.CursorPos)
|
||||
if pos.X >= 0 && pos.Y >= 0 {
|
||||
window.CurrentBuffer.CursorPos = pos
|
||||
PrintMessage(window, "Match found.")
|
||||
window.PrintMessage("Match found.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", string(input)))
|
||||
window.PrintMessage(fmt.Sprintf("'%s' not found in buffer!", string(input)))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -193,9 +193,9 @@ func initCommands() {
|
||||
pos := window.CurrentBuffer.FindSubstring(input, window.CurrentBuffer.CursorPos)
|
||||
if pos.X >= 0 && pos.Y >= 0 {
|
||||
window.CurrentBuffer.CursorPos = pos
|
||||
PrintMessage(window, "Match found.")
|
||||
window.PrintMessage("Match found.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", string(input)))
|
||||
window.PrintMessage(fmt.Sprintf("'%s' not found in buffer!", string(input)))
|
||||
}
|
||||
}()
|
||||
},
|
||||
@@ -215,9 +215,9 @@ func initCommands() {
|
||||
pos := window.CurrentBuffer.FindAndReplaceSubstring(findStr, replaceStr, window.CurrentBuffer.CursorPos)
|
||||
if pos.X >= 0 && pos.Y >= 0 {
|
||||
window.CurrentBuffer.CursorPos = pos
|
||||
PrintMessage(window, "Match replaced successfully.")
|
||||
window.PrintMessage("Match replaced successfully.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
window.PrintMessage(fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -236,9 +236,9 @@ func initCommands() {
|
||||
pos := window.CurrentBuffer.FindAndReplaceSubstring(findStr, replaceStr, window.CurrentBuffer.CursorPos)
|
||||
if pos.X >= 0 && pos.Y >= 0 {
|
||||
window.CurrentBuffer.CursorPos = pos
|
||||
PrintMessage(window, "Match replaced successfully.")
|
||||
window.PrintMessage("Match replaced successfully.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
window.PrintMessage(fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
}
|
||||
}()
|
||||
},
|
||||
@@ -257,9 +257,9 @@ func initCommands() {
|
||||
|
||||
replacements := window.CurrentBuffer.FindAndReplaceAll(findStr, replaceStr)
|
||||
if replacements > 0 {
|
||||
PrintMessage(window, fmt.Sprintf("Replaced all %d matches successfully.", replacements))
|
||||
window.PrintMessage(fmt.Sprintf("Replaced all %d matches successfully.", replacements))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
window.PrintMessage(fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -277,9 +277,9 @@ func initCommands() {
|
||||
|
||||
replacements := window.CurrentBuffer.FindAndReplaceAll(findStr, replaceStr)
|
||||
if replacements > 0 {
|
||||
PrintMessage(window, fmt.Sprintf("Replaced all %d matches successfully.", replacements))
|
||||
window.PrintMessage(fmt.Sprintf("Replaced all %d matches successfully.", replacements))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
window.PrintMessage(fmt.Sprintf("'%s' not found in buffer!", string(findStr)))
|
||||
}
|
||||
}()
|
||||
},
|
||||
@@ -300,7 +300,7 @@ func initCommands() {
|
||||
}
|
||||
|
||||
window.CurrentBuffer = Buffers[index]
|
||||
PrintMessage(window, fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
|
||||
window.PrintMessage(fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
|
||||
},
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ func initCommands() {
|
||||
}
|
||||
|
||||
window.CurrentBuffer = Buffers[index]
|
||||
PrintMessage(window, fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
|
||||
window.PrintMessage(fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
|
||||
},
|
||||
}
|
||||
|
||||
@@ -335,7 +335,7 @@ func initCommands() {
|
||||
}
|
||||
|
||||
window.CursorMode = CursorModeBuffer
|
||||
PrintMessage(window, fmt.Sprintf("New buffer created with the name '%s'.", window.CurrentBuffer.Name))
|
||||
window.PrintMessage(fmt.Sprintf("New buffer created with the name '%s'.", window.CurrentBuffer.Name))
|
||||
},
|
||||
}
|
||||
|
||||
@@ -354,7 +354,7 @@ func initCommands() {
|
||||
window.CurrentBuffer = Buffers[bufferIndex]
|
||||
}
|
||||
window.CursorMode = CursorModeBuffer
|
||||
PrintMessage(window, "Buffer closed.")
|
||||
window.PrintMessage("Buffer closed.")
|
||||
},
|
||||
}
|
||||
|
||||
@@ -383,14 +383,14 @@ func initCommands() {
|
||||
}
|
||||
|
||||
if _, ok := AvailableStyles[input]; !ok {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
window.PrintMessage(fmt.Sprintf("Could not set style to '%s'", input))
|
||||
return
|
||||
}
|
||||
|
||||
if ok := SetCurrentStyle(window.screen, input); ok {
|
||||
PrintMessage(window, fmt.Sprintf("Setting style to '%s'", input))
|
||||
window.PrintMessage(fmt.Sprintf("Setting style to '%s'", input))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
window.PrintMessage(fmt.Sprintf("Could not set style to '%s'", input))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -405,14 +405,14 @@ func initCommands() {
|
||||
}
|
||||
|
||||
if _, ok := AvailableStyles[input]; !ok {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
window.PrintMessage(fmt.Sprintf("Could not set style to '%s'", input))
|
||||
return
|
||||
}
|
||||
|
||||
if ok := SetCurrentStyle(window.screen, input); ok {
|
||||
PrintMessage(window, fmt.Sprintf("Setting style to '%s'", input))
|
||||
window.PrintMessage(fmt.Sprintf("Setting style to '%s'", input))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
window.PrintMessage(fmt.Sprintf("Could not set style to '%s'", input))
|
||||
}
|
||||
}()
|
||||
},
|
||||
@@ -533,7 +533,7 @@ func RunCommand(window *Window, cmd string, args ...string) bool {
|
||||
command.run(window, args...)
|
||||
return true
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("Could not find command '%s'!", cmd))
|
||||
window.PrintMessage(fmt.Sprintf("Could not find command '%s'!", cmd))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ func main() {
|
||||
for i, file := range os.Args[1:] {
|
||||
b, err := CreateFileBuffer(file, true)
|
||||
if err != nil {
|
||||
PrintMessage(window, "Could not open file: "+file)
|
||||
window.PrintMessage("Could not open file: " + file)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,8 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
type TyperMessage struct {
|
||||
@@ -12,7 +13,7 @@ type TyperMessage struct {
|
||||
|
||||
var messageLog = make([]TyperMessage, 0)
|
||||
|
||||
func PrintMessage(window *Window, message string) {
|
||||
func (window *Window) PrintMessage(message string) {
|
||||
messageLog = append(messageLog, TyperMessage{timestamp: time.Now().UnixMilli(), message: message})
|
||||
|
||||
err := window.screen.PostEvent(tcell.NewEventInterrupt(nil))
|
||||
|
||||
@@ -100,7 +100,6 @@ func readStyles() {
|
||||
for _, stylesPath := range stylesPaths {
|
||||
// Ensure directory exists at path
|
||||
if stat, err := os.Stat(stylesPath); err != nil || !stat.IsDir() {
|
||||
fmt.Println(stylesPath)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ func initTopMenu() {
|
||||
|
||||
d := CreateDropdownMenu(buffersSlice, 0, y, 0, func(i int) {
|
||||
window.CurrentBuffer = Buffers[i]
|
||||
PrintMessage(window, fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
|
||||
window.PrintMessage(fmt.Sprintf("Set current buffer to '%s'.", window.CurrentBuffer.Name))
|
||||
ClearDropdowns()
|
||||
window.CursorMode = CursorModeBuffer
|
||||
})
|
||||
|
||||
+1
-1
@@ -86,7 +86,7 @@ func CreateWindow() (*Window, error) {
|
||||
if ok := SetCurrentStyle(screen, Config.FallbackStyle); !ok {
|
||||
// Use hard-coded fallback style
|
||||
screen.SetStyle(tcell.StyleDefault.Foreground(CurrentStyle.BufferAreaFg).Background(CurrentStyle.BufferAreaBg))
|
||||
PrintMessage(&window, "Could not set style either to selected one nor to fallback one!")
|
||||
window.PrintMessage("Could not set style either to selected one nor to fallback one!")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user