Draw a frame from a render state: check what's dirty, walk the rows and cells, and read styles, colors, the cursor and the selection. Use this when you render the terminal yourself.
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
case GHOSTTY_STYLE_COLOR_RGB:
case GHOSTTY_STYLE_COLOR_PALETTE:
default:
return fallback;
}
}
int main(void) {
const char* content =
"Hello, \033[1;32mworld\033[0m!\r\n"
"\033[4munderlined\033[0m text\r\n"
"\033[38;2;255;128;0morange\033[0m\r\n";
terminal, (const uint8_t*)content, strlen(content));
.value = { .coordinate = { .x = 0, .y = 1 } },
};
terminal, selection_start_pt, &selection_start);
.value = { .coordinate = { .x = 9, .y = 1 } },
};
selection.
start = selection_start;
selection.
end = selection_end;
switch (dirty) {
printf("Frame is clean, nothing to draw.\n");
break;
printf("Partial redraw needed.\n");
break;
printf("Full redraw needed.\n");
break;
}
printf("Background: #%02x%02x%02x\n",
printf("Foreground: #%02x%02x%02x\n",
const char* style_name = "unknown";
style_name = "bar";
break;
style_name = "block";
break;
style_name = "underline";
break;
style_name = "hollow";
break;
}
printf("Cursor at (%u, %u), style: %s\n",
}
uint16_t row_y = 0;
printf("Row %2u [dirty]: ", row_y);
printf("selection=%u..%u ",
}
uint32_t grapheme_len = 0;
&grapheme_len);
if (grapheme_len == 0) {
putchar(' ');
continue;
}
printf(
"\033[38;2;%u;%u;%um", fg.
r, fg.
g, fg.
b);
uint32_t codepoints[16];
.ptr = codepoints,
.cap = sizeof(codepoints) / sizeof(codepoints[0]),
};
&graphemes);
size_t len = graphemes.
len < graphemes.
cap ? graphemes.
len : graphemes.
cap;
for (size_t i = 0; i < len; i++) {
if (codepoints[i] < 128)
putchar((char)codepoints[i]);
else
printf("U+%04X", codepoints[i]);
}
printf("\033[0m");
}
printf("\n");
}
return 0;
}
#define GHOSTTY_INIT_SIZED(type)
@ GHOSTTY_POINT_TAG_ACTIVE
GHOSTTY_API GhosttyResult ghostty_render_state_row_cells_new(const GhosttyAllocator *allocator, GhosttyRenderStateRowCells *out_cells)
GHOSTTY_API GhosttyResult ghostty_render_state_row_cells_get(GhosttyRenderStateRowCells cells, GhosttyRenderStateRowCellsData data, void *out)
GHOSTTY_API GhosttyResult ghostty_render_state_clean(GhosttyRenderState state)
GHOSTTY_API bool ghostty_render_state_row_cells_next(GhosttyRenderStateRowCells cells)
GHOSTTY_API GhosttyResult ghostty_render_state_update(GhosttyRenderState state, GhosttyTerminal terminal)
struct GhosttyRenderStateRowIteratorImpl * GhosttyRenderStateRowIterator
struct GhosttyRenderStateImpl * GhosttyRenderState
GHOSTTY_API GhosttyResult ghostty_render_state_new(const GhosttyAllocator *allocator, GhosttyRenderState *state)
GHOSTTY_API GhosttyResult ghostty_render_state_row_iterator_new(const GhosttyAllocator *allocator, GhosttyRenderStateRowIterator *out_iterator)
GHOSTTY_API void ghostty_render_state_row_cells_free(GhosttyRenderStateRowCells cells)
GHOSTTY_API void ghostty_render_state_row_iterator_free(GhosttyRenderStateRowIterator iterator)
GHOSTTY_API void ghostty_render_state_free(GhosttyRenderState state)
GHOSTTY_API GhosttyResult ghostty_render_state_get(GhosttyRenderState state, GhosttyRenderStateData data, void *out)
GHOSTTY_API bool ghostty_render_state_row_iterator_next_dirty(GhosttyRenderStateRowIterator iterator, uint16_t *out_y)
GHOSTTY_API GhosttyResult ghostty_render_state_row_get(GhosttyRenderStateRowIterator iterator, GhosttyRenderStateRowData data, void *out)
struct GhosttyRenderStateRowCellsImpl * GhosttyRenderStateRowCells
@ GHOSTTY_RENDER_STATE_ROW_DATA_SELECTION
@ GHOSTTY_RENDER_STATE_ROW_DATA_CELLS
@ GHOSTTY_RENDER_STATE_DATA_DIRTY
@ GHOSTTY_RENDER_STATE_DATA_COLORS
@ GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR
@ GHOSTTY_RENDER_STATE_DATA_CURSOR
@ GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_LEN
@ GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_BUF
@ GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_STYLE
@ GHOSTTY_RENDER_STATE_DIRTY_FALSE
@ GHOSTTY_RENDER_STATE_DIRTY_PARTIAL
@ GHOSTTY_RENDER_STATE_DIRTY_FULL
#define GHOSTTY_STYLE_UNDERLINE(style)
#define GHOSTTY_STYLE_FLAG_BOLD
GHOSTTY_API GhosttyResult ghostty_terminal_set(GhosttyTerminal terminal, GhosttyTerminalOption option, const void *value)
struct GhosttyTerminalImpl * GhosttyTerminal
GHOSTTY_API void ghostty_terminal_free(GhosttyTerminal terminal)
GHOSTTY_API void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)
GHOSTTY_API GhosttyResult ghostty_terminal_grid_ref(GhosttyTerminal terminal, GhosttyPoint point, GhosttyGridRef *out_ref)
GHOSTTY_API GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, uint16_t cols, uint16_t rows)
@ GHOSTTY_CURSOR_STYLE_BAR
@ GHOSTTY_CURSOR_STYLE_UNDERLINE
@ GHOSTTY_CURSOR_STYLE_BLOCK_HOLLOW
@ GHOSTTY_CURSOR_STYLE_BLOCK
@ GHOSTTY_TERMINAL_OPT_SELECTION
GhosttyColorRgb foreground
GhosttyColorRgb background
GhosttyColorRgb palette[256]
GhosttyCursorStyle visual_style
GhosttyStyleColorValue value
GhosttyStyleColor fg_color