From: hgn Date: Mon, 16 Jan 2023 03:27:15 +0000 (+0000) Subject: console amenities X-Git-Url: https://harrygodden.com/git/?a=commitdiff_plain;ds=sidebyside;h=382274fcde3b501fa3979ef70d53dae2b02857ba;p=vg.git console amenities --- diff --git a/vg_console.h b/vg_console.h index e18515d..8f8517c 100644 --- a/vg_console.h +++ b/vg_console.h @@ -56,9 +56,23 @@ struct vg_console } functions[ 32 ]; + struct + { + const char *str; + int len; + + u32 lev_score; + } + suggestions[12]; + u32 suggestion_count; + int suggestion_select, + suggestion_pastepos, + suggestion_maxlen; + u32 convar_count, function_count; - char input[96]; + char input[96], + input_copy[96]; int cursor_user, cursor_pos, string_length; char history[32][96]; @@ -136,9 +150,12 @@ VG_STATIC void _vg_console_draw( void ) vg_uictx.cursor[3] = log_lines*fh; ui_fill_x(); + /* + * log + */ ui_new_node(); { - ui_fill_rect( vg_uictx.cursor, 0x77333333 ); + ui_fill_rect( vg_uictx.cursor, 0x77181818 ); vg_uictx.cursor[3] = fh; ui_align_bottom(); @@ -157,12 +174,14 @@ VG_STATIC void _vg_console_draw( void ) } ui_end_down(); + + /* Input area */ vg_uictx.cursor[1] += 2; vg_uictx.cursor[3] = fh; - + ui_new_node(); { - ui_fill_rect( vg_uictx.cursor, 0x77333333 ); + ui_fill_rect( vg_uictx.cursor, 0x77111111 ); ui_text( vg_uictx.cursor, vg_console.input, 1, 0 ); int start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user ), @@ -175,6 +194,33 @@ VG_STATIC void _vg_console_draw( void ) ui_fill_rect( vg_uictx.cursor, 0x66ffffff ); } ui_end_down(); + + + + /* suggestions */ + if( vg_console.suggestion_count ) + { + vg_uictx.cursor[1] += 2; + vg_uictx.cursor[3] = vg_console.suggestion_count * fh; + vg_uictx.cursor[2] = UI_GLYPH_SPACING_X * vg_console.suggestion_maxlen; + + ui_new_node(); + { + ui_fill_rect( vg_uictx.cursor, 0x77040404 ); + + vg_uictx.cursor[3] = fh; + for( int i=0; i=0; j -- ) + if( score > vg_console.suggestions[j].lev_score ) + best_pos = j; + + /* insert if good score */ + if( best_pos < vg_list_size( vg_console.suggestions ) ) + { + int start = VG_MIN( vg_console.suggestion_count, + vg_list_size( vg_console.suggestions )-1 ); + for( int j=start; j>best_pos; j -- ) + vg_console.suggestions[j] = vg_console.suggestions[j-1]; + + vg_console.suggestions[ best_pos ].str = str; + vg_console.suggestions[ best_pos ].len = strlen( str ); + vg_console.suggestions[ best_pos ].lev_score = score; + + if( vg_console.suggestion_count < + vg_list_size( vg_console.suggestions ) ) + vg_console.suggestion_count ++; + } +} + +VG_STATIC void console_update_suggestions(void) +{ + vg_console.suggestion_count = 0; + vg_console.suggestion_select = -1; + vg_console.suggestion_maxlen = 0; + + /* find current term */ + int start_index = 0; + + for( int i=0; 1; i++ ) + { + if( !vg_console.input[i] ) + break; + + if( isspace( vg_console.input[i] ) ) + { + start_index = i; + + /* TODO: not just functions + */ + + return; + } + } + + vg_console.suggestion_pastepos = start_index; + const char *input_ref = &vg_console.input[ start_index ]; + + /* Score all our commands and cvars */ + for( int i=0; iname ); + } + + for( int i=0; iname ); + } + + /* some post processing */ + for( int i=0; i= vg_console.suggestion_count ) + vg_console.suggestion_select = -1; + + _console_fetch_suggestion(); + } +} + +VG_STATIC void _console_suggest_prev(void) +{ + if( vg_console.suggestion_count ) + { + _console_suggest_store_normal(); + + vg_console.suggestion_select --; + + if( vg_console.suggestion_select < -1 ) + vg_console.suggestion_select = vg_console.suggestion_count-1; + + _console_fetch_suggestion(); + } +} + +/* + * Handles binds + */ VG_STATIC void console_proc_key( SDL_Keysym ev ) { /* Open / close console */ @@ -681,10 +950,21 @@ VG_STATIC void console_proc_key( SDL_Keysym ev ) { KMOD_CTRL, SDLK_c, console_to_clipboard }, { KMOD_CTRL, SDLK_x, _console_cut }, { KMOD_CTRL, SDLK_v, console_clipboard_paste }, - { 0, SDLK_RETURN, _console_enter } + { 0, SDLK_RETURN, _console_enter }, + { KMOD_CTRL, SDLK_n, _console_suggest_next }, + { KMOD_CTRL, SDLK_p, _console_suggest_prev } }; - SDL_Keymod mod = ev.mod & (KMOD_SHIFT|KMOD_CTRL|KMOD_ALT); + SDL_Keymod mod = 0; + + if( ev.mod & KMOD_SHIFT ) + mod |= KMOD_SHIFT; + + if( ev.mod & KMOD_CTRL ) + mod |= KMOD_CTRL; + + if( ev.mod & KMOD_ALT ) + mod |= KMOD_ALT; for( int i=0; i