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

This example demonstrates how to schedule incremental scrollback compression after compression-relevant terminal activity becomes idle.

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <ghostty/vt.h>
// Perform one step after the application's idle timer fires. Returning true
// asks the application to schedule another step while the terminal is idle.
static bool compression_idle_step(GhosttyTerminal terminal) {
terminal,
&compression_result);
assert(result == GHOSTTY_SUCCESS);
switch (compression_result) {
return true;
return false;
default:
assert(false);
return false;
}
}
int main(void) {
GhosttyTerminal terminal;
GhosttyResult result = ghostty_terminal_new(NULL, &terminal, 80, 24);
assert(result == GHOSTTY_SUCCESS);
size_t max_scrollback_bytes = 10 * 1024 * 1024;
terminal,
&max_scrollback_bytes);
assert(result == GHOSTTY_SUCCESS);
uint64_t compression_activity;
terminal,
&compression_activity);
assert(result == GHOSTTY_SUCCESS);
// Terminal mutations may change the token. When it changes, restart the
// application's idle timer rather than compressing on the output path.
const char *line = "repeated and compressible terminal history\r\n";
for (size_t i = 0; i < 4000; i++) {
terminal,
(const uint8_t *)line,
strlen(line));
}
uint64_t new_activity;
result = ghostty_terminal_compression_activity(terminal, &new_activity);
assert(result == GHOSTTY_SUCCESS);
if (new_activity != compression_activity) {
compression_activity = new_activity;
// Restart the application's compression idle timer here.
}
// Simulate the idle timer and its short pending-work continuations.
while (compression_idle_step(terminal)) {}
return 0;
}
GHOSTTY_API GhosttyResult ghostty_terminal_set(GhosttyTerminal terminal, GhosttyTerminalOption option, const void *value)
GHOSTTY_API GhosttyResult ghostty_terminal_compression_activity(GhosttyTerminal terminal, uint64_t *out_activity)
GhosttyTerminalCompressionResult
Definition terminal.h:194
struct GhosttyTerminalImpl * GhosttyTerminal
Definition types.h:95
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)
GHOSTTY_API GhosttyResult ghostty_terminal_compress(GhosttyTerminal terminal, GhosttyTerminalCompressionMode mode, GhosttyTerminalCompressionResult *out_result)
@ GHOSTTY_TERMINAL_COMPRESSION_MODE_INCREMENTAL
Definition terminal.h:182
@ GHOSTTY_TERMINAL_COMPRESSION_RESULT_UNSUPPORTED
Definition terminal.h:196
@ GHOSTTY_TERMINAL_COMPRESSION_RESULT_COMPLETE
Definition terminal.h:202
@ GHOSTTY_TERMINAL_COMPRESSION_RESULT_PENDING
Definition terminal.h:199
@ GHOSTTY_TERMINAL_OPT_SCROLLBACK_MAX_BYTES
Definition terminal.h:892
GhosttyResult
Definition types.h:74
@ GHOSTTY_SUCCESS
Definition types.h:76