From 2f1978e6da04ce50346942fe3a45862c4eeaf4fa Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 28 Apr 2026 21:34:58 +0300 Subject: [PATCH] Turn PrintMessage into receiver function --- src/command.go | 74 +++++++++++++++++++++++----------------------- src/main.go | 2 +- src/message_bar.go | 5 ++-- src/top_menu.go | 2 +- src/window.go | 2 +- 5 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/command.go b/src/command.go index 9f2084f..6a90eb7 100644 --- a/src/command.go +++ b/src/command.go @@ -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 } } diff --git a/src/main.go b/src/main.go index 51e3a88..73707c8 100644 --- a/src/main.go +++ b/src/main.go @@ -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 } diff --git a/src/message_bar.go b/src/message_bar.go index 976c762..aa16e97 100644 --- a/src/message_bar.go +++ b/src/message_bar.go @@ -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)) diff --git a/src/top_menu.go b/src/top_menu.go index 70b878d..d980d27 100644 --- a/src/top_menu.go +++ b/src/top_menu.go @@ -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 }) diff --git a/src/window.go b/src/window.go index be127df..3eedd1f 100644 --- a/src/window.go +++ b/src/window.go @@ -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!") } }