Module: C0::Pretty

Defined in:
lib/c0/pretty.rb

Overview

Pretty form: Unicode Control Pictures (U+2400 block) for the codes, line breaks and indentation as layout. format and parse are inverses on well-formed input. GlyphInDataError (a C0::Error) is raised when a value holds a Control Picture character and cannot be shown unambiguously.

Constant Summary collapse

MODES =
{ compact: 0, aligned: 1, spaced: 2 }.freeze

Class Method Summary collapse

Class Method Details

.format(buf, indent: " ", mode: :compact) ⇒ Object

Compact bytes → pretty form (a UTF-8 String). mode is :compact (one element per line), :aligned (table columns aligned) or :spaced (aligned, plus a space after each prefix and around the US glyph).



19
20
21
22
# File 'lib/c0/pretty.rb', line 19

def format(buf, indent: "  ", mode: :compact)
  m = MODES.fetch(mode) { raise ArgumentError, "unknown mode #{mode.inspect}; use #{MODES.keys.inspect}" }
  Ext.pretty_format(C0.to_bin(buf), indent, m)
end

.glyph(byte) ⇒ Object

The glyph for a code byte.



14
# File 'lib/c0/pretty.rb', line 14

def glyph(byte) = (0x2400 + byte).chr(Encoding::UTF_8)

.parse(text) ⇒ Object

Pretty form → compact bytes. Glyphs become codes; outside text, whitespace touching a glyph or a line edge is dropped.



26
# File 'lib/c0/pretty.rb', line 26

def parse(text) = Ext.pretty_parse(text.to_s.encode(Encoding::UTF_8))