class Termisu::Terminfo::Parser

Overview

Binary terminfo database parser.

Parses compiled terminfo database files in the ncurses binary format, supporting both standard 16-bit and extended 32-bit formats. The parser uses name-based capability lookup via the Capabilities::STRING_CAPS ordering to map capability names to their escape sequences.

Binary Format

The terminfo binary format consists of:

Format Detection

Error Handling

The parser raises ParseError with specific error types:

Usage

data = File.read("/usr/share/terminfo/x/xterm-256color")
caps = Parser.parse(data, ["clear", "bold", "smcup"])
# Raises ParseError if data is malformed

Defined in:

termisu/terminfo/parser.cr

Constant Summary

EXTENDED_MAGIC = 542_i16

Magic number for extended 32-bit terminfo format.

HEADER_LENGTH = 12

Size of terminfo binary header in bytes.

MAGIC = 282_i16

Magic number for standard 16-bit terminfo format.

MAX_BOOLEANS_COUNT = 512
MAX_NAMES_LENGTH = 4096

Maximum reasonable header values to detect corruption.

MAX_NUMBERS_COUNT = 512
MAX_STRINGS_COUNT = 512
MAX_TABLE_SIZE = 65536

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(data : Bytes) #

[View source]

Class Method Detail

def self.parse(data : Bytes, cap_names : Array(String)) : Hash(String, String) #

Parses terminfo binary data and returns requested capabilities.

Creates a new parser instance and extracts the specified capabilities from the terminfo database.

Parameters

  • data: Raw terminfo binary data
  • cap_names: Array of capability names to extract (e.g., ["clear", "bold"])

Returns

Hash mapping capability names to their escape sequence values.

Raises

  • ParseError if the data is malformed or corrupted

[View source]
def self.parse?(data : Bytes, cap_names : Array(String)) : Hash(String, String) | Nil #

Parses terminfo data, returning nil on parse errors instead of raising.

Useful when you want to handle parse failures gracefully without exceptions.

Returns

Hash of capabilities, or nil if parsing failed.


[View source]

Instance Method Detail

def parse(required_caps : Array(String)) : Hash(String, String) #

Parses capability values from terminfo binary data.

Reads the binary format, extracts all string capabilities, then filters to only the requested capability names using STRING_CAPS ordering.

Parameters

  • required_caps: Capability names to extract

Returns

Hash of capability name => escape sequence. Missing capabilities are omitted.

Raises


[View source]