#include "config.h" #include #include #include #include #include #include #include #include #if HAVE_NCURSES_H #include #else #include #endif #if HAVE_TERMIO_H #include #endif #if HAVE_TERM_H #include #elif HAVE_NCURSES_TERM_H #include #endif #include #ifdef HAVE_GETOPT_H #include #endif #if HAVE_LIBINTL_H #include #endif #include "fallback.h" #include "print_help.h" #include "color.h" /* Small utility for setting the color. Usage: set_color COLOR where COLOR is either an integer from 0 to seven or one of the strings in the col array. */ #define COLORS (sizeof(col)/sizeof(char *)) /** Program name */ #define SET_COLOR "set_color" /** Getopt short switches for set_color */ #define GETOPT_STRING "b:hvocu" #ifdef USE_GETTEXT #define _(string) gettext(string) #else #define _(string) (string) #endif const char *col[]= { "black", "red", "green", "brown", "yellow", "blue", "magenta", "purple", "cyan", "white", "normal" }; const int col_idx[]= { 0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8 }; int translate_color( char *str ) { char *endptr; int color; if( !str ) return -1; errno = 0; color = strtol( str, &endptr, 10 ); if( *endptr || color<0 || errno ) { size_t i; color = -1; for( i=0; i(fish_term256); } else { const char *term = getenv("TERM"); support_term256 = term && strstr(term, "256color"); } if( !fgcolor && !bgcolor && !bold && !underline ) { check_locale_init(); fprintf( stderr, _("%s: Expected an argument\n"), SET_COLOR ); print_help( argv[0], 2 ); return 1; } rgb_color_t fg = rgb_color_t(fgcolor ? fgcolor : ""); if( fgcolor && fg.is_none()) { check_locale_init(); fprintf( stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, fgcolor ); return 1; } rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : ""); if( bgcolor && bg.is_none()) { check_locale_init(); fprintf( stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, bgcolor ); return 1; } setupterm( 0, STDOUT_FILENO, 0); if( bold ) { if( enter_bold_mode ) putp( enter_bold_mode ); } if( underline ) { if( enter_underline_mode ) putp( enter_underline_mode ); } if( bgcolor ) { if( bg.is_normal() ) { write_background_color(0); putp( tparm(exit_attribute_mode) ); } } if( fgcolor ) { if( fg.is_normal() ) { write_foreground_color(0); putp( tparm(exit_attribute_mode) ); } else { write_foreground_color(index_for_color(fg)); } } if( bgcolor ) { if( ! bg.is_normal() ) { write_background_color(index_for_color(bg)); } } if( del_curterm( cur_term ) == ERR ) { fprintf( stderr, "%s", _("Error while closing terminfo") ); } }