diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2012-11-18 16:30:30 -0800 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2012-11-18 16:30:30 -0800 |
commit | 9992b8eb0e3366ff8a3948aa0b66a19c3c12c737 (patch) | |
tree | 6dda0fef85812016fbba9ea067c9d586092b506d /set_color.cpp | |
parent | bab69f26724028d16054a3daf5c78aad7c67bb2d (diff) |
Apply new indentation, brace, and whitespace style
Diffstat (limited to 'set_color.cpp')
-rw-r--r-- | set_color.cpp | 407 |
1 files changed, 216 insertions, 191 deletions
diff --git a/set_color.cpp b/set_color.cpp index 4de3ba60..ff8abe17 100644 --- a/set_color.cpp +++ b/set_color.cpp @@ -59,7 +59,7 @@ #define GETOPT_STRING "b:hvocu" #ifdef _ - #undef _ +#undef _ #endif #ifdef USE_GETTEXT @@ -70,74 +70,80 @@ const char *col[]= { - "black", - "red", - "green", - "brown", - "yellow", - "blue", - "magenta", - "purple", - "cyan", - "white", - "normal" + "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 + 0, + 1, + 2, + 3, + 3, + 4, + 5, + 5, + 6, + 7, + 8 }; void print_colors() { - size_t i; - for( i=0; i<COLORS; i++ ) - { - printf( "%s\n", col[i] ); - } + size_t i; + for (i=0; i<COLORS; i++) + { + printf("%s\n", col[i]); + } } static void check_locale_init() { - static int is_init = 0; - if( is_init ) - return; - - is_init = 1; - setlocale( LC_ALL, "" ); - bindtextdomain( PACKAGE_NAME, LOCALEDIR ); - textdomain( PACKAGE_NAME ); + static int is_init = 0; + if (is_init) + return; + + is_init = 1; + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE_NAME, LOCALEDIR); + textdomain(PACKAGE_NAME); } /* A lot of this code is taken straight from output.cpp; it sure would be nice to factor these together. */ static bool support_term256; -static bool output_get_supports_term256(void) { +static bool output_get_supports_term256(void) +{ return support_term256; } -static bool term256_support_is_native(void) { +static bool term256_support_is_native(void) +{ /* Return YES if we think the term256 support is "native" as opposed to forced. */ return max_colors == 256; } -static bool write_color(char *todo, unsigned char idx, bool is_fg) { +static bool write_color(char *todo, unsigned char idx, bool is_fg) +{ bool result = false; - if (idx < 16 || term256_support_is_native()) { + if (idx < 16 || term256_support_is_native()) + { /* Use tparm */ - putp( tparm( todo, idx ) ); + putp(tparm(todo, idx)); result = true; - } else { + } + else + { /* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */ char stridx[128]; format_long_safe(stridx, (long)idx); @@ -151,225 +157,244 @@ static bool write_color(char *todo, unsigned char idx, bool is_fg) { return result; } -static bool write_foreground_color(unsigned char idx) { - if (set_a_foreground && set_a_foreground[0]) { +static bool write_foreground_color(unsigned char idx) +{ + if (set_a_foreground && set_a_foreground[0]) + { return write_color(set_a_foreground, idx, true); - } else if (set_foreground && set_foreground[0]) { + } + else if (set_foreground && set_foreground[0]) + { return write_color(set_foreground, idx, true); - } else { + } + else + { return false; } } -static bool write_background_color(unsigned char idx) { - if (set_a_background && set_a_background[0]) { +static bool write_background_color(unsigned char idx) +{ + if (set_a_background && set_a_background[0]) + { return write_color(set_a_background, idx, false); - } else if (set_background && set_background[0]) { + } + else if (set_background && set_background[0]) + { return write_color(set_background, idx, false); - } else { + } + else + { return false; } } -static unsigned char index_for_color(rgb_color_t c) { - if (c.is_named() || ! output_get_supports_term256()) { +static unsigned char index_for_color(rgb_color_t c) +{ + if (c.is_named() || ! output_get_supports_term256()) + { return c.to_name_index(); - } else { + } + else + { return c.to_term256_index(); } } -int main( int argc, char **argv ) +int main(int argc, char **argv) { /* Some code passes variables to set_color that don't exist, like $fish_user_whatever. As a hack, quietly return failure. */ if (argc <= 1) return EXIT_FAILURE; - char *bgcolor=0; - char *fgcolor=0; - bool bold=false; - bool underline=false; + char *bgcolor=0; + char *fgcolor=0; + bool bold=false; + bool underline=false; - while( 1 ) - { - static struct option - long_options[] = - { - { - "background", required_argument, 0, 'b' - } - , - { - "help", no_argument, 0, 'h' - } - , - { - "bold", no_argument, 0, 'o' - } - , - { - "underline", no_argument, 0, 'u' - } - , - { - "version", no_argument, 0, 'v' - } - , - { - "print-colors", no_argument, 0, 'c' - } - , + while (1) + { + static struct option + long_options[] = { - 0, 0, 0, 0 + { + "background", required_argument, 0, 'b' + } + , + { + "help", no_argument, 0, 'h' + } + , + { + "bold", no_argument, 0, 'o' + } + , + { + "underline", no_argument, 0, 'u' + } + , + { + "version", no_argument, 0, 'v' + } + , + { + "print-colors", no_argument, 0, 'c' + } + , + { + 0, 0, 0, 0 + } } - } - ; + ; - int opt_index = 0; + int opt_index = 0; - int opt = getopt_long( argc, - argv, - GETOPT_STRING, - long_options, - &opt_index ); + int opt = getopt_long(argc, + argv, + GETOPT_STRING, + long_options, + &opt_index); - if( opt == -1 ) - break; + if (opt == -1) + break; - switch( opt ) - { - case 0: - break; + switch (opt) + { + case 0: + break; - case 'b': - bgcolor = optarg; - break; - case 'h': - print_help( argv[0], 1 ); - exit(0); + case 'b': + bgcolor = optarg; + break; + case 'h': + print_help(argv[0], 1); + exit(0); - case 'o': - bold=true; - break; + case 'o': + bold=true; + break; - case 'u': - underline=true; - break; + case 'u': + underline=true; + break; - case 'v': - check_locale_init(); - fprintf( stderr, _("%s, version %s\n"), SET_COLOR, PACKAGE_VERSION ); - exit( 0 ); + case 'v': + check_locale_init(); + fprintf(stderr, _("%s, version %s\n"), SET_COLOR, PACKAGE_VERSION); + exit(0); - case 'c': - print_colors(); - exit(0); + case 'c': + print_colors(); + exit(0); - case '?': - return 1; + case '?': + return 1; - } + } - } + } - switch( argc-optind) - { + switch (argc-optind) + { case 0: // printf( "no fg\n" ); - break; + break; case 1: - fgcolor=argv[optind]; + fgcolor=argv[optind]; // printf( "fg %s\n", fgcolor ); - break; + break; default: - check_locale_init(); - printf( _("%s: Too many arguments\n"), SET_COLOR ); - return 1; - } + check_locale_init(); + printf(_("%s: Too many arguments\n"), SET_COLOR); + return 1; + } /* Infer term256 support */ char *fish_term256 = getenv("fish_term256"); - if (fish_term256) { + if (fish_term256) + { support_term256 = from_string<bool>(fish_term256); - } else { + } + 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; - } + 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; - } + 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() ) + if (bgcolor && bg.is_none()) { - write_background_color(0); - putp( tparm(exit_attribute_mode) ); + check_locale_init(); + fprintf(stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, bgcolor); + return 1; } - } - if( fgcolor ) - { - if( fg.is_normal() ) + setupterm(0, STDOUT_FILENO, 0); + + if (bold) { - write_foreground_color(0); - putp( tparm(exit_attribute_mode) ); + if (enter_bold_mode) + putp(enter_bold_mode); } - else + + 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() ) + 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") ); - } + if (del_curterm(cur_term) == ERR) + { + fprintf(stderr, "%s", _("Error while closing terminfo")); + } } |