Compare commits
3 Commits
c2726bc261
...
814c849462
| Author | SHA1 | Date | |
|---|---|---|---|
| 814c849462 | |||
| 1ef028f919 | |||
| 29e377aea3 |
82
ce/edit.c
82
ce/edit.c
@@ -48,8 +48,9 @@ static const char* string_modes[] = {
|
|||||||
[EDIT_MODE_COMAMND] = "Command",
|
[EDIT_MODE_COMAMND] = "Command",
|
||||||
};
|
};
|
||||||
|
|
||||||
struct edit_select {
|
struct edit_command_buf {
|
||||||
struct list_node_link* lines;
|
char buffer[256];
|
||||||
|
size_t len;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct editor {
|
struct editor {
|
||||||
@@ -59,10 +60,10 @@ struct editor {
|
|||||||
size_t col_offset;
|
size_t col_offset;
|
||||||
size_t row_offset;
|
size_t row_offset;
|
||||||
size_t total_lines;
|
size_t total_lines;
|
||||||
struct edit_select select;
|
|
||||||
int mode;
|
int mode;
|
||||||
const char* volume;
|
const char* volume;
|
||||||
const char* path;
|
const char* path;
|
||||||
|
struct edit_command_buf cmdbuf;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct editor editor;
|
static struct editor editor;
|
||||||
@@ -218,13 +219,14 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
|
|||||||
current_idx++;
|
current_idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bbptr +=
|
char* command_str = (editor.mode == EDIT_MODE_COMAMND) ? editor.cmdbuf.buffer : "";
|
||||||
snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)),
|
|
||||||
|
bbptr += snprintf (
|
||||||
|
bbptr, (backbuffer_max - (bbptr - backbuffer)),
|
||||||
ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE
|
ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE
|
||||||
"%*s" ANSIQ_DYN_CUR_SET
|
"%*s" ANSIQ_DYN_CUR_SET
|
||||||
" Editing %s:%s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET
|
" %s | %s:%s | %zu:%zu | Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET ANSIQ_CUR_VISIBLE,
|
||||||
ANSIQ_CUR_VISIBLE,
|
(int)rows, 1, (int)cols, "", (int)rows, 1, command_str, volume, file_path,
|
||||||
(int)rows, 1, (int)cols, "", (int)rows, 1, volume, file_path,
|
|
||||||
editor.cursor.line + 1, editor.cursor.col + 1, string_modes[editor.mode],
|
editor.cursor.line + 1, editor.cursor.col + 1, string_modes[editor.mode],
|
||||||
(int)(editor.cursor.line - editor.row_offset) + 1,
|
(int)(editor.cursor.line - editor.row_offset) + 1,
|
||||||
(int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width);
|
(int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width);
|
||||||
@@ -299,6 +301,32 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
|
|||||||
editor.cursor.line++;
|
editor.cursor.line++;
|
||||||
editor.current_line = new_line;
|
editor.current_line = new_line;
|
||||||
editor.total_lines++;
|
editor.total_lines++;
|
||||||
|
} else if (editor.mode == EDIT_MODE_COMAMND) {
|
||||||
|
if (strcmp (editor.cmdbuf.buffer, "q") == 0) {
|
||||||
|
edit_run = false;
|
||||||
|
editor.mode = EDIT_MODE_NORMAL;
|
||||||
|
} else if (strcmp (editor.cmdbuf.buffer, "w") == 0) {
|
||||||
|
struct filewriter fw;
|
||||||
|
if (!(filewriter_init (&fw, editor.volume, editor.path, FW_CREATE_FILE | FW_APPEND) <
|
||||||
|
0)) {
|
||||||
|
struct list_node_link *line_link, *tmp_line_link;
|
||||||
|
list_foreach (editor.lines, line_link, tmp_line_link) {
|
||||||
|
struct edit_line* line = list_entry (line_link, struct edit_line, lines_link);
|
||||||
|
|
||||||
|
filewriter_write (&fw, (uint8_t*)line->gb.buffer, line->gb.gap_start);
|
||||||
|
filewriter_write (&fw, (uint8_t*)line->gb.buffer + line->gb.gap_end,
|
||||||
|
line->gb.size - line->gb.gap_end);
|
||||||
|
|
||||||
|
if (line->lines_link.next != NULL)
|
||||||
|
filewriter_write (&fw, (uint8_t*)"\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
filewriter_fini (&fw);
|
||||||
|
}
|
||||||
|
editor.mode = EDIT_MODE_NORMAL;
|
||||||
|
} else {
|
||||||
|
editor.mode = EDIT_MODE_NORMAL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case KB_DELETE:
|
case KB_DELETE:
|
||||||
@@ -358,41 +386,23 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
|
|||||||
editor.cursor.col = gapbuffer_length (&editor.current_line->gb);
|
editor.cursor.col = gapbuffer_length (&editor.current_line->gb);
|
||||||
break;
|
break;
|
||||||
case KB_ESCAPE:
|
case KB_ESCAPE:
|
||||||
|
if (editor.mode == EDIT_MODE_COMAMND) {
|
||||||
editor.mode = EDIT_MODE_NORMAL;
|
editor.mode = EDIT_MODE_NORMAL;
|
||||||
|
editor.cmdbuf.len = 0;
|
||||||
|
memset (editor.cmdbuf.buffer, 0, sizeof (editor.cmdbuf.buffer));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case KB_CTRL ('X'):
|
case KB_CTRL ('X'):
|
||||||
editor.mode = EDIT_MODE_COMAMND;
|
editor.mode = EDIT_MODE_COMAMND;
|
||||||
|
editor.cmdbuf.len = 0;
|
||||||
|
memset (editor.cmdbuf.buffer, 0, sizeof (editor.cmdbuf.buffer));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
if (isprint (ch)) {
|
if (isprint (ch)) {
|
||||||
if (editor.mode == EDIT_MODE_COMAMND) {
|
if (editor.mode == EDIT_MODE_COMAMND) {
|
||||||
switch (ch) {
|
if (editor.cmdbuf.len < sizeof (editor.cmdbuf.buffer) - 1) {
|
||||||
case 'q':
|
editor.cmdbuf.buffer[editor.cmdbuf.len++] = ch;
|
||||||
edit_run = false;
|
editor.cmdbuf.buffer[editor.cmdbuf.len] = '\0';
|
||||||
editor.mode = EDIT_MODE_NORMAL;
|
|
||||||
break;
|
|
||||||
case 'w': {
|
|
||||||
struct filewriter fw;
|
|
||||||
if (!(filewriter_init (&fw, editor.volume, editor.path, FW_CREATE_FILE | FW_APPEND) <
|
|
||||||
0)) {
|
|
||||||
struct list_node_link *line_link, *tmp_line_link;
|
|
||||||
list_foreach (editor.lines, line_link, tmp_line_link) {
|
|
||||||
struct edit_line* line = list_entry (line_link, struct edit_line, lines_link);
|
|
||||||
|
|
||||||
filewriter_write (&fw, (uint8_t*)line->gb.buffer, line->gb.gap_start);
|
|
||||||
filewriter_write (&fw, (uint8_t*)line->gb.buffer + line->gb.gap_end,
|
|
||||||
line->gb.size - line->gb.gap_end);
|
|
||||||
|
|
||||||
if (line->lines_link.next != NULL)
|
|
||||||
filewriter_write (&fw, (uint8_t*)"\n", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
filewriter_fini (&fw);
|
|
||||||
}
|
|
||||||
editor.mode = EDIT_MODE_NORMAL;
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
editor.mode = EDIT_MODE_NORMAL;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else if (editor.mode == EDIT_MODE_NORMAL) {
|
} else if (editor.mode == EDIT_MODE_NORMAL) {
|
||||||
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
||||||
|
|||||||
32
ce/interp.c
32
ce/interp.c
@@ -17,7 +17,9 @@
|
|||||||
#include <str_status.h>
|
#include <str_status.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <system.h>
|
#include <system.h>
|
||||||
|
#include <tcursor.h>
|
||||||
#include <terminal.h>
|
#include <terminal.h>
|
||||||
|
#include <tscreen.h>
|
||||||
|
|
||||||
static bool run = true;
|
static bool run = true;
|
||||||
|
|
||||||
@@ -26,23 +28,16 @@ bool interp_is_running (void) { return run; }
|
|||||||
void interp_shutdown (void) { run = false; }
|
void interp_shutdown (void) { run = false; }
|
||||||
|
|
||||||
static void human_size (double size, double* out, char** str) {
|
static void human_size (double size, double* out, char** str) {
|
||||||
if (size >= 1024.0f && size < 1024.0f * 1024.0f) {
|
static char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
||||||
*out = (double)size / 1024.0f;
|
|
||||||
*str = "KiB";
|
size_t i = 0;
|
||||||
} else if (size >= 1024.0f * 1024.0f && size < 1024.0f * 1024.0f * 1024.0f) {
|
while (size >= 1024.0 && i < (sizeof (units) / sizeof (units[0])) - 1) {
|
||||||
*out = (double)size / (1024.0f * 1024.0f);
|
size /= 1024.0;
|
||||||
*str = "MiB";
|
i++;
|
||||||
} else if (size >= 1024.0f * 1024.0f * 1024.0f && size < 1024.0f * 1024.0f * 1024.0f * 1024.0f) {
|
|
||||||
*out = (double)size / (1024.0f * 1024.0f * 1024.0f);
|
|
||||||
*str = "GiB";
|
|
||||||
} else if (size >= 1024.0f * 1024.0f * 1024.0f * 1024.0f &&
|
|
||||||
size < 1024.0f * 1024.0f * 1024.0f * 1024.0f * 1024.0f) {
|
|
||||||
*out = (double)size / (1024.0f * 1024.0f * 1024.0f * 1024.0f);
|
|
||||||
*str = "TiB";
|
|
||||||
} else {
|
|
||||||
*out = 0.0f;
|
|
||||||
*str = "???";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*out = size;
|
||||||
|
*str = units[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void echo (struct context* context, char** strings, size_t strings_count) {
|
static void echo (struct context* context, char** strings, size_t strings_count) {
|
||||||
@@ -275,6 +270,8 @@ static void quit1 (struct context* context) {
|
|||||||
interp_shutdown ();
|
interp_shutdown ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cls (struct context* context) { cprintf (context, ANSIQ_CUR_HOME ANSIQ_SCR_CLR_ALL); }
|
||||||
|
|
||||||
static void help (struct context* context) {
|
static void help (struct context* context) {
|
||||||
cprintf (context, "Available commands:\n");
|
cprintf (context, "Available commands:\n");
|
||||||
cprintf (context, "help\n");
|
cprintf (context, "help\n");
|
||||||
@@ -286,6 +283,7 @@ static void help (struct context* context) {
|
|||||||
cprintf (context, "rm <path>\n");
|
cprintf (context, "rm <path>\n");
|
||||||
cprintf (context, "edit <path>\n");
|
cprintf (context, "edit <path>\n");
|
||||||
cprintf (context, "terminfo\n");
|
cprintf (context, "terminfo\n");
|
||||||
|
cprintf (context, "cls\n");
|
||||||
cprintf (context, "quit\n");
|
cprintf (context, "quit\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,6 +322,8 @@ static void execute_cmd (struct ast_cmd* cmd, struct context* context) {
|
|||||||
edit (context, cmd->args[0]);
|
edit (context, cmd->args[0]);
|
||||||
} else if (strcmp (cmd->name, "terminfo") == 0) {
|
} else if (strcmp (cmd->name, "terminfo") == 0) {
|
||||||
terminfo (context);
|
terminfo (context);
|
||||||
|
} else if (strcmp (cmd->name, "cls") == 0) {
|
||||||
|
cls (context);
|
||||||
} else {
|
} else {
|
||||||
char volume[VOLUME_MAX];
|
char volume[VOLUME_MAX];
|
||||||
const char* path;
|
const char* path;
|
||||||
|
|||||||
Reference in New Issue
Block a user