LCOV - code coverage report
Current view: top level - src - chalk.hpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 17 26 65.4 %
Date: 2025-06-01 01:13:32 Functions: 6 6 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** Copyright (C) 2020 Sylvain Fargier
       3                 :            : **
       4                 :            : ** This software is provided 'as-is', without any express or implied
       5                 :            : ** warranty.  In no event will the authors be held liable for any damages
       6                 :            : ** arising from the use of this software.
       7                 :            : **
       8                 :            : ** Permission is granted to anyone to use this software for any purpose,
       9                 :            : ** including commercial applications, and to alter it and redistribute it
      10                 :            : ** freely, subject to the following restrictions:
      11                 :            : **
      12                 :            : ** 1. The origin of this software must not be misrepresented; you must not
      13                 :            : **    claim that you wrote the original software. If you use this software
      14                 :            : **    in a product, an acknowledgment in the product documentation would be
      15                 :            : **    appreciated but is not required.
      16                 :            : ** 2. Altered source versions must be plainly marked as such, and must not be
      17                 :            : **    misrepresented as being the original software.
      18                 :            : ** 3. This notice may not be removed or altered from any source distribution.
      19                 :            : **
      20                 :            : ** Created on: 2020-11-10T19:40:05+01:00
      21                 :            : **     Author: Sylvain Fargier <fargie_s> <fargier.sylvain@gmail.com>
      22                 :            : **
      23                 :            : */
      24                 :            : 
      25                 :            : #ifndef CHALK_HPP__
      26                 :            : #define CHALK_HPP__
      27                 :            : 
      28                 :            : #include <string>
      29                 :            : 
      30                 :            : #include <unistd.h>
      31                 :            : 
      32                 :            : namespace logger {
      33                 :            : 
      34                 :            : namespace chalk {
      35                 :            : 
      36                 :            : struct Color
      37                 :            : {
      38                 :          3 :     constexpr Color(const char *value, const char *term, int istty = 0) :
      39                 :          3 :         m_istty(istty), m_begin(value), m_end(term)
      40                 :          3 :     {}
      41                 :            :     constexpr Color() : m_istty('n'), m_begin(nullptr), m_end(nullptr) {}
      42                 :            :     Color(Color &&) = default;
      43                 :            :     Color(const Color &) = default;
      44                 :            : 
      45                 :         83 :     inline bool istty() const
      46                 :            :     {
      47                 :         83 :         static int istty = isatty(1);
      48                 :         83 :         return m_istty ? (m_istty == 'y') : istty;
      49                 :            :     }
      50                 :            : 
      51                 :            :     /**
      52                 :            :      * @brief generate a new color for fd
      53                 :            :      * @param[in] fd file descriptor associated with color
      54                 :            :      * @return new color for this terminal
      55                 :            :      */
      56                 :          3 :     Color fd(int fd) const
      57                 :            :     {
      58                 :          3 :         return Color(m_begin, m_end, isatty(fd) ? 'y' : 'n');
      59                 :            :     }
      60                 :            : 
      61                 :         43 :     inline std::string operator()(const std::string &str) const
      62                 :            :     {
      63                 :         43 :         if (istty() && m_begin && m_end)
      64                 :            :         {
      65                 :          0 :             std::string result;
      66                 :          0 :             result.reserve(str.size() + 10);
      67                 :            : 
      68                 :          0 :             result += "\033[";
      69                 :          0 :             result += m_begin;
      70                 :          0 :             result += str;
      71                 :          0 :             result += "\033[";
      72                 :          0 :             result += m_end;
      73                 :          0 :             return result;
      74                 :          0 :         }
      75                 :            :         else
      76                 :         43 :             return str;
      77                 :            :     }
      78                 :            : 
      79                 :         20 :     inline std::string begin() const
      80                 :            :     {
      81                 :         40 :         return (istty() && m_begin) ? (std::string("\033[") + m_begin) :
      82                 :         60 :                                       std::string();
      83                 :            :     }
      84                 :            : 
      85                 :         20 :     inline std::string end() const
      86                 :            :     {
      87                 :         40 :         return (istty() && m_end) ? (std::string("\033[") + m_end) :
      88                 :         60 :                                     std::string();
      89                 :            :     }
      90                 :            : 
      91                 :            : protected:
      92                 :            :     const char m_istty; /* 0=not tested, 'y'=tty, 'n'=not tty */
      93                 :            :     const char *m_begin;
      94                 :            :     const char *m_end;
      95                 :            : };
      96                 :            : 
      97                 :            : struct Foreground : Color
      98                 :            : {
      99                 :            :     constexpr explicit Foreground(const char *value) : Color(value, "39m") {}
     100                 :            : };
     101                 :            : 
     102                 :            : struct Background : Color
     103                 :            : {
     104                 :            :     constexpr explicit Background(const char *value) : Color(value, "49m") {}
     105                 :            : };
     106                 :            : 
     107                 :            : namespace fg {
     108                 :            : 
     109                 :            : constexpr Foreground black = Foreground("30m");
     110                 :            : constexpr Foreground red = Foreground("31m");
     111                 :            : constexpr Foreground green = Foreground("32m");
     112                 :            : constexpr Foreground yellow = Foreground("33m");
     113                 :            : constexpr Foreground blue = Foreground("34m");
     114                 :            : constexpr Foreground magenta = Foreground("35m");
     115                 :            : constexpr Foreground cyan = Foreground("36m");
     116                 :            : constexpr Foreground white = Foreground("37m");
     117                 :            : 
     118                 :            : namespace bright {
     119                 :            : 
     120                 :            : constexpr Foreground black = Foreground("90m");
     121                 :            : constexpr Foreground red = Foreground("91m");
     122                 :            : constexpr Foreground green = Foreground("92m");
     123                 :            : constexpr Foreground yellow = Foreground("93m");
     124                 :            : constexpr Foreground blue = Foreground("94m");
     125                 :            : constexpr Foreground magenta = Foreground("95m");
     126                 :            : constexpr Foreground cyan = Foreground("96m");
     127                 :            : constexpr Foreground white = Foreground("97m");
     128                 :            : 
     129                 :            : } // namespace bright
     130                 :            : 
     131                 :            : } // namespace fg
     132                 :            : 
     133                 :            : namespace bg {
     134                 :            : 
     135                 :            : constexpr Background black = Background("40m");
     136                 :            : constexpr Background red = Background("41m");
     137                 :            : constexpr Background green = Background("42m");
     138                 :            : constexpr Background yellow = Background("43m");
     139                 :            : constexpr Background blue = Background("44m");
     140                 :            : constexpr Background magenta = Background("45m");
     141                 :            : constexpr Background cyan = Background("46m");
     142                 :            : constexpr Background white = Background("47m");
     143                 :            : 
     144                 :            : namespace bright {
     145                 :            : 
     146                 :            : constexpr Background black = Background("100m");
     147                 :            : constexpr Background red = Background("101m");
     148                 :            : constexpr Background green = Background("102m");
     149                 :            : constexpr Background yellow = Background("103m");
     150                 :            : constexpr Background blue = Background("104m");
     151                 :            : constexpr Background magenta = Background("105m");
     152                 :            : constexpr Background cyan = Background("106m");
     153                 :            : constexpr Background white = Background("107m");
     154                 :            : 
     155                 :            : } // namespace bright
     156                 :            : 
     157                 :            : } // namespace bg
     158                 :            : 
     159                 :            : constexpr Color bold = Color("1m", "22m");
     160                 :            : constexpr Color faint = Color("2m", "22m");
     161                 :            : constexpr Color italic = Color("3m", "23m");
     162                 :            : constexpr Color underline = Color("4m", "24m");
     163                 :            : constexpr Color slowBlink = Color("5m", "25m");
     164                 :            : constexpr Color rapidBlink = Color("6m", "25m");
     165                 :            : constexpr Color reverse = Color("7m", "27m");
     166                 :            : constexpr Color conceal = Color("8m", "28m");
     167                 :            : constexpr Color crossedOut = Color("9m", "29m");
     168                 :            : 
     169                 :            : } // namespace chalk
     170                 :            : } // namespace logger
     171                 :            : 
     172                 :            : #endif

Generated by: LCOV version 1.14