Skip to content
pi-c Documentation
Login

Documentation

/* The theme set; see themes.h. */
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include "tui/themes.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "input.h"
#include "platform.h"

#define THEME_MAX (1u << 20)

typedef struct {
    char **v;
    size_t n;
} thm_names;

static int add_name(void *ctx, const char *name)
{
    thm_names *l = ctx;
    size_t len = strlen(name);
    char **g;
    if (len < 6 || strcmp(name + len - 5, ".json") != 0)
        return 0;
    if (!(g = realloc(l->v, (l->n + 1) * sizeof *g)))
        return 0;
    l->v = g;
    if ((l->v[l->n] = strdup(name)))
        l->n++;
    return 0;
}

static int by_str(const void *a, const void *b)
{
    return strcmp(*(char *const *)a, *(char *const *)b);
}

static int by_name(const void *a, const void *b)
{
    return strcmp(((const tui_theme *)a)->name, ((const tui_theme *)b)->name);
}

typedef struct {
    char *p;
    size_t n, cap;
} thm_sbuf;

static void warn(thm_sbuf *w, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
static void warn(thm_sbuf *w, const char *fmt, ...)
{
    char line[2300];
    va_list ap;
    int n;
    char *g;
    if (!w)
        return;
    va_start(ap, fmt);
    n = vsnprintf(line, sizeof line, fmt, ap);
    va_end(ap);
    if (n <= 0)
        return;
    if ((size_t)n >= sizeof line)
        n = (int)sizeof line - 1;
    if (w->n + (size_t)n + 2 > w->cap) {
        size_t cap = w->cap ? w->cap : 512;
        while (cap < w->n + (size_t)n + 2)
            cap *= 2;
        if (!(g = realloc(w->p, cap)))
            return;
        w->p = g;
        w->cap = cap;
    }
    memcpy(w->p + w->n, line, (size_t)n);
    w->n += (size_t)n;
    w->p[w->n++] = '\n';
    w->p[w->n] = '\0';
}

static int have(const tui_themeset *s, const char *name)
{
    for (size_t i = 0; i < s->n; i++)
        if (strcmp(s->t[i].name, name) == 0)
            return 1;
    return 0;
}

static int load_dir(tui_themeset *s, const char *dir, thm_sbuf *w, size_t *cap)
{
    thm_names l = {NULL, 0};
    int rc = 0;
    if (pic_platform_get()->input_list(dir, add_name, &l) != 0) {
        free(l.v);
        return 0; /* no such directory */
    }
    if (l.n > 1) { /* glibc: qsort's array may not be NULL, even empty */
        qsort(l.v, l.n, sizeof *l.v, by_str);
    }
    for (size_t i = 0; i < l.n && rc == 0; i++) {
        char path[1024], err[2048];
        char *text;
        size_t len = 0;
        tui_theme t;
        if (snprintf(path, sizeof path, "%s/%s", dir, l.v[i]) >= (int)sizeof path)
            continue;
        text = pic_input_read(path, THEME_MAX, 0, &len, NULL);
        if (!text) {
            warn(w, "Failed to read theme %s", path);
            continue;
        }
        if (tui_theme_parse(text, len, path, &t, err, sizeof err) != 0) {
            warn(w, "%s", err);
        } else if (strcmp(t.name, "system") == 0) {
            warn(w, "Theme \"%s\" in %s is ignored: \"system\" is a reserved name", t.name, path);
        } else if (have(s, t.name)) {
            warn(w, "Theme name \"%s\" collision: %s is skipped", t.name, path);
        } else {
            tui_theme *g;
            if (s->n == *cap) {
                *cap = *cap ? *cap * 2 : 8;
                if (!(g = realloc(s->t, *cap * sizeof *g)))
                    rc = -1;
                else
                    s->t = g;
            }
            if (rc == 0) {
                (void)snprintf(t.path, sizeof t.path, "%s", path);
                s->t[s->n++] = t;
            }
        }
        free(text);
    }
    for (size_t i = 0; i < l.n; i++)
        free(l.v[i]);
    free(l.v);
    return rc;
}

int tui_themeset_load(tui_themeset *s, const char *home, const char *workspace, int trusted, char **warnings)
{
    thm_sbuf w = {NULL, 0, 0};
    size_t cap = 0;
    int rc = 0;
    memset(s, 0, sizeof *s);
    cap = 8;
    if (!(s->t = malloc(cap * sizeof *s->t)))
        return -1;
    if (tui_theme_builtin("dark", &s->t[0]) != 0 || tui_theme_builtin("light", &s->t[1]) != 0) {
        free(s->t);
        memset(s, 0, sizeof *s);
        return -1;
    }
    s->n = 2;
    if (home && *home) {
        char dir[1024];
        if (snprintf(dir, sizeof dir, "%s/.pic/themes", home) < (int)sizeof dir)
            rc = load_dir(s, dir, warnings ? &w : NULL, &cap);
    }
    if (rc == 0 && trusted && workspace && *workspace) {
        char dir[1024];
        if (snprintf(dir, sizeof dir, "%s/.pic/themes", workspace) < (int)sizeof dir)
            rc = load_dir(s, dir, warnings ? &w : NULL, &cap);
    }
    if (s->n > 1) { /* glibc: qsort's array may not be NULL, even empty */
        qsort(s->t, s->n, sizeof *s->t, by_name);
    }
    if (warnings)
        *warnings = w.p ? w.p : strdup("");
    else
        free(w.p);
    if (rc != 0)
        tui_themeset_free(s);
    return rc;
}

void tui_themeset_free(tui_themeset *s)
{
    free(s->t);
    memset(s, 0, sizeof *s);
}

const tui_theme *tui_themeset_find(const tui_themeset *s, const char *name)
{
    for (size_t i = 0; name && i < s->n; i++)
        if (strcmp(s->t[i].name, name) == 0)
            return &s->t[i];
    return NULL;
}

static void trim_copy(char *out, size_t cap, const char *s, size_t n)
{
    while (n && (*s == ' ' || *s == '\t'))
        s++, n--;
    while (n && (s[n - 1] == ' ' || s[n - 1] == '\t'))
        n--;
    if (n >= cap)
        n = cap - 1;
    memcpy(out, s, n);
    out[n] = '\0';
}

const char *tui_theme_setting_name(const char *setting, int light, char *out, size_t cap)
{
    const char *slash;
    if (!setting || !*setting || strcmp(setting, "system") == 0) {
        (void)snprintf(out, cap, "%s", light ? "light" : "dark");
        return out;
    }
    slash = strchr(setting, '/');
    if (slash) {
        char l[64], d[64];
        trim_copy(l, sizeof l, setting, (size_t)(slash - setting));
        trim_copy(d, sizeof d, slash + 1, strlen(slash + 1));
        if (strchr(slash + 1, '/') || !*l || !*d) {
            (void)snprintf(out, cap, "%s", light ? "light" : "dark");
            return out;
        }
        (void)snprintf(out, cap, "%s", light ? l : d);
        return out;
    }
    (void)snprintf(out, cap, "%s", setting);
    return out;
}