From 8d95d0834d4f327ba2a4d829c64050cda80cb302 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 14 Feb 2013 15:50:24 -0800 Subject: [PATCH] First stab at builtin set_color. Moved set_color.cpp to builtin_set_color.cpp and taught fish about it. --- builtin.cpp | 4 +- builtin_set_color.cpp | 195 ++++++++++++++++ color.cpp | 12 + color.h | 3 + doc_src/set_color.txt | 1 - env.cpp | 2 +- fish.xcodeproj/project.pbxproj | 40 ++-- output.cpp | 6 +- output.h | 5 + reader.cpp | 12 +- set_color.cpp | 400 --------------------------------- 11 files changed, 249 insertions(+), 431 deletions(-) create mode 100644 builtin_set_color.cpp delete mode 100644 set_color.cpp diff --git a/builtin.cpp b/builtin.cpp index e82545330..007adc123 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -167,7 +167,7 @@ static const io_chain_t *real_io; static int builtin_count_args(wchar_t **argv) { int argc = 1; - while (argv[argc] != 0) + while (argv[argc] != NULL) { argc++; } @@ -390,6 +390,7 @@ static void builtin_missing_argument(parser_t &parser, const wchar_t *cmd, const #include "builtin_complete.cpp" #include "builtin_ulimit.cpp" #include "builtin_jobs.cpp" +#include "builtin_set_color.cpp" /* builtin_test lives in builtin_test.cpp */ int builtin_test(parser_t &parser, wchar_t **argv); @@ -4030,6 +4031,7 @@ static const builtin_data_t builtin_datas[]= { L"read", &builtin_read, N_(L"Read a line of input into variables") }, { L"return", &builtin_return, N_(L"Stop the currently evaluated function") }, { L"set", &builtin_set, N_(L"Handle environment variables") }, + { L"set_color", &builtin_set_color, N_(L"Set the terminal color") }, { L"status", &builtin_status, N_(L"Return status information about fish") }, { L"switch", &builtin_switch, N_(L"Conditionally execute a block of commands") }, { L"test", &builtin_test, N_(L"Test a condition") }, diff --git a/builtin_set_color.cpp b/builtin_set_color.cpp new file mode 100644 index 000000000..afd8da0e3 --- /dev/null +++ b/builtin_set_color.cpp @@ -0,0 +1,195 @@ +/** \file builtin_set_color.cpp Functions defining the set_color builtin + +Functions used for implementing the set_color builtin. + +*/ +#include "config.h" + +#include "builtin.h" +#include "color.h" +#include "output.h" + +#if HAVE_TERM_H +#include +#elif HAVE_NCURSES_TERM_H +#include +#endif + + +/* We know about these buffers */ +extern wcstring stdout_buffer, stderr_buffer; + +/** + Error message for invalid path operations +*/ +#define BUILTIN_SET_PATH_ERROR L"%ls: Warning: path component %ls may not be valid in %ls.\n" + +/** + Hint for invalid path operation with a colon +*/ +#define BUILTIN_SET_PATH_HINT L"%ls: Did you mean 'set %ls $%ls %ls'?\n" + +/** + Error for mismatch between index count and elements +*/ +#define BUILTIN_SET_ARG_COUNT L"%ls: The number of variable indexes does not match the number of values\n" + +static void print_colors(void) +{ + const wcstring_list_t result = rgb_color_t::named_color_names(); + size_t i; + for (i=0; i < result.size(); i++) + { + stdout_buffer.append(result.at(i)); + stdout_buffer.push_back(L'\n'); + } +} + +/** + set_color builtin +*/ +static int builtin_set_color(parser_t &parser, wchar_t **argv) +{ + /** Variables used for parsing the argument list */ + + const struct woption long_options[] = + { + { L"background", required_argument, 0, 'b'}, + { L"help", no_argument, 0, 'h' }, + { L"bold", no_argument, 0, 'o' }, + { L"underline", no_argument, 0, 'u' }, + { L"version", no_argument, 0, 'v' }, + { L"print-colors", no_argument, 0, 'c' }, + { 0, 0, 0, 0 } + }; + + const wchar_t *short_options = L"b:hvocu"; + + int argc = builtin_count_args(argv); + + const wchar_t *bgcolor = NULL; + bool bold = false, underline=false; + + /* Parse options to obtain the requested operation and the modifiers */ + woptind = 0; + while (1) + { + int c = wgetopt_long(argc, argv, short_options, long_options, 0); + + if (c == -1) + { + break; + } + + switch (c) + { + case 0: + break; + + case 'b': + bgcolor = woptarg; + break; + + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; + + case 'o': + bold = true; + break; + + case 'u': + underline = true; + break; + + case 'c': + print_colors(); + return STATUS_BUILTIN_OK; + + case '?': + return STATUS_BUILTIN_ERROR; + } + } + + /* Remaining argument is foreground color */ + const wchar_t *fgcolor = NULL; + if (woptind < argc) + { + if (woptind + 1 == argc) + { + fgcolor = argv[woptind]; + } + else + { + append_format(stderr_buffer, + _(L"%ls: Too many arguments\n"), + argv[0]); + return STATUS_BUILTIN_ERROR; + } + } + + if (fgcolor == NULL && bgcolor == NULL && !bold && !underline) + { + append_format(stderr_buffer, + _(L"%ls: Expected an argument\n"), + argv[0]); + return STATUS_BUILTIN_ERROR; + } + + const rgb_color_t fg = rgb_color_t(fgcolor ? fgcolor : L""); + if (fgcolor && fg.is_none()) + { + append_format(stderr_buffer, _("%s: Unknown color '%s'\n"), argv[0], fgcolor); + return STATUS_BUILTIN_ERROR; + } + + const rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : L""); + if (bgcolor && bg.is_none()) + { + append_format(stderr_buffer, _("%s: Unknown color '%s'\n"), argv[0], bgcolor); + return STATUS_BUILTIN_ERROR; + } + + if (bold) + { + if (enter_bold_mode) + putp(enter_bold_mode); + } + + if (underline) + { + if (enter_underline_mode) + putp(enter_underline_mode); + } + + if (bgcolor != NULL) + { + if (bg.is_normal()) + { + write_background_color(0); + putp(tparm(exit_attribute_mode)); + } + } + + if (fgcolor != NULL) + { + if (fg.is_normal()) + { + write_foreground_color(0); + putp(tparm(exit_attribute_mode)); + } + else + { + write_foreground_color(index_for_color(fg)); + } + } + + if (bgcolor != NULL) + { + if (! bg.is_normal()) + { + write_background_color(index_for_color(bg)); + } + } + return STATUS_BUILTIN_OK; +} diff --git a/color.cpp b/color.cpp index d4dd9453e..884410d61 100644 --- a/color.cpp +++ b/color.cpp @@ -170,6 +170,18 @@ static const named_color_t named_colors[11] = {L"normal", 8, {0xFF, 0xFF, 0XFF}} }; +wcstring_list_t rgb_color_t::named_color_names(void) +{ + size_t count = sizeof named_colors / sizeof *named_colors; + wcstring_list_t result; + result.reserve(count); + for (size_t i=0; i < count; i++) + { + result.push_back(named_colors[i].name); + } + return result; +} + bool rgb_color_t::try_parse_named(const wcstring &str) { bzero(&data, sizeof data); diff --git a/color.h b/color.h index 0694d693f..6e22aaed5 100644 --- a/color.h +++ b/color.h @@ -169,6 +169,9 @@ public: { return !(*this == other); } + + /** Returns the names of all named colors */ + static wcstring_list_t named_color_names(void); }; #endif diff --git a/doc_src/set_color.txt b/doc_src/set_color.txt index ccd2d244c..08196d19f 100644 --- a/doc_src/set_color.txt +++ b/doc_src/set_color.txt @@ -18,7 +18,6 @@ as A0FF33 or f2f. fish will choose the closest supported color. - \c -h, \c --help Display help message and exit - \c -o, \c --bold Set bold or extra bright mode - \c -u, \c --underline Set underlined mode -- \c -v, \c --version Display version and exit Calling set_color normal will set the terminal color to diff --git a/env.cpp b/env.cpp index 118eeb01a..43113990e 100644 --- a/env.cpp +++ b/env.cpp @@ -1424,7 +1424,7 @@ static void export_func(const std::map &envs, std::vectorfirst); std::string vs = wcs2string(iter->second); - + for (size_t i=0; i < vs.size(); i++) { char &vc = vs.at(i); diff --git a/fish.xcodeproj/project.pbxproj b/fish.xcodeproj/project.pbxproj index 4e3226bbb..3ebdad936 100644 --- a/fish.xcodeproj/project.pbxproj +++ b/fish.xcodeproj/project.pbxproj @@ -45,7 +45,7 @@ D0F019F715A977A00034B3B1 /* CopyFiles */, D0F019FC15A977B40034B3B1 /* CopyFiles */, D033780F15DC6D2A00A634BA /* CopyFiles */, - D01A2C9B16964C8200767098 /* Copy Files */, + D01A2C9B16964C8200767098 /* Copy Files */, ); dependencies = ( D0F01A1315AA36280034B3B1 /* PBXTargetDependency */, @@ -61,8 +61,8 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ - D01A2D24169B736200767098 /* man1 in Copy Files */ = {isa = PBXBuildFile; fileRef = D01A2D23169B730A00767098 /* man1 */; }; - D01A2D25169B737700767098 /* man1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = D01A2D23169B730A00767098 /* man1 */; }; + D01A2D24169B736200767098 /* man1 in Copy Files */ = {isa = PBXBuildFile; fileRef = D01A2D23169B730A00767098 /* man1 */; }; + D01A2D25169B737700767098 /* man1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = D01A2D23169B730A00767098 /* man1 */; }; D031890C15E36E4600D9CC39 /* base in Resources */ = {isa = PBXBuildFile; fileRef = D031890915E36D9800D9CC39 /* base */; }; D033781115DC6D4C00A634BA /* completions in CopyFiles */ = {isa = PBXBuildFile; fileRef = D025C02715D1FEA100B9DB63 /* completions */; }; D033781215DC6D5200A634BA /* functions in CopyFiles */ = {isa = PBXBuildFile; fileRef = D025C02815D1FEA100B9DB63 /* functions */; }; @@ -74,7 +74,7 @@ D07D266D15E33B86009E43F6 /* functions in Copy Files */ = {isa = PBXBuildFile; fileRef = D025C02815D1FEA100B9DB63 /* functions */; }; D07D266E15E33B86009E43F6 /* tools in Copy Files */ = {isa = PBXBuildFile; fileRef = D025C02915D1FEA100B9DB63 /* tools */; }; D07D267215E34171009E43F6 /* config.fish in Copy Files */ = {isa = PBXBuildFile; fileRef = D0CBD580159EE48F0024809C /* config.fish */; }; - D0879AC816BF9AAB00E98E56 /* fish_term_icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = D0879AC616BF9A1A00E98E56 /* fish_term_icon.icns */; }; + D0879AC816BF9AAB00E98E56 /* fish_term_icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = D0879AC616BF9A1A00E98E56 /* fish_term_icon.icns */; }; D0A564FE168D23D800AF6161 /* man in CopyFiles */ = {isa = PBXBuildFile; fileRef = D0A564F1168D0BAB00AF6161 /* man */; }; D0A56501168D258300AF6161 /* man in Copy Files */ = {isa = PBXBuildFile; fileRef = D0A564F1168D0BAB00AF6161 /* man */; }; D0CBD587159EF0E10024809C /* launch_fish.scpt in Resources */ = {isa = PBXBuildFile; fileRef = D0CBD586159EF0E10024809C /* launch_fish.scpt */; }; @@ -257,24 +257,24 @@ /* End PBXContainerItemProxy section */ /* Begin PBXCopyFilesBuildPhase section */ - D01A2C9B16964C8200767098 /* Copy Files */ = { + D01A2C9B16964C8200767098 /* Copy Files */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; - dstPath = base/share/man; + dstPath = base/share/man; dstSubfolderSpec = 1; files = ( - D01A2D24169B736200767098 /* man1 in Copy Files */, + D01A2D24169B736200767098 /* man1 in Copy Files */, ); - name = "Copy Files"; + name = "Copy Files"; runOnlyForDeploymentPostprocessing = 0; }; D01A2CA716965ADD00767098 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 8; - dstPath = "${INSTALL_PATH}/share/man/"; + dstPath = "${INSTALL_PATH}/share/man/"; dstSubfolderSpec = 0; files = ( - D01A2D25169B737700767098 /* man1 in CopyFiles */, + D01A2D25169B737700767098 /* man1 in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 1; }; @@ -353,7 +353,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - D01A2D23169B730A00767098 /* man1 */ = {isa = PBXFileReference; lastKnownFileType = text; name = man1; path = pages_for_manpath/man1; sourceTree = BUILT_PRODUCTS_DIR; }; + D01A2D23169B730A00767098 /* man1 */ = {isa = PBXFileReference; lastKnownFileType = text; name = man1; path = pages_for_manpath/man1; sourceTree = BUILT_PRODUCTS_DIR; }; D025C02715D1FEA100B9DB63 /* completions */ = {isa = PBXFileReference; lastKnownFileType = folder; name = completions; path = share/completions; sourceTree = ""; }; D025C02815D1FEA100B9DB63 /* functions */ = {isa = PBXFileReference; lastKnownFileType = folder; name = functions; path = share/functions; sourceTree = ""; }; D025C02915D1FEA100B9DB63 /* tools */ = {isa = PBXFileReference; lastKnownFileType = folder; name = tools; path = share/tools; sourceTree = ""; }; @@ -361,7 +361,7 @@ D03EE83814DF88B200FC7150 /* lru.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lru.h; sourceTree = ""; }; D07B247215BCC15700D4ADB4 /* add-shell */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = "add-shell"; path = "build_tools/osx_package_scripts/add-shell"; sourceTree = ""; }; D07B247515BCC4BE00D4ADB4 /* install.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = install.sh; path = osx/install.sh; sourceTree = ""; }; - D0879AC616BF9A1A00E98E56 /* fish_term_icon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = fish_term_icon.icns; path = osx/fish_term_icon.icns; sourceTree = ""; }; + D0879AC616BF9A1A00E98E56 /* fish_term_icon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = fish_term_icon.icns; path = osx/fish_term_icon.icns; sourceTree = ""; }; D09B1C1914FC7B5B00F91077 /* postfork.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = postfork.cpp; sourceTree = ""; }; D09B1C1A14FC7B5B00F91077 /* postfork.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = postfork.h; sourceTree = ""; }; D0A0850313B3ACEE0099B651 /* builtin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = builtin.h; sourceTree = ""; }; @@ -469,6 +469,7 @@ D0C4FD9415A7D7EE00212EF1 /* config.fish */ = {isa = PBXFileReference; lastKnownFileType = text; name = config.fish; path = etc/config.fish; sourceTree = ""; }; D0C6FCC914CFA4B0004CE8AD /* autoload.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = autoload.cpp; sourceTree = ""; }; D0C6FCCB14CFA4B7004CE8AD /* autoload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = autoload.h; sourceTree = ""; }; + D0C861EA16CC7054003B5A04 /* builtin_set_color.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_set_color.cpp; sourceTree = ""; }; D0CBD580159EE48F0024809C /* config.fish */ = {isa = PBXFileReference; lastKnownFileType = text; name = config.fish; path = share/config.fish; sourceTree = ""; }; D0CBD583159EEE010024809C /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; D0CBD586159EF0E10024809C /* launch_fish.scpt */ = {isa = PBXFileReference; lastKnownFileType = file; name = launch_fish.scpt; path = osx/launch_fish.scpt; sourceTree = ""; }; @@ -543,12 +544,12 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - D01A2C9C16964CF600767098 /* pages_for_manpath */ = { + D01A2C9C16964CF600767098 /* pages_for_manpath */ = { isa = PBXGroup; children = ( - D01A2D23169B730A00767098 /* man1 */, + D01A2D23169B730A00767098 /* man1 */, ); - name = pages_for_manpath; + name = pages_for_manpath; sourceTree = ""; }; D031890A15E36DB500D9CC39 /* Other Build Products */ = { @@ -592,6 +593,7 @@ D0A0853113B3ACEE0099B651 /* builtin_complete.cpp */, D0A0853213B3ACEE0099B651 /* builtin_jobs.cpp */, D0A0853313B3ACEE0099B651 /* builtin_set.cpp */, + D0C861EA16CC7054003B5A04 /* builtin_set_color.cpp */, D0A0853413B3ACEE0099B651 /* builtin_ulimit.cpp */, D0F3373A1506DE3C00ECEFC0 /* builtin_test.cpp */, D0A0853513B3ACEE0099B651 /* builtin.cpp */, @@ -697,7 +699,7 @@ D0D02AAB15985C14008E62BD /* Resources */ = { isa = PBXGroup; children = ( - D0879AC616BF9A1A00E98E56 /* fish_term_icon.icns */, + D0879AC616BF9A1A00E98E56 /* fish_term_icon.icns */, D07B247215BCC15700D4ADB4 /* add-shell */, D0CBD586159EF0E10024809C /* launch_fish.scpt */, D0CBD580159EE48F0024809C /* config.fish */, @@ -705,7 +707,7 @@ D07B247515BCC4BE00D4ADB4 /* install.sh */, D0D02AA915985C0C008E62BD /* Info.plist */, D0A564F2168D1F2000AF6161 /* build_documentation.sh */, - D01A2C9C16964CF600767098 /* pages_for_manpath */, + D01A2C9C16964CF600767098 /* pages_for_manpath */, D0A564F1168D0BAB00AF6161 /* man */, D025C02715D1FEA100B9DB63 /* completions */, D025C02815D1FEA100B9DB63 /* functions */, @@ -903,7 +905,7 @@ files = ( D031890C15E36E4600D9CC39 /* base in Resources */, D0CBD587159EF0E10024809C /* launch_fish.scpt in Resources */, - D0879AC816BF9AAB00E98E56 /* fish_term_icon.icns in Resources */, + D0879AC816BF9AAB00E98E56 /* fish_term_icon.icns in Resources */, D07B247315BCC15700D4ADB4 /* add-shell in Resources */, D07B247615BCC4BE00D4ADB4 /* install.sh in Resources */, ); @@ -1065,7 +1067,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "cd \"${SRCROOT}\" ;\n# Run build_documentation.sh\n# Do this in a subshell so that we keep going even if it calls exit\n( . \"./build_tools/build_documentation.sh\" \"./Doxyfile.help\" \"./doc_src\" \"$BUILT_PRODUCTS_DIR\" )\n\n# Copy certain files into man1, destined for share/man/man1 (instead of share/fish/man/man1)\n# These copies will fail of the documentation did not build; that's OK\n# We want to create the directory even if the documentation did not build, so that the Xcode build can still succeed\nmanpathdir=\"${BUILT_PRODUCTS_DIR}/pages_for_manpath/man1\"\necho \"Copying pages destined for manpath into $manpathdir\"\nrm -Rf \"$manpathdir\"\nmkdir -p \"$manpathdir\"\nfor manpage in fish.1 set_color.1 fish_pager.1 fishd.1 fish_indent.1; do\n manpagepath=\"${BUILT_PRODUCTS_DIR}/man/man1/${manpage}\"\n test -f \"$manpagepath\" && cp \"$manpagepath\" \"${BUILT_PRODUCTS_DIR}/pages_for_manpath/man1/\"\ndone\n\n# Always succeed\ntrue\n"; + shellScript = "cd \"${SRCROOT}\" ;\n# Run build_documentation.sh\n# Do this in a subshell so that we keep going even if it calls exit\n( . \"./build_tools/build_documentation.sh\" \"./Doxyfile.help\" \"./doc_src\" \"$BUILT_PRODUCTS_DIR\" )\n\n# Copy certain files into man1, destined for share/man/man1 (instead of share/fish/man/man1)\n# These copies will fail of the documentation did not build; that's OK\n# We want to create the directory even if the documentation did not build, so that the Xcode build can still succeed\nmanpathdir=\"${BUILT_PRODUCTS_DIR}/pages_for_manpath/man1\"\necho \"Copying pages destined for manpath into $manpathdir\"\nrm -Rf \"$manpathdir\"\nmkdir -p \"$manpathdir\"\nfor manpage in fish.1 set_color.1 fish_pager.1 fishd.1 fish_indent.1; do\n manpagepath=\"${BUILT_PRODUCTS_DIR}/man/man1/${manpage}\"\n test -f \"$manpagepath\" && cp \"$manpagepath\" \"${BUILT_PRODUCTS_DIR}/pages_for_manpath/man1/\"\ndone\n\n# Always succeed\ntrue\n"; }; /* End PBXShellScriptBuildPhase section */ diff --git a/output.cpp b/output.cpp index 69b7c181b..3b5f9d4b0 100644 --- a/output.cpp +++ b/output.cpp @@ -146,7 +146,7 @@ void output_set_supports_term256(bool val) support_term256 = val; } -static unsigned char index_for_color(rgb_color_t c) +unsigned char index_for_color(rgb_color_t c) { if (c.is_named() || ! output_get_supports_term256()) { @@ -192,7 +192,7 @@ static bool write_color(char *todo, unsigned char idx, bool is_fg) return result; } -static bool write_foreground_color(unsigned char idx) +bool write_foreground_color(unsigned char idx) { if (set_a_foreground && set_a_foreground[0]) { @@ -208,7 +208,7 @@ static bool write_foreground_color(unsigned char idx) } } -static bool write_background_color(unsigned char idx) +bool write_background_color(unsigned char idx) { if (set_a_background && set_a_background[0]) { diff --git a/output.h b/output.h index 0dafcfc05..2d05c3d05 100644 --- a/output.h +++ b/output.h @@ -163,4 +163,9 @@ const wchar_t *output_get_term(); bool output_get_supports_term256(); void output_set_supports_term256(bool val); +/* Exported for builtin_set_color's usage only */ +bool write_foreground_color(unsigned char idx); +bool write_background_color(unsigned char idx); +unsigned char index_for_color(rgb_color_t c); + #endif diff --git a/reader.cpp b/reader.cpp index b05234254..f8fe26192 100644 --- a/reader.cpp +++ b/reader.cpp @@ -1147,14 +1147,14 @@ static void run_pager(const wcstring &prefix, int is_quoted, const std::vector -#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 _ -#undef _ -#endif - -#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 -}; - -void print_colors() -{ - size_t i; - 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")); - } - -}