From bce944d1498eaa3b6940ee234c863b3548a66b37 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 24 Jan 2021 12:00:11 -0500 Subject: graphics! --- src/user/highway/cmds.c | 10 ++++-- src/user/highway/line.c | 85 ++++++++++++++++++++++++++++++++----------------- src/user/highway/line.h | 1 + src/user/highway/main.c | 31 ++++++++++++++---- src/user/highway/vars.c | 28 ++++------------ 5 files changed, 94 insertions(+), 61 deletions(-) (limited to 'src/user/highway') diff --git a/src/user/highway/cmds.c b/src/user/highway/cmds.c index d7a89d1..0420ae1 100644 --- a/src/user/highway/cmds.c +++ b/src/user/highway/cmds.c @@ -1,12 +1,16 @@ +#include + #include -#include + #include "line.h" #include "vars.h" void source(const char *path) { struct file *f = open_file(path); - if (!f) - tell_user_sz("couldn't open file.\n"); + if (!f) { + term_addf("Could not open %s.\n", path); + return; + } char buf[128]; while (read_line_from_file(f, buf, 127)) run_line(buf); diff --git a/src/user/highway/line.c b/src/user/highway/line.c index c8e5c60..bf171e5 100644 --- a/src/user/highway/line.c +++ b/src/user/highway/line.c @@ -1,19 +1,37 @@ -#include +#include + #include #include -#include + +#include + #include "cmds.h" +#include "term.h" #include "vars.h" #define LINE_SIZE 4096 static char line[LINE_SIZE]; +static inline uint8_t hex_to_int(char ch) { + return ch - (ch <= '9' ? '0' : 'a' - 10); +} + +void ensure_color() { + const struct no_null_sn *fg = get_var((struct no_null_sn){.data = "_color_fg", .length = 9}); + const struct no_null_sn *bg = get_var((struct no_null_sn){.data = "_color_bg", .length = 9}); + if (fg && bg) + set_color( + (hex_to_int(fg->data[0]) << 4) | hex_to_int(fg->data[1]), + (hex_to_int(bg->data[0]) << 4) | hex_to_int(bg->data[1]) + ); +} + static void line_replace(const char *from) { const char *fi = from; char *ti = line; while (*fi) { if (ti == line + LINE_SIZE) { - tell_user_sz("Line too long.\n"); + term_add_sz("Line too long.\n"); line[0] = '\0'; return; } @@ -27,12 +45,15 @@ static void line_replace(const char *from) { const char *var_end = var_start; while (*var_end != '$') if (!*var_end++) { - tell_user_sz("Unterminated variable name.\n"); + if (var_end - fi > 10) + term_addf("Unterminated variable at\"%10s...\".\n", fi); + else + term_addf("Unterminated variable at \"%s\".\n", fi); line[0] = '\0'; return; } if (ti + (var_end - var_start) >= line + LINE_SIZE) { - tell_user_sz("Line too long.\n"); + term_add_sz("Line too long.\n"); line[0] = '\0'; return; } @@ -59,46 +80,50 @@ void run_line(const char *original_line) { ; if (blockequ(line, "source ", 7)) source(space + 1); - else if (blockequ(line, "set ", 4)) + else if (blockequ(line, "set ", 4)) { set(space + 1); + ensure_color(); + } else if (blockequ(line, "echo ", 5)) { - tell_user_sz(space + 1); - tell_user_sz("\n"); + term_add_sz(space + 1); + term_add_char('\n'); } else if (blockequ(line, "vars", 5)) dump_vars(); - else if (blockequ(line, "quit", 5)) + else if (blockequ(line, "quit", 5)) { + del_term(active_term); __pcrt_quit(); + } else if (blockequ(line, "clear", 6)) - _clear_screen(); + clear_term(); else if (blockequ(line, "help", 5)) - tell_user_sz("Highway is a command shell for Portland OS. It includes variables and a couple\n" - "of pseudo-commands. Variables are addressed by surrounding with \"$\". The\n" - "following list shows each of the pseudo-commands.\n\n" - " source FILE run each command in FILE\n" - " clear clear the screen\n" - " echo STRING print STRING\n" - " set VAR VALUE set $VAR$ to VALUE\n" - " vars dump variables\n" - " quit exit highway\n" - " help show this\n"); - else if (!try_run_command_blocking(line)) { - struct no_null_sn arg = { - .data = "_path", - .length = 5 - }; - const struct no_null_sn *path = get_var(arg); + term_add_sz("Highway is a command shell for Portland OS. It includes variable support and a couple of pseudo-commands. Variables are addressed by surrounding with \"$\". The following list shows each of the pseudo-commands.\n\n" + " source FILE\t" "run each command in FILE\n" + " clear\t\t\t" "clear the screen\n" + " echo STRING\t" "print STRING\n" + " set VAR VALUE\t" "set $VAR$ to VALUE\n" + " vars\t\t\t" "dump variables\n" + " quit\t\t\t" "exit highway\n" + " help\t\t\t" "show this\n"); + else if (!try_run_command_blocking(line, stdio_task)) { + const struct no_null_sn *path = get_var((struct no_null_sn){.data = "_path", .length = 5}); if (!path->length) { - tell_user_sz("Could not run command.\n"); + term_add_sz("Could not run command.\n"); return; } for (uint16_t to_i = LINE_SIZE - 1; to_i >= path->length; --to_i) line[to_i] = line[to_i - path->length]; blockcpy(line, path->data, path->length); - if (!try_run_command_blocking(line)) { - tell_user_sz("Could not run command.\n"); + if (!try_run_command_blocking(line, stdio_task)) { + term_add_sz("Could not run command.\n"); return; } + else { + ensure_color(); + if (active_term->cursor_x) + term_newline(); + } } - _set_color(0x07); + else + ensure_color(); } \ No newline at end of file diff --git a/src/user/highway/line.h b/src/user/highway/line.h index 4785034..fc131ec 100644 --- a/src/user/highway/line.h +++ b/src/user/highway/line.h @@ -2,5 +2,6 @@ #define LINE_H void run_line(const char *line); +void ensure_color(); #endif \ No newline at end of file diff --git a/src/user/highway/main.c b/src/user/highway/main.c index 4599315..60d5a69 100644 --- a/src/user/highway/main.c +++ b/src/user/highway/main.c @@ -1,17 +1,36 @@ -#include +#include +#include + +#include + +#include +#include #include + #include "cmds.h" #include "line.h" +#define FONT_NAME "berry" + void main(const char *arg) { + struct font_info *f = get_font(FONT_NAME); + + if (!f) + return; + + active_term = make_term(f, 50, 18); + if (!active_term) + return; + source(*arg ? arg : "user/default.rc"); + ensure_color(); + + term_add_sz("Portland Highway\nType \"help\" for help.\n"); + paint_term(); + char cmd_buf[128]; - yield_task(); - struct history *cmd_hs = new_history(128); - tell_user_sz("Portland Highway\nType \"help\" for help.\n"); while (1) { - tell_user_sz("\n> "); - ask_user_line_sz_with_history(cmd_buf, 127, cmd_hs); + read_line(cmd_buf, 127, "> "); run_line(cmd_buf); } } \ No newline at end of file diff --git a/src/user/highway/vars.c b/src/user/highway/vars.c index 6090b76..5f56621 100644 --- a/src/user/highway/vars.c +++ b/src/user/highway/vars.c @@ -1,6 +1,10 @@ +#include + +#include #include #include -#include + +#include struct no_null_sn { char *data; @@ -73,26 +77,6 @@ void del_var(struct no_null_sn name) { void dump_vars() { for (struct var_dict_node *node = var_dict_start; node; node = node->next) { - tell_user_sz("$"); - - char *buf = get_block(node->name.length + 1); - blockcpy(buf, node->name.data, node->name.length); - buf[node->name.length] = '\0'; - - tell_user_sz(buf); - - free_block(buf); - - tell_user_sz("$ = "); - - buf = get_block(node->value.length + 1); - blockcpy(buf, node->value.data, node->value.length); - buf[node->value.length] = '\0'; - - tell_user_sz(buf); - - free_block(buf); - - tell_user_sz("\n"); + term_addf_no_ww("$%ns$ = %ns\n", node->name.length, node->name.data, node->value.length, node->value.data); } } \ No newline at end of file -- cgit v1.2.3