Complete the text viewer functionality

This commit is contained in:
Timothy Warren 2023-11-21 11:35:56 -05:00
parent c9b37c205c
commit 35f949c4b5
2 changed files with 31 additions and 7 deletions

View File

@ -33,17 +33,27 @@ export class Editor {
* The scrolling offset for the rendered row * The scrolling offset for the rendered row
* @private * @private
*/ */
#renderX: number; #renderX: number = 0;
/** /**
* The name of the currently open file * The name of the currently open file
* @private * @private
*/ */
#filename: string; #filename: string = '';
/**
* A message to display at the bottom of the screen
* @private
*/
#statusMessage: string = '';
/**
* Timeout for status messages
* @private
*/
#statusTimeout: number = 0;
constructor(terminalSize: ITerminalSize) { constructor(terminalSize: ITerminalSize) {
this.#buffer = new Buffer(); this.#buffer = new Buffer();
this.#screen = terminalSize; this.#screen = terminalSize;
this.#screen.rows -= 1; this.#screen.rows -= 2;
this.#cursor = { this.#cursor = {
x: 0, x: 0,
y: 0, y: 0,
@ -52,10 +62,7 @@ export class Editor {
x: 0, x: 0,
y: 0, y: 0,
}; };
this.#renderX = 0;
this.#document = Document.empty(); this.#document = Document.empty();
this.#filename = '';
} }
private get currentRow(): Row | null { private get currentRow(): Row | null {
@ -195,6 +202,12 @@ export class Editor {
// Terminal Output / Drawing // Terminal Output / Drawing
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public setStatusMessage(msg: string): void {
// TODO: consider some sort of formatting for passed strings
this.#statusMessage = msg;
this.#statusTimeout = Date.now();
}
/** /**
* Clear the screen and write out the buffer * Clear the screen and write out the buffer
*/ */
@ -204,6 +217,7 @@ export class Editor {
this.#buffer.append(Ansi.ResetCursor); this.#buffer.append(Ansi.ResetCursor);
this.drawRows(); this.drawRows();
this.drawStatusBar(); this.drawStatusBar();
this.drawMessageBar();
this.#buffer.append( this.#buffer.append(
Ansi.moveCursor( Ansi.moveCursor(
this.#cursor.y - this.#offset.y, this.#cursor.y - this.#offset.y,
@ -288,6 +302,14 @@ export class Editor {
len++; len++;
} }
} }
this.#buffer.append(Ansi.ResetFormatting); this.#buffer.appendLine(Ansi.ResetFormatting);
}
private drawMessageBar(): void {
this.#buffer.append(Ansi.ClearLine);
const msgLen = this.#statusMessage.length;
if (msgLen > 0 && (Date.now() - this.#statusTimeout < 5000)) {
this.#buffer.append(this.#statusMessage, this.#screen.cols);
}
} }
} }

View File

@ -26,6 +26,8 @@ export async function main() {
// Create the editor itself // Create the editor itself
const editor = new Editor(terminalSize); const editor = new Editor(terminalSize);
editor.setStatusMessage('HELP: Ctrl-Q = quit');
if (term.argv.length > 0) { if (term.argv.length > 0) {
const filename = term.argv[0]; const filename = term.argv[0];
if (filename.trim() !== '') { if (filename.trim() !== '') {