2023-11-10 18:22:09 -05:00
|
|
|
import Ansi, { KeyCommand } from './ansi.ts';
|
2023-11-08 18:07:34 -05:00
|
|
|
import Buffer from './buffer.ts';
|
2023-11-21 15:14:08 -05:00
|
|
|
import Document from './document.ts';
|
2023-11-29 16:09:58 -05:00
|
|
|
import Row from './row.ts';
|
|
|
|
|
|
|
|
import { SCROLL_QUIT_TIMES, SCROLL_VERSION } from './config.ts';
|
2023-11-22 11:27:46 -05:00
|
|
|
import {
|
2023-11-29 16:09:58 -05:00
|
|
|
ctrlKey,
|
2023-11-22 17:09:41 -05:00
|
|
|
isControl,
|
2023-11-29 16:09:58 -05:00
|
|
|
maxAdd,
|
|
|
|
posSub,
|
2023-11-27 15:05:48 -05:00
|
|
|
readKey,
|
2023-11-29 16:09:58 -05:00
|
|
|
truncate,
|
|
|
|
} from './fns.ts';
|
|
|
|
import { getRuntime, logToFile } from './runtime.ts';
|
|
|
|
import { ITerminalSize, Position } from './types.ts';
|
2023-11-08 11:11:19 -05:00
|
|
|
|
2023-11-21 15:14:08 -05:00
|
|
|
class Editor {
|
2023-11-21 10:55:23 -05:00
|
|
|
/**
|
|
|
|
* The document being edited
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
#document: Document;
|
2023-11-14 15:53:45 -05:00
|
|
|
/**
|
|
|
|
* The output buffer for the terminal
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-08 11:11:19 -05:00
|
|
|
#buffer: Buffer;
|
2023-11-14 15:53:45 -05:00
|
|
|
/**
|
|
|
|
* The size of the screen in rows/columns
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-10 08:36:18 -05:00
|
|
|
#screen: ITerminalSize;
|
2023-11-14 15:53:45 -05:00
|
|
|
/**
|
|
|
|
* The current location of the mouse cursor
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-21 15:14:08 -05:00
|
|
|
#cursor: Position;
|
2023-11-20 11:12:22 -05:00
|
|
|
/**
|
|
|
|
* The current scrolling offset
|
|
|
|
*/
|
2023-11-21 15:14:08 -05:00
|
|
|
#offset: Position;
|
2023-11-20 15:14:36 -05:00
|
|
|
/**
|
|
|
|
* The scrolling offset for the rendered row
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-21 11:35:56 -05:00
|
|
|
#renderX: number = 0;
|
2023-11-20 15:39:27 -05:00
|
|
|
/**
|
|
|
|
* The name of the currently open file
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-21 11:35:56 -05:00
|
|
|
#filename: string = '';
|
|
|
|
/**
|
|
|
|
* A message to display at the bottom of the screen
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
#statusMessage: string = '';
|
|
|
|
/**
|
|
|
|
* Timeout for status messages
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
#statusTimeout: number = 0;
|
2023-11-22 11:27:46 -05:00
|
|
|
/**
|
|
|
|
* The number of times required to quit a dirty document
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
#quitTimes: number = SCROLL_QUIT_TIMES;
|
2023-11-20 14:21:42 -05:00
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
constructor(terminalSize: ITerminalSize) {
|
2023-11-08 11:11:19 -05:00
|
|
|
this.#buffer = new Buffer();
|
2023-11-21 15:14:08 -05:00
|
|
|
|
|
|
|
// Subtract two rows from the terminal size
|
|
|
|
// for displaying the status bar
|
|
|
|
// and message bar
|
2023-11-10 08:36:18 -05:00
|
|
|
this.#screen = terminalSize;
|
2023-11-21 11:35:56 -05:00
|
|
|
this.#screen.rows -= 2;
|
2023-11-21 15:14:08 -05:00
|
|
|
|
|
|
|
this.#cursor = Position.default();
|
|
|
|
this.#offset = Position.default();
|
2023-11-27 10:25:30 -05:00
|
|
|
this.#document = Document.default();
|
2023-11-14 15:53:45 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 15:14:08 -05:00
|
|
|
private get numRows(): number {
|
|
|
|
return this.#document.numRows;
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:14:36 -05:00
|
|
|
private get currentRow(): Row | null {
|
|
|
|
return this.#document.row(this.#cursor.y);
|
|
|
|
}
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
public async open(filename: string): Promise<Editor> {
|
|
|
|
await this.#document.open(filename);
|
2023-11-20 15:39:27 -05:00
|
|
|
this.#filename = filename;
|
2023-11-14 15:53:45 -05:00
|
|
|
|
|
|
|
return this;
|
2023-11-08 11:11:19 -05:00
|
|
|
}
|
|
|
|
|
2023-11-27 10:25:30 -05:00
|
|
|
public async save(): Promise<void> {
|
|
|
|
if (this.#filename === '') {
|
|
|
|
const filename = await this.prompt('Save as: ');
|
|
|
|
if (filename === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#filename = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.#document.save(this.#filename);
|
|
|
|
this.setStatusMessage(`${this.#filename} was saved to disk.`);
|
|
|
|
}
|
|
|
|
|
2023-11-10 08:36:18 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Command/input mapping
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2023-11-08 11:11:19 -05:00
|
|
|
/**
|
|
|
|
* Determine what to do based on input
|
|
|
|
* @param input - the decoded chunk of stdin
|
|
|
|
*/
|
2023-11-21 15:14:08 -05:00
|
|
|
public async processKeyPress(input: string): Promise<boolean> {
|
2023-11-08 11:11:19 -05:00
|
|
|
switch (input) {
|
2023-11-22 17:09:41 -05:00
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// Ctrl-key chords
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
case ctrlKey('s'):
|
2023-11-27 10:25:30 -05:00
|
|
|
await this.save();
|
2023-11-21 15:14:08 -05:00
|
|
|
break;
|
|
|
|
|
2023-11-16 21:22:24 -05:00
|
|
|
case ctrlKey('q'):
|
2023-11-22 11:27:46 -05:00
|
|
|
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;
|
|
|
|
}
|
2023-11-21 15:14:08 -05:00
|
|
|
await this.clearScreen();
|
2023-11-08 11:11:19 -05:00
|
|
|
return false;
|
|
|
|
|
2023-11-22 17:09:41 -05:00
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// Movement keys
|
|
|
|
// ----------------------------------------------------------------------
|
2023-11-21 15:14:08 -05:00
|
|
|
|
2023-11-10 19:17:36 -05:00
|
|
|
case KeyCommand.Home:
|
|
|
|
this.#cursor.x = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCommand.End:
|
2023-11-20 15:14:36 -05:00
|
|
|
if (this.currentRow !== null) {
|
2023-11-22 17:09:41 -05:00
|
|
|
this.#cursor.x = this.currentRow.size;
|
2023-11-22 11:07:33 -05:00
|
|
|
}
|
2023-11-21 15:14:08 -05:00
|
|
|
break;
|
|
|
|
|
2023-11-10 19:17:36 -05:00
|
|
|
case KeyCommand.PageUp:
|
|
|
|
case KeyCommand.PageDown:
|
|
|
|
{
|
2023-11-20 15:14:36 -05:00
|
|
|
if (input === KeyCommand.PageUp) {
|
|
|
|
this.#cursor.y = this.#offset.y;
|
|
|
|
} else if (input === KeyCommand.PageDown) {
|
|
|
|
this.#cursor.y = maxAdd(
|
|
|
|
this.#offset.y,
|
|
|
|
this.#screen.rows - 1,
|
2023-11-21 15:14:08 -05:00
|
|
|
this.numRows,
|
2023-11-20 15:14:36 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:17:36 -05:00
|
|
|
let times = this.#screen.rows;
|
|
|
|
while (times--) {
|
|
|
|
this.moveCursor(
|
|
|
|
input === KeyCommand.PageUp
|
|
|
|
? KeyCommand.ArrowUp
|
|
|
|
: KeyCommand.ArrowDown,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
case KeyCommand.ArrowUp:
|
|
|
|
case KeyCommand.ArrowDown:
|
|
|
|
case KeyCommand.ArrowRight:
|
|
|
|
case KeyCommand.ArrowLeft:
|
|
|
|
this.moveCursor(input);
|
|
|
|
break;
|
2023-11-21 15:14:08 -05:00
|
|
|
|
2023-11-22 17:09:41 -05:00
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// Text manipulation keys
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
case KeyCommand.Enter:
|
|
|
|
this.#document.insertNewline(this.#cursor);
|
|
|
|
this.#cursor.x = 0;
|
|
|
|
this.#cursor.y++;
|
2023-11-21 15:14:08 -05:00
|
|
|
break;
|
|
|
|
|
2023-11-22 17:09:41 -05:00
|
|
|
case KeyCommand.Delete:
|
|
|
|
this.#document.delete(this.#cursor);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCommand.Backspace:
|
|
|
|
{
|
|
|
|
if (this.#cursor.x > 0 || this.#cursor.y > 0) {
|
|
|
|
this.moveCursor(KeyCommand.ArrowLeft);
|
|
|
|
this.#document.delete(this.#cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// Direct input
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
default: {
|
|
|
|
if (!this.shouldFilter(input)) {
|
|
|
|
this.#document.insert(this.#cursor, input);
|
|
|
|
this.#cursor.x++;
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 08:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-11-22 11:27:46 -05:00
|
|
|
if (this.#quitTimes < SCROLL_QUIT_TIMES) {
|
|
|
|
this.#quitTimes = SCROLL_QUIT_TIMES;
|
|
|
|
this.setStatusMessage('');
|
|
|
|
}
|
|
|
|
|
2023-11-10 08:36:18 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-27 10:25:30 -05:00
|
|
|
public async prompt(p: string): Promise<string | null> {
|
|
|
|
const { term } = await getRuntime();
|
|
|
|
|
|
|
|
let res = '';
|
|
|
|
|
2023-11-27 15:05:48 -05:00
|
|
|
outer: while (true) {
|
2023-11-27 10:25:30 -05:00
|
|
|
this.setStatusMessage(`${p}${res}`);
|
|
|
|
await this.refreshScreen();
|
2023-11-27 15:05:48 -05:00
|
|
|
for await (const chunk of term.inputLoop()) {
|
|
|
|
const char = readKey(chunk);
|
|
|
|
if (char === null) {
|
|
|
|
continue;
|
2023-11-27 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:05:48 -05:00
|
|
|
// End the prompt
|
|
|
|
if (char === KeyCommand.Enter) {
|
|
|
|
this.setStatusMessage('');
|
|
|
|
if (res.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2023-11-27 10:25:30 -05:00
|
|
|
|
2023-11-27 15:05:48 -05:00
|
|
|
// Allow backspacing
|
|
|
|
if (char === KeyCommand.Backspace || char === KeyCommand.Delete) {
|
|
|
|
res = truncate(res, res.length - 1);
|
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to the prompt result
|
|
|
|
if (!isControl(char!)) {
|
|
|
|
res += char;
|
|
|
|
}
|
2023-11-27 10:25:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-22 17:09:41 -05:00
|
|
|
/**
|
|
|
|
* Filter out any additional unwanted keyboard input
|
|
|
|
* @param input
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private shouldFilter(input: string): boolean {
|
|
|
|
const isEscapeSequence = input[0] === KeyCommand.Escape;
|
|
|
|
const isCtrl = isControl(input);
|
|
|
|
const shouldFilter = isEscapeSequence || isCtrl;
|
|
|
|
const whitelist = ['\t'];
|
|
|
|
|
|
|
|
if (shouldFilter && !whitelist.includes(input)) {
|
|
|
|
logToFile({
|
|
|
|
'msg': `Ignoring input: ${input}`,
|
|
|
|
isEscapeSequence,
|
|
|
|
isCtrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-10 08:36:18 -05:00
|
|
|
private moveCursor(char: string): void {
|
|
|
|
switch (char) {
|
2023-11-10 18:22:09 -05:00
|
|
|
case KeyCommand.ArrowLeft:
|
2023-11-10 19:17:36 -05:00
|
|
|
if (this.#cursor.x > 0) {
|
|
|
|
this.#cursor.x--;
|
2023-11-20 14:21:42 -05:00
|
|
|
} else if (this.#cursor.y > 0) {
|
|
|
|
this.#cursor.y--;
|
|
|
|
this.#cursor.x = (this.currentRow !== null)
|
2023-11-22 17:09:41 -05:00
|
|
|
? this.currentRow.size
|
2023-11-20 14:21:42 -05:00
|
|
|
: 0;
|
2023-11-10 19:17:36 -05:00
|
|
|
}
|
2023-11-10 08:36:18 -05:00
|
|
|
break;
|
2023-11-10 18:22:09 -05:00
|
|
|
case KeyCommand.ArrowRight:
|
2023-11-20 14:21:42 -05:00
|
|
|
if (
|
2023-11-22 17:09:41 -05:00
|
|
|
this.currentRow !== null && this.#cursor.x < this.currentRow.size
|
2023-11-20 14:21:42 -05:00
|
|
|
) {
|
2023-11-10 19:17:36 -05:00
|
|
|
this.#cursor.x++;
|
2023-11-20 14:21:42 -05:00
|
|
|
} else if (
|
|
|
|
this.currentRow !== null &&
|
2023-11-22 17:09:41 -05:00
|
|
|
this.#cursor.x === this.currentRow.size
|
2023-11-20 14:21:42 -05:00
|
|
|
) {
|
|
|
|
this.#cursor.y++;
|
|
|
|
this.#cursor.x = 0;
|
2023-11-10 19:17:36 -05:00
|
|
|
}
|
2023-11-10 08:36:18 -05:00
|
|
|
break;
|
2023-11-10 18:22:09 -05:00
|
|
|
case KeyCommand.ArrowUp:
|
2023-11-10 19:17:36 -05:00
|
|
|
if (this.#cursor.y > 0) {
|
|
|
|
this.#cursor.y--;
|
|
|
|
}
|
2023-11-10 08:36:18 -05:00
|
|
|
break;
|
2023-11-10 18:22:09 -05:00
|
|
|
case KeyCommand.ArrowDown:
|
2023-11-21 15:14:08 -05:00
|
|
|
if (this.#cursor.y < this.numRows) {
|
2023-11-10 19:17:36 -05:00
|
|
|
this.#cursor.y++;
|
|
|
|
}
|
2023-11-10 08:36:18 -05:00
|
|
|
break;
|
2023-11-08 11:11:19 -05:00
|
|
|
}
|
2023-11-20 14:21:42 -05:00
|
|
|
|
|
|
|
const rowLen = this.currentRow?.size ?? 0;
|
|
|
|
if (this.#cursor.x > rowLen) {
|
|
|
|
this.#cursor.x = rowLen;
|
|
|
|
}
|
2023-11-08 11:11:19 -05:00
|
|
|
}
|
|
|
|
|
2023-11-20 11:12:22 -05:00
|
|
|
private scroll(): void {
|
2023-11-21 10:55:23 -05:00
|
|
|
this.#renderX = 0;
|
2023-11-20 15:14:36 -05:00
|
|
|
if (this.currentRow !== null) {
|
2023-11-21 10:55:23 -05:00
|
|
|
this.#renderX = this.currentRow.cxToRx(this.#cursor.x);
|
2023-11-20 15:14:36 -05:00
|
|
|
}
|
|
|
|
|
2023-11-20 11:12:22 -05:00
|
|
|
if (this.#cursor.y < this.#offset.y) {
|
|
|
|
this.#offset.y = this.#cursor.y;
|
|
|
|
}
|
|
|
|
if (this.#cursor.y >= this.#offset.y + this.#screen.rows) {
|
|
|
|
this.#offset.y = this.#cursor.y - this.#screen.rows + 1;
|
|
|
|
}
|
2023-11-21 10:55:23 -05:00
|
|
|
if (this.#renderX < this.#offset.x) {
|
|
|
|
this.#offset.x = this.#renderX;
|
2023-11-20 14:21:42 -05:00
|
|
|
}
|
2023-11-21 10:55:23 -05:00
|
|
|
if (this.#renderX >= this.#offset.x + this.#screen.cols) {
|
|
|
|
this.#offset.x = this.#renderX - this.#screen.cols + 1;
|
2023-11-20 14:21:42 -05:00
|
|
|
}
|
2023-11-20 11:12:22 -05:00
|
|
|
}
|
|
|
|
|
2023-11-10 08:36:18 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2023-11-09 12:05:30 -05:00
|
|
|
// Terminal Output / Drawing
|
2023-11-10 08:36:18 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2023-11-09 12:05:30 -05:00
|
|
|
|
2023-11-21 11:35:56 -05:00
|
|
|
public setStatusMessage(msg: string): void {
|
|
|
|
// TODO: consider some sort of formatting for passed strings
|
|
|
|
this.#statusMessage = msg;
|
|
|
|
this.#statusTimeout = Date.now();
|
|
|
|
}
|
|
|
|
|
2023-11-08 11:11:19 -05:00
|
|
|
/**
|
|
|
|
* Clear the screen and write out the buffer
|
|
|
|
*/
|
|
|
|
public async refreshScreen(): Promise<void> {
|
2023-11-20 11:12:22 -05:00
|
|
|
this.scroll();
|
2023-11-09 12:05:30 -05:00
|
|
|
this.#buffer.append(Ansi.HideCursor);
|
|
|
|
this.#buffer.append(Ansi.ResetCursor);
|
2023-11-08 17:02:59 -05:00
|
|
|
this.drawRows();
|
2023-11-20 15:39:27 -05:00
|
|
|
this.drawStatusBar();
|
2023-11-21 11:35:56 -05:00
|
|
|
this.drawMessageBar();
|
2023-11-10 08:36:18 -05:00
|
|
|
this.#buffer.append(
|
2023-11-20 14:21:42 -05:00
|
|
|
Ansi.moveCursor(
|
|
|
|
this.#cursor.y - this.#offset.y,
|
2023-11-21 10:55:23 -05:00
|
|
|
this.#renderX - this.#offset.x,
|
2023-11-20 14:21:42 -05:00
|
|
|
),
|
2023-11-10 08:36:18 -05:00
|
|
|
);
|
2023-11-09 12:05:30 -05:00
|
|
|
this.#buffer.append(Ansi.ShowCursor);
|
2023-11-08 17:02:59 -05:00
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
await this.#buffer.flush();
|
2023-11-03 12:26:09 -04:00
|
|
|
}
|
2023-11-08 11:11:19 -05:00
|
|
|
|
2023-11-09 12:32:41 -05:00
|
|
|
private async clearScreen(): Promise<void> {
|
|
|
|
this.#buffer.append(Ansi.ClearScreen);
|
|
|
|
this.#buffer.append(Ansi.ResetCursor);
|
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
await this.#buffer.flush();
|
2023-11-08 17:02:59 -05:00
|
|
|
}
|
|
|
|
|
2023-11-09 12:05:30 -05:00
|
|
|
private drawRows(): void {
|
2023-11-13 14:46:04 -05:00
|
|
|
for (let y = 0; y < this.#screen.rows; y++) {
|
2023-11-20 15:14:36 -05:00
|
|
|
this.#buffer.append(Ansi.ClearLine);
|
2023-11-21 15:14:08 -05:00
|
|
|
const fileRow = y + this.#offset.y;
|
|
|
|
if (fileRow >= this.numRows) {
|
|
|
|
this.drawPlaceholderRow(fileRow);
|
2023-11-13 14:46:04 -05:00
|
|
|
} else {
|
2023-11-21 15:14:08 -05:00
|
|
|
this.drawFileRow(fileRow);
|
2023-11-20 14:21:42 -05:00
|
|
|
}
|
|
|
|
|
2023-11-20 15:39:27 -05:00
|
|
|
this.#buffer.appendLine();
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
2023-11-09 13:08:00 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
private drawFileRow(y: number): void {
|
|
|
|
const row = this.#document.row(y);
|
2023-11-16 16:00:03 -05:00
|
|
|
if (row === null) {
|
|
|
|
logToFile(`Warning: trying to draw non-existent row '${y}'`);
|
|
|
|
return this.drawPlaceholderRow(y);
|
|
|
|
}
|
|
|
|
|
2023-11-20 14:21:42 -05:00
|
|
|
const len = Math.min(
|
2023-11-20 15:14:36 -05:00
|
|
|
posSub(row.rsize, this.#offset.x),
|
2023-11-20 14:21:42 -05:00
|
|
|
this.#screen.cols,
|
|
|
|
);
|
2023-11-09 13:08:00 -05:00
|
|
|
|
2023-11-20 15:14:36 -05:00
|
|
|
this.#buffer.append(row.rstring(this.#offset.x), len);
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private drawPlaceholderRow(y: number): void {
|
2023-11-14 15:53:45 -05:00
|
|
|
if (y === Math.trunc(this.#screen.rows / 2) && this.#document.isEmpty()) {
|
2023-11-21 16:06:29 -05:00
|
|
|
const message = `Scroll editor -- version ${SCROLL_VERSION}`;
|
2023-11-13 14:46:04 -05:00
|
|
|
const messageLen = (message.length > this.#screen.cols)
|
|
|
|
? this.#screen.cols
|
|
|
|
: message.length;
|
|
|
|
let padding = Math.trunc((this.#screen.cols - messageLen) / 2);
|
|
|
|
if (padding > 0) {
|
2023-11-09 12:05:30 -05:00
|
|
|
this.#buffer.append('~');
|
2023-11-13 14:46:04 -05:00
|
|
|
padding -= 1;
|
2023-11-09 12:05:30 -05:00
|
|
|
|
2023-11-13 14:46:04 -05:00
|
|
|
this.#buffer.append(' '.repeat(padding));
|
2023-11-09 12:05:30 -05:00
|
|
|
}
|
2023-11-13 14:46:04 -05:00
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
this.#buffer.append(message, messageLen);
|
2023-11-13 14:46:04 -05:00
|
|
|
} else {
|
|
|
|
this.#buffer.append('~');
|
|
|
|
}
|
2023-11-03 12:26:09 -04:00
|
|
|
}
|
2023-11-20 15:39:27 -05:00
|
|
|
|
|
|
|
private drawStatusBar(): void {
|
|
|
|
this.#buffer.append(Ansi.InvertColor);
|
|
|
|
const name = (this.#filename !== '') ? this.#filename : '[No Name]';
|
2023-11-21 15:14:08 -05:00
|
|
|
const modified = (this.#document.dirty) ? '(modified)' : '';
|
|
|
|
const status = `${truncate(name, 20)} - ${this.numRows} lines ${modified}`;
|
|
|
|
const rStatus = `${this.#cursor.y + 1}/${this.numRows}`;
|
2023-11-20 15:39:27 -05:00
|
|
|
let len = Math.min(status.length, this.#screen.cols);
|
|
|
|
this.#buffer.append(status, len);
|
|
|
|
|
|
|
|
while (len < this.#screen.cols) {
|
2023-11-21 15:14:08 -05:00
|
|
|
if (this.#screen.cols - len === rStatus.length) {
|
|
|
|
this.#buffer.append(rStatus);
|
2023-11-20 15:39:27 -05:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
this.#buffer.append(' ');
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 11:35:56 -05:00
|
|
|
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);
|
|
|
|
}
|
2023-11-20 15:39:27 -05:00
|
|
|
}
|
2023-11-03 12:26:09 -04:00
|
|
|
}
|
2023-11-21 15:14:08 -05:00
|
|
|
|
|
|
|
export default Editor;
|