Skip to content

matyalatte/gtk-ansi-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gtk-ansi-parser

License: MIT

ANSI string parser for GtkTextBuffer

gtk_ansi_parser

About

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.

Example

// 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);

Documentation

Supported Escape Sequences
GtkAnsiParser Struct Reference
include/gtk_ansi.h File Reference

Building

Requirements

  • Meson (0.54.0 or later) for building
  • GTK 3.10+

Build whole project

meson setup build
meson compile -C build
meson test -C build

Build library only

meson setup build -Dtests=false -Ddemo=false
meson compile -C build

Build as subproject

You 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_dep

Then, 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

Projects Which Use gtk-ansi-parser

  • Tuw: A tiny GUI wrapper for command-line tools.

About

ANSI string parser for GtkTextBuffer

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors