root/src/libsir/include/sir/ansimacros.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


   1 /**
   2  * @file ansimacros.h
   3  *
   4  * @brief ANSI escape sequence macros
   5  *
   6  * A collection of macros that can be used with libsir's logging functions as well
   7  * as [f]printf, puts, etc. to manipulate terminal output.
   8  *
   9  * **Example**
  10  *   ~~~
  11  *   printf(RED("Something terrible happened!" SIR_EOL));
  12  *   ~~~
  13  *
  14  * @version 2.2.5
  15  *
  16  * -----------------------------------------------------------------------------
  17  *
  18  * SPDX-License-Identifier: MIT
  19  *
  20  * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
  21  *
  22  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  23  * this software and associated documentation files (the "Software"), to deal in
  24  * the Software without restriction, including without limitation the rights to
  25  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  26  * the Software, and to permit persons to whom the Software is furnished to do so,
  27  * subject to the following conditions:
  28  *
  29  * The above copyright notice and this permission notice shall be included in all
  30  * copies or substantial portions of the Software.
  31  *
  32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  34  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  35  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  36  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  37  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38  *
  39  * -----------------------------------------------------------------------------
  40  */
  41 
  42 #ifndef _SIR_ANSI_MACROS_H_INCLUDED
  43 # define _SIR_ANSI_MACROS_H_INCLUDED
  44 
  45 # if !defined(SIR_NO_TEXT_STYLING)
  46 #  define SIR_ESC   "\x1b[" /**< Begins an ANSI escape sequence. */
  47 #  define SIR_ESC_M "m"     /**< Marks the end of a sequence. */
  48 # else
  49 #  define SIR_ESC   ""
  50 #  define SIR_ESC_M ""
  51 # endif
  52 
  53 # if !defined(SIR_NO_TEXT_STYLING)
  54 #  define SIR_ESC_SEQ(codes, s) SIR_ESC codes SIR_ESC_M s
  55 # else
  56 #  define SIR_ESC_SEQ(codes, s) s
  57 # endif
  58 
  59 # define SIR_ESC_SEQE(codes)   SIR_ESC_SEQ(codes, "")
  60 
  61 /** Resets all previously applied colors and effects/attributes. */
  62 # if !defined(SIR_NO_TEXT_STYLING)
  63 #  define SIR_ESC_RST SIR_ESC_SEQE("0")
  64 # else
  65 #  define SIR_ESC_RST ""
  66 # endif
  67 
  68 /** A few fun characters. */
  69 # if !defined(__WIN__)
  70 #  define SIR_R_ARROW "\xe2\x86\x92"
  71 #  define SIR_L_ARROW "\xe2\x86\x90"
  72 #  define SIR_BULLET  "\xe2\x80\xa2"
  73 # else /* __WIN__ */
  74 #  define SIR_R_ARROW "->"
  75 #  define SIR_L_ARROW "<-"
  76 #  define SIR_BULLET  "-"
  77 # endif
  78 
  79 /**
  80  * Creates a sequence of colored characters 's' with foreground color 'fg',
  81  * background color 'bg', and attributes 'attr' (`0=normal, 1=bold, 2=dim,
  82  * 3=italic, 4=underlined, 5=blinking, 7=inverted, 9=strikethrough`).
  83  * Ends by resetting to the default fg/bg color, and normal attr.
  84  */
  85 # if !defined(SIR_NO_TEXT_STYLING)
  86 #  define SIR_COLOR(attr, fg, bg, s) \
  87                            SIR_ESC_SEQ(#attr ";" #fg ";" #bg, s) SIR_ESC_SEQE("0;39;49")
  88 
  89 #  define SIR_STRIKE(s)    SIR_ESC_SEQ("9", s) SIR_ESC_SEQE("29") /**< Strike-through. */
  90 #  define SIR_INVERT(s)    SIR_ESC_SEQ("7", s) SIR_ESC_SEQE("27") /**< Inverted fg/bg. */
  91 #  define SIR_ULINE(s)     SIR_ESC_SEQ("4", s) SIR_ESC_SEQE("24") /**< Underlined. */
  92 #  define SIR_EMPH(s)      SIR_ESC_SEQ("3", s) SIR_ESC_SEQE("23") /**< Emphasis/italic. */
  93 #  define SIR_BOLD(s)      SIR_ESC_SEQ("1", s) SIR_ESC_SEQE("22") /**< Bold. */
  94 #  define SIR_BLINK(s)     SIR_ESC_SEQ("5", s) SIR_ESC_SEQE("25") /**< Blinking text. */
  95 
  96 #  define SIR_BLACK(s)     SIR_COLOR(0, 30, 49, s) /**< Black foreground text. */
  97 #  define SIR_BLACKB(s)    SIR_COLOR(1, 30, 49, s) /**< Bold black foreground text. */
  98 
  99 #  define SIR_RED(s)       SIR_COLOR(0, 31, 49, s) /**< Red foreground text. */
 100 #  define SIR_REDB(s)      SIR_COLOR(1, 31, 49, s) /**< Bold red foreground text. */
 101 #  define SIR_BRED(s)      SIR_COLOR(0, 91, 49, s) /**< Bright red foreground text. */
 102 #  define SIR_BREDB(s)     SIR_COLOR(1, 91, 49, s) /**< Bold bright red foreground text. */
 103 
 104 #  define SIR_GREEN(s)     SIR_COLOR(0, 32, 49, s) /**< Green foreground text. */
 105 #  define SIR_GREENB(s)    SIR_COLOR(1, 32, 49, s) /**< Bold green foreground text. */
 106 #  define SIR_BGREEN(s)    SIR_COLOR(0, 92, 49, s) /**< Bright green foreground text. */
 107 #  define SIR_BGREENB(s)   SIR_COLOR(1, 92, 49, s) /**< Bold bright green foreground text. */
 108 
 109 #  define SIR_YELLOW(s)    SIR_COLOR(0, 33, 49, s) /**< Yellow foreground text. */
 110 #  define SIR_YELLOWB(s)   SIR_COLOR(1, 33, 49, s) /**< Bold yellow foreground text. */
 111 #  define SIR_BYELLOW(s)   SIR_COLOR(0, 93, 49, s) /**< Bright yellow foreground text. */
 112 #  define SIR_BYELLOWB(s)  SIR_COLOR(1, 93, 49, s) /**< Bold bright yellow foreground text. */
 113 
 114 #  define SIR_BLUE(s)      SIR_COLOR(0, 34, 49, s) /**< Blue foreground text. */
 115 #  define SIR_BLUEB(s)     SIR_COLOR(1, 34, 49, s) /**< Bold blue foreground text. */
 116 #  define SIR_BBLUE(s)     SIR_COLOR(0, 94, 49, s) /**< Bright blue foreground text. */
 117 #  define SIR_BBLUEB(s)    SIR_COLOR(1, 94, 49, s) /**< Bold bright blue foreground text. */
 118 
 119 #  define SIR_MAGENTA(s)   SIR_COLOR(0, 35, 49, s) /**< Magenta foreground text. */
 120 #  define SIR_MAGENTAB(s)  SIR_COLOR(1, 35, 49, s) /**< Bold magenta foreground text. */
 121 #  define SIR_BMAGENTA(s)  SIR_COLOR(0, 95, 49, s) /**< Bright magenta foreground text. */
 122 #  define SIR_BMAGENTAB(s) SIR_COLOR(1, 95, 49, s) /**< Bold bright magenta foreground text. */
 123 
 124 #  define SIR_CYAN(s)      SIR_COLOR(0, 36, 49, s) /**< Cyan foreground text. */
 125 #  define SIR_CYANB(s)     SIR_COLOR(1, 36, 49, s) /**< Bold cyan foreground text. */
 126 #  define SIR_BCYAN(s)     SIR_COLOR(0, 96, 49, s) /**< Bright cyan foreground text. */
 127 #  define SIR_BCYANB(s)    SIR_COLOR(1, 96, 49, s) /**< Bold bright cyan foreground text. */
 128 
 129 #  define SIR_BGRAY(s)     SIR_COLOR(0, 37, 49, s) /**< Bright gray foreground text. */
 130 #  define SIR_BGRAYB(s)    SIR_COLOR(1, 37, 49, s) /**< Bold bright gray foreground text. */
 131 #  define SIR_DGRAY(s)     SIR_COLOR(0, 90, 49, s) /**< Dark gray foreground text. */
 132 #  define SIR_DGRAYB(s)    SIR_COLOR(1, 90, 49, s) /**< Bold dark gray foreground text. */
 133 
 134 #  define SIR_WHITE(s)     SIR_COLOR(0, 97, 49, s) /**< White foreground text. */
 135 #  define SIR_WHITEB(s)    SIR_COLOR(1, 97, 49, s) /**< Bold white foreground text. */
 136 # else /* SIR_NO_TEXT_STYLING */
 137 #  define SIR_COLOR(attr, fg, bg, s) s
 138 
 139 #  define SIR_STRIKE(s)    s
 140 #  define SIR_INVERT(s)    s
 141 #  define SIR_ULINE(s)     s
 142 #  define SIR_EMPH(s)      s
 143 #  define SIR_BOLD(s)      s
 144 #  define SIR_BLINK(s)     s
 145 
 146 #  define SIR_BLACK(s)     s
 147 #  define SIR_BLACKB(s)    s
 148 
 149 #  define SIR_RED(s)       s
 150 #  define SIR_REDB(s)      s
 151 #  define SIR_BRED(s)      s
 152 #  define SIR_BREDB(s)     s
 153 
 154 #  define SIR_GREEN(s)     s
 155 #  define SIR_GREENB(s)    s
 156 #  define SIR_BGREEN(s)    s
 157 #  define SIR_BGREENB(s)   s
 158 
 159 #  define SIR_YELLOW(s)    s
 160 #  define SIR_YELLOWB(s)   s
 161 #  define SIR_BYELLOW(s)   s
 162 #  define SIR_BYELLOWB(s)  s
 163 
 164 #  define SIR_BLUE(s)      s
 165 #  define SIR_BLUEB(s)     s
 166 #  define SIR_BBLUE(s)     s
 167 #  define SIR_BBLUEB(s)    s
 168 
 169 #  define SIR_MAGENTA(s)   s
 170 #  define SIR_MAGENTAB(s)  s
 171 #  define SIR_BMAGENTA(s)  s
 172 #  define SIR_BMAGENTAB(s) s
 173 
 174 #  define SIR_CYAN(s)      s
 175 #  define SIR_CYANB(s)     s
 176 #  define SIR_BCYAN(s)     s
 177 #  define SIR_BCYANB(s)    s
 178 
 179 #  define SIR_BGRAY(s)     s
 180 #  define SIR_BGRAYB(s)    s
 181 #  define SIR_DGRAY(s)     s
 182 #  define SIR_DGRAYB(s)    s
 183 
 184 #  define SIR_WHITE(s)     s
 185 #  define SIR_WHITEB(s)    s
 186 # endif
 187 #endif /* ! _SIR_ANSI_MACROS_H_INCLUDED */

/* [previous][next][first][last][top][bottom][index][help] */