From 6b3ef29515c5b9841d2f4db0b0b34544c154d733 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sat, 7 Jun 2025 21:57:26 +0300 Subject: [PATCH] Implement File->Save dropdown option --- src/input_bar.go | 6 ++++-- src/top_menu.go | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/input_bar.go b/src/input_bar.go index b9170b9..d0a9653 100644 --- a/src/input_bar.go +++ b/src/input_bar.go @@ -13,10 +13,10 @@ type TyperInputRequest struct { var currentInputRequest *TyperInputRequest -func RequestInput(window *Window, text string) chan string { +func RequestInput(window *Window, text string, defaultInput string) chan string { request := &TyperInputRequest{ Text: text, - input: "", + input: defaultInput, cursorPos: 0, inputChannel: make(chan string), } @@ -25,6 +25,8 @@ func RequestInput(window *Window, text string) chan string { window.CursorMode = CursorModeInputBar + _ = window.screen.PostEvent(tcell.NewEventInterrupt(nil)) + return request.inputChannel } diff --git a/src/top_menu.go b/src/top_menu.go index 313fc16..c4c45b4 100644 --- a/src/top_menu.go +++ b/src/top_menu.go @@ -38,10 +38,40 @@ func initTopMenu() { window.SetCursorPos(0) window.CursorMode = CursorModeBuffer case 1: - _ = RequestInput(window, "Save buffer to:") - PrintMessage(window, "Input requested...") + if !window.textArea.CurrentBuffer.canSave { + PrintMessage(window, "Cannot save this buffer!") + return + } + + inputChannel := RequestInput(window, "Save file [y\\N]:", "") + go func() { + input := <-inputChannel + + if strings.ToLower(input) != "y" && strings.ToLower(input) != "yes" { + return + } + + inputChannel = RequestInput(window, "Save buffer to:", window.textArea.CurrentBuffer.filename) + + input = <-inputChannel + + if strings.TrimSpace(input) == "" { + PrintMessage(window, "No save location was given!") + return + } + + window.textArea.CurrentBuffer.filename = strings.TrimSpace(input) + err := window.textArea.CurrentBuffer.Save() + if err != nil { + PrintMessage(window, fmt.Sprintf("Could not save file: %s", err)) + window.textArea.CurrentBuffer.filename = "" + return + } + + PrintMessage(window, "File saved.") + }() case 2: - inputChannel := RequestInput(window, "File to open:") + inputChannel := RequestInput(window, "File to open:", "") go func() { input := <-inputChannel