Skip to content

Instantly share code, notes, and snippets.

@ghostofpokemon
Created May 2, 2025 04:54
Show Gist options
  • Save ghostofpokemon/50807f3a78618ab74b5d6d9b92d47db6 to your computer and use it in GitHub Desktop.
Save ghostofpokemon/50807f3a78618ab74b5d6d9b92d47db6 to your computer and use it in GitHub Desktop.
Pipes Inception Labs diffusion LLM streamed responses through an animated ncurses window → locks on the final frame until you tap any key

diffuse.c — minimalist ncurses stream‑viewer

  • Streams incremental JSON chunks from InceptionLabs’ mercury-coder API, but any data:‑prefixed SSE works).
  • Renders them live in ncurses.
  • On diffusion_progress >= 1.0
    ↳ shows the full transcript ↳ waits on /dev/tty, not the pipe, so it blocks even when stdin has hit EOF.
  • Wipes the screen before endwin() → zero prompt garbage.

build

gcc -o diffuse diffuse.c -I/opt/homebrew/include -L/opt/homebrew/lib -lncurses -ljson-c

use

curl -sN https://api.inceptionlabs.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $INCEPTION_API_KEY" \
  -d '{
    "model": "mercury-coder-small",
    "messages": [
      {"role": "user", "content": "Hello! What is a diffusion model? Explain like a MrBeast video"}
    ],
    "max_tokens": 500,
    "stream": true,
    "diffusing": true
  }' |./diffuse

requirements?

You'll want to have these:

brew install ncurses json-c

why not just less?

Because less doesn’t animate in place, and its “follow” mode still drops you back to the prompt the instant the pipe closes. diffuse.c is single‑purpose, 200 LOC, zero dependencies besides ncurses + json‑c—perfect for embedding in your dotfiles or piping through fzf later.

/* d.c – stream-and-freeze demo, v2 */
#include <ncurses.h>
#include <json-c/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
static void wait_on_tty(void) {
int tty = open("/dev/tty", O_RDONLY | O_CLOEXEC);
if (tty >= 0) {
char c;
read(tty, &c, 1); /* blocks */
close(tty);
}
}
int main(void)
{
/* ── ncurses init ─────────────────────────────────────────── */
initscr();
cbreak();
noecho();
curs_set(0);
const char *PROMPT = ""; /* ≤ UTF-8, looks clean in NerdFont/Powerline */
char line[4096], final_output[4096] = {0};
bool got_final = false;
/* ── stream frames from stdin ─────────────────────────────── */
while (fgets(line, sizeof line, stdin)) {
if (strncmp(line, "data: ", 6) != 0) continue;
json_object *root = json_tokener_parse(line + 6);
if (!root) continue;
json_object *choices = json_object_object_get(root, "choices");
json_object *first = json_object_array_get_idx(choices, 0);
json_object *delta = json_object_object_get(first, "delta");
json_object *content_o = json_object_object_get(delta, "content");
const char *content = json_object_get_string(content_o);
json_object *meta = json_object_object_get(root, "diffusion_meta");
json_object *prog_o = json_object_object_get(meta, "diffusion_progress");
double progress = json_object_get_double(prog_o);
if (content && progress < 1.0) { /* live frame */
clear();
mvprintw(0, 0, "%s", content);
refresh();
usleep(100000);
} else if (content && progress >= 1.0) { /* final frame */
strncat(final_output, content,
sizeof(final_output) - strlen(final_output) - 1);
clear();
mvprintw(0, 0, "%s", final_output);
int col = (COLS - (int)strlen(PROMPT)) / 2;
attron(COLOR_PAIR(1) | A_BOLD);
mvprintw(LINES - 1, col < 0 ? 0 : col, "%s", PROMPT);
attroff(COLOR_PAIR(1) | A_BOLD);
refresh();
got_final = true;
json_object_put(root);
break;
}
json_object_put(root);
}
/* ── hold screen ──────────────────────────────────────────── */
if (got_final) wait_on_tty();
/* wipe status line so shell sees a clean slate */
clear();
refresh();
endwin(); /* back to normal tty */
puts(final_output); /* optional echo */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment