Advertisement
avp210159

color.h

Feb 12th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1. #ifndef _COLOR_H
  2. #define _COLOR_H
  3.  
  4. // avp 2014 VERY simple color.h
  5. //   (не восстанавливает экран при завершении по сигналу)
  6. //   (does not restore the screen when terminating by signal)
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11.  
  12. #define SCOLOR_RESET      "\033[0m"
  13. #define SCOLOR_BOLD       "\033[1m"
  14. #define SCOLOR_UNDER      "\033[4m"
  15. #define SCOLOR_INVERS     "\033[7m"
  16. #define SCOLOR_NORMAL     "\033[30m"
  17. #define SCOLOR_BLACK      "\033[30m"
  18. #define SCOLOR_RED        "\033[31m"
  19. #define SCOLOR_GREEN      "\033[32m"
  20. #define SCOLOR_BROWN      "\033[33m"
  21. #define SCOLOR_BLUE       "\033[34m"
  22. #define SCOLOR_MAGENTA    "\033[35m"
  23. #define SCOLOR_CYAN       "\033[36m"
  24. #define SCOLOR_LIGHT      "\033[37m"
  25.  
  26. static int color_tty (int op) {
  27.   static int colortty_fd = -1;
  28.   if (op < 0)
  29.     return colortty_fd;
  30.   return colortty_fd = op;
  31. }
  32.  
  33. static FILE *color_stream (FILE *f) {
  34.   static FILE *sfile = 0;
  35.   if (f)
  36.     return sfile = f;
  37.   return sfile ? sfile : stdout;
  38. }
  39.  
  40. static void reset_color () {
  41.   int fd = color_tty(-1);
  42.   if (fd >= 0)
  43.     write(fd, SCOLOR_RESET, sizeof(SCOLOR_RESET) - 1);
  44. }
  45.  
  46. static void setinit_color (int fd, int ini)
  47. {
  48.   int save = errno;
  49.   if (isatty(fd)) {
  50.     write (color_tty(fd), SCOLOR_RESET, sizeof(SCOLOR_RESET) - 1);
  51.     if (ini)
  52.       atexit(reset_color);
  53.   }
  54.   errno = save;
  55. }
  56.  
  57. static void init_color (FILE *f) {
  58.   int fd = color_tty(-1);
  59.   if (fd == -1) {
  60.     /*
  61.     int save = errno;
  62.     if (isatty(fileno(f)))
  63.       write (color_tty(fileno(color_stream(f))),
  64.          SCOLOR_RESET, sizeof(SCOLOR_RESET) - 1);
  65.     errno = save;
  66.     atexit(reset_color);
  67.     */
  68.     setinit_color(fileno(f), 1);
  69.     color_stream(f);
  70.   }
  71. }
  72.  
  73.  
  74. #define FC_RESET(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_RESET, _f), fflush(_f);})
  75. #define FC_BOLD(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BOLD, _f), fflush(_f);})
  76. #define FC_UNDER(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_UNDER, _f), fflush(_f);})
  77. #define FC_INVERS(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_INVERS, _f), fflush(_f);})
  78. #define FC_NORMAL(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_NORMAL, _f), fflush(_f);})
  79. #define FC_BLACK(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BLACK, _f), fflush(_f);})
  80. #define FC_RED(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_RED, _f), fflush(_f);})
  81. #define FC_GREEN(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_GREEN, _f), fflush(_f);})
  82. #define FC_BROWN(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BROWN, _f), fflush(_f);})
  83. #define FC_BLUE(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BLUE, _f), fflush(_f);})
  84. #define FC_MAGENTA(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_MAGENTA, _f), fflush(_f);})
  85. #define FC_CYAN(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_CYAN, _f), fflush(_f);})
  86. #define FC_LIGHT(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_LIGHT, _f), fflush(_f);})
  87.  
  88. #define COLOR_RESET() (FC_RESET(color_stream(0)))
  89. #define COLOR_BOLD() (FC_BOLD(color_stream(0)))
  90. #define COLOR_UNDER() (FC_UNDER(color_stream(0)))
  91. #define COLOR_INVERS() (FC_INVERS(color_stream(0)))
  92. #define COLOR_NORMAL() (FC_NORMAL(color_stream(0)))
  93. #define COLOR_BLACK() (FC_BLACK(color_stream(0)))
  94. #define COLOR_RED() (FC_RED(color_stream(0)))
  95. #define COLOR_GREEN() (FC_GREEN(color_stream(0)))
  96. #define COLOR_BROWN() (FC_BROWN(color_stream(0)))
  97. #define COLOR_BLUE() (FC_BLUE(color_stream(0)))
  98. #define COLOR_MAGENTA() (FC_MAGENTA(color_stream(0)))
  99. #define COLOR_CYAN() (FC_CYAN(color_stream(0)))
  100. #define COLOR_LIGHT() (FC_LIGHT(color_stream(0)))
  101.  
  102. #if 0
  103.  
  104. #include <stdio.h>
  105. #include <stdlib.h>
  106.  
  107. #include "color.h"
  108.  
  109. int
  110. main (int ac, char *av[])
  111. {
  112.   init_color(stdout);
  113.   perror("init");
  114.   puts("INIT NORMAL");
  115.   FC_RED(stdout); puts("Red");
  116.   COLOR_UNDER(); puts("Under");
  117.   COLOR_INVERS(); puts("Invers");
  118.   COLOR_RESET(); puts("Reset");
  119.   COLOR_RED(); puts("red again");
  120.   COLOR_NORMAL(); puts("Normal");
  121.   COLOR_BLACK(); puts("Black");
  122.   COLOR_GREEN(); puts("Green");
  123.   COLOR_BROWN(); puts("Brown");
  124.   COLOR_BLUE(); puts("Blue");
  125.   COLOR_MAGENTA(); puts("Magenta");
  126.   COLOR_CYAN(); puts("Cyan");
  127.   COLOR_LIGHT(); puts("Light");
  128.   COLOR_BOLD(); puts("Now BOLD");
  129.   COLOR_UNDER(); puts("+Under");
  130.   COLOR_INVERS(); puts("+Invers");
  131.   COLOR_NORMAL(); puts("Normal");
  132.   COLOR_RED(); puts("red again");
  133.   COLOR_BLACK(); puts("Black");
  134.   COLOR_GREEN(); puts("Green");
  135.   COLOR_BROWN(); puts("Brown");
  136.   COLOR_BLUE(); puts("Blue");
  137.   COLOR_MAGENTA(); puts("Magenta");
  138.   COLOR_CYAN(); puts("Cyan");
  139.   COLOR_LIGHT(); puts("Light");
  140.   perror("xaxa2");
  141.   COLOR_RESET(); puts("RESET");
  142.   COLOR_BOLD(); puts("Now BOLD");
  143.   COLOR_NORMAL(); puts("Normal BOLD");
  144.   COLOR_RED(); puts("red again BOLD");
  145.   COLOR_BLACK(); puts("Black BOLD");
  146.   COLOR_GREEN(); puts("Green BOLD");
  147.   COLOR_BROWN(); puts("Brown BOLD");
  148.   COLOR_BLUE(); puts("Blue BOLD");
  149.   COLOR_MAGENTA(); puts("Magenta BOLD");
  150.   COLOR_CYAN(); puts("Cyan BOLD");
  151.   COLOR_LIGHT(); puts("Light BOLD");
  152.  
  153.   getchar();
  154.  
  155.   return 0;
  156. }
  157. #endif
  158.  
  159. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement