Add status bar

This commit is contained in:
Timothy Warren 2023-11-20 15:39:27 -05:00
parent 4df0c70c32
commit e5988c173d
2 changed files with 40 additions and 4 deletions

View File

@ -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++;

View File

@ -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<Editor> {
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,11 +242,9 @@ export class Editor {
this.drawFileRow(filerow);
}
if (y < this.#screen.rows - 1) {
this.#buffer.appendLine();
}
}
}
private drawFileRow(y: number): void {
const row = this.#document.row(y);
@ -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);
}
}