mirror of
https://github.com/php/php-src.git
synced 2025-08-18 15:08:55 +02:00
get good control over colors and prompt
This commit is contained in:
parent
c85f247eac
commit
ddcfa42f15
7 changed files with 207 additions and 160 deletions
111
phpdbg_utils.c
111
phpdbg_utils.c
|
@ -34,12 +34,20 @@ ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
|
|||
|
||||
/* {{{ color structures */
|
||||
const static phpdbg_color_t colors[] = {
|
||||
PHPDBG_COLOR_D("white", "0:0"),
|
||||
PHPDBG_COLOR_D("red", "0:31"),
|
||||
PHPDBG_COLOR_D("green", "0:32"),
|
||||
PHPDBG_COLOR_D("blue", "0;34"),
|
||||
PHPDBG_COLOR_D("purple", "0;35"),
|
||||
PHPDBG_COLOR_D("cyan", "0;36"),
|
||||
PHPDBG_COLOR_D("none", "0;0"),
|
||||
|
||||
PHPDBG_COLOR_D("white", "0;64"),
|
||||
PHPDBG_COLOR_D("white-bold", "1;64"),
|
||||
PHPDBG_COLOR_D("red", "0;31"),
|
||||
PHPDBG_COLOR_D("red-bold", "1;31"),
|
||||
PHPDBG_COLOR_D("green", "0;32"),
|
||||
PHPDBG_COLOR_D("green-bold", "1;32"),
|
||||
PHPDBG_COLOR_D("blue", "0;34"),
|
||||
PHPDBG_COLOR_D("blue-bold", "1;34"),
|
||||
PHPDBG_COLOR_D("purple", "0;35"),
|
||||
PHPDBG_COLOR_D("purple-bold", "1;35"),
|
||||
PHPDBG_COLOR_D("cyan", "0;36"),
|
||||
PHPDBG_COLOR_D("cyan-bold", "1;36"),
|
||||
PHPDBG_COLOR_END
|
||||
}; /* }}} */
|
||||
|
||||
|
@ -170,35 +178,35 @@ PHPDBG_API int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ..
|
|||
|
||||
switch (type) {
|
||||
case P_ERROR:
|
||||
rc = fprintf(fp, "%s%s%s\n",
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[1;31m[" : "["),
|
||||
buffer,
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "]\033[0m" : "]"));
|
||||
if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
|
||||
rc = fprintf(fp,
|
||||
"\033[%sm[%s]\033[0m\n",
|
||||
PHPDBG_G(colors)[PHPDBG_COLOR_ERROR]->code, buffer);
|
||||
} else {
|
||||
rc = fprintf(fp, "[%s]\n", buffer);
|
||||
}
|
||||
break;
|
||||
|
||||
case P_NOTICE:
|
||||
rc = fprintf(fp, "%s%s%s\n",
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[1;64m[" : "["),
|
||||
buffer,
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "]\033[0m" : "]"));
|
||||
if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
|
||||
rc = fprintf(fp,
|
||||
"\033[%sm[%s]\033[0m\n",
|
||||
PHPDBG_G(colors)[PHPDBG_COLOR_NOTICE]->code, buffer);
|
||||
} else {
|
||||
rc = fprintf(fp, "[%s]\n", buffer);
|
||||
}
|
||||
break;
|
||||
|
||||
case P_WRITELN: {
|
||||
if (buffer) {
|
||||
rc = fprintf(fp, "%s%s%s\n",
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[37m" : ""),
|
||||
buffer,
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[0m" : ""));
|
||||
rc = fprintf(fp, "%s\n", buffer);
|
||||
} else {
|
||||
rc = fprintf(fp, "\n");
|
||||
}
|
||||
} break;
|
||||
|
||||
case P_WRITE: if (buffer) {
|
||||
rc = fprintf(fp, "%s%s%s",
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[37m" : ""),
|
||||
buffer,
|
||||
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[0m" : ""));
|
||||
rc = fprintf(fp, "%s", buffer);
|
||||
} break;
|
||||
|
||||
/* no formatting on logging output */
|
||||
|
@ -218,17 +226,74 @@ PHPDBG_API int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ..
|
|||
return rc;
|
||||
} /* }}} */
|
||||
|
||||
PHPDBG_API const phpdbg_color_t* phpdbg_get_color(const char *name, size_t name_length) /* {{{ */
|
||||
PHPDBG_API const phpdbg_color_t* phpdbg_get_color(const char *name, size_t name_length TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
const phpdbg_color_t *color = colors;
|
||||
|
||||
while (color && color->name) {
|
||||
if (name_length == color->name_length &&
|
||||
memcmp(name, color->name, name_length) == SUCCESS) {
|
||||
phpdbg_debug(
|
||||
"phpdbg_get_color(%s, %lu): %s", name, name_length, color->code);
|
||||
return color;
|
||||
}
|
||||
++color;
|
||||
}
|
||||
|
||||
phpdbg_debug(
|
||||
"phpdbg_get_color(%s, %lu): failed", name, name_length);
|
||||
|
||||
return NULL;
|
||||
} /* }}} */
|
||||
|
||||
PHPDBG_API void phpdbg_set_color(int element, const phpdbg_color_t *color TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
PHPDBG_G(colors)[element] = color;
|
||||
} /* }}} */
|
||||
|
||||
PHPDBG_API void phpdbg_set_color_ex(int element, const char *name, size_t name_length TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
const phpdbg_color_t *color = phpdbg_get_color(name, name_length TSRMLS_CC);
|
||||
if (color) {
|
||||
phpdbg_set_color(element, color TSRMLS_CC);
|
||||
} else PHPDBG_G(colors)[element] = colors;
|
||||
} /* }}} */
|
||||
|
||||
PHPDBG_API void phpdbg_set_prompt(const char *prompt TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
/* free formatted prompt */
|
||||
if (PHPDBG_G(prompt)[1]) {
|
||||
free(PHPDBG_G(prompt)[1]);
|
||||
PHPDBG_G(prompt)[1] = NULL;
|
||||
}
|
||||
/* free old prompt */
|
||||
if (PHPDBG_G(prompt)[0]) {
|
||||
free(PHPDBG_G(prompt)[0]);
|
||||
PHPDBG_G(prompt)[0] = NULL;
|
||||
}
|
||||
|
||||
/* copy new prompt */
|
||||
PHPDBG_G(prompt)[0] = strdup(prompt);
|
||||
} /* }}} */
|
||||
|
||||
PHPDBG_API const char *phpdbg_get_prompt(TSRMLS_D) /* {{{ */
|
||||
{
|
||||
/* find cached prompt */
|
||||
if (PHPDBG_G(prompt)[1]) {
|
||||
return PHPDBG_G(prompt)[1];
|
||||
}
|
||||
|
||||
/* create cached prompt */
|
||||
if ((PHPDBG_G(flags) & PHPDBG_IS_COLOURED)) {
|
||||
asprintf(
|
||||
&PHPDBG_G(prompt)[1], "\033[%sm%s\033[0m ",
|
||||
PHPDBG_G(colors)[PHPDBG_COLOR_PROMPT]->code,
|
||||
PHPDBG_G(prompt)[0]);
|
||||
} else {
|
||||
asprintf(
|
||||
&PHPDBG_G(prompt)[1], "%s ",
|
||||
PHPDBG_G(prompt)[0]);
|
||||
}
|
||||
|
||||
return PHPDBG_G(prompt)[1];
|
||||
} /* }}} */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue