libghostty-vt 0.0.0
Loading...
Searching...
No Matches
c-vt-colors/src/main.c

Set the default colors and palette, then read effective and default values as an OSC 10 override layers on top.

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ghostty/vt.h>
void set_color_theme(GhosttyTerminal terminal) {
// Set default foreground (light gray) and background (dark)
GhosttyColorRgb fg = { .r = 0xDD, .g = 0xDD, .b = 0xDD };
GhosttyColorRgb bg = { .r = 0x1E, .g = 0x1E, .b = 0x2E };
GhosttyColorRgb cursor = { .r = 0xF5, .g = 0xE0, .b = 0xDC };
// Set a custom palette — start from the built-in default and override
// the first 8 entries with a custom dark theme.
GhosttyColorRgb palette[256];
palette[GHOSTTY_COLOR_NAMED_BLACK] = (GhosttyColorRgb){ 0x45, 0x47, 0x5A };
palette[GHOSTTY_COLOR_NAMED_RED] = (GhosttyColorRgb){ 0xF3, 0x8B, 0xA8 };
palette[GHOSTTY_COLOR_NAMED_GREEN] = (GhosttyColorRgb){ 0xA6, 0xE3, 0xA1 };
palette[GHOSTTY_COLOR_NAMED_YELLOW] = (GhosttyColorRgb){ 0xF9, 0xE2, 0xAF };
palette[GHOSTTY_COLOR_NAMED_BLUE] = (GhosttyColorRgb){ 0x89, 0xB4, 0xFA };
palette[GHOSTTY_COLOR_NAMED_MAGENTA] = (GhosttyColorRgb){ 0xF5, 0xC2, 0xE7 };
palette[GHOSTTY_COLOR_NAMED_CYAN] = (GhosttyColorRgb){ 0x94, 0xE2, 0xD5 };
palette[GHOSTTY_COLOR_NAMED_WHITE] = (GhosttyColorRgb){ 0xBA, 0xC2, 0xDE };
}
void print_color(GhosttyTerminal terminal,
const char* name,
GhosttyTerminalData effective_data,
GhosttyTerminalData default_data) {
GhosttyResult res = ghostty_terminal_get(terminal, effective_data, &color);
if (res == GHOSTTY_SUCCESS) {
printf(" %-12s effective: #%02X%02X%02X", name, color.r, color.g, color.b);
} else {
printf(" %-12s effective: (not set)", name);
}
res = ghostty_terminal_get(terminal, default_data, &color);
if (res == GHOSTTY_SUCCESS) {
printf(" default: #%02X%02X%02X\n", color.r, color.g, color.b);
} else {
printf(" default: (not set)\n");
}
}
void print_all_colors(GhosttyTerminal terminal, const char* label) {
printf("%s:\n", label);
print_color(terminal, "foreground",
print_color(terminal, "background",
print_color(terminal, "cursor",
// Show palette index 0 (black) as an example
GhosttyColorRgb palette[256];
printf(" %-12s effective: #%02X%02X%02X", "palette[0]",
palette[0].r, palette[0].g, palette[0].b);
palette);
printf(" default: #%02X%02X%02X\n", palette[0].r, palette[0].g, palette[0].b);
}
int main() {
// Create a terminal
GhosttyTerminal terminal = NULL;
if (ghostty_terminal_new(NULL, &terminal, 80, 24) != GHOSTTY_SUCCESS) {
fprintf(stderr, "Failed to create terminal\n");
return 1;
}
// Before setting any colors, everything is unset
print_all_colors(terminal, "Before setting defaults");
// Set our color theme defaults
set_color_theme(terminal);
print_all_colors(terminal, "\nAfter setting defaults");
// Simulate an OSC override (e.g. a program running inside the
// terminal changes the foreground via OSC 10)
const char* osc_fg = "\x1B]10;rgb:FF/00/00\x1B\\";
ghostty_terminal_vt_write(terminal, (const uint8_t*)osc_fg,
strlen(osc_fg));
print_all_colors(terminal, "\nAfter OSC foreground override");
// Clear the foreground default — the OSC override is still active
print_all_colors(terminal, "\nAfter clearing foreground default");
return 0;
}
#define GHOSTTY_COLOR_NAMED_YELLOW
Definition color.h:153
#define GHOSTTY_COLOR_NAMED_WHITE
Definition color.h:161
#define GHOSTTY_COLOR_NAMED_CYAN
Definition color.h:159
#define GHOSTTY_COLOR_NAMED_MAGENTA
Definition color.h:157
#define GHOSTTY_COLOR_NAMED_RED
Definition color.h:149
#define GHOSTTY_COLOR_NAMED_BLUE
Definition color.h:155
#define GHOSTTY_COLOR_NAMED_BLACK
Definition color.h:147
#define GHOSTTY_COLOR_NAMED_GREEN
Definition color.h:151
GhosttyResult
Definition types.h:143
@ GHOSTTY_SUCCESS
Definition types.h:145
GHOSTTY_API GhosttyResult ghostty_terminal_set(GhosttyTerminal terminal, GhosttyTerminalOption option, const void *value)
GHOSTTY_API GhosttyResult ghostty_terminal_get(GhosttyTerminal terminal, GhosttyTerminalData data, void *out)
struct GhosttyTerminalImpl * GhosttyTerminal
Definition types.h:196
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_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, uint16_t cols, uint16_t rows)
GhosttyTerminalData
Definition terminal.h:2658
@ GHOSTTY_TERMINAL_DATA_COLOR_BACKGROUND_DEFAULT
Definition terminal.h:2860
@ GHOSTTY_TERMINAL_DATA_COLOR_BACKGROUND
Definition terminal.h:2826
@ GHOSTTY_TERMINAL_DATA_COLOR_PALETTE_DEFAULT
Definition terminal.h:2876
@ GHOSTTY_TERMINAL_DATA_COLOR_FOREGROUND_DEFAULT
Definition terminal.h:2851
@ GHOSTTY_TERMINAL_DATA_COLOR_CURSOR_DEFAULT
Definition terminal.h:2869
@ GHOSTTY_TERMINAL_DATA_COLOR_FOREGROUND
Definition terminal.h:2817
@ GHOSTTY_TERMINAL_DATA_COLOR_PALETTE
Definition terminal.h:2842
@ GHOSTTY_TERMINAL_DATA_COLOR_CURSOR
Definition terminal.h:2835
@ GHOSTTY_TERMINAL_OPT_COLOR_CURSOR
Definition terminal.h:2158
@ GHOSTTY_TERMINAL_OPT_COLOR_FOREGROUND
Definition terminal.h:2140
@ GHOSTTY_TERMINAL_OPT_COLOR_PALETTE
Definition terminal.h:2168
@ GHOSTTY_TERMINAL_OPT_COLOR_BACKGROUND
Definition terminal.h:2149
uint8_t g
Definition color.h:50
uint8_t b
Definition color.h:51
uint8_t r
Definition color.h:49