Skip to content

Commit

Permalink
* Persistent console history
Browse files Browse the repository at this point in the history
  • Loading branch information
timangus committed Jan 24, 2006
1 parent 5a29e8d commit 8a6be4a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 6 deletions.
1 change: 1 addition & 0 deletions code/client/cl_console.c
Expand Up @@ -321,6 +321,7 @@ void Con_Init (void) {
Field_Clear( &historyEditLines[i] );
historyEditLines[i].widthInChars = g_console_field_width;
}
CL_LoadConsoleHistory( );

Cmd_AddCommand ("toggleconsole", Con_ToggleConsole_f);
Cmd_AddCommand ("messagemode", Con_MessageMode_f);
Expand Down
108 changes: 107 additions & 1 deletion code/client/cl_keys.c
Expand Up @@ -486,7 +486,7 @@ void Console_Key (int key) {
// if not in the game explicitly prepend a slash if needed
if ( cls.state != CA_ACTIVE && g_consoleField.buffer[0] != '\\'
&& g_consoleField.buffer[0] != '/' ) {
char temp[MAX_STRING_CHARS];
char temp[MAX_EDIT_LINE-1];

Q_strncpyz( temp, g_consoleField.buffer, sizeof( temp ) );
Com_sprintf( g_consoleField.buffer, sizeof( g_consoleField.buffer ), "\\%s", temp );
Expand Down Expand Up @@ -519,6 +519,8 @@ void Console_Key (int key) {

g_consoleField.widthInChars = g_console_field_width;

CL_SaveConsoleHistory( );

if ( cls.state == CA_DISCONNECTED ) {
SCR_UpdateScreen (); // force an update, because the command
} // may take some time
Expand Down Expand Up @@ -1256,3 +1258,107 @@ void Key_ClearStates (void)
}
}

// This must not exceed MAX_CMD_LINE
#define MAX_CONSOLE_SAVE_BUFFER 1024
static char consoleSaveBuffer[ MAX_CONSOLE_SAVE_BUFFER ];

/*
================
CL_LoadConsoleHistory
Load the console history from cl_consoleHistory
================
*/
void CL_LoadConsoleHistory( void )
{
char *token, *text_p;
int i, numChars, numLines = 0;
cvar_t *cv;

cv = Cvar_Get( "cl_consoleHistory", "", CVAR_ARCHIVE|CVAR_ROM );
Q_strncpyz( consoleSaveBuffer, cv->string, MAX_CONSOLE_SAVE_BUFFER );

text_p = consoleSaveBuffer;

for( i = COMMAND_HISTORY - 1; i >= 0; i-- )
{
if( !*( token = COM_Parse( &text_p ) ) )
break;

historyEditLines[ i ].cursor = atoi( token );

if( !*( token = COM_Parse( &text_p ) ) )
break;

historyEditLines[ i ].scroll = atoi( token );

if( !*( token = COM_Parse( &text_p ) ) )
break;

numChars = atoi( token );
text_p++;
if( numChars > ( strlen( consoleSaveBuffer ) - ( text_p - consoleSaveBuffer ) ) )
{
Com_DPrintf( S_COLOR_YELLOW "WARNING: probable corrupt history\n" );
break;
}
Com_Memcpy( historyEditLines[ i ].buffer,
text_p, numChars );
historyEditLines[ i ].buffer[ numChars ] = '\0';
text_p += numChars;

numLines++;
}

memmove( &historyEditLines[ 0 ], &historyEditLines[ i + 1 ],
numLines * sizeof( field_t ) );
for( i = numLines; i < COMMAND_HISTORY; i++ )
Field_Clear( &historyEditLines[ i ] );

historyLine = nextHistoryLine = numLines;
}

/*
================
CL_SaveConsoleHistory
Save the console history into the cvar cl_consoleHistory
so that it persists across invocations of q3
================
*/
void CL_SaveConsoleHistory( void )
{
int i;
int lineLength, saveBufferLength, additionalLength;

consoleSaveBuffer[ 0 ] = '\0';

i = ( nextHistoryLine - 1 ) % COMMAND_HISTORY;
do
{
if( historyEditLines[ i ].buffer[ 0 ] )
{
lineLength = strlen( historyEditLines[ i ].buffer );
saveBufferLength = strlen( consoleSaveBuffer );

//ICK "seta cl_consoleHistory " + "%d %d %d " = 23 + 13 = 36
additionalLength = lineLength + 36;

if( saveBufferLength + additionalLength < MAX_CONSOLE_SAVE_BUFFER )
{
Q_strcat( consoleSaveBuffer, MAX_CONSOLE_SAVE_BUFFER,
va( "%d %d %d %s ",
historyEditLines[ i ].cursor,
historyEditLines[ i ].scroll,
lineLength,
historyEditLines[ i ].buffer ) );
}
else
break;
}
i = ( i - 1 + COMMAND_HISTORY ) % COMMAND_HISTORY;
}
while( i != ( nextHistoryLine - 1 ) % COMMAND_HISTORY );

Cvar_Set( "cl_consoleHistory", consoleSaveBuffer );
}
2 changes: 2 additions & 0 deletions code/client/client.h
Expand Up @@ -452,6 +452,8 @@ void Con_Top( void );
void Con_Bottom( void );
void Con_Close( void );

void CL_LoadConsoleHistory( void );
void CL_SaveConsoleHistory( void );

//
// cl_scrn.c
Expand Down
5 changes: 4 additions & 1 deletion code/qcommon/common.c
Expand Up @@ -2965,8 +2965,11 @@ PrintCvarMatches
===============
*/
static void PrintCvarMatches( const char *s ) {
char value[ TRUNCATE_LENGTH ];

if ( !Q_stricmpn( s, shortestMatch, strlen( shortestMatch ) ) ) {
Com_Printf( " %s = \"%s\"\n", s, Cvar_VariableString( s ) );
Com_TruncateLongString( value, Cvar_VariableString( s ) );
Com_Printf( " %s = \"%s\"\n", s, value );
}
}

Expand Down
25 changes: 21 additions & 4 deletions code/qcommon/cvar.c
Expand Up @@ -469,7 +469,10 @@ Handles variable inspection and changing from the console
============
*/
qboolean Cvar_Command( void ) {
cvar_t *v;
cvar_t *v;
char string[ TRUNCATE_LENGTH ];
char resetString[ TRUNCATE_LENGTH ];
char latchedString[ TRUNCATE_LENGTH ];

// check variables
v = Cvar_FindVar (Cmd_Argv(0));
Expand All @@ -479,9 +482,13 @@ qboolean Cvar_Command( void ) {

// perform a variable print or set
if ( Cmd_Argc() == 1 ) {
Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\" default:\"%s" S_COLOR_WHITE "\"\n", v->name, v->string, v->resetString );
Com_TruncateLongString( string, v->string );
Com_TruncateLongString( resetString, v->resetString );
Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\" default:\"%s" S_COLOR_WHITE "\"\n",
v->name, string, resetString );
if ( v->latchedString ) {
Com_Printf( "latched: \"%s\"\n", v->latchedString );
Com_TruncateLongString( latchedString, v->latchedString );
Com_Printf( "latched: \"%s\"\n", latchedString );
}
return qtrue;
}
Expand Down Expand Up @@ -645,11 +652,21 @@ void Cvar_WriteVariables( fileHandle_t f ) {
if( var->flags & CVAR_ARCHIVE ) {
// write the latched value, even if it hasn't taken effect yet
if ( var->latchedString ) {
if( strlen( var->name ) + strlen( var->latchedString ) + 10 > sizeof( buffer ) ) {
Com_Printf( S_COLOR_YELLOW "WARNING: value of variable "
"\"%s\" too long to write to file\n", var->name );
continue;
}
Com_sprintf (buffer, sizeof(buffer), "seta %s \"%s\"\n", var->name, var->latchedString);
} else {
if( strlen( var->name ) + strlen( var->string ) + 10 > sizeof( buffer ) ) {
Com_Printf( S_COLOR_YELLOW "WARNING: value of variable "
"\"%s\" too long to write to file\n", var->name );
continue;
}
Com_sprintf (buffer, sizeof(buffer), "seta %s \"%s\"\n", var->name, var->string);
}
FS_Printf (f, "%s", buffer);
FS_Write( buffer, strlen( buffer ), f );
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions code/qcommon/q_shared.c
Expand Up @@ -918,6 +918,26 @@ char * QDECL va( char *format, ... ) {
return buf;
}

/*
============
Com_TruncateLongString
Assumes buffer is atleast TRUNCATE_LENGTH big
============
*/
void Com_TruncateLongString( char *buffer, const char *s )
{
int length = strlen( s );

if( length <= TRUNCATE_LENGTH )
Q_strncpyz( buffer, s, TRUNCATE_LENGTH );
else
{
Q_strncpyz( buffer, s, ( TRUNCATE_LENGTH / 2 ) - 3 );
Q_strcat( buffer, TRUNCATE_LENGTH, " ... " );
Q_strcat( buffer, TRUNCATE_LENGTH, s + length - ( TRUNCATE_LENGTH / 2 ) + 3 );
}
}

/*
=====================================================================
Expand Down
3 changes: 3 additions & 0 deletions code/qcommon/q_shared.h
Expand Up @@ -682,6 +682,9 @@ void Swap_Init (void);
*/
char * QDECL va(char *format, ...);

#define TRUNCATE_LENGTH 64
void Com_TruncateLongString( char *buffer, const char *s );

//=============================================

//
Expand Down

0 comments on commit 8a6be4a

Please sign in to comment.