Merge pull request #73 from yquake2/console-history

Yamagi said it's ok
This commit is contained in:
Daniel Gibson 2015-05-31 18:28:18 +02:00
commit 27bdaa7650
4 changed files with 96 additions and 12 deletions

View file

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

View file

@ -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,80 @@ Key_WriteBindings(FILE *f)
}
}
void
Key_WriteConsoleHistory()
{
int i;
char path[MAX_OSPATH];
Com_sprintf(path, sizeof(path), "%sconsole_history.txt", Sys_GetHomeDir());
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)
const char* lastWrittenLine = "";
for(i=0; i<NUM_KEY_LINES; ++i)
{
int lineIdx = (edit_line+i) & (NUM_KEY_LINES-1);
const char* line = key_lines[lineIdx];
if(line[1] != '\0' && strcmp(lastWrittenLine, line ) != 0)
{
// if the line actually contains something besides the ] prompt,
// and is not identical to the last written line, write it to the file
fputs(line, f);
fputc('\n', f);
lastWrittenLine = line;
}
}
fclose(f);
}
/* initializes key_lines from history file, if available */
void
Key_ReadConsoleHistory()
{
int i;
char path[MAX_OSPATH];
Com_sprintf(path, sizeof(path), "%sconsole_history.txt", Sys_GetHomeDir());
FILE* f = fopen(path, "r");
if(f==NULL)
{
Com_DPrintf("Opening console history %s for reading failed!\n", path);
return;
}
for (i = 0; i < NUM_KEY_LINES; i++)
{
if(fgets(key_lines[i], MAXCMDLINE, f) == NULL)
{
// probably EOF.. adjust edit_line and history_line and we're done here
edit_line = i;
history_line = i;
break;
}
// remove trailing newlines
int lastCharIdx = strlen(key_lines[i])-1;
while((key_lines[i][lastCharIdx] == '\n' || key_lines[i][lastCharIdx] == '\r') && lastCharIdx >= 0)
{
key_lines[i][lastCharIdx] = '\0';
--lastCharIdx;
}
}
fclose(f);
}
void
Key_Bindlist_f(void)
{
@ -804,12 +876,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;

View file

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

View file

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