Require confirmation for quitting a 'dirty' document

This commit is contained in:
Timothy Warren 2023-11-22 11:27:46 -05:00
parent b665ce8ce7
commit 5cd59ba943
3 changed files with 34 additions and 2 deletions

View File

@ -74,6 +74,8 @@ export class Document {
row.delete(at.x); row.delete(at.x);
row.updateRender(); row.updateRender();
this.dirty = true;
} }
public row(i: number): Row | null { public row(i: number): Row | null {

View File

@ -1,7 +1,13 @@
import Ansi, { KeyCommand } from './ansi.ts'; import Ansi, { KeyCommand } from './ansi.ts';
import Buffer from './buffer.ts'; import Buffer from './buffer.ts';
import Document from './document.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 Row from './row.ts';
import { ctrlKey, maxAdd, posSub, truncate } from './utils.ts'; import { ctrlKey, maxAdd, posSub, truncate } from './utils.ts';
@ -50,6 +56,11 @@ class Editor {
* @private * @private
*/ */
#statusTimeout: number = 0; #statusTimeout: number = 0;
/**
* The number of times required to quit a dirty document
* @private
*/
#quitTimes: number = SCROLL_QUIT_TIMES;
constructor(terminalSize: ITerminalSize) { constructor(terminalSize: ITerminalSize) {
this.#buffer = new Buffer(); this.#buffer = new Buffer();
@ -95,6 +106,14 @@ class Editor {
break; break;
case ctrlKey('q'): 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(); await this.clearScreen();
return false; return false;
@ -165,6 +184,11 @@ class Editor {
this.#cursor.x++; this.#cursor.x++;
} }
if (this.#quitTimes < SCROLL_QUIT_TIMES) {
this.#quitTimes = SCROLL_QUIT_TIMES;
this.setStatusMessage('');
}
return true; return true;
} }

View File

@ -39,7 +39,13 @@ const DenoFFI: IFFI = {
tcsetattr, tcsetattr,
cfmakeraw, cfmakeraw,
getPointer: Deno.UnsafePointer.of, getPointer: Deno.UnsafePointer.of,
close: cStdLib.close, close: () => {
try {
cStdLib.close();
} catch {
// The error thrown is annoying, but harmless.
}
},
}; };
export default DenoFFI; export default DenoFFI;