From 1ce9bdba51c9921dc918d28a01868a4f821bd67c Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Wed, 20 May 2015 14:59:32 +0200 Subject: [PATCH] Implemented a persistent, per game/mod console history it's saved in $HOME/.yq2/$mod/history.txt While I was at it, I made the max number of lines in the history configurable at compiletime by introducing a NUM_KEY_LINES #define --- src/client/cl_console.c | 4 +- src/client/cl_keyboard.c | 86 ++++++++++++++++++++++++++++++++---- src/client/cl_main.c | 4 ++ src/client/header/keyboard.h | 10 +++++ 4 files changed, 92 insertions(+), 12 deletions(-) diff --git a/src/client/cl_console.c b/src/client/cl_console.c index e69338c5..c5dbe51d 100644 --- a/src/client/cl_console.c +++ b/src/client/cl_console.c @@ -30,9 +30,7 @@ console_t con; cvar_t *con_notifytime; -#define MAXCMDLINE 256 - -extern char key_lines[32][MAXCMDLINE]; +extern char key_lines[NUM_KEY_LINES][MAXCMDLINE]; extern int edit_line; extern int key_linepos; diff --git a/src/client/cl_keyboard.c b/src/client/cl_keyboard.c index 503b6787..8c2befd8 100644 --- a/src/client/cl_keyboard.c +++ b/src/client/cl_keyboard.c @@ -36,9 +36,7 @@ static cvar_t *cfg_unbindall; * key up events are sent even if in console mode */ -#define MAXCMDLINE 256 - -char key_lines[32][MAXCMDLINE]; +char key_lines[NUM_KEY_LINES][MAXCMDLINE]; int key_linepos; int anykeydown; @@ -283,7 +281,7 @@ Key_Console(int key) Cbuf_AddText("\n"); Com_Printf("%s\n", key_lines[edit_line]); - edit_line = (edit_line + 1) & 31; + edit_line = (edit_line + 1) & (NUM_KEY_LINES-1); history_line = edit_line; key_lines[edit_line][0] = ']'; key_linepos = 1; @@ -329,14 +327,14 @@ Key_Console(int key) { do { - history_line = (history_line - 1) & 31; + history_line = (history_line - 1) & (NUM_KEY_LINES-1); } while (history_line != edit_line && !key_lines[history_line][1]); if (history_line == edit_line) { - history_line = (edit_line + 1) & 31; + history_line = (edit_line + 1) & (NUM_KEY_LINES-1); } strcpy(key_lines[edit_line], key_lines[history_line]); @@ -354,7 +352,7 @@ Key_Console(int key) do { - history_line = (history_line + 1) & 31; + history_line = (history_line + 1) & (NUM_KEY_LINES-1); } while (history_line != edit_line && !key_lines[history_line][1]); @@ -786,6 +784,76 @@ Key_WriteBindings(FILE *f) } } +void +Key_WriteConsoleHistory() +{ + int i; + char path[MAX_OSPATH]; + Com_sprintf(path, sizeof(path), "%s/history.txt", FS_Gamedir()); + + FILE* f = fopen(path, "w"); + + if(f==NULL) + { + Com_Printf("Opening console history %s for writing failed!\n", path); + return; + } + + // save the oldest lines first by starting at edit_line + // and going forward (and wrapping around) + for(i=0; i= 0) + { + key_lines[i][lastCharIdx] = '\0'; + --lastCharIdx; + } + } + + fclose(f); +} + void Key_Bindlist_f(void) { @@ -804,12 +872,12 @@ void Key_Init(void) { int i; - - for (i = 0; i < 32; i++) + for (i = 0; i < NUM_KEY_LINES; i++) { key_lines[i][0] = ']'; key_lines[i][1] = 0; } + // can't call Key_ReadConsoleHistory() here because FS_Gamedir() isn't set yet key_linepos = 1; diff --git a/src/client/cl_main.c b/src/client/cl_main.c index 4163f255..46d8d0a8 100644 --- a/src/client/cl_main.c +++ b/src/client/cl_main.c @@ -899,6 +899,8 @@ CL_Init(void) FS_ExecAutoexec(); Cbuf_Execute(); + + Key_ReadConsoleHistory(); } void @@ -916,6 +918,8 @@ CL_Shutdown(void) CL_WriteConfiguration(); + Key_WriteConsoleHistory(); + #ifdef CDA CDAudio_Shutdown(); #endif diff --git a/src/client/header/keyboard.h b/src/client/header/keyboard.h index e8474ea2..f91d12a6 100644 --- a/src/client/header/keyboard.h +++ b/src/client/header/keyboard.h @@ -27,6 +27,14 @@ #ifndef CL_HEADER_KEYBOARD_H #define CL_HEADER_KEYBOARD_H +/* max length of a console command line */ +#define MAXCMDLINE 256 + +/* number of console command lines saved in history, + * must be a power of two, because we use & (NUM_KEY_LINES-1) + * instead of % so -1 wraps to NUM_KEY_LINES-1 */ +#define NUM_KEY_LINES 32 + /* these are the key numbers that should be passed to Key_Event they must be mached by the low level key event processing! */ enum QKEYS { @@ -291,6 +299,8 @@ void Char_Event(int key); void Key_Event(int key, qboolean down, qboolean special); void Key_Init(void); void Key_WriteBindings(FILE *f); +void Key_ReadConsoleHistory(); +void Key_WriteConsoleHistory(); void Key_SetBinding(int keynum, char *binding); void Key_MarkAllUp(void); int Key_GetKey(void);