From e5988c173d001ce8e747dfabb5f290435ea26167 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 20 Nov 2023 15:39:27 -0500 Subject: [PATCH] Add status bar --- src/common/ansi.ts | 2 ++ src/common/editor.ts | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/common/ansi.ts b/src/common/ansi.ts index d4d5873..3a4ba13 100644 --- a/src/common/ansi.ts +++ b/src/common/ansi.ts @@ -28,6 +28,8 @@ export const Ansi = { HideCursor: ANSI_PREFIX + '?25l', ShowCursor: ANSI_PREFIX + '?25h', GetCursorLocation: ANSI_PREFIX + '6n', + InvertColor: ANSI_PREFIX + '7m', + ResetFormatting: ANSI_PREFIX + 'm', moveCursor: function moveCursor(row: number, col: number): string { // Convert to 1-based counting row++; diff --git a/src/common/editor.ts b/src/common/editor.ts index 51b1bba..1261276 100644 --- a/src/common/editor.ts +++ b/src/common/editor.ts @@ -1,7 +1,14 @@ import Ansi, { KeyCommand } from './ansi.ts'; import Buffer from './buffer.ts'; import Document, { Row } from './document.ts'; -import { IPoint, ITerminalSize, logToFile, maxAdd, VERSION } from './mod.ts'; +import { + IPoint, + ITerminalSize, + logToFile, + maxAdd, + truncate, + VERSION, +} from './mod.ts'; import { ctrlKey, posSub } from './utils.ts'; export class Editor { @@ -34,10 +41,16 @@ export class Editor { * @private */ #render: IPoint; + /** + * The name of the currently open file + * @private + */ + #filename: string; constructor(terminalSize: ITerminalSize) { this.#buffer = new Buffer(); this.#screen = terminalSize; + this.#screen.rows -= 1; this.#cursor = { x: 0, y: 0, @@ -52,6 +65,7 @@ export class Editor { }; this.#document = Document.empty(); + this.#filename = ''; } private get currentRow(): Row | null { @@ -60,6 +74,7 @@ export class Editor { public async open(filename: string): Promise { await this.#document.open(filename); + this.#filename = filename; return this; } @@ -198,6 +213,7 @@ export class Editor { this.#buffer.append(Ansi.HideCursor); this.#buffer.append(Ansi.ResetCursor); this.drawRows(); + this.drawStatusBar(); this.#buffer.append( Ansi.moveCursor( this.#cursor.y - this.#offset.y, @@ -226,9 +242,7 @@ export class Editor { this.drawFileRow(filerow); } - if (y < this.#screen.rows - 1) { - this.#buffer.appendLine(); - } + this.#buffer.appendLine(); } } @@ -266,4 +280,24 @@ export class Editor { this.#buffer.append('~'); } } + + private drawStatusBar(): void { + this.#buffer.append(Ansi.InvertColor); + const name = (this.#filename !== '') ? this.#filename : '[No Name]'; + const status = `${truncate(name, 20)} - ${this.#document.numRows} lines`; + const rstatus = `${this.#cursor.y + 1}/${this.#document.numRows}`; + let len = Math.min(status.length, this.#screen.cols); + this.#buffer.append(status, len); + + while (len < this.#screen.cols) { + if (this.#screen.cols - len === rstatus.length) { + this.#buffer.append(rstatus); + break; + } else { + this.#buffer.append(' '); + len++; + } + } + this.#buffer.append(Ansi.ResetFormatting); + } }