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
// ----------------------------------------------------------------------------
const KiloVersion = "0.0.1"
const KiloTabStop = 4
const (
KiloVersion = "0.0.1"
KiloTabStop = 4
KiloQuitTimes = 3
)

View File

@ -27,6 +27,7 @@ type editor struct {
offset *point
document *document
status *statusMsg
quitTimes uint8
renderX int
}
@ -50,6 +51,7 @@ func New() *editor {
offset,
document,
status,
KiloQuitTimes,
0,
}
}
@ -80,6 +82,13 @@ func (e *editor) ProcessKeypress() bool {
switch ch {
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
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
@ -113,6 +122,10 @@ func (e *editor) ProcessKeypress() bool {
e.insertChar(ch)
}
if e.quitTimes != KiloQuitTimes {
e.quitTimes = KiloQuitTimes
}
return true
}