Cthulhu  0.2.10
Cthulhu compiler collection
colour.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "format/colour.h"
4 #include "base/panic.h"
5 
6 #include "core/macros.h"
7 
8 #include "std/str.h"
9 
11  .colours = {
12  [eColourRed] = "",
13  [eColourGreen] = "",
14  [eColourYellow] = "",
15  [eColourBlue] = "",
16  [eColourMagenta] = "",
17  [eColourCyan] = "",
18  [eColourWhite] = "",
19  [eColourDefault] = "",
20  },
21  .reset = ""
22 };
23 
25  .colours = {
33  [eColourDefault] = "",
34  },
35  .reset = CT_ANSI_RESET
36 };
37 
39 const char *colour_get(const colour_pallete_t *colours, colour_t idx)
40 {
41  CTASSERT(colours != NULL);
43 
44  return colours->colours[idx];
45 }
46 
48 const char *colour_reset(const colour_pallete_t *colours)
49 {
50  CTASSERT(colours != NULL);
51 
52  return colours->reset;
53 }
54 
56 char *colour_text(format_context_t context, colour_t idx, const char *text)
57 {
58  const char *colour = colour_get(context.pallete, idx);
59  const char *reset = colour_reset(context.pallete);
60 
61  return str_format(context.arena, "%s%s%s", colour, text, reset);
62 }
63 
65 char *colour_format(format_context_t context, colour_t idx, const char *fmt, ...)
66 {
67  va_list args;
68  va_start(args, fmt);
69  char *msg = colour_vformat(context, idx, fmt, args);
70  va_end(args);
71 
72  return msg;
73 }
74 
76 char *colour_vformat(format_context_t context, colour_t idx, const char *fmt, va_list args)
77 {
78  const char *colour = colour_get(context.pallete, idx);
79  const char *reset = colour_reset(context.pallete);
80 
81  char *msg = str_vformat(context.arena, fmt, args);
82 
83  return str_format(context.arena, "%s%s%s", colour, msg, reset);
84 }
STA_DECL char * colour_format(format_context_t context, colour_t idx, const char *fmt,...)
Definition: colour.c:65
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
#define CT_ANSI_RESET
ANSI reset.
Definition: macros.h:80
#define CT_ANSI_MAGENTA
ANSI magenta.
Definition: macros.h:76
#define CT_ANSI_RED
ANSI red.
Definition: macros.h:72
#define CT_ANSI_YELLOW
ANSI yellow.
Definition: macros.h:74
#define CT_ANSI_BLUE
ANSI blue.
Definition: macros.h:75
#define CT_ANSI_WHITE
ANSI white.
Definition: macros.h:78
#define CT_ANSI_GREEN
ANSI green.
Definition: macros.h:73
#define CT_ANSI_CYAN
ANSI cyan.
Definition: macros.h:77
colour_t
a colour code
Definition: colour.h:23
STA_DECL char * colour_vformat(format_context_t context, colour_t idx, const char *fmt, va_list args)
format a string and add colour to it
Definition: colour.c:76
STA_DECL const char * colour_get(const colour_pallete_t *colours, colour_t idx)
get a colours string form from a pallete
Definition: colour.c:39
const colour_pallete_t kColourNone
a colour pallete that applies no colours
Definition: colour.c:10
STA_RET_STRING colour_t idx
Definition: colour.h:98
const colour_pallete_t kColourDefault
a colour pallete that applies ANSI VT100 colours
Definition: colour.c:24
STA_DECL const char * colour_reset(const colour_pallete_t *colours)
get a reset string from a pallete
Definition: colour.c:48
STA_DECL char * colour_text(format_context_t context, colour_t idx, const char *text)
add colour to a string
Definition: colour.c:56
@ eColourDefault
Definition: colour.h:32
@ eColourMagenta
Definition: colour.h:28
@ eColourCount
Definition: colour.h:34
@ eColourYellow
Definition: colour.h:26
@ eColourGreen
Definition: colour.h:25
@ eColourWhite
Definition: colour.h:30
@ eColourBlue
Definition: colour.h:27
@ eColourRed
Definition: colour.h:24
@ eColourCyan
Definition: colour.h:29
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
CT_NODISCARD STA_FORMAT_STRING const char * fmt
Definition: str.h:68
CT_NODISCARD STA_FORMAT_STRING const char CT_NODISCARD CT_STD_API char * str_vformat(arena_t *arena, const char *fmt, va_list args)
format a string
Definition: str.c:110
STA_DECL char * str_format(arena_t *arena, const char *fmt,...)
Definition: str.c:97
a colour pallete
Definition: colour.h:39
const char * colours[eColourCount]
Definition: colour.h:40
STA_FIELD_STRING const char * reset
Definition: colour.h:41
a formatting context when using colours
Definition: colour.h:46
arena_t * arena
Definition: colour.h:47
const colour_pallete_t * pallete
Definition: colour.h:48