From e4dd07aeadcd87ecd62ddb49b0c8de97e99a075d Mon Sep 17 00:00:00 2001 From: Felix Angell Date: Mon, 14 May 2018 18:35:23 +0100 Subject: [PATCH] editor now complains about unsaved files being closed; added exit command properly --- gui/action.go | 7 +------ gui/close_buffer.go | 20 ++++++++++++++++++-- gui/exit_action.go | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 gui/exit_action.go diff --git a/gui/action.go b/gui/action.go index 12a6870..8523516 100644 --- a/gui/action.go +++ b/gui/action.go @@ -2,7 +2,6 @@ package gui import ( "log" - "os" "strconv" ) @@ -115,9 +114,5 @@ var actions = map[string]BufferAction{ "close_buffer": NewBufferAction("close_buffer", CloseBuffer), "paste": NewBufferAction("paste", Paste), "show_palette": NewBufferAction("show_palette", ShowPalette), - "exit": NewBufferAction("exit", func(*View, []string) bool { - // TODO do this properly lol - os.Exit(0) - return false - }), + "exit": NewBufferAction("exit", ExitPhi), } diff --git a/gui/close_buffer.go b/gui/close_buffer.go index 35a9f39..56b6e0b 100644 --- a/gui/close_buffer.go +++ b/gui/close_buffer.go @@ -1,14 +1,30 @@ package gui +import ( + "fmt" + + "github.com/sqweek/dialog" +) + func CloseBuffer(v *View, commands []string) bool { b := v.getCurrentBuff() if b == nil { return false } + // TODO eventually we should have our own + // little dialog IO message thingies. if b.modified { - // do command palette thing! - return false + // TODO basename? + text := fmt.Sprintf("Do you want to save the changes you made to %s?", b.filePath) + + dontSave := dialog.Message("%s", text).YesNo() + if !dontSave { + return false + } + + // save the buffer! + Save(v, []string{}) } v.removeBuffer(b.index) diff --git a/gui/exit_action.go b/gui/exit_action.go new file mode 100644 index 0000000..57c577d --- /dev/null +++ b/gui/exit_action.go @@ -0,0 +1,20 @@ +package gui + +import ( + "log" + "os" +) + +func ExitPhi(v *View, commands []string) bool { + // todo this probably wont work... + // would also be nice to have a thing + // that asks if we want to save all buffers + // rather than going thru each one specifically? + for i, _ := range v.buffers { + CloseBuffer(v, []string{}) + log.Println("Closing buffer ", i) + } + + os.Exit(0) + return false +}