Skip to content

Commit

Permalink
work on opening files
Browse files Browse the repository at this point in the history
  • Loading branch information
kjloveless committed Nov 11, 2024
1 parent e316b1b commit 3cdf2e8
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ interface EditorConfig {
cursorY: number;
screenRows: number;
screenCols: number;
numRows: number;
row: string;
}

enum EditorKey {
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
Expand Down Expand Up @@ -85,6 +88,9 @@ function editorReadKey(): number {
case '1':
return EditorKey.HOME_KEY;
break;
case '3':
return EditorKey.DEL_KEY;
break;
case '4':
return EditorKey.END_KEY;
break;
Expand Down Expand Up @@ -164,6 +170,16 @@ function getWindowSize() {
return { columns, rows };
}

function editorOpen(filename: string) {
const data = Deno.readFileSync(filename);
const contents = decoder.decode(data);

console.log(contents);
Deno.exit();
e.row = contents;
e.numRows++;
}

function resetScreen() {
write("\x1b[2J", true);
write("\x1b[H", true);
Expand All @@ -172,19 +188,27 @@ function resetScreen() {
function editorDrawRows() {
let y = 0;
while (y < e.screenRows) {
if (y == Math.floor(e.screenRows / 3)) {
const welcome = `editor -- version ${VERSION}`;
let padding = Math.floor((e.screenCols - welcome.length) / 2);
if (padding > 0 ) {
if (y >= e.numRows) {
if (y == Math.floor(e.screenRows / 3)) {
const welcome = `editor -- version ${VERSION}`;
let padding = Math.floor((e.screenCols - welcome.length) / 2);
if (padding > 0 ) {
abAppend("~");
padding--;
}
while (padding--) {
abAppend(" ");
}
abAppend(welcome);
} else {
abAppend("~");
padding--;
}
while (padding--) {
abAppend(" ");
}
abAppend(welcome);
} else {
abAppend("~");
let len = e.row.length;
if (len > e.screenCols) {
len = e.screenCols;
}
abAppend(e.row);
}

abAppend("\x1b[K");
Expand Down Expand Up @@ -290,6 +314,7 @@ function ctrlKey(key: number | string): number {
function initEditor() {
e.cursorX = 0;
e.cursorY = 0;
e.numRows = 0;

const { columns, rows } = getWindowSize();
e.screenRows = rows;
Expand All @@ -299,6 +324,9 @@ function initEditor() {
if (import.meta.main) {
enableRawMode();
initEditor();
if (Deno.args.length > 0) {
editorOpen(Deno.args[0]);
}

while(true) {
editorRefreshScreen();
Expand Down

0 comments on commit 3cdf2e8

Please sign in to comment.