Skip to content
pi-c Artifact [d80ef0e497]
Login

Artifact [d80ef0e497]

Artifact d80ef0e49737018dc0888901a18f126b83a8539053984e99575515e837e32839:


/* The committed transcript, drawn again on a toggle; see transcript.h. */
#include "transcript.h"

#include "markdown.h"
#include "theme.h"

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

enum { ITEM_LINES, ITEM_TOOL, ITEM_BASH, ITEM_THINKING, ITEM_EXPANDABLE };

typedef struct {
    int kind;
    tui_lines lines, more; /* ITEM_LINES; ITEM_EXPANDABLE: collapsed, expanded */
    char *name, *title, *diff, *output, *text; /* tool strings (text: write's content); bash command and output; thinking */
    int state, exit_code, no_context;
    long ms;
} item;

struct tui_transcript {
    item *items;
    size_t n, cap;
};

tui_transcript *tui_transcript_new(void) {
    return calloc(1, sizeof(tui_transcript));
}

static void item_free(item *it) {
    tui_lines_clear(&it->lines);
    tui_lines_clear(&it->more);
    free(it->name);
    free(it->title);
    free(it->diff);
    free(it->output);
    free(it->text);
}

void tui_transcript_clear(tui_transcript *t) {
    if (!t) {
        return;
    }
    for (size_t i = 0; i < t->n; i++) {
        item_free(&t->items[i]);
    }
    t->n = 0;
}

void tui_transcript_free(tui_transcript *t) {
    if (t) {
        tui_transcript_clear(t);
        free(t->items);
        free(t);
    }
}

static item *push(tui_transcript *t, int kind) {
    if (t->n == t->cap) {
        size_t cap = t->cap ? t->cap * 2 : 64;
        item *p = realloc(t->items, cap * sizeof *p);
        if (!p) {
            return NULL;
        }
        t->items = p;
        t->cap = cap;
    }
    memset(&t->items[t->n], 0, sizeof t->items[t->n]);
    t->items[t->n].kind = kind;
    return &t->items[t->n++];
}

/* A copy of s, or NULL for NULL; *bad set when out of memory. */
static char *dup_or_null(const char *s, int *bad) {
    char *d = s ? strdup(s) : NULL;
    *bad |= s && !d;
    return d;
}

static int pop_bad(tui_transcript *t) {
    item_free(&t->items[--t->n]);
    return -1;
}

int tui_transcript_lines(tui_transcript *t, const char *const *lines, size_t n) {
    item *it;
    if (n == 0) {
        return 0;
    }
    /* consecutive plain lines share one item */
    it = t->n && t->items[t->n - 1].kind == ITEM_LINES ? &t->items[t->n - 1] : push(t, ITEM_LINES);
    if (!it) {
        return -1;
    }
    for (size_t i = 0; i < n; i++) {
        if (tui_lines_add(&it->lines, lines[i], strlen(lines[i])) != 0) {
            return -1;
        }
    }
    return 0;
}

int tui_transcript_tool(tui_transcript *t, const tui_tool *tool) {
    item *it = push(t, ITEM_TOOL);
    int bad = 0;
    if (!it) {
        return -1;
    }
    it->name = dup_or_null(tool->name ? tool->name : "tool", &bad);
    it->title = dup_or_null(tool->title ? tool->title : "", &bad);
    it->diff = dup_or_null(tool->diff, &bad);
    it->output = dup_or_null(tool->output, &bad);
    it->text = dup_or_null(tool->content, &bad);
    it->state = tool->state;
    it->ms = tool->ms;
    return bad ? pop_bad(t) : 0;
}

int tui_transcript_bash(tui_transcript *t, const tui_bash *bash) {
    item *it = push(t, ITEM_BASH);
    int bad = 0;
    if (!it) {
        return -1;
    }
    it->title = dup_or_null(bash->command ? bash->command : "", &bad);
    it->output = dup_or_null(bash->output ? bash->output : "", &bad);
    it->state = bash->state;
    it->exit_code = bash->exit_code;
    it->no_context = bash->no_context;
    return bad ? pop_bad(t) : 0;
}

int tui_transcript_thinking(tui_transcript *t, const char *text) {
    item *it = push(t, ITEM_THINKING);
    int bad = 0;
    if (!it) {
        return -1;
    }
    it->text = dup_or_null(text ? text : "", &bad);
    return bad ? pop_bad(t) : 0;
}

int tui_transcript_expandable(tui_transcript *t, const char *const *collapsed, size_t nc, const char *const *expanded,
                              size_t ne) {
    item *it = push(t, ITEM_EXPANDABLE);
    if (!it) {
        return -1;
    }
    for (size_t i = 0; i < nc; i++) {
        if (tui_lines_add(&it->lines, collapsed[i], strlen(collapsed[i])) != 0) {
            return pop_bad(t);
        }
    }
    for (size_t i = 0; i < ne; i++) {
        if (tui_lines_add(&it->more, expanded[i], strlen(expanded[i])) != 0) {
            return pop_bad(t);
        }
    }
    return 0;
}

const char *tui_thinking_style(void) {
    static char b[48];
    (void)snprintf(b, sizeof b, "%s\x1b[3m", tui_fg(TUI_T_THINKING_TEXT));
    return b;
}

int tui_thinking_lines(const char *text, int hidden, size_t cols, tui_lines *out) {
    const char *style = tui_thinking_style();
    size_t len;
    if (hidden) {
        char line[96];
        (void)snprintf(line, sizeof line, "%sThinking...\x1b[0m", style);
        return tui_lines_add(out, line, strlen(line));
    }
    /* Pi trims the block */
    text += strspn(text, " \t\r\n");
    len = strlen(text);
    while (len > 0 && strchr(" \t\r\n", text[len - 1])) {
        len--;
    }
    {
        tui_md_opts o = {style, 0, 0};
        tui_lines l = {NULL, 0, 0};
        int rc = tui_markdown(text, len, cols ? cols : 1, &o, &l);
        for (size_t i = 0; rc == 0 && i < l.n; i++) {
            /* each line opens in the style, for a renderer that draws lines one at a time */
            size_t n = strlen(style) + strlen(l.lines[i]) + 8;
            char *s = malloc(n);
            if (!s) {
                rc = -1;
                break;
            }
            (void)snprintf(s, n, "%s%s\x1b[0m", style, l.lines[i]);
            rc = tui_lines_add(out, s, strlen(s));
            free(s);
        }
        tui_lines_clear(&l);
        return rc;
    }
}

int tui_transcript_render(const tui_transcript *t, int expanded, int hide_thinking, size_t cols, tui_lines *out) {
    int rc = 0;
    for (size_t i = 0; rc == 0 && i < t->n; i++) {
        const item *it = &t->items[i];
        if (it->kind == ITEM_LINES || it->kind == ITEM_EXPANDABLE) {
            const tui_lines *l = it->kind == ITEM_EXPANDABLE && expanded ? &it->more : &it->lines;
            for (size_t j = 0; rc == 0 && j < l->n; j++) {
                rc = tui_lines_add(out, l->lines[j], strlen(l->lines[j]));
            }
        } else if (it->kind == ITEM_TOOL) {
            tui_tool tool = {it->name, it->title, it->diff, it->output, it->state, it->ms, it->text};
            rc = tui_tool_lines(&tool, expanded, cols, out);
        } else if (it->kind == ITEM_BASH) {
            tui_bash b = {it->title, it->output, it->state, it->exit_code, it->no_context};
            rc = tui_bash_lines(&b, expanded, cols, out);
        } else {
            rc = tui_thinking_lines(it->text, hide_thinking, cols, out);
        }
    }
    return rc;
}