CE edit Introduce viewer mode
This commit is contained in:
15
ce/edit.c
15
ce/edit.c
@@ -130,7 +130,8 @@ static size_t count_digits(size_t n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
void edit_start(const char* volume, const char* file_path, const char* text, size_t text_len) {
|
||||
void edit_start(const char* volume, const char* file_path, const char* text, size_t text_len,
|
||||
bool readonly) {
|
||||
editor.volume = volume;
|
||||
editor.path = file_path;
|
||||
|
||||
@@ -244,7 +245,7 @@ void edit_start(const char* volume, const char* file_path, const char* text, siz
|
||||
|
||||
switch (ch) {
|
||||
case '\b':
|
||||
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||
if (editor.mode == EDIT_MODE_NORMAL && !readonly) {
|
||||
if (editor.cursor.col > 0) {
|
||||
in_gb_move(&editor.current_line->gb, editor.cursor.col);
|
||||
editor.cursor.col--;
|
||||
@@ -271,7 +272,7 @@ void edit_start(const char* volume, const char* file_path, const char* text, siz
|
||||
}
|
||||
break;
|
||||
case '\t':
|
||||
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||
if (editor.mode == EDIT_MODE_NORMAL && !readonly) {
|
||||
in_gb_move(&editor.current_line->gb, editor.cursor.col);
|
||||
for (size_t i = 0; i < sizeof(TAB_INSERT) - 1; i++)
|
||||
in_gb_insert(&wrealloc, NULL, &editor.current_line->gb, TAB_INSERT[i]);
|
||||
@@ -280,7 +281,7 @@ void edit_start(const char* volume, const char* file_path, const char* text, siz
|
||||
}
|
||||
break;
|
||||
case '\n': {
|
||||
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||
if (editor.mode == EDIT_MODE_NORMAL && !readonly) {
|
||||
in_gb_move(&editor.current_line->gb, editor.cursor.col);
|
||||
|
||||
struct edit_line* new_line = malloc(sizeof(*new_line));
|
||||
@@ -311,7 +312,7 @@ void edit_start(const char* volume, const char* file_path, const char* text, siz
|
||||
if (strcmp(editor.cmdbuf.buffer, "q") == 0) {
|
||||
edit_run = false;
|
||||
editor.mode = EDIT_MODE_NORMAL;
|
||||
} else if (strcmp(editor.cmdbuf.buffer, "w") == 0) {
|
||||
} else if (strcmp(editor.cmdbuf.buffer, "w") == 0 && !readonly) {
|
||||
struct filewriter fw;
|
||||
if (!(filewriter_init(&fw, editor.volume, editor.path, FW_CREATE_FILE) < 0)) {
|
||||
struct list_node_link *line_link, *tmp_line_link;
|
||||
@@ -335,7 +336,7 @@ void edit_start(const char* volume, const char* file_path, const char* text, siz
|
||||
}
|
||||
} break;
|
||||
case KB_DELETE:
|
||||
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||
if (editor.mode == EDIT_MODE_NORMAL && !readonly) {
|
||||
size_t len = in_gb_length(&editor.current_line->gb);
|
||||
|
||||
if (editor.cursor.col < len) {
|
||||
@@ -409,7 +410,7 @@ void edit_start(const char* volume, const char* file_path, const char* text, siz
|
||||
editor.cmdbuf.buffer[editor.cmdbuf.len++] = ch;
|
||||
editor.cmdbuf.buffer[editor.cmdbuf.len] = '\0';
|
||||
}
|
||||
} else if (editor.mode == EDIT_MODE_NORMAL) {
|
||||
} else if (editor.mode == EDIT_MODE_NORMAL && !readonly) {
|
||||
in_gb_move(&editor.current_line->gb, editor.cursor.col);
|
||||
in_gb_insert(&wrealloc, NULL, &editor.current_line->gb, ch);
|
||||
editor.cursor.col++;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#ifndef _EDIT_H
|
||||
#define _EDIT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void edit_start(const char* volume, const char* path, const char* text, size_t text_len);
|
||||
void edit_start(const char* volume, const char* path, const char* text, size_t text_len,
|
||||
bool readonly);
|
||||
|
||||
#endif // _EDIT_H
|
||||
|
||||
50
ce/interp.c
50
ce/interp.c
@@ -385,7 +385,49 @@ static void edit(struct context* context, const char* path_string) {
|
||||
|
||||
volume_close();
|
||||
|
||||
edit_start(volume, path, str, desc.size);
|
||||
edit_start(volume, path, str, desc.size, false);
|
||||
}
|
||||
|
||||
static void view(struct context* context, const char* path_string) {
|
||||
struct desc desc;
|
||||
char volume[VOLUME_MAX];
|
||||
const char* path;
|
||||
int ret;
|
||||
|
||||
if (!path_parse(path_string, volume, &path)) {
|
||||
cprintf(context, "ERROR bad path '%s'\n", path_string);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((ret = volume_open(volume)) < 0) {
|
||||
cprintf(context, "ERROR could not open volume '%s': %s\n", volume, str_status[-ret]);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((ret = describe(path, &desc)) < 0) {
|
||||
cprintf(context, "ERROR could not describe '%s': %s\n", path, str_status[-ret]);
|
||||
volume_close();
|
||||
return;
|
||||
}
|
||||
|
||||
char* str = malloc(desc.size + 1);
|
||||
|
||||
if (str == NULL) {
|
||||
volume_close();
|
||||
return;
|
||||
}
|
||||
|
||||
memset(str, 0, desc.size);
|
||||
|
||||
if ((ret = read_file(path, 0, (uint8_t*)str, desc.size)) < 0) {
|
||||
free(str);
|
||||
volume_close();
|
||||
return;
|
||||
}
|
||||
|
||||
volume_close();
|
||||
|
||||
edit_start(volume, path, str, desc.size, true);
|
||||
}
|
||||
|
||||
static void quit1(struct context* context) {
|
||||
@@ -407,6 +449,7 @@ static void help(struct context* context) {
|
||||
cprintf(context, "mkdir <directory path>\n");
|
||||
cprintf(context, "rm <path>\n");
|
||||
cprintf(context, "edit <path>\n");
|
||||
cprintf(context, "view <path>\n");
|
||||
cprintf(context, "terminfo\n");
|
||||
cprintf(context, "cls\n");
|
||||
cprintf(context, "procinfo\n");
|
||||
@@ -476,6 +519,11 @@ static void execute_cmd(struct ast_cmd* cmd, struct context* context, bool run_b
|
||||
edit(context, cmd->args[0]);
|
||||
else
|
||||
cprintf(context, "ERROR No file path provided\n");
|
||||
} else if (strcmp(cmd->name, "view") == 0) {
|
||||
if (cmd->arg_count == 1)
|
||||
view(context, cmd->args[0]);
|
||||
else
|
||||
cprintf(context, "ERROR No file path provided\n");
|
||||
} else if (strcmp(cmd->name, "terminfo") == 0) {
|
||||
terminfo(context);
|
||||
} else if (strcmp(cmd->name, "cls") == 0) {
|
||||
|
||||
Reference in New Issue
Block a user