Skip to content
pi-c Documentation
Login

Documentation

/* Prompt templates, as Pi has them (prompts.c): Markdown files in
 * ~/.pic/prompts (user) and WORKSPACE/.pic/prompts (project, only when the
 * workspace is trusted). Typing /name ARGS sends the file's body with the
 * arguments substituted. */
#ifndef PIC_PROMPTS_H
#define PIC_PROMPTS_H

#include <stddef.h>
#include <stdio.h>

typedef struct {
    char *name;        /* the file name without .md */
    char *description; /* frontmatter, else the body's first line cut at 60 characters */
    char *hint;        /* frontmatter argument-hint, "" when none */
    char *body;        /* after the frontmatter */
    char *path;
    int project;       /* 1: WORKSPACE/.pic/prompts, 0: ~/.pic/prompts */
} pic_prompt;

typedef struct {
    pic_prompt *v;
    size_t n;
} pic_prompts;

/* Loads the user's templates, then the workspace's when PROJECT (the caller
 * has checked trust). A name in both is listed twice; find returns the user's,
 * as Pi does. Unreadable files are skipped. Always 0 or -1 (out of memory). */
int pic_prompts_load(pic_prompts *p, const char *workspace, int project);
void pic_prompts_free(pic_prompts *p);
const pic_prompt *pic_prompts_find(const pic_prompts *p, const char *name);

/* TEXT expanded when it is "/name ARGS" naming a template (malloc'd), else
 * NULL: the caller keeps TEXT unchanged. */
char *pic_prompts_expand(const pic_prompts *p, const char *text);

/* The pieces, exposed for tests. Args are split like a shell's, quotes only
 * opening at the start of an argument. Both return malloc'd memory (NULL on
 * out of memory); free the args with pic_prompt_args_free. */
char **pic_prompt_args(const char *s, size_t *n);
void pic_prompt_args_free(char **args, size_t n);
char *pic_prompt_substitute(const char *content, char *const *args, size_t n);

/* name, scope, hint, description, path per line (tab separated), or JSON. */
void pic_prompts_print(const pic_prompts *p, FILE *out, int json);

#endif