From 571ac3b3fac4a4f399aef95bc22301039b43bfda Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Wed, 23 Mar 2016 21:09:27 -0400 Subject: [PATCH] Add ctrlo for open file --- src/view.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/view.go b/src/view.go index b97b7986..d191764a 100644 --- a/src/view.go +++ b/src/view.go @@ -3,6 +3,7 @@ package main import ( "github.com/atotto/clipboard" "github.com/gdamore/tcell" + "io/ioutil" "os" "strconv" "strings" @@ -286,6 +287,22 @@ func (v *View) HandleEvent(event tcell.Event) int { v.cursor.y = 0 v.cursor.loc = 0 ret = 2 + case tcell.KeyCtrlO: + // Open file + if v.buf.IsDirty() { + quit, canceled := v.m.Prompt("You have unsaved changes. Continue? ") + if !canceled { + if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" { + return v.OpenFile() + } else { + return 2 + } + } else { + return 2 + } + } else { + return v.OpenFile() + } case tcell.KeyPgUp: // Page up v.PageUp() @@ -385,6 +402,22 @@ func (v *View) HandleEvent(event tcell.Event) int { return ret } +// OpenFile Prompts the user for a filename and opens the file in the current buffer +func (v *View) OpenFile() int { + filename, canceled := v.m.Prompt("File to open: ") + if canceled { + return 2 + } + file, err := ioutil.ReadFile(filename) + + if err != nil { + v.m.Error(err.Error()) + return 2 + } + v.buf = NewBuffer(string(file), filename) + return 2 +} + // Display renders the view to the screen func (v *View) Display() { var x int