Commit 21ca5d70 authored by rswindell's avatar rswindell
Browse files

getstr() - both the C++ and JS (console method) versions, now accept an

additional, optional, "history" argument - an array of strings that can
be recalled/scrolled using the up/down arrow keys. Currently, I just plan
to use this for netmail destination address recall, but it could be used
other places easily enough.
parent 95af2c33
......@@ -43,13 +43,14 @@
/* a word, ^X backspaces a line, ^Gs, BSs, TABs are processed, LFs ignored. */
/* ^N non-destructive BS, ^V center line. Valid keys are echoed. */
/****************************************************************************/
size_t sbbs_t::getstr(char *strout, size_t maxlen, long mode)
size_t sbbs_t::getstr(char *strout, size_t maxlen, long mode, const str_list_t history)
{
size_t i,l,x,z; /* i=current position, l=length, j=printed chars */
/* x&z=misc */
char str1[256],str2[256],undo[256];
uchar ch;
uchar atr;
int hidx = -1;
console&=~(CON_UPARROW|CON_DOWNARROW|CON_LEFTARROW|CON_BACKSPACE|CON_DELETELINE);
if(!(mode&K_WRAP))
......@@ -436,7 +437,40 @@ size_t sbbs_t::getstr(char *strout, size_t maxlen, long mode)
i--;
}
break;
case TERM_KEY_DOWN:
if(history != NULL) {
if(hidx < 0) {
outchar(BEL);
break;
}
hidx--;
if(hidx < 0)
SAFECOPY(str1, undo);
else
SAFECOPY(str1, history[hidx]);
while(i--)
backspace();
i=l=strlen(str1);
rputs(str1);
cleartoeol();
break;
}
break;
case TERM_KEY_UP: /* Ctrl-^/Up Arrow */
if(history != NULL) {
if(history[hidx + 1] == NULL) {
outchar(BEL);
break;
}
hidx++;
while(i--)
backspace();
SAFECOPY(str1, history[hidx]);
i=l=strlen(str1);
rputs(str1);
cleartoeol();
break;
}
if(!(mode&K_EDIT))
break;
#if 1
......
......@@ -500,6 +500,7 @@ js_getstr(JSContext *cx, uintN argc, jsval *arglist)
sbbs_t* sbbs;
JSString* js_str=NULL;
jsrefcount rc;
str_list_t history = NULL;
JS_SET_RVAL(cx, arglist, JSVAL_VOID);
......@@ -516,13 +517,33 @@ js_getstr(JSContext *cx, uintN argc, jsval *arglist)
if(!JS_ValueToInt32(cx,argv[i],&mode))
return JS_FALSE;
}
continue;
}
if(JSVAL_IS_STRING(argv[i])) {
else if(JSVAL_IS_STRING(argv[i])) {
js_str = JS_ValueToString(cx, argv[i]);
if (!js_str)
return(JS_FALSE);
}
else if(JSVAL_IS_OBJECT(argv[i])) {
JSObject* array = JSVAL_TO_OBJECT(argv[i]);
jsuint len=0;
if(!JS_GetArrayLength(cx, array, &len))
return JS_FALSE;
history = (str_list_t)alloca(sizeof(char*) * (len + 1));
memset(history, 0, sizeof(char*) * (len + 1));
for(jsuint j=0; j < len; j++) {
jsval val;
if(!JS_GetElement(cx, array, j, &val))
break;
JSString* hist = JS_ValueToString(cx, val);
if (hist == NULL)
return JS_FALSE;
char* cstr = NULL;
JSSTRING_TO_ASTRING(cx, hist, cstr, (uint)(maxlen ? maxlen : 80), NULL);
if(cstr == NULL)
return JS_FALSE;
history[j] = cstr;
}
}
}
if(!maxlen) maxlen=128;
......@@ -541,7 +562,7 @@ js_getstr(JSContext *cx, uintN argc, jsval *arglist)
}
rc=JS_SUSPENDREQUEST(cx);
sbbs->getstr(p,maxlen,mode);
sbbs->getstr(p, maxlen, mode, history);
JS_RESUMEREQUEST(cx, rc);
js_str = JS_NewStringCopyZ(cx, p);
......
......@@ -714,7 +714,7 @@ public:
/* getstr.cpp */
size_t getstr_offset;
size_t getstr(char *str, size_t length, long mode);
size_t getstr(char *str, size_t length, long mode, const str_list_t history = NULL);
long getnum(ulong max, ulong dflt=0);
void insert_indicator(void);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment