Skip to content
Snippets Groups Projects
Commit 98df700c authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add strip_ansi() function, expose as JS method

A function/method to strip all ANSI terminal control sequences from a string.
parent fb4c88c6
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #464 canceled
......@@ -1115,6 +1115,37 @@ js_strip_ctrl(JSContext *cx, uintN argc, jsval *arglist)
return(JS_TRUE);
}
static JSBool
js_strip_ansi(JSContext *cx, uintN argc, jsval *arglist)
{
jsval *argv=JS_ARGV(cx, arglist);
char* buf = NULL;
JSString* js_str;
jsrefcount rc;
JS_SET_RVAL(cx, arglist, JSVAL_VOID);
if(argc==0 || JSVAL_IS_VOID(argv[0]))
return JS_TRUE;
JSVALUE_TO_MSTRING(cx, argv[0], buf, NULL)
HANDLE_PENDING(cx, buf);
if(buf==NULL)
return JS_TRUE;
rc=JS_SUSPENDREQUEST(cx);
strip_ansi(buf);
JS_RESUMEREQUEST(cx, rc);
js_str = JS_NewStringCopyZ(cx, buf);
free(buf);
if(js_str==NULL)
return JS_FALSE;
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
return JS_TRUE;
}
static JSBool
js_strip_exascii(JSContext *cx, uintN argc, jsval *arglist)
{
......@@ -4574,6 +4605,10 @@ static jsSyncMethodSpec js_global_functions[] = {
,JSDOCSTR("strip control characters from string, returns modified string")
,310
},
{"strip_ansi", js_strip_ansi, 1, JSTYPE_STRING, JSDOCSTR("text")
,JSDOCSTR("strip all ANSI terminal control sequences from string, returns modified string")
,31802
},
{"strip_exascii", js_strip_exascii, 1, JSTYPE_STRING, JSDOCSTR("text")
,JSDOCSTR("strip all extended-ASCII characters from string, returns modified string")
,310
......
......@@ -1221,6 +1221,7 @@ extern "C" {
DLLEXPORT BOOL trashcan(scfg_t* cfg, const char *insearch, const char *name);
DLLEXPORT char * trashcan_fname(scfg_t* cfg, const char *name, char* fname, size_t);
DLLEXPORT str_list_t trashcan_list(scfg_t* cfg, const char* name);
DLLEXPORT char * strip_ansi(char* str);
DLLEXPORT char * strip_exascii(const char *str, char* dest);
DLLEXPORT char * strip_space(const char *str, char* dest);
DLLEXPORT char * prep_file_desc(const char *str, char* dest);
......
......@@ -89,6 +89,25 @@ char* strip_ctrl(const char *str, char* dest)
return dest;
}
char* strip_ansi(char* str)
{
char* s = str;
char* d = str;
while(*s != '\0') {
if(*s == ESC && *(s + 1) == '[') {
s += 2;
while(*s != '\0' && (*s < '@' || *s > '~'))
s++;
if(*s != '\0') // Skip "final byte""
s++;
} else {
*(d++) = *(s++);
}
}
*d = '\0';
return str;
}
char* strip_exascii(const char *str, char* dest)
{
int i,j;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment