Skip to content

Commit

Permalink
pound: keep track whether the buffer is "dirty"
Browse files Browse the repository at this point in the history
  • Loading branch information
ivop committed Sep 10, 2024
1 parent c02f314 commit d533345
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions apps/pound.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct editorConfig {
unsigned int numrows;
unsigned int rowsroom;
struct erow *row;
bool dirty;
} E;

#define STYLE_NORMAL 0
Expand Down Expand Up @@ -143,7 +144,7 @@ void editorSave(void) {
if (cpm_open_file(&cpm_fcb)) {
editorDrawStatusMsg("Saving to new file...");
if (write_file(&cpm_fcb)) editorDrawStatusMsg("Failed to save file");
return;
goto success;
}

editorDrawStatusMsg("Saving to temporary file...");
Expand All @@ -169,7 +170,9 @@ void editorSave(void) {
return;
}

success:
editorDrawStatusMsg("File saved");
E.dirty = false;
}

char line[256];
Expand Down Expand Up @@ -231,22 +234,23 @@ void editorScroll() {
}
}

static const char *const dirty_string = " (modified)";

void editorDrawStatusBar() {
uint8_t x, p;
uint8_t x, y, p;
screen_setcursor(0, E.screenrows);
screen_setstyle(STYLE_REVERSE);
x = p = 0;
p = 0;
while (p<11) {
if (cpm_fcb.f[p] != ' ') {
screen_putchar(cpm_fcb.f[p]);
x++;
}
p++;
if (p == 8) {
screen_putchar('.');
x++;
}
if (p == 8) screen_putchar('.');
}
for (p = 0; E.dirty && p<strlen(dirty_string); p++)
screen_putchar(dirty_string[p]);
screen_getcursor(&x, &y);
for ( ; x<E.screencols; x++)
screen_putchar(' ');
screen_setstyle(STYLE_NORMAL);
Expand Down Expand Up @@ -336,6 +340,7 @@ void editorProcessKeypress(void) {
case 'S':
case CTRL('S'): // ^KS Save
editorSave();
editorDrawStatusBar();
break;
}
ctrlk = false;
Expand Down Expand Up @@ -399,7 +404,13 @@ void editorProcessKeypress(void) {
ctrlq = true;
break;
default:
if (c >= ' ' && c <= '~') editorInsertChar(c);
if (c >= ' ' && c <= '~') {
editorInsertChar(c);
if (!E.dirty) {
E.dirty = true;
editorDrawStatusBar();
}
}
break;
}
}
Expand All @@ -419,6 +430,7 @@ void initEditor(void) {
E.screenrows--; // two lines for status bar and status message
E.cx = E.cy = E.rowoff = E.coloff = E.numrows = E.rowsroom = 0;
E.row = NULL;
E.dirty = false;
}

// -------------------- MAIN --------------------
Expand Down

0 comments on commit d533345

Please sign in to comment.