From 799af21a24e1738e92d5016d7d6e16a9428e1732 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 2 Apr 2021 10:07:37 -0400 Subject: [PATCH] Add 'dirty' flag to document --- editor/document.go | 6 ++++++ editor/draw.go | 6 +++++- editor/editor.go | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/editor/document.go b/editor/document.go index 258852a..8c08caf 100644 --- a/editor/document.go +++ b/editor/document.go @@ -36,6 +36,8 @@ func (d *document) open(filename string) { for scanner.Scan() { d.appendRow(scanner.Text()) } + + d.dirty = false } func (d *document) save() int { @@ -67,6 +69,8 @@ func (d *document) save() int { return size } + d.dirty = false + return 0 } @@ -74,6 +78,8 @@ func (d *document) appendRow(s string) { newRow := newRow(s) newRow.update() d.rows = append(d.rows, newRow) + + d.dirty = true } func (d *document) toString() string { diff --git a/editor/draw.go b/editor/draw.go index b7d4711..32f6f75 100644 --- a/editor/draw.go +++ b/editor/draw.go @@ -114,8 +114,12 @@ func (e *editor) drawStatusBar(ab *buffer) { if e.document.filename != "" { fileName = e.document.filename } + modified := "" + if e.document.dirty { + modified = "(modified)" + } - leftStatus := fmt.Sprintf("%.20s - %d lines", fileName, e.document.rowCount()) + leftStatus := fmt.Sprintf("%.20s - %d lines %s", fileName, e.document.rowCount(), modified) length := len(leftStatus) if length > cols { leftStatus = truncate(leftStatus, cols) diff --git a/editor/editor.go b/editor/editor.go index 68630c6..1039f60 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -193,6 +193,7 @@ func (e *editor) insertChar(ch rune) { e.document.rows[e.cursor.y].insertRune(ch, e.cursor.x) e.cursor.x += 1 + e.document.dirty = true } // Convert the raw ANSI escape sequences to the type of key input