Class: Viewchars::CLI
- Inherits:
-
Object
- Object
- Viewchars::CLI
- Defined in:
- lib/viewchars.rb,
lib/viewchars/core.rb
Class Method Summary collapse
Instance Method Summary collapse
- #basex2dec(n) ⇒ Object
- #build_info_list(codepoints, encoding) ⇒ Object
- #chars2codepoints(chars) ⇒ Object
- #codepoints2chars(codepoints) ⇒ Object
- #dec2code(n) ⇒ Object
- #encode_with(i, encoding) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #list_known_encodings ⇒ Object
- #prepare_output(lists) ⇒ Object
- #print_usage ⇒ Object
- #unicode_name(ch) ⇒ Object
- #utf8_reencode(ch) ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/viewchars.rb', line 39 def initialize begin @opts = Slop.parse do |o| o.bool '-l', '--list-encodings', '' o.string '-e', '--encoding', '' o.string '-b', '--base', '' o.bool '-c', '--codes', '' o.bool '-n', '--names', '' o.bool '-t', '--tabular', '' o.string '-d', '--direction', '' end rescue Slop::UnknownOption, Slop::MissingArgument, Slop::MissingRequiredOption print_usage end @opts[:direction] ||= 'reverse' @opts[:encoding] ||= 'UTF-8' @opts[:tabular] = true if !(@opts[:codes] || @opts[:names]) @known_encodings = Encoding.list.map{|e| [e.to_s.downcase, e.to_s]}.to_h.sort.to_h @requested_encodings = [] @opts[:encoding].split(',').each do |encoding| (@requested_encodings = @known_encodings.values; break) if encoding == 'all' if @known_encodings[encoding.downcase] @requested_encodings << @known_encodings[encoding.downcase] else warn "Error: No such encoding: #{encoding}" exit end end if @opts[:list_encodings] list_known_encodings exit else print_usage unless @opts.arguments.any? case @opts[:direction] when 'reverse' then chars = @opts.arguments.join(' ').chars chars2codepoints(chars) when 'forward' then codepoints = @opts.arguments codepoints2chars(codepoints) end end end |
Class Method Details
.start ⇒ Object
86 |
# File 'lib/viewchars.rb', line 86 def self.start; new; end |
Instance Method Details
#basex2dec(n) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/viewchars/core.rb', line 5 def basex2dec(n) base = case @opts[:base].to_sym when :bin then 2 when :oct then 8 when :dec then return n.to_i when :hex then 16 else print_usage end n.to_s.to_i(base) end |
#build_info_list(codepoints, encoding) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/viewchars/core.rb', line 44 def build_info_list(codepoints, encoding) info = [] codepoints.each do |n| n_dec = basex2dec(n.downcase.gsub(/([\da-f]+)/, '\1')) ch = encode_with(n_dec, encoding) ch_utf8 = utf8_reencode(ch) code = dec2code(n_dec) name = unicode_name(ch) unless name ch = ch_utf8 = '-' name = '(not a Unicode codepoint)' end info << [ch, ch_utf8, n, code, name] end info end |
#chars2codepoints(chars) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/viewchars/core.rb', line 89 def chars2codepoints(chars) @opts[:base] ||= 'dec' codepoints = @opts.arguments.join(' ').codepoints.map(&:to_s) info_lists = @requested_encodings.map{|e| [e, build_info_list(codepoints, e)]}.to_h prepare_output(info_lists) end |
#codepoints2chars(codepoints) ⇒ Object
96 97 98 99 100 |
# File 'lib/viewchars/core.rb', line 96 def codepoints2chars(codepoints) @opts[:base] ||= 'hex' info_lists = @requested_encodings.map{|e| [e, build_info_list(codepoints, e)]}.to_h prepare_output(info_lists) end |
#dec2code(n) ⇒ Object
40 41 42 |
# File 'lib/viewchars/core.rb', line 40 def dec2code(n) "U+#{n.to_s(16).rjust(4,'0')}".upcase end |
#encode_with(i, encoding) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/viewchars/core.rb', line 16 def encode_with(i, encoding) begin i.chr(encoding) rescue '-' end end |
#list_known_encodings ⇒ Object
35 36 37 |
# File 'lib/viewchars.rb', line 35 def list_known_encodings @known_encodings.each{|k,v| puts k} end |
#prepare_output(lists) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/viewchars/core.rb', line 61 def prepare_output(lists) lists.each do |encoding,info| # -c|--codes and/or -n|--names # -t|--tabular (default) _ch = encoding _ch_utf8 = 'Forced' _n = @opts[:base].capitalize _code = 'Code' _name = 'Name' header = [] rows = [] if @opts[:tabular] header = [_ch, _ch_utf8, _n, _code, _name] rows = info else header = nil rows = info.map do |e| r = [e.first] if @opts[:codes] r = [e.last] if @opts[:names] r = [e.first, e.last] if @opts[:codes] and @opts[:names] r end end table = TTY::Table.new(header, rows) puts TTY::Table::Renderer::Basic.new(table).render end end |
#print_usage ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/viewchars.rb', line 15 def print_usage warn "Usage: #{File.basename(__FILE__)} [options] <arg1> <arg2> <...>" warn "" warn " where arguments depend on direction: strings for 'reverse', numbers for 'forward'" warn " and where options may be:" warn "" warn " -l|--list-encodings: do nothing except list all known encodings" warn "" warn " -d|--direction: <reverse|forward> chars to codepoints (default) or vice versa" warn "" warn " -e|--encoding <comma separated list | all> (default: utf-8)" warn " -b|--base <bin|oct|dec|hex> (default: numbers are interpreted as hexadecimals)" warn "" warn " -t|--tabular: output codepoints in input base and base 10, requested-encoding, forced utf8, and name (default)" warn " -c|--codes: output each char's code point as a hex number (can be combined with -n)" warn " -n|--names: if applicable, show the Unicode Character Name for each codepoint (can be combined with -c)" warn "" exit end |
#unicode_name(ch) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/viewchars/core.rb', line 32 def unicode_name(ch) begin Unicode::Name.of(ch).split(' ').map(&:capitalize).join(' ') rescue nil end end |
#utf8_reencode(ch) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/viewchars/core.rb', line 24 def utf8_reencode(ch) begin ch.encode(Encoding::UTF_8) rescue '-' end end |