Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fae",
"module": "./index.ts",
"module": "./src/index.ts",
"type": "module",
"private": true,
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Editor } from "./services/editor.ts";
import { Renderer } from "./services/renderer.ts";
import { Terminal } from "./services/terminal.ts";
import { parseKey } from "./utils/inputs.ts";
import { Viewport } from "./services/viewport.ts";
import { InputHandler } from "./services/inputs.ts";
import { Listener } from "./services/listeners.ts";

const editor = new Editor();
const terminal = new Terminal();
const size = terminal.size();
const viewport = new Viewport(size.width, size.height);
const renderer = new Renderer(terminal, viewport);
const handler = new InputHandler(editor);
const listeners = new Listener(editor, renderer, handler, terminal, viewport);

terminal.start();
viewport.followCursor(editor.cursor);
renderer.render(editor);
listeners.start();
2 changes: 1 addition & 1 deletion src/buffer.ts → src/services/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BufferType, InsertType } from "./types";
import type { BufferType, InsertType } from "../types";

export class TextBuffer {
lines: string[] = [""];
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/editor.ts → src/services/editor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Cursor } from "./cursor.ts";
import { TextBuffer } from "./buffer.ts";
import type { BufferType, InsertType } from "./types";
import type { BufferType, InsertType } from "../types";

export class Editor {
readonly buffer: TextBuffer;
Expand Down
22 changes: 22 additions & 0 deletions src/services/inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Key } from "../types";
import { Editor } from "../services/editor";

export class InputHandler {
constructor(private editor: Editor) {}
private commands = new Map<string, () => void>([
["backspace", () => this.editor.backspace()],
["enter", () => this.editor.newline()],
["left", () => this.editor.moveLeft()],
["right", () => this.editor.moveRight()],
["down", () => this.editor.moveDown()],
["up", () => this.editor.moveUp()],
]);

handle(key: Key) {
if (key.type === "character") {
this.editor.insert(key.value);
return;
}
this.commands.get(key.type)?.();
}
}
38 changes: 38 additions & 0 deletions src/services/listeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Editor } from "./editor.ts";
import type { Renderer } from "./renderer.ts";
import type { InputHandler } from "./inputs.ts";
import type { Terminal } from "./terminal.ts";
import type { Viewport } from "./viewport.ts";
import { parseKey } from "../utils/inputs.ts";

export class Listener {
constructor(
private editor: Editor,
private renderer: Renderer,
private handler: InputHandler,
private terminal: Terminal,
private viewport: Viewport,
) {}

start() {
process.stdin.on("data", (data: string) => {
const key = parseKey(data);
if (key === null) return;
if (key.type === "quit") {
this.terminal.stop();
process.exit(0);
}
this.handler.handle(key);
this.viewport.followCursor(this.editor.cursor);
this.renderer.render(this.editor);
});

process.stdout.on("resize", () => {
const size = this.terminal.size();
this.viewport.height = size.height;
this.viewport.width = size.width;
this.viewport.followCursor(this.editor.cursor);
this.renderer.render(this.editor);
});
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/tests/buffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
import { TextBuffer } from "../buffer";
import type { BufferType, InsertType } from "../types.ts";
import { TextBuffer } from "../services/buffer.ts";
import type { BufferType, InsertType } from "../types";

test("starts on empty line", () => {
const buffer = new TextBuffer();
Expand Down
4 changes: 2 additions & 2 deletions src/tests/cursor.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
import { TextBuffer } from "../buffer";
import { Cursor } from "../cursor";
import { TextBuffer } from "../services/buffer.ts";
import { Cursor } from "../services/cursor.ts";

test("starts at 0,0", () => {
const cursor = new Cursor();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/editor.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { Editor } from "../editor";
import { Editor } from "../services/editor.ts";

test("starts on empty buffer", () => {
const editor = new Editor();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/terminal.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { Terminal } from "../terminal";
import { Terminal } from "../services/terminal.ts";

test("fetches terminal size", () => {
const terminal = new Terminal();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { parseKey } from "../utils.ts";
import { parseKey } from "../utils/inputs.ts";

test("parse character", () => {
expect(parseKey("a")).toEqual({
Expand Down
4 changes: 2 additions & 2 deletions src/tests/viewport.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
import { Viewport } from "../viewport";
import { Cursor } from "../cursor";
import { Viewport } from "../services/viewport.ts";
import { Cursor } from "../services/cursor.ts";

test("viewport shows cursor (right)", () => {
const viewport = new Viewport(8, 8);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utils.ts → src/utils/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Key } from "./types.ts";
import type { Key } from "../types";

export function parseKey(input: string): Key | null {
if (input === "\x1b[A") {
Expand Down