Skip to content

Commit

Permalink
basic saving on existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
kjloveless committed Nov 14, 2024
1 parent c9c5df6 commit 911f077
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function abFree() {

function exit(msg: string, code: number = 0) {
resetScreen();

// TODO: fix how bytes are being accumulated
// console.log(`${bytesWritten} bytes written this session!\r\n${msg}\r\n`);
console.log(`${msg}\r\n`);
Expand Down Expand Up @@ -223,6 +223,26 @@ function editorInsertChar(char: string) {
e.cursorX++;
}

function editorRowsToString(): string {
const rows = e.row.length;

let i = 0;
let contents = "";
while (i < rows) {
contents += e.row[i] + "\n";
i++;
}

return contents;
}

async function editorSave() {
if (e.filename == "" || e.filename == undefined) return;

const data = encoder.encode(editorRowsToString());
await Deno.writeFile(e.filename, data, { mode: 0o644 });
}

function editorOpen(filename: string) {
console.log(`filename: ${filename}`);
e.filename = filename;
Expand Down Expand Up @@ -394,17 +414,21 @@ function editorMoveCursor(key: string | number) {
}
}

function editorProcessKeypress() {
async function editorProcessKeypress() {
const char = editorReadKey();

switch (char) {
case '\r'.charCodeAt(0):
case "\r".charCodeAt(0):
// TODO
break;
case ctrlKey("q"):
exit("ciao, ciao");
break;

case ctrlKey("s"):
await editorSave();
break;

case EditorKey.HOME_KEY:
e.cursorX = 0;
break;
Expand All @@ -417,7 +441,7 @@ function editorProcessKeypress() {

case EditorKey.BACKSPACE:
case EditorKey.DEL_KEY:
case ctrlKey('h'):
case ctrlKey("h"):
// TODO
break;

Expand Down Expand Up @@ -448,7 +472,7 @@ function editorProcessKeypress() {
editorMoveCursor(char);
break;

case ctrlKey('l'):
case ctrlKey("l"):
case 0x1b:
break;

Expand Down Expand Up @@ -509,6 +533,6 @@ if (import.meta.main) {

while (true) {
editorRefreshScreen();
editorProcessKeypress();
await editorProcessKeypress();
}
}

0 comments on commit 911f077

Please sign in to comment.