LCOV - code coverage report
Current view: top level - src/tools/kdb - ansicolors.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 12 39 30.8 %
Date: 2019-09-12 12:28:41 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include "ansicolors.hpp"
      10             : 
      11             : #ifdef _WIN32
      12             : #include <io.h>
      13             : #include <stdio.h>
      14             : #undef STDERR_FILENO
      15             : #undef STDOUT_FILENO
      16             : #define STDERR_FILENO _fileno (stderr)
      17             : #define STDOUT_FILENO _fileno (stdout)
      18             : #define isatty _isatty
      19             : #else
      20             : #include <unistd.h>
      21             : #endif
      22             : 
      23           0 : std::string getColorEscape (ANSI_COLOR color, ANSI_COLOR_LAYER layer)
      24             : {
      25           0 :         if (color == ANSI_COLOR::RESET) return "\x1b[0m";
      26           0 :         if (color == ANSI_COLOR::BOLD) return "\x1b[1m";
      27           0 :         if (color == ANSI_COLOR::UNDERSCORE) return "\x1b[4m";
      28           0 :         if (layer == ANSI_COLOR_LAYER::FG)
      29             :         {
      30           0 :                 switch (color)
      31             :                 {
      32             :                 case ANSI_COLOR::BLACK:
      33           0 :                         return "\x1b[30m";
      34             :                         break;
      35             :                 case ANSI_COLOR::RED:
      36           0 :                         return "\x1b[31m";
      37             :                         break;
      38             :                 case ANSI_COLOR::GREEN:
      39           0 :                         return "\x1b[32m";
      40             :                         break;
      41             :                 case ANSI_COLOR::YELLOW:
      42           0 :                         return "\x1b[33m";
      43             :                         break;
      44             :                 case ANSI_COLOR::BLUE:
      45           0 :                         return "\x1b[34m";
      46             :                         break;
      47             :                 case ANSI_COLOR::MAGENTA:
      48           0 :                         return "\x1b[35m";
      49             :                         break;
      50             :                 case ANSI_COLOR::CYAN:
      51           0 :                         return "\x1b[36m";
      52             :                         break;
      53             :                 case ANSI_COLOR::WHITE:
      54           0 :                         return "\x1b[37m";
      55             :                         break;
      56             :                 default:
      57           0 :                         return "";
      58             :                 }
      59             :         }
      60             :         else
      61             :         {
      62           0 :                 switch (color)
      63             :                 {
      64             :                 case ANSI_COLOR::BLACK:
      65           0 :                         return "\x1b[40m";
      66             :                         break;
      67             :                 case ANSI_COLOR::RED:
      68           0 :                         return "\x1b[41m";
      69             :                         break;
      70             :                 case ANSI_COLOR::GREEN:
      71           0 :                         return "\x1b[42m";
      72             :                         break;
      73             :                 case ANSI_COLOR::YELLOW:
      74           0 :                         return "\x1b[43m";
      75             :                         break;
      76             :                 case ANSI_COLOR::BLUE:
      77           0 :                         return "\x1b[44m";
      78             :                         break;
      79             :                 case ANSI_COLOR::MAGENTA:
      80           0 :                         return "\x1b[45m";
      81             :                         break;
      82             :                 case ANSI_COLOR::CYAN:
      83           0 :                         return "\x1b[46m";
      84             :                         break;
      85             :                 case ANSI_COLOR::WHITE:
      86           0 :                         return "\x1b[47m";
      87             :                         break;
      88             :                 default:
      89           0 :                         return "";
      90             :                 }
      91             :         }
      92             : }
      93             : 
      94        7160 : static bool hasColor (int no, std::string const & c)
      95             : {
      96        7160 :         return !(c.compare ("never") == 0 || (c.compare ("auto") == 0 && !isatty (no)));
      97             : }
      98             : 
      99        4241 : bool hasErrorColor (std::string const & c)
     100             : {
     101        4241 :         static bool has = hasColor (STDERR_FILENO, c);
     102        4241 :         return has;
     103             : }
     104             : 
     105        9976 : bool hasStdColor (std::string const & c)
     106             : {
     107        9976 :         static bool has = hasColor (STDOUT_FILENO, c);
     108        9976 :         return has;
     109             : }
     110             : 
     111         661 : std::string getErrorColor (ANSI_COLOR color, ANSI_COLOR_LAYER layer)
     112             : {
     113        3305 :         if (!hasErrorColor ()) return "";
     114           0 :         return getColorEscape (color, layer);
     115             : }
     116             : 
     117        6396 : std::string getStdColor (ANSI_COLOR color, ANSI_COLOR_LAYER layer)
     118             : {
     119       31980 :         if (!hasStdColor ()) return "";
     120           0 :         return getColorEscape (color, layer);
     121             : }
     122             : 
     123             : #ifdef _WIN32
     124             : #undef isatty
     125             : #undef STDERR_FILENO
     126             : #undef STDOUT_FILENO
     127             : #endif

Generated by: LCOV version 1.13