Prompt at bar

This commit is contained in:
Night Kaly 2024-07-03 03:00:14 +01:00
parent 4854d4bcee
commit 5549a3d93e
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
2 changed files with 42 additions and 1 deletions

View file

@ -62,6 +62,7 @@ void abAppend(struct abuf *ab, const char *s, int len);
void append_row(char *s, size_t len); void append_row(char *s, size_t len);
void row_insert_char(row *row, int at, int c); void row_insert_char(row *row, int at, int c);
void row_del_char(row *row, int at); void row_del_char(row *row, int at);
char *prompt_editor(char *prompt);
extern editor vip; extern editor vip;

View file

@ -117,7 +117,13 @@ void open_editor(char *filename)
void save_file() void save_file()
{ {
if (vip.filename == NULL) return; if (vip.filename == NULL) {
vip.filename = prompt_editor("Save as: %s (ESC to cancel)");
if (vip.filename == NULL) {
set_status_bar_message("Save aborted");
return;
}
}
int len; int len;
char *buf = rows_to_str(&len); char *buf = rows_to_str(&len);
int fd = open(vip.filename, O_RDWR | O_CREAT, 0644); int fd = open(vip.filename, O_RDWR | O_CREAT, 0644);
@ -228,6 +234,40 @@ void refresh_screen()
abFree(&ab); abFree(&ab);
} }
char *prompt_editor(char *prompt)
{
size_t bufsize = 128;
char *buf = malloc(bufsize);
size_t buflen = 0;
buf[0] = '\0';
while (1) {
set_status_bar_message(prompt, buf);
refresh_screen();
int c = read_key();
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
if (buflen != 0) {
buf[--buflen] = '\0';
}
} else if (c == '\x1b') {
set_status_bar_message("");
free(buf);
return NULL;
} else if (c == '\r') {
if (buflen != 0) {
set_status_bar_message("");
return buf;
}
} else if (!iscntrl(c) && c < 128) {
if (buflen == bufsize - 1) {
bufsize *= 2;
buf = realloc(buf, bufsize);
}
buf[buflen++] = c;
buf[buflen] = '\0';
}
}
}
void move_cursor(int key) void move_cursor(int key)
{ {
row *row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy]; row *row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy];