Skip to content

Commit

Permalink
hacked in a visual indicator for unsaved file; closes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
felixangell committed May 6, 2018
1 parent c03af21 commit c383c81
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
29 changes: 29 additions & 0 deletions gui/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -135,16 +136,25 @@ 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()
}
}

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
Expand All @@ -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 {
Expand All @@ -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])

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion gui/buffer_pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions gui/shortcuts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c383c81

Please sign in to comment.