Browse Source

feat: add #format {%P} and {%W}

    #format {test} {%P}   {string}  strip ESCAPE CODES and COLORs
    #format {test} {%W}   {string}  get the screen width of string
dzp 3 years ago
parent
commit
e0ca3d8db4
3 changed files with 17 additions and 0 deletions
  1. 2 0
      src/help.c
  2. 1 0
      src/tintin.h
  3. 14 0
      src/variable.c

+ 2 - 0
src/help.c

@@ -1869,9 +1869,11 @@ struct help_type help_table[] =
 		"<278>         #format {hash} {%H}   {string}  store a 64 bit string hash in {hash}\n"
 		"<278>         #format {hash} {%H}   {string}  store a 64 bit string hash in {hash}\n"
 		"<278>         #format {test} {%L}   {string}  store the string length in {test}\n"
 		"<278>         #format {test} {%L}   {string}  store the string length in {test}\n"
 		"<278>         #format {test} {%M}   {number}  convert number to metric in {test}\n"
 		"<278>         #format {test} {%M}   {number}  convert number to metric in {test}\n"
+		"<278>         #format {test} {%P}   {string}  strip ESCAPE CODES and COLORs\n"
 		"<278>         #format {test} {%S}   {string}  store the number of spelling errors\n"
 		"<278>         #format {test} {%S}   {string}  store the number of spelling errors\n"
 		"<278>         #format {time} {%T}         {}  store the epoch time in {time}\n"
 		"<278>         #format {time} {%T}         {}  store the epoch time in {time}\n"
 		"<278>         #format {time} {%U}         {}  store the micro epoch time in {time}\n"
 		"<278>         #format {time} {%U}         {}  store the micro epoch time in {time}\n"
+		"<278>         #format {test} {%W}   {string}  get the screen width of string\n"
 		"<278>         #format {test} {%X}      {dec}  convert dec to hexadecimal in {test}\n\n"
 		"<278>         #format {test} {%X}      {dec}  convert dec to hexadecimal in {test}\n\n"
 		"<278>         #format {test} {%%}             a literal % character\n"
 		"<278>         #format {test} {%%}             a literal % character\n"
 		"\n"
 		"\n"

+ 1 - 0
src/tintin.h

@@ -2773,6 +2773,7 @@ extern int get_raw_off_str_range_raw_width(struct session *ses, char *str, int s
 
 
 extern int raw_len_str(struct session *ses, char *str, int start, int end);
 extern int raw_len_str(struct session *ses, char *str, int start, int end);
 extern int str_len_raw(struct session *ses, char *str, int start, int end);
 extern int str_len_raw(struct session *ses, char *str, int start, int end);
+extern int str_len_str(struct session *ses, char *str, int start, int end);
 
 
 extern char *str_ins_str(struct session *ses, char **str, char *ins, int str_start, int str_end);
 extern char *str_ins_str(struct session *ses, char **str, char *ins, int str_start, int str_end);
 
 

+ 14 - 0
src/variable.c

@@ -1588,6 +1588,15 @@ void format_string(struct session *ses, char *format, char *arg, char *out)
 						metricgroupingstring(ses, arglist[i]);
 						metricgroupingstring(ses, arglist[i]);
 						break;
 						break;
 
 
+					case 'P': {
+						char *strip;
+						substitute(ses, arglist[i], arglist[i], SUB_VAR|SUB_FUN|SUB_ESC|SUB_COL);
+						strip = str_alloc_stack(0);
+						strip_vt102_codes(arglist[i], strip);
+						sprintf(arglist[i], "%s", strip);
+						break;
+					}
+
 					case 'R':
 					case 'R':
 						if (*arglist[i] == 0)
 						if (*arglist[i] == 0)
 						{
 						{
@@ -1611,6 +1620,11 @@ void format_string(struct session *ses, char *format, char *arg, char *out)
 						sprintf(arglist[i], "%lld", utime());
 						sprintf(arglist[i], "%lld", utime());
 						break;
 						break;
 
 
+					case 'W':
+						substitute(ses, arglist[i], arglist[i], SUB_VAR|SUB_FUN|SUB_ESC|SUB_COL);
+						sprintf(arglist[i], "%d", str_len_str(ses, arglist[i], 0, strlen(arglist[i])));
+						break;
+
 					case 'X':
 					case 'X':
 						strcat(argformat, "llX");
 						strcat(argformat, "llX");
 						charactertohex(ses, arglist[i]);
 						charactertohex(ses, arglist[i]);