Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _COLOR_H
- #define _COLOR_H
- // avp 2014 VERY simple color.h
- // (не восстанавливает экран при завершении по сигналу)
- // (does not restore the screen when terminating by signal)
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #define SCOLOR_RESET "\033[0m"
- #define SCOLOR_BOLD "\033[1m"
- #define SCOLOR_UNDER "\033[4m"
- #define SCOLOR_INVERS "\033[7m"
- #define SCOLOR_NORMAL "\033[30m"
- #define SCOLOR_BLACK "\033[30m"
- #define SCOLOR_RED "\033[31m"
- #define SCOLOR_GREEN "\033[32m"
- #define SCOLOR_BROWN "\033[33m"
- #define SCOLOR_BLUE "\033[34m"
- #define SCOLOR_MAGENTA "\033[35m"
- #define SCOLOR_CYAN "\033[36m"
- #define SCOLOR_LIGHT "\033[37m"
- static int color_tty (int op) {
- static int colortty_fd = -1;
- if (op < 0)
- return colortty_fd;
- return colortty_fd = op;
- }
- static FILE *color_stream (FILE *f) {
- static FILE *sfile = 0;
- if (f)
- return sfile = f;
- return sfile ? sfile : stdout;
- }
- static void reset_color () {
- int fd = color_tty(-1);
- if (fd >= 0)
- write(fd, SCOLOR_RESET, sizeof(SCOLOR_RESET) - 1);
- }
- static void setinit_color (int fd, int ini)
- {
- int save = errno;
- if (isatty(fd)) {
- write (color_tty(fd), SCOLOR_RESET, sizeof(SCOLOR_RESET) - 1);
- if (ini)
- atexit(reset_color);
- }
- errno = save;
- }
- static void init_color (FILE *f) {
- int fd = color_tty(-1);
- if (fd == -1) {
- /*
- int save = errno;
- if (isatty(fileno(f)))
- write (color_tty(fileno(color_stream(f))),
- SCOLOR_RESET, sizeof(SCOLOR_RESET) - 1);
- errno = save;
- atexit(reset_color);
- */
- setinit_color(fileno(f), 1);
- color_stream(f);
- }
- }
- #define FC_RESET(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_RESET, _f), fflush(_f);})
- #define FC_BOLD(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BOLD, _f), fflush(_f);})
- #define FC_UNDER(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_UNDER, _f), fflush(_f);})
- #define FC_INVERS(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_INVERS, _f), fflush(_f);})
- #define FC_NORMAL(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_NORMAL, _f), fflush(_f);})
- #define FC_BLACK(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BLACK, _f), fflush(_f);})
- #define FC_RED(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_RED, _f), fflush(_f);})
- #define FC_GREEN(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_GREEN, _f), fflush(_f);})
- #define FC_BROWN(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BROWN, _f), fflush(_f);})
- #define FC_BLUE(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_BLUE, _f), fflush(_f);})
- #define FC_MAGENTA(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_MAGENTA, _f), fflush(_f);})
- #define FC_CYAN(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_CYAN, _f), fflush(_f);})
- #define FC_LIGHT(f) ({FILE *_f = (f); if (color_tty(-1) >= 0) fputs(SCOLOR_LIGHT, _f), fflush(_f);})
- #define COLOR_RESET() (FC_RESET(color_stream(0)))
- #define COLOR_BOLD() (FC_BOLD(color_stream(0)))
- #define COLOR_UNDER() (FC_UNDER(color_stream(0)))
- #define COLOR_INVERS() (FC_INVERS(color_stream(0)))
- #define COLOR_NORMAL() (FC_NORMAL(color_stream(0)))
- #define COLOR_BLACK() (FC_BLACK(color_stream(0)))
- #define COLOR_RED() (FC_RED(color_stream(0)))
- #define COLOR_GREEN() (FC_GREEN(color_stream(0)))
- #define COLOR_BROWN() (FC_BROWN(color_stream(0)))
- #define COLOR_BLUE() (FC_BLUE(color_stream(0)))
- #define COLOR_MAGENTA() (FC_MAGENTA(color_stream(0)))
- #define COLOR_CYAN() (FC_CYAN(color_stream(0)))
- #define COLOR_LIGHT() (FC_LIGHT(color_stream(0)))
- #if 0
- #include <stdio.h>
- #include <stdlib.h>
- #include "color.h"
- int
- main (int ac, char *av[])
- {
- init_color(stdout);
- perror("init");
- puts("INIT NORMAL");
- FC_RED(stdout); puts("Red");
- COLOR_UNDER(); puts("Under");
- COLOR_INVERS(); puts("Invers");
- COLOR_RESET(); puts("Reset");
- COLOR_RED(); puts("red again");
- COLOR_NORMAL(); puts("Normal");
- COLOR_BLACK(); puts("Black");
- COLOR_GREEN(); puts("Green");
- COLOR_BROWN(); puts("Brown");
- COLOR_BLUE(); puts("Blue");
- COLOR_MAGENTA(); puts("Magenta");
- COLOR_CYAN(); puts("Cyan");
- COLOR_LIGHT(); puts("Light");
- COLOR_BOLD(); puts("Now BOLD");
- COLOR_UNDER(); puts("+Under");
- COLOR_INVERS(); puts("+Invers");
- COLOR_NORMAL(); puts("Normal");
- COLOR_RED(); puts("red again");
- COLOR_BLACK(); puts("Black");
- COLOR_GREEN(); puts("Green");
- COLOR_BROWN(); puts("Brown");
- COLOR_BLUE(); puts("Blue");
- COLOR_MAGENTA(); puts("Magenta");
- COLOR_CYAN(); puts("Cyan");
- COLOR_LIGHT(); puts("Light");
- perror("xaxa2");
- COLOR_RESET(); puts("RESET");
- COLOR_BOLD(); puts("Now BOLD");
- COLOR_NORMAL(); puts("Normal BOLD");
- COLOR_RED(); puts("red again BOLD");
- COLOR_BLACK(); puts("Black BOLD");
- COLOR_GREEN(); puts("Green BOLD");
- COLOR_BROWN(); puts("Brown BOLD");
- COLOR_BLUE(); puts("Blue BOLD");
- COLOR_MAGENTA(); puts("Magenta BOLD");
- COLOR_CYAN(); puts("Cyan BOLD");
- COLOR_LIGHT(); puts("Light BOLD");
- getchar();
- return 0;
- }
- #endif
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement