ANSI string parser for GtkTextBuffer
gtk-ansi-parser is a small C library to support ANSI strings for GtkTextBuffer.
You can use most of the graphics modes (\033[*m) and carriage returns to display messages with GtkTextView.
// Create objects
GtkWidget *text_view = gtk_text_view_new();
GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
GtkAnsiParser* parser = gtk_ansi_new(buffer);
// Use monospace font
GtkStyleContext* widget = gtk_widget_get_style_context(text_view);
gtk_style_context_add_class(widget, "monospace");
// GtkAnsiParser removes the first bytes when the buffer size exceeds 512KiB.
gtk_ansi_set_max_length(parser, 512 * 1024);
// Copies the color settings from the text view.
gtk_ansi_set_default_color_with_textview(parser, GTK_TEXT_VIEW(text_view));
// Append ANSI strings.
gtk_ansi_append(parser, "\033[1mBold\033[0m\n");
gtk_ansi_append(parser, "\033[31mRed\033[32mBlue\033[0m\n");
gtk_ansi_append(parser, "\033[38;2;0;255;0mGreen\033[0m\n");
gtk_ansi_append(parser, "Carriage return\rOverwrite\n");
// Free memory
g_object_unref(text_view);
gtk_ansi_free(parser);Supported Escape Sequences
GtkAnsiParser Struct Reference
include/gtk_ansi.h File Reference
- Meson (0.54.0 or later) for building
- GTK 3.10+
meson setup build
meson compile -C build
meson test -C buildmeson setup build -Dtests=false -Ddemo=false
meson compile -C buildYou don't need to clone the git repo if you build your project with meson.
Save the following text as subprojects/gtk_ansi_parser.wrap.
[wrap-git]
url = https://github.com/matyalatte/gtk-ansi-parser.git
revision = head
depth = 1
[provide]
gtk_ansi_parser = gtkansi_depThen, you can use gtk-ansi-parser in your meson project.
gtkansi_dep = dependency('gtk_ansi_parser', fallback : ['gtk_ansi_parser', 'gtkansi_dep'])
executable('your_exe_name', ['your_code.cpp'], dependencies : [gtkansi_dep])meson setup build -Dgtk_ansi_parser:tests=false -Dgtk_ansi_parser:demo=false
meson compile -C build- Tuw: A tiny GUI wrapper for command-line tools.
