pe: import modules selected in file browser

This commit is contained in:
Lephenixnoir 2022-11-08 22:21:42 +01:00
parent 5f1a066c94
commit 9ea4c17112
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
5 changed files with 82 additions and 44 deletions

View File

@ -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 ===//

View File

@ -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 ===//

View File

@ -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;

View File

@ -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();
}

View File

@ -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