#!/usr/bin/env ruby require "rationalist" require "unibits" argv = Rationalist.parse( ARGV, string: '_', alias: { c: 'convert', e: 'encoding', v: 'version', w: 'width', }, boolean: [ 'help', 'stats', 'version', 'wide-ambiguous', ], default: { stats: true }, ) if argv[:version] puts "unibits #{Unibits::VERSION} by #{Paint["J-_-L", :bold]} " exit(0) end if argv[:help] puts <<-HELP #{Paint["DESCRIPTION", :underline]} Visualizes Unicode and ASCII/single byte encodings in the terminal. #{Paint["USAGE", :underline]} #{Paint["unibits", :bold]} [options] data --encoding | -e | which encoding to use for given data --convert | -c | which encoding to convert to (if possible) --width | -w | force a specific number of terminal columns --no-stats | | no stats header with length info --version | | displays version of unibits --wide-ambiguous | | ambiguous characters #{Paint["ENCODINGS", :underline]} #{Unibits::SUPPORTED_ENCODINGS.join(', ').scan(/.{,80}(?> |\z)/).join("\n ")} #{Paint["COLOR CODING", :underline]} #{Paint["invalid", Unibits::COLORS[:invalid]]} #{Paint["unassigned", Unibits::COLORS[:unassigned]]} #{Paint["control", Unibits::COLORS[:control]]} #{Paint["blank", Unibits::COLORS[:blank]]} #{Paint["format", Unibits::COLORS[:format]]} #{Paint["mark", Unibits::COLORS[:mark]]} #{Paint["STATS", :underline]} ( bytes / codepoints / glyphs / expected terminal width ) #{Paint["INVALID BYTES", :underline]} UTF-8 n.e.con. | not enough continuation bytes unexp.c. | unexpected continuation byte toolarge | codepoint value exceeds maximum allowed (U+10FFFF) sur.gate | codepoint value would be a surrogate half overlong | unnecessary null padding UTF-16 incompl. | not enough bytes left to finish codepoint hlf.srg. | other half of surrogate missing UTF-32 incompl. | not enough bytes left to finish codepoint toolarge | codepoint value exceeds maximum allowed (U+10FFFF) sur.gate | codepoint value would be a surrogate half #{Paint["MORE INFO", :underline]} https://github.com/janlelis/unibits HELP exit(0) end if argv[:_] && argv[:_][0] data = argv[:_][0] elsif !$stdin.tty? data = $stdin.read else data = nil end begin Unibits.of( data, encoding: argv[:encoding], convert: argv[:convert], stats: argv[:stats], wide_ambiguous: argv[:'wide-ambiguous'], width: argv[:width], ) rescue ArgumentError $stderr.puts Paint[$!.message, :red] exit(1) end