diff --git a/src/common/document.ts b/src/common/document.ts index 15cc3d5..9b83a16 100644 --- a/src/common/document.ts +++ b/src/common/document.ts @@ -74,6 +74,8 @@ export class Document { row.delete(at.x); row.updateRender(); + + this.dirty = true; } public row(i: number): Row | null { diff --git a/src/common/editor.ts b/src/common/editor.ts index 8e22843..3a941f5 100644 --- a/src/common/editor.ts +++ b/src/common/editor.ts @@ -1,7 +1,13 @@ import Ansi, { KeyCommand } from './ansi.ts'; import Buffer from './buffer.ts'; import Document from './document.ts'; -import { ITerminalSize, logToFile, Position, SCROLL_VERSION } from './mod.ts'; +import { + ITerminalSize, + logToFile, + Position, + SCROLL_QUIT_TIMES, + SCROLL_VERSION, +} from './mod.ts'; import Row from './row.ts'; import { ctrlKey, maxAdd, posSub, truncate } from './utils.ts'; @@ -50,6 +56,11 @@ class Editor { * @private */ #statusTimeout: number = 0; + /** + * The number of times required to quit a dirty document + * @private + */ + #quitTimes: number = SCROLL_QUIT_TIMES; constructor(terminalSize: ITerminalSize) { this.#buffer = new Buffer(); @@ -95,6 +106,14 @@ class Editor { break; case ctrlKey('q'): + if (this.#quitTimes > 0 && this.#document.dirty) { + this.setStatusMessage( + 'WARNING!!! File has unsaved changes. ' + + `Press Ctrl-Q ${this.#quitTimes} more times to quit.`, + ); + this.#quitTimes--; + return true; + } await this.clearScreen(); return false; @@ -165,6 +184,11 @@ class Editor { this.#cursor.x++; } + if (this.#quitTimes < SCROLL_QUIT_TIMES) { + this.#quitTimes = SCROLL_QUIT_TIMES; + this.setStatusMessage(''); + } + return true; } diff --git a/src/deno/ffi.ts b/src/deno/ffi.ts index 01c8786..c0fe557 100644 --- a/src/deno/ffi.ts +++ b/src/deno/ffi.ts @@ -39,7 +39,13 @@ const DenoFFI: IFFI = { tcsetattr, cfmakeraw, getPointer: Deno.UnsafePointer.of, - close: cStdLib.close, + close: () => { + try { + cStdLib.close(); + } catch { + // The error thrown is annoying, but harmless. + } + }, }; export default DenoFFI;