LCOV - code coverage report
Current view: top level - lib/dpkg - color.c (source / functions) Coverage Total Hit
Test: dpkg 1.22.7-3-g89f48 C code coverage Lines: 81.0 % 21 17
Test Date: 2024-07-17 02:53:43 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 70.0 % 10 7

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * libdpkg - Debian packaging suite library routines
       3                 :             :  * color.c - color support
       4                 :             :  *
       5                 :             :  * Copyright © 2015-2016 Guillem Jover <guillem@debian.org>
       6                 :             :  *
       7                 :             :  * This is free software; you can redistribute it and/or modify
       8                 :             :  * it under the terms of the GNU General Public License as published by
       9                 :             :  * the Free Software Foundation; either version 2 of the License, or
      10                 :             :  * (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :             :  * GNU General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU General Public License
      18                 :             :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      19                 :             :  */
      20                 :             : 
      21                 :             : #include <config.h>
      22                 :             : #include <compat.h>
      23                 :             : 
      24                 :             : #include <stdbool.h>
      25                 :             : #include <stdlib.h>
      26                 :             : #include <string.h>
      27                 :             : #include <unistd.h>
      28                 :             : 
      29                 :             : #include <dpkg/macros.h>
      30                 :             : #include <dpkg/color.h>
      31                 :             : 
      32                 :             : static enum color_mode color_mode = COLOR_MODE_UNKNOWN;
      33                 :             : static bool use_color = false;
      34                 :             : 
      35                 :             : bool
      36                 :          91 : color_set_mode(const char *mode)
      37                 :             : {
      38         [ +  + ]:          91 :         if (strcmp(mode, "auto") == 0) {
      39                 :          83 :                 color_mode = COLOR_MODE_AUTO;
      40                 :          83 :                 use_color = isatty(STDOUT_FILENO);
      41         [ -  + ]:           8 :         } else if (strcmp(mode, "always") == 0) {
      42                 :           0 :                 color_mode = COLOR_MODE_ALWAYS;
      43                 :           0 :                 use_color = true;
      44                 :             :         } else {
      45                 :           8 :                 color_mode = COLOR_MODE_NEVER;
      46                 :           8 :                 use_color = false;
      47                 :             :         }
      48                 :             : 
      49                 :          91 :         return use_color;
      50                 :             : }
      51                 :             : 
      52                 :             : static bool
      53                 :         448 : color_enabled(void)
      54                 :             : {
      55                 :             :         const char *mode;
      56                 :             : 
      57         [ +  + ]:         448 :         if (color_mode != COLOR_MODE_UNKNOWN)
      58                 :         357 :                 return use_color;
      59                 :             : 
      60                 :          91 :         mode = getenv("DPKG_COLORS");
      61         [ -  + ]:          91 :         if (mode == NULL)
      62                 :           0 :                 mode = "auto";
      63                 :             : 
      64                 :          91 :         return color_set_mode(mode);
      65                 :             : }
      66                 :             : 
      67                 :             : const char *
      68                 :         448 : color_get(const char *color)
      69                 :             : {
      70         [ +  - ]:         448 :         if (!color_enabled())
      71                 :         448 :                 return "";
      72                 :             : 
      73                 :           0 :         return color;
      74                 :             : }
        

Generated by: LCOV version 2.0-1