Add quit confirmation for modified document
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-02 10:34:19 -04:00
parent 799af21a24
commit 42e50dfebb
2 changed files with 18 additions and 2 deletions

View File

@ -4,5 +4,8 @@ package editor
// !Constants // !Constants
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const KiloVersion = "0.0.1" const (
const KiloTabStop = 4 KiloVersion = "0.0.1"
KiloTabStop = 4
KiloQuitTimes = 3
)

View File

@ -27,6 +27,7 @@ type editor struct {
offset *point offset *point
document *document document *document
status *statusMsg status *statusMsg
quitTimes uint8
renderX int renderX int
} }
@ -50,6 +51,7 @@ func New() *editor {
offset, offset,
document, document,
status, status,
KiloQuitTimes,
0, 0,
} }
} }
@ -80,6 +82,13 @@ func (e *editor) ProcessKeypress() bool {
switch ch { switch ch {
case key.Ctrl('q'): case key.Ctrl('q'):
if e.document.dirty && e.quitTimes > 0 {
e.SetStatusMessage("WARNING!!! File has unsaved changes. Press Ctrl-Q %d more tiems to quite.", e.quitTimes)
e.quitTimes -= 1
return true
}
// Clean up on exit // Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor) terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
@ -113,6 +122,10 @@ func (e *editor) ProcessKeypress() bool {
e.insertChar(ch) e.insertChar(ch)
} }
if e.quitTimes != KiloQuitTimes {
e.quitTimes = KiloQuitTimes
}
return true return true
} }