From: Ken Hornstein Date: Fri, 3 Jan 2014 20:55:10 +0000 (-0500) Subject: Add support for %(hascolor), and a new function for parameterized X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/6125f5f8e7f544ccdcecb3d216c9c1035d693b4e?ds=sidebyside Add support for %(hascolor), and a new function for parameterized terminfo capabilities. --- diff --git a/h/prototypes.h b/h/prototypes.h index adedd6a2..26666eae 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -112,13 +112,47 @@ char *getfolder(int); * * Arguments: * - * capability - The name of a terminfo capability (see terminfo(5)). + * capability - The name of the terminfo capability (see terminfo(5)). * * Returns a tputs-processed string, or NULL if terminal initialization failed * or the capability wasn't found. */ char *get_term_stringcap(char *capability); +/* + * Get a parameterized string from the terminfo database for the current + * terminal. + * + * We don't yet have a standardized tparm() that will take a stdarg + * argument. Right now we don't want many parameters, so we only + * take two. Everything gets passed to tparm() as-is. If we need + * a capability with more arguments, we'll just add more later. + * + * Arguments: + * + * capability - The name of the terminfo capability (see terminfo(5)). + * arg1..argN - Arguments 1-N. + * + * Returns a tparm and tputs-processed string, or NULL if there was a problem + * initializating the terminal or retrieving the capability. + */ +char *get_term_stringparm(char *capability, long arg1, long arg2); + +/* + * Get a number from the terminfo database for the current terminal. + * + * Retrieve the specified terminfo capability and return the numeric + * value of that capability from the terminfo database. + * + * Arguments: + * + * capability - The name of the terminfo capability (see terminfo(5)). + * + * Returns the output of tigetnum() for that capability, or -1 if it was + * unable to initialize the terminfo database. + */ +int get_term_numcap(char *capability); + /* * Lock open/close routines. * diff --git a/sbr/fmt_compile.c b/sbr/fmt_compile.c index 7392c8c6..2e1a0ff5 100644 --- a/sbr/fmt_compile.c +++ b/sbr/fmt_compile.c @@ -86,6 +86,7 @@ extern struct mailname fmt_mnull; #define TF_UNDERLN 13 /* special - enter underline mode */ #define TF_STNDOUT 14 /* special - enter underline mode */ #define TF_RESET 15 /* special - reset terminal modes */ +#define TF_HASCLR 16 /* special - terminal have color? */ /* ftable->flags */ /* NB that TFL_PUTS is also used to decide whether the test @@ -223,6 +224,7 @@ static struct ftable functable[] = { { "underline", TF_UNDERLN,FT_LS_LIT, 0, TFL_PUTS }, { "standout", TF_STNDOUT,FT_LS_LIT, 0, TFL_PUTS }, { "resetterm", TF_RESET, FT_LS_LIT, 0, TFL_PUTS }, + { "hascolor", TF_HASCLR, FT_LV_LIT, 0, 0 }, { NULL, 0, 0, 0, 0 } }; @@ -656,6 +658,10 @@ do_func(char *sp) LS(t->f_type, get_term_stringcap("sgr0")); break; + case TF_HASCLR: + LV(t->f_type, get_term_numcap("colors") > 1); + break; + case TF_NOW: LV(t->f_type, time((time_t *) 0)); break; diff --git a/sbr/terminal.c b/sbr/terminal.c index fe7619be..89d0dafc 100644 --- a/sbr/terminal.c +++ b/sbr/terminal.c @@ -180,6 +180,52 @@ get_term_stringcap(char *capability) return termcbuf; } +/* + * Return a parameterized terminfo capability + */ + +char * +get_term_stringparm(char *capability, long arg1, long arg2) +{ + char *parm; + + initialize_terminfo(); + + if (termstatus == -1) + return NULL; + + termcbufp = termcbuf; + + parm = tigetstr(capability); + + if (parm == (char *) -1 || parm == NULL) { + return NULL; + } + + parm = tparm(parm, arg1, arg2, 0, 0, 0, 0, 0, 0, 0); + + tputs(parm, 1, termbytes); + + termcbufp = '\0'; + + return termcbuf; +} + +/* + * Return the value of the specified numeric capability + */ + +int +get_term_numcap(char *capability) +{ + initialize_terminfo(); + + if (termstatus == -1) + return -1; + + return tigetnum(capability); +} + /* * Store a sequence of characters in our local buffer */