viewchars 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 79a4fd4d68a2317fa6f6727ec7ed45de9728c9326af516046bdb1bd61e71b354
4
+ data.tar.gz: 03a8e8e5a6f40d76f47528a0c0942530306b813d10cfcf636d911fd145a2f852
5
+ SHA512:
6
+ metadata.gz: fe44a0d3fd947369105cc592e444e3b6c83e41f5c114ff8d381d0143048a5ad6358c2cbe82224897e4b3b6e87b3369507a298f7143940d1f257315ccacf239ff
7
+ data.tar.gz: db2f7b66dc2f970b2dad6370012eadb6b4a52dfb88bbd90d539d821d4f6caa23abc598d16a455e69c28b29d7a415ff343c8db5295d89a76e129b38fa3ffe4289
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in viewchars.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'slop'
10
+ gem 'colorize'
11
+ gem 'unicode-name'
12
+ gem 'tty-table'
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ viewchars (0.1.0)
5
+ colorize
6
+ slop
7
+ tty-table
8
+ unicode-name
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ colorize (0.8.1)
14
+ pastel (0.8.0)
15
+ tty-color (~> 0.5)
16
+ rake (13.0.6)
17
+ slop (4.9.1)
18
+ strings (0.2.1)
19
+ strings-ansi (~> 0.2)
20
+ unicode-display_width (>= 1.5, < 3.0)
21
+ unicode_utils (~> 1.4)
22
+ strings-ansi (0.2.0)
23
+ tty-color (0.6.0)
24
+ tty-screen (0.8.1)
25
+ tty-table (0.12.0)
26
+ pastel (~> 0.8)
27
+ strings (~> 0.2.0)
28
+ tty-screen (~> 0.8)
29
+ unicode-display_width (2.1.0)
30
+ unicode-name (1.10.0)
31
+ unicode-types (~> 1.7)
32
+ unicode-types (1.7.0)
33
+ unicode_utils (1.4.0)
34
+
35
+ PLATFORMS
36
+ x86_64-linux
37
+
38
+ DEPENDENCIES
39
+ colorize
40
+ rake (~> 13.0)
41
+ slop
42
+ tty-table
43
+ unicode-name
44
+ viewchars!
45
+
46
+ BUNDLED WITH
47
+ 2.2.28
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 J. R. Schmid
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # Viewchars
2
+
3
+ ## Installation
4
+
5
+ - Windows: [install Windows Subsystem For Linux](https://docs.microsoft.com/en-us/windows/wsl/install) and a distribution of your choice, then from within that, follow the instructions for Linux.
6
+ - Linux: follow your distribution's instructions for installing Rubygems. For example, in Ubuntu it's `sudo apt install rubygems` or in Manjaro it's `pacman -Sy rubygems`. Afterwards, install _Viewchars_ with `gem install viewchars`.
7
+ - macOS: [install Brew](https://brew.sh/), then open a terminal and install Rubygems with `brew update` followed by `brew install brew-gem`. Afterwards, install _Viewchars_ with `brew gem install viewchars`.
8
+
9
+ ## Usage
10
+
11
+ Simply `viewchars` will output a short help message.
12
+
13
+ Try e.g. `viewchars mary had a little lamb` or `viewchars -c -d forward -b dec 109 97 114 121 32 104 97 100 32 97 32 108 105 116 116 108 101 32 108 97 109 98` for either of the two usage modes, respectively.
14
+
15
+ ## Contributing
16
+
17
+ Bug reports and pull requests are more than welcome at https://github.com/sixtyfive/viewchars.
18
+
19
+ ## License
20
+
21
+ This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "viewchars"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/viewchars ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # -----------------------------------------------------------------------------
4
+ # viewchars: interpret bin/oct/dec/hex number(s) in specified character
5
+ # encoding and try to convert to UTF-8, then display the result
6
+ # if possible.
7
+ # (C) 2021 J. R. Schmid <jrs@weitnahbei.de>
8
+ # May be shared and used according to CC BY-SA 4.0
9
+ # (https://creativecommons.org/licenses/by-sa/4.0/)
10
+ # -----------------------------------------------------------------------------
11
+
12
+ if __FILE__.match(/exe\/viewchars$/)
13
+ require_relative '../lib/viewchars'
14
+ else
15
+ require 'viewchars'
16
+ end
17
+
18
+ Viewchars::CLI.start
@@ -0,0 +1,102 @@
1
+ require 'tty/table'
2
+
3
+ module Viewchars
4
+ class CLI
5
+ def basex2dec(n)
6
+ base = case @opts[:base].to_sym
7
+ when :bin then 2
8
+ when :oct then 8
9
+ when :dec then return n.to_i
10
+ when :hex then 16
11
+ else print_usage
12
+ end
13
+ n.to_s.to_i(base)
14
+ end
15
+
16
+ def encode_with(i, encoding)
17
+ begin
18
+ i.chr(encoding)
19
+ rescue
20
+ '-'
21
+ end
22
+ end
23
+
24
+ def utf8_reencode(ch)
25
+ begin
26
+ ch.encode(Encoding::UTF_8)
27
+ rescue
28
+ '-'
29
+ end
30
+ end
31
+
32
+ def unicode_name(ch)
33
+ begin
34
+ Unicode::Name.of(ch).split(' ').map(&:capitalize).join(' ')
35
+ rescue
36
+ nil
37
+ end
38
+ end
39
+
40
+ def dec2code(n)
41
+ "U+#{n.to_s(16).rjust(4,'0')}".upcase
42
+ end
43
+
44
+ def build_info_list(codepoints, encoding)
45
+ info = []
46
+ codepoints.each do |n|
47
+ n_dec = basex2dec(n.downcase.gsub(/([\da-f]+)/, '\1'))
48
+ ch = encode_with(n_dec, encoding)
49
+ ch_utf8 = utf8_reencode(ch)
50
+ code = dec2code(n_dec)
51
+ name = unicode_name(ch)
52
+ unless name
53
+ ch = ch_utf8 = '-'
54
+ name = '(not a Unicode codepoint)'
55
+ end
56
+ info << [ch, ch_utf8, n, code, name]
57
+ end
58
+ info
59
+ end
60
+
61
+ def prepare_output(lists)
62
+ lists.each do |encoding,info|
63
+ # -c|--codes and/or -n|--names
64
+ # -t|--tabular (default)
65
+ _ch = encoding
66
+ _ch_utf8 = 'Forced'
67
+ _n = @opts[:base].capitalize
68
+ _code = 'Code'
69
+ _name = 'Name'
70
+ header = []
71
+ rows = []
72
+ if @opts[:tabular]
73
+ header = [_ch, _ch_utf8, _n, _code, _name]
74
+ rows = info
75
+ else
76
+ header = nil
77
+ rows = info.map do |e|
78
+ r = [e.first] if @opts[:codes]
79
+ r = [e.last] if @opts[:names]
80
+ r = [e.first, e.last] if @opts[:codes] and @opts[:names]
81
+ r
82
+ end
83
+ end
84
+ table = TTY::Table.new(header, rows)
85
+ puts TTY::Table::Renderer::Basic.new(table).render
86
+ end
87
+ end
88
+
89
+ def chars2codepoints(chars)
90
+ @opts[:base] ||= 'dec'
91
+ codepoints = @opts.arguments.join(' ').codepoints.map(&:to_s)
92
+ info_lists = @requested_encodings.map{|e| [e, build_info_list(codepoints, e)]}.to_h
93
+ prepare_output(info_lists)
94
+ end
95
+
96
+ def codepoints2chars(codepoints)
97
+ @opts[:base] ||= 'hex'
98
+ info_lists = @requested_encodings.map{|e| [e, build_info_list(codepoints, e)]}.to_h
99
+ prepare_output(info_lists)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Viewchars
4
+ VERSION = "0.1.0"
5
+ end
data/lib/viewchars.rb ADDED
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'viewchars/version'
4
+ require_relative 'viewchars/core'
5
+
6
+ require 'slop' # gem install slop
7
+ require 'colorize' # gem install colorize
8
+ require 'unicode/name' # gem install unicode-name
9
+ require 'pp'
10
+
11
+ module Viewchars
12
+ class Error < StandardError; end
13
+
14
+ class CLI
15
+ def print_usage
16
+ warn "Usage: #{File.basename(__FILE__)} [options] <arg1> <arg2> <...>"
17
+ warn ""
18
+ warn " where arguments depend on direction: strings for 'reverse', numbers for 'forward'"
19
+ warn " and where options may be:"
20
+ warn ""
21
+ warn " -l|--list-encodings: do nothing except list all known encodings"
22
+ warn ""
23
+ warn " -d|--direction: <reverse|forward> chars to codepoints (default) or vice versa"
24
+ warn ""
25
+ warn " -e|--encoding <comma separated list | all> (default: utf-8)"
26
+ warn " -b|--base <bin|oct|dec|hex> (default: numbers are interpreted as hexadecimals)"
27
+ warn ""
28
+ warn " -t|--tabular: output codepoints in input base and base 10, requested-encoding, forced utf8, and name (default)"
29
+ warn " -c|--codes: output each char's code point as a hex number (can be combined with -n)"
30
+ warn " -n|--names: if applicable, show the Unicode Character Name for each codepoint (can be combined with -c)"
31
+ warn ""
32
+ exit
33
+ end
34
+
35
+ def list_known_encodings
36
+ @known_encodings.each{|k,v| puts k}
37
+ end
38
+
39
+ def initialize
40
+ begin
41
+ @opts = Slop.parse do |o|
42
+ o.bool '-l', '--list-encodings', ''
43
+ o.string '-e', '--encoding', ''
44
+ o.string '-b', '--base', ''
45
+ o.bool '-c', '--codes', ''
46
+ o.bool '-n', '--names', ''
47
+ o.bool '-t', '--tabular', ''
48
+ o.string '-d', '--direction', ''
49
+ end
50
+ rescue Slop::UnknownOption, Slop::MissingArgument, Slop::MissingRequiredOption
51
+ print_usage
52
+ end
53
+
54
+ @opts[:direction] ||= 'reverse'
55
+ @opts[:encoding] ||= 'UTF-8'
56
+ @opts[:tabular] = true if !(@opts[:codes] || @opts[:names])
57
+
58
+ @known_encodings = Encoding.list.map{|e| [e.to_s.downcase, e.to_s]}.to_h.sort.to_h
59
+ @requested_encodings = []
60
+ @opts[:encoding].split(',').each do |encoding|
61
+ (@requested_encodings = @known_encodings.values; break) if encoding == 'all'
62
+ if @known_encodings[encoding.downcase]
63
+ @requested_encodings << @known_encodings[encoding.downcase]
64
+ else
65
+ warn "Error: No such encoding: #{encoding}"
66
+ exit
67
+ end
68
+ end
69
+
70
+ if @opts[:list_encodings]
71
+ list_known_encodings
72
+ exit
73
+ else
74
+ print_usage unless @opts.arguments.any?
75
+ case @opts[:direction]
76
+ when 'reverse' then
77
+ chars = @opts.arguments.join(' ').chars
78
+ chars2codepoints(chars)
79
+ when 'forward' then
80
+ codepoints = @opts.arguments
81
+ codepoints2chars(codepoints)
82
+ end
83
+ end
84
+ end
85
+
86
+ def self.start; new; end
87
+ end
88
+ end
data/viewchars.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/viewchars/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "viewchars"
7
+ spec.version = Viewchars::VERSION
8
+ spec.authors = ["J. R. Schmid"]
9
+ spec.email = ["jrs+git@weitnahbei.de"]
10
+
11
+ spec.summary = "Little command line tool to help find the way through the character jungle ... or jumble"
12
+ spec.description = "See what codepoints are hiding behind a string, or what string might be hiding behind a list of numbers."
13
+ spec.homepage = "https://github.com/sixtyfive/viewchars"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/sixtyfive/viewchars.git"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency 'slop'
34
+ spec.add_dependency 'colorize'
35
+ spec.add_dependency 'unicode-name'
36
+ spec.add_dependency 'tty-table'
37
+
38
+ # For more information and examples about making a new gem, checkout our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: viewchars
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - J. R. Schmid
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: slop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: unicode-name
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-table
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: See what codepoints are hiding behind a string, or what string might
70
+ be hiding behind a list of numbers.
71
+ email:
72
+ - jrs+git@weitnahbei.de
73
+ executables:
74
+ - viewchars
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - exe/viewchars
86
+ - lib/viewchars.rb
87
+ - lib/viewchars/core.rb
88
+ - lib/viewchars/version.rb
89
+ - viewchars.gemspec
90
+ homepage: https://github.com/sixtyfive/viewchars
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ homepage_uri: https://github.com/sixtyfive/viewchars
96
+ source_code_uri: https://github.com/sixtyfive/viewchars.git
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 3.0.0
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.2.21
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Little command line tool to help find the way through the character jungle
116
+ ... or jumble
117
+ test_files: []