diff --git a/ports/sh/console.c b/ports/sh/console.c index 208ed267a..9cbcda610 100644 --- a/ports/sh/console.c +++ b/ports/sh/console.c @@ -192,6 +192,43 @@ void console_clean_backlog(console_t *cons) cons->line_count = remains; } +void console_render(int x, int y, console_t const *cons, int w, int dy, + int lines) +{ + int watermark = lines; + + for(int i = 0; i < cons->line_count; i++) { + console_line_update_render_lines(&cons->lines[i], w); + watermark -= cons->lines[i].render_lines; + } + + /* Show only visible lines */ + for(int i = 0; i < cons->line_count; i++) { + console_line_t *line = &cons->lines[i]; + bool show_cursor = (i == cons->line_count - 1); + + if(watermark + line->render_lines > 0) + y = console_line_render(x, y, line, w, dy, -watermark, + show_cursor ? cons->cursor : -1); + watermark += line->render_lines; + } +} + +void console_clear_render_flag(console_t *cons) +{ + cons->render_needed = false; +} + +void console_destroy(console_t *cons) +{ + for(int i = 0; i < cons->line_count; i++) + console_line_deinit(&cons->lines[i]); + free(cons->lines); + free(cons); +} + +//=== Edition functions ===// + bool console_write_block_at_cursor(console_t *cons, char const *str, int n) { if(!cons->line_count && !console_new_line(cons)) @@ -219,6 +256,8 @@ bool console_write_block_at_cursor(console_t *cons, char const *str, int n) bool console_write_at_cursor(console_t *cons, char const *buf, int n) { int offset = 0; + if(n < 0) + n = strlen(buf); while(offset < n) { /* Find the first '\n', '\e' or end of buffer */ @@ -271,39 +310,11 @@ bool console_write_at_cursor(console_t *cons, char const *buf, int n) return true; } -void console_render(int x, int y, console_t const *cons, int w, int dy, - int lines) +void console_clear_current_line(console_t *cons) { - int watermark = lines; - - for(int i = 0; i < cons->line_count; i++) { - console_line_update_render_lines(&cons->lines[i], w); - watermark -= cons->lines[i].render_lines; - } - - /* Show only visible lines */ - for(int i = 0; i < cons->line_count; i++) { - console_line_t *line = &cons->lines[i]; - bool show_cursor = (i == cons->line_count - 1); - - if(watermark + line->render_lines > 0) - y = console_line_render(x, y, line, w, dy, -watermark, - show_cursor ? cons->cursor : -1); - watermark += line->render_lines; - } -} - -void console_clear_render_flag(console_t *cons) -{ - cons->render_needed = false; -} - -void console_destroy(console_t *cons) -{ - for(int i = 0; i < cons->line_count; i++) - console_line_deinit(&cons->lines[i]); - free(cons->lines); - free(cons); + console_line_t *last_line = &cons->lines[cons->line_count - 1]; + console_line_delete(last_line, 0, last_line->size); + cons->cursor = 0; } //=== Input method ===// diff --git a/ports/sh/console.h b/ports/sh/console.h index 04b307d87..f0c4b88da 100644 --- a/ports/sh/console.h +++ b/ports/sh/console.h @@ -93,6 +93,17 @@ bool console_new_line(console_t *cons); /* Clean up backlog if the total memory usage is exceeded. */ void console_clean_backlog(console_t *cons); +/* Render the console with the current font, at (x,y) in a rectangle of width + `w` and `lines` total lines separated by `dy` pixels. */ +void console_render(int x, int y, console_t const *cons, int w, int dy, + int lines); + +void console_clear_render_flag(console_t *cons); + +void console_destroy(console_t *cons); + +//=== Edition functions ===// + /* Write string at the cursor's position within the last line. This writes a raw string without interpreting escape sequences and newlines. */ bool console_write_block_at_cursor(console_t *cons, char const *str, int n); @@ -101,13 +112,8 @@ bool console_write_block_at_cursor(console_t *cons, char const *str, int n); interprets escape sequences and newlines. */ bool console_write_at_cursor(console_t *cons, char const *str, int n); -/* TODO: Expand this function */ -void console_render(int x, int y, console_t const *cons, int w, int dy, - int lines); - -void console_clear_render_flag(console_t *cons); - -void console_destroy(console_t *cons); +/* Clear the current line. */ +void console_clear_current_line(console_t *cons); //=== Input method ===// diff --git a/ports/sh/main.c b/ports/sh/main.c index 88d7ec774..cc1de0b3c 100644 --- a/ports/sh/main.c +++ b/ports/sh/main.c @@ -181,9 +181,6 @@ int main(int argc, char **argv) while(1) { jevent e = jscene_run(scene); - if(e.type == WIDGET_SHELL_MOD_CHANGED) - scene->widget.update = true; - if(e.type == JSCENE_PAINT) { dclear(C_WHITE); jscene_render(scene); @@ -204,6 +201,26 @@ int main(int argc, char **argv) dupdate(); } + if(e.type == WIDGET_SHELL_MOD_CHANGED) + scene->widget.update = true; + + if(e.type == WIDGET_SHELL_CHAR_INPUT) + shell_write_char(e.data); + + if(e.type == JFILESELECT_VALIDATED) { + char const *path = jfileselect_selected_file(fileselect); + char *module = path_to_module(path); + if(module) { + jscene_show_and_focus(scene, shell); + jwidget_set_visible(title, show_title_in_shell); + + shell_write_str("import "); + shell_write_str(module); + shell_write_str("\r\n"); + free(module); + } + } + if(e.type != JWIDGET_KEY || e.key.type == KEYEV_UP) continue; int key = e.key.key; diff --git a/ports/sh/widget_shell.c b/ports/sh/widget_shell.c index 5ebab020a..80554204e 100644 --- a/ports/sh/widget_shell.c +++ b/ports/sh/widget_shell.c @@ -14,6 +14,7 @@ static int widget_shell_id = -1; /* Events */ uint16_t WIDGET_SHELL_MOD_CHANGED; +uint16_t WIDGET_SHELL_CHAR_INPUT; //=== Modifier states ===// @@ -216,9 +217,10 @@ static bool widget_shell_poly_event(void *s0, jevent e) /* TODO: Handle input events better in the shell widget! */ int c = console_key_event_to_char(ev); - /* TODO: Can widget_shell_poly_event please not call into MicroPython? */ + if(c != 0) { - pyexec_event_repl_process_char(c); + jevent e = { .type = WIDGET_SHELL_CHAR_INPUT, .data = c }; + jwidget_emit(s, e); return true; } @@ -246,4 +248,5 @@ static void j_register_widget_shell(void) { widget_shell_id = j_register_widget(&type_widget_shell, "jwidget"); WIDGET_SHELL_MOD_CHANGED = j_register_event(); + WIDGET_SHELL_CHAR_INPUT = j_register_event(); } diff --git a/ports/sh/widget_shell.h b/ports/sh/widget_shell.h index 8bcba174a..28dc14926 100644 --- a/ports/sh/widget_shell.h +++ b/ports/sh/widget_shell.h @@ -66,6 +66,7 @@ typedef struct { /* Event IDs */ extern uint16_t WIDGET_SHELL_MOD_CHANGED; +extern uint16_t WIDGET_SHELL_CHAR_INPUT; /* Update frequency, ie. cap on the number of shell redraws per second. */ #define WIDGET_SHELL_FPS 30