Skip to content

Commit

Permalink
Add ansi escape codes to color text
Browse files Browse the repository at this point in the history
  • Loading branch information
chinarjoshi committed Sep 20, 2023
1 parent 2306e4d commit 724976b
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/toml/toml_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,19 @@ char *toml_dumps(toml_table_t *table) {
if (!table)
return 0;

// First determine length of string.
// First determine length of string, including ANSI escape codes
int len = 0;
toml_table_t *tab;
for (int i = 0; i < table->ntab; i++) {
tab = table->tab[i];
len += strlen(tab->key) + 3; // [%s]\n
len += strlen(tab->key) + 28; // \033[1;34m[\033[1;35m%s\033[1;34m]\033[0m\n
for (int j = 0; j < tab->nkval; j++) {
// "%s = \"%s\"\n"
len += strlen(tab->kval[j]->key) + strlen(tab->kval[j]->val) + 6;
// %s \033[34m=\033[32m\"%s\"\033[0m\n
len += strlen(tab->kval[j]->key) + strlen(tab->kval[j]->val) + 20;
}
len += 1; // "\n"
if (i < table->ntab - 1)
len += 1; // "\n"
}
len += 1; // \0

Expand All @@ -179,11 +181,11 @@ char *toml_dumps(toml_table_t *table) {
int pos = 0;
for (int i = 0; i < table->ntab; i++) {
tab = table->tab[i];
sprintf(toml_str + pos, "[%s]\n", tab->key);
pos += strlen(tab->key) + 3;
sprintf(toml_str + pos, "\033[1;34m[\033[1;35m%s\033[1;34m]\033[0m\n", tab->key);
pos += strlen(tab->key) + 28;
for (int j = 0; j < tab->nkval; j++) {
sprintf(toml_str + pos, "%s = \"%s\"\n", tab->kval[j]->key, tab->kval[j]->val);
pos += strlen(tab->kval[j]->key) + strlen(tab->kval[j]->val) + 6;
sprintf(toml_str + pos, "%s \033[34m= \033[32m\"%s\"\033[0m\n", tab->kval[j]->key, tab->kval[j]->val);
pos += strlen(tab->kval[j]->key) + strlen(tab->kval[j]->val) + 20;
}
if (i < table->ntab - 1) {
sprintf(toml_str + pos, "\n");
Expand Down

0 comments on commit 724976b

Please sign in to comment.