From c383c81178ab5e89626c6fb12be8587402f82fe7 Mon Sep 17 00:00:00 2001 From: Felix Angell Date: Sun, 6 May 2018 15:14:46 +0100 Subject: [PATCH] hacked in a visual indicator for unsaved file; closes #65 --- gui/buffer.go | 29 +++++++++++++++++++++++++++++ gui/buffer_pane.go | 7 ++++++- gui/shortcuts.go | 8 ++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/gui/buffer.go b/gui/buffer.go index 1facd8f..d873a39 100644 --- a/gui/buffer.go +++ b/gui/buffer.go @@ -55,6 +55,7 @@ type Buffer struct { filePath string languageInfo *cfg.LanguageSyntaxConfig ex, ey int + modified bool } func NewBuffer(conf *cfg.TomlConfig, buffOpts BufferConfig, parent *View, index int) *Buffer { @@ -135,9 +136,16 @@ func (b *Buffer) OpenFile(filePath string) { for _, line := range lines { b.appendLine(line) } + + // because appendLine sets modified to true + // we should reset this to false since weve + // only loaded a file. + b.modified = false } func (b *Buffer) setLine(idx int, val string) { + b.modified = true + b.contents[idx] = rope.New(val) if b.curs.y == idx { b.moveToEndOfLine() @@ -145,6 +153,8 @@ func (b *Buffer) setLine(idx int, val string) { } func (b *Buffer) appendLine(val string) { + b.modified = true + b.contents = append(b.contents, rope.New(val)) // because we've added a new line // we have to set the x to the start @@ -153,6 +163,8 @@ func (b *Buffer) appendLine(val string) { // inserts a string, handling all of the newlines etc func (b *Buffer) insertString(idx int, val string) { + b.modified = true + lines := strings.Split(val, "\n") for _, l := range lines { @@ -165,6 +177,8 @@ func (b *Buffer) insertString(idx int, val string) { } func (b *Buffer) insertRune(r rune) { + b.modified = true + log.Println("Inserting rune ", r, " into current line at ", b.curs.x, ":", b.curs.y) log.Println("Line before insert> ", b.contents[b.curs.y]) @@ -224,6 +238,9 @@ var altAlternative = map[rune]rune{ } func (b *Buffer) deleteLine() { + // HACK FIXME + b.modified = true + if len(b.contents) > 1 { b.contents = remove(b.contents, b.curs.y) } else { @@ -313,6 +330,9 @@ func (b *Buffer) processTextInput(r rune) bool { } } + // HACK FIXME + b.modified = true + b.contents[b.curs.y] = b.contents[b.curs.y].Insert(b.curs.x, string(r)) b.curs.move(1, 0) @@ -584,6 +604,9 @@ func (b *Buffer) processActionKey(key int) bool { // nicely indented! } + // HACK FIXME + b.modified = true + initial_x := b.curs.x prevLineLen := b.contents[b.curs.y].Len() @@ -621,6 +644,9 @@ func (b *Buffer) processActionKey(key int) bool { b.contents[b.curs.y] = newRope case sdl.K_BACKSPACE: + // HACK FIXME + b.modified = true + if SUPER_DOWN { b.deleteBeforeCursor() } else { @@ -738,6 +764,9 @@ func (b *Buffer) processActionKey(key int) bool { } case sdl.K_TAB: + // HACK FIXME + b.modified = true + if b.cfg.Editor.Tabs_Are_Spaces { // make an empty rune array of TAB_SIZE, cast to string // and insert it. diff --git a/gui/buffer_pane.go b/gui/buffer_pane.go index 3d9466c..624b13d 100644 --- a/gui/buffer_pane.go +++ b/gui/buffer_pane.go @@ -43,7 +43,12 @@ func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) { } { - infoLine := fmt.Sprintf("Line %d, Column %d", b.Buff.curs.y, b.Buff.curs.x) + modified := ' ' + if b.Buff.modified { + modified = '*' + } + + infoLine := fmt.Sprintf("%s%c Line %d, Column %d", b.Buff.filePath, modified, b.Buff.curs.y, b.Buff.curs.x) ctx.SetColor(strife.HexRGB(conf.Suggestion.Foreground)) _, strHeight := ctx.String(infoLine, b.x+pad, mpY+(pad/2)+1) metaPanelHeight = strHeight + pad diff --git a/gui/shortcuts.go b/gui/shortcuts.go index 03fa5e5..13387a9 100644 --- a/gui/shortcuts.go +++ b/gui/shortcuts.go @@ -64,10 +64,18 @@ func Save(v *View, commands []string) bool { // - lots of checks to do here: does the file exist/not exist // handle the errors... etc. + // ALWAYS use atomic save possibly? + // start writing the file on a gothread + // and then move the file overwriting when the + // file has been written? + err := ioutil.WriteFile(b.filePath, buffer.Bytes(), 0775) if err != nil { log.Println(err.Error()) } log.Println("Wrote file '" + b.filePath + "' to disk") + + b.modified = false + return false }