• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aremmell / libsir / 376

03 Sep 2023 10:18AM UTC coverage: 94.796% (-0.09%) from 94.888%
376

push

gitlab-ci

aremmell
update config.h to use %Y instead of %y (gcc warnings)

3060 of 3228 relevant lines covered (94.8%)

621037.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.79
/src/sirtextstyle.c
1
/*
2
 * sirtextstyle.c
3
 *
4
 * Author:    Ryan M. Lederman <lederman@gmail.com>
5
 * Copyright: Copyright (c) 2018-2023
6
 * Version:   2.2.3
7
 * License:   The MIT License (MIT)
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
10
 * this software and associated documentation files (the "Software"), to deal in
11
 * the Software without restriction, including without limitation the rights to
12
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13
 * the Software, and to permit persons to whom the Software is furnished to do so,
14
 * subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 */
26
#include "sir/textstyle.h"
27
#include "sir/internal.h"
28
#include "sir/defaults.h"
29

30
static sir_colormode sir_color_mode = SIRCM_16;
31

32
/** Wrapper around the level-to-style map and the color mode. This is the data
33
 * structure protected by the SIRMI_TEXTSTYLE mutex. */
34
sir_text_style_data sir_text_style_section = {
35
    &sir_level_to_style_map[0],
36
    &sir_color_mode
37
};
38

39
const char* _sir_gettextstyle(sir_level level) {
2,156,875✔
40
    static const size_t low  = 0;
41
    static const size_t high = SIR_NUMLEVELS - 1;
42

43
    _SIR_LOCK_SECTION(sir_text_style_data, data, SIRMI_TEXTSTYLE, NULL);
2,156,875✔
44
    const char* retval = SIR_UNKNOWN;
2,133,139✔
45

46
    _SIR_DECLARE_BIN_SEARCH(low, high);
2,133,139✔
47
    _SIR_BEGIN_BIN_SEARCH()
48

49
    if (data->map[_mid].level == level) {
8,583,968✔
50
        retval = data->map[_mid].str;
2,156,875✔
51
        break;
2,156,875✔
52
    }
53

54
    _SIR_ITERATE_BIN_SEARCH((data->map[_mid].level < level ? 1 : -1));
6,427,093✔
55
    _SIR_END_BIN_SEARCH();
56
    _SIR_UNLOCK_SECTION(SIRMI_TEXTSTYLE);
2,156,875✔
57

58
    return retval;
2,156,875✔
59
}
60

61
bool _sir_settextstyle(sir_level level, sir_textstyle* style) {
155,550✔
62
    (void)_sir_seterror(_SIR_E_NOERROR);
155,550✔
63

64
    if (!_sir_sanity() || !_sir_validlevel(level))
155,550✔
65
        return false;
×
66

67
    _SIR_LOCK_SECTION(sir_text_style_data, data, SIRMI_TEXTSTYLE, false);
155,550✔
68
    bool updated              = false;
129,975✔
69
    static const size_t low   = 0;
70
    static const size_t high  = SIR_NUMLEVELS - 1;
71

72
    _SIR_DECLARE_BIN_SEARCH(low, high);
129,975✔
73
    _SIR_BEGIN_BIN_SEARCH()
74

75
    if (data->map[_mid].level == level) {
549,870✔
76
        memcpy(&data->map[_mid].style, style, sizeof(sir_textstyle));
155,550✔
77
        updated = _sir_formatstyle(*data->color_mode, style, data->map[_mid].str);
155,550✔
78
        break;
155,550✔
79
    }
80

81
    _SIR_ITERATE_BIN_SEARCH((data->map[_mid].level < level ? 1 : -1));
394,320✔
82
    _SIR_END_BIN_SEARCH();
83
    _SIR_UNLOCK_SECTION(SIRMI_TEXTSTYLE);
155,550✔
84

85
    SIR_ASSERT(updated);
147,025✔
86
    return updated;
138,500✔
87
}
88

89
const sir_textstyle* _sir_getdefstyle(sir_level level) {
12,624✔
90
    switch (level) {
12,624✔
91
        case SIRL_EMERG:  return &sir_lvl_emerg_def_style;
1,359✔
92
        case SIRL_ALERT:  return &sir_lvl_alert_def_style;
1,578✔
93
        case SIRL_CRIT:   return &sir_lvl_crit_def_style;
1,578✔
94
        case SIRL_ERROR:  return &sir_lvl_error_def_style;
1,578✔
95
        case SIRL_WARN:   return &sir_lvl_warn_def_style;
1,578✔
96
        case SIRL_NOTICE: return &sir_lvl_notice_def_style;
1,578✔
97
        case SIRL_INFO:   return &sir_lvl_info_def_style;
1,359✔
98
        case SIRL_DEBUG:  return &sir_lvl_debug_def_style;
1,578✔
99
        // GCOVR_EXCL_START
100
        case SIRL_NONE: /* this should never happen */
101
        default:
102
            SIR_ASSERT(level);
103
            return &sir_lvl_info_def_style;
104
        // GCOVR_EXCL_STOP
105
    }
106
}
107

108
bool _sir_resettextstyles(void) {
1,578✔
109
    (void)_sir_seterror(_SIR_E_NOERROR);
1,578✔
110

111
    if (!_sir_sanity())
1,578✔
112
        return false;
×
113

114
    _SIR_LOCK_SECTION(sir_text_style_data, data, SIRMI_TEXTSTYLE, false);
1,578✔
115
    bool all_ok = true;
1,359✔
116
    for (size_t n = 0; n < SIR_NUMLEVELS; n++) {
14,202✔
117
        memcpy(&data->map[n].style, _sir_getdefstyle(data->map[n].level),
12,624✔
118
             sizeof(sir_textstyle));
119
        all_ok &= _sir_formatstyle(*data->color_mode, &data->map[n].style,
12,624✔
120
            data->map[n].str);
12,624✔
121
    }
122

123
    _SIR_UNLOCK_SECTION(SIRMI_TEXTSTYLE);
1,578✔
124
    return all_ok;
1,578✔
125
}
126

127
bool _sir_formatstyle(sir_colormode mode, const sir_textstyle* style,
168,174✔
128
    char buf[SIR_MAXSTYLE]) {
129
    if (!_sir_validtextstyle(mode, style))
168,174✔
130
        return false;
57✔
131

132
    _sir_resetstr(buf);
168,108✔
133

134
    switch (mode) {
168,108✔
135
        case SIRCM_16:
156,492✔
136
            /* \x1b[attr;fg;bgm */
137
            return 0 < snprintf(buf, SIR_MAXSTYLE, "%s%"PRIu8";%"PRIu8";%"PRIu8"m",
287,250✔
138
                SIR_ESC, (uint8_t)style->attr, (uint8_t)_sir_mkansifgcolor(style->fg),
156,492✔
139
                (uint8_t)_sir_mkansibgcolor(style->bg));
156,492✔
140
        case SIRCM_256: {
5,786✔
141
            /* \x1b[attr;38;5;fg;48;5;bgm */
142
            return 0 < snprintf(buf, SIR_MAXSTYLE,
15,780✔
143
                "%s%"PRIu8";%"PRIu8";5;%"PRIu8";%"PRIu8";5;%"PRIu8"m", SIR_ESC,
144
                (uint8_t)style->attr, (uint8_t)_sir_getansifgcmd(style->fg),
5,786✔
145
                (uint8_t)style->fg, (uint8_t)_sir_getansibgcmd(style->bg),
5,786✔
146
                (uint8_t)style->bg);
5,786✔
147
        }
148
        case SIRCM_RGB: {
5,830✔
149
            /* \x1b[attr;38;2;rrr;ggg;bbb;48;2;rrr;ggg;bbbm */
150
            return 0 < snprintf(buf, SIR_MAXSTYLE,
15,900✔
151
                "%s%"PRIu8";%"PRIu8";2;%"PRIu8";%"PRIu8";%"PRIu8";%"PRIu8
152
                ";2;%"PRIu8";%"PRIu8";%"PRIu8"m", SIR_ESC, (uint8_t)style->attr,
5,830✔
153
                (uint8_t)_sir_getansifgcmd(style->fg), _sir_getredfromcolor(style->fg),
5,830✔
154
                _sir_getgreenfromcolor(style->fg), _sir_getbluefromcolor(style->fg),
5,830✔
155
                (uint8_t)_sir_getansibgcmd(style->bg), _sir_getredfromcolor(style->bg),
5,830✔
156
                _sir_getgreenfromcolor(style->bg), _sir_getbluefromcolor(style->bg));
5,830✔
157
        }
158
        case SIRCM_INVALID: // GCOVR_EXCL_START
159
        default:
160
            return false;
161
    } // GCOVR_EXCL_STOP
162
}
163

164
bool _sir_validtextstyle(sir_colormode mode, const sir_textstyle* style) {
168,174✔
165
    if (!_sir_validptr(style) || !_sir_validcolormode(mode))
168,174✔
166
        return false;
×
167

168
    sir_textcolor fg = SIRCM_16 == mode ? _sir_mkansifgcolor(style->fg) : style->fg;
168,174✔
169
    sir_textcolor bg = SIRCM_16 == mode ? _sir_mkansibgcolor(style->bg) : style->bg;
168,174✔
170

171
    if (!_sir_validtextattr(style->attr) || !_sir_validtextcolor(mode, fg) ||
195,495✔
172
        !_sir_validtextcolor(mode, bg))
168,130✔
173
        return false;
44✔
174

175
    if (SIRTC_DEFAULT != style->fg && SIRTC_DEFAULT != style->bg &&
168,130✔
176
        style->fg == style->bg) {
132,579✔
177
        _sir_selflog("error: fg color %08"PRIx32" and bg color %08"PRIx32
21✔
178
                     " are identical; text would be invisible", style->fg,
179
                     style->bg);
180
        SIR_ASSERT(SIRTC_DEFAULT != style->fg && SIRTC_DEFAULT != style->bg &&
21✔
181
                style->fg == style->bg);
182
        return _sir_seterror(_SIR_E_TEXTSTYLE);
22✔
183
    }
184

185
    return true;
140,790✔
186
}
187

188
bool _sir_setcolormode(sir_colormode mode) {
822✔
189
    if (!_sir_validcolormode(mode))
822✔
190
        return false;
19✔
191

192
    _SIR_LOCK_SECTION(sir_text_style_data, data, SIRMI_TEXTSTYLE, false);
800✔
193
    if (*data->color_mode != mode) {
800✔
194
        sir_colormode old = *data->color_mode;
57✔
195
        *data->color_mode = mode;
66✔
196
        _sir_selflog("color mode changed from %"PRId32" to %"PRId32, old, mode);
63✔
197

198
        /* when the color mode changes, it's necessary to regenerate the text styles
199
         * we're holding. for example in the case of downgrading color modes, the
200
         * styles in memory could be incompatible with the new mode. */
201
        if (!_sir_resettextstyles())
66✔
202
            _sir_selflog("error: failed to reset text styles!");
×
203
    } else {
204
        _sir_selflog("skipped superfluous update of color mode: %"PRId32, mode);
700✔
205
    }
206
    _SIR_UNLOCK_SECTION(SIRMI_TEXTSTYLE);
800✔
207

208
    return true;
800✔
209
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc