wcswidth 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.travis.yml +18 -0
- data/CHANGELOG.md +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE.txt +20 -0
- data/README.md +33 -0
- data/Rakefile +38 -0
- data/lib/wcswidth.rb +18 -0
- data/lib/wcswidth/kernel_method.rb +9 -0
- data/lib/wcswidth/libc.rb +12 -0
- data/lib/wcswidth/sizes.rb +31 -0
- data/lib/wcswidth/version.rb +4 -0
- data/spec/display_width_spec.rb +42 -0
- data/wcswidth.gemspec +24 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b3ceaaac3bc55ccc85ea05b9ccaf98b017b340ae
|
4
|
+
data.tar.gz: 963303ab5e2e82757d198356746ffbad58363845
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 424bd3a2a633ea4ac9db70ddb54627e8e8d42b500ce77f32c4f68dfe5c57316945ce5a90c89e4760ea1e5a07262c8d066fe56a7b0fe7a0afd4a16518e563ad6a
|
7
|
+
data.tar.gz: c11a9df0a5c10d29d079d353d29078d5490dfa6fe3655b99dc3596a9a11b3cbfeadc3c4c0361198385bb18764412088b0f83813926e277a2fb9a899571dcc3ae
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at opensource@janlelis.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2016 Jan Lelis, mail@janlelis.de
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# wcswidth [![[version]](https://badge.fury.io/rb/wcswidth.svg)](http://badge.fury.io/rb/wcswidth) [![[travis]](https://travis-ci.org/janlelis/wcswidth-ruby.png)](https://travis-ci.org/janlelis/wcswidth-ruby)
|
2
|
+
|
3
|
+
Determine terminal display width (columns) of Ruby strings via [FFI bindings](https://github.com/ffi/ffi/wiki) to libc's [wcswidth()](http://man7.org/linux/man-pages/man3/wcswidth.3.html) function.
|
4
|
+
|
5
|
+
See [unicode/display_width](https://github.com/janlelis/unicode-display_width) for a Ruby-only unicode data based approach.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
require 'wcswidth/kernel_method'
|
11
|
+
|
12
|
+
# full width chars
|
13
|
+
wcswidth("!") # => 2
|
14
|
+
wcswidth("一") # => 2
|
15
|
+
|
16
|
+
# single width chars
|
17
|
+
wcswidth("A") # => 1
|
18
|
+
wcswidth("·") # => 1
|
19
|
+
|
20
|
+
# zero width chars
|
21
|
+
wcswidth("ֿ") # => 0
|
22
|
+
wcswidth("\0") # => 0
|
23
|
+
|
24
|
+
# control chars
|
25
|
+
wcswidth("\x01") # => -1
|
26
|
+
wcswidth("\n") # => -1
|
27
|
+
```
|
28
|
+
|
29
|
+
Without opting in for `wcswidth/kernel_method` it is `Wcswidth.of("string")`
|
30
|
+
|
31
|
+
## MIT License
|
32
|
+
|
33
|
+
Copyright (C) 2016 Jan Lelis <http://janlelis.com>. Released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# # #
|
2
|
+
# Get gemspec info
|
3
|
+
|
4
|
+
gemspec_file = Dir['*.gemspec'].first
|
5
|
+
gemspec = eval File.read(gemspec_file), binding, gemspec_file
|
6
|
+
info = "#{gemspec.name} | #{gemspec.version} | " \
|
7
|
+
"#{gemspec.runtime_dependencies.size} dependencies | " \
|
8
|
+
"#{gemspec.files.size} files"
|
9
|
+
|
10
|
+
# # #
|
11
|
+
# Gem build and install task
|
12
|
+
|
13
|
+
desc info
|
14
|
+
task :gem do
|
15
|
+
puts info + "\n\n"
|
16
|
+
print " "; sh "gem build #{gemspec_file}"
|
17
|
+
FileUtils.mkdir_p 'pkg'
|
18
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
|
19
|
+
puts; sh %{gem install --no-document pkg/#{gemspec.name}-#{gemspec.version}.gem}
|
20
|
+
end
|
21
|
+
|
22
|
+
# # #
|
23
|
+
# Start an IRB session with the gem loaded
|
24
|
+
|
25
|
+
desc "#{gemspec.name} | IRB"
|
26
|
+
task :irb do
|
27
|
+
sh "irb -I ./lib -r #{gemspec.name.gsub '-','/'}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# # #
|
31
|
+
# Run all specs
|
32
|
+
|
33
|
+
desc "#{gemspec.name} | Test"
|
34
|
+
task :test do
|
35
|
+
sh "rspec spec"
|
36
|
+
end
|
37
|
+
task :default => :test
|
38
|
+
|
data/lib/wcswidth.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative "wcswidth/version"
|
2
|
+
require_relative "wcswidth/sizes"
|
3
|
+
require_relative "wcswidth/libc"
|
4
|
+
|
5
|
+
module Wcswidth
|
6
|
+
def self.of(string, max_width = MAX_WIDTH)
|
7
|
+
LibC.wcswidth(
|
8
|
+
[
|
9
|
+
*string.to_s. # ensure we are working with a string
|
10
|
+
tr("\0", ""). # remove 0 bytes (length is 0)
|
11
|
+
codepoints, # convert to integer character representations
|
12
|
+
0 # append 0 byte
|
13
|
+
].pack(WCHAR_TEMPLATE), # generate wchar_t byte representation
|
14
|
+
max_width # pass max column width
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Wcswidth
|
2
|
+
end
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "rbconfig/sizeof"
|
6
|
+
|
7
|
+
Wcswidth::MAX_WIDTH = 256 ** RbConfig::SIZEOF["size_t"] - 1
|
8
|
+
Wcswidth::WCHAR_SIZE = RbConfig::SIZEOF["wchar_t"]
|
9
|
+
rescue LoadError
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rbconfig"
|
13
|
+
|
14
|
+
unless defined?(Wcswidth::MAX_WIDTH) && Wcswidth::MAX_WIDTH
|
15
|
+
Wcswidth::MAX_WIDTH = 18446744073709551615
|
16
|
+
end
|
17
|
+
|
18
|
+
unless defined?(Wcswidth::WCHAR_SIZE) && Wcswidth::WCHAR_SIZE
|
19
|
+
if RbConfig::CONFIG["host_os"] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
|
20
|
+
Wcswidth::WCHAR_SIZE = 2
|
21
|
+
else
|
22
|
+
Wcswidth::WCHAR_SIZE = 4
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Wcswidth::WCHAR_TEMPLATE = {
|
27
|
+
1 => 'C*',
|
28
|
+
2 => 'S*',
|
29
|
+
4 => 'L*',
|
30
|
+
8 => 'Q*'
|
31
|
+
}[Wcswidth::WCHAR_SIZE].freeze
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'wcswidth/kernel_method'
|
4
|
+
|
5
|
+
describe 'wcswidth()' do
|
6
|
+
describe '[east asian width]' do
|
7
|
+
it 'returns 2 for F chars' do
|
8
|
+
expect( wcswidth('!') ).to eq 2
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns 2 for W chars' do
|
12
|
+
expect( wcswidth('一') ).to eq 2
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns 1 for N chars' do
|
16
|
+
expect( wcswidth('À') ).to eq 1
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns 1 for Na chars' do
|
20
|
+
expect( wcswidth('A') ).to eq 1
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns 1 for H chars' do
|
24
|
+
expect( wcswidth('。') ).to eq 1
|
25
|
+
end
|
26
|
+
|
27
|
+
# NOTE: This is not always correct
|
28
|
+
it 'returns 1 for A chars' do
|
29
|
+
expect( wcswidth('·') ).to eq 1
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '[general category]' do
|
34
|
+
it 'returns 1 for non-special (non east width) chars' do
|
35
|
+
expect( wcswidth('A') ).to eq 1
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'returns 0 for Mn chars' do
|
39
|
+
expect( wcswidth('ֿ') ).to eq 0
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/wcswidth.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/lib/wcswidth/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "wcswidth"
|
7
|
+
gem.version = Wcswidth::VERSION
|
8
|
+
gem.summary = "FFI bindings to libc's wcswidth()"
|
9
|
+
gem.description = "Determine terminal display width (columns) of Ruby strings via FFI bindings to libc's wcswidth() function"
|
10
|
+
gem.authors = ["Jan Lelis"]
|
11
|
+
gem.email = ["mail@janlelis.de"]
|
12
|
+
gem.homepage = "https://github.com/janlelis/wcswidth-ruby"
|
13
|
+
gem.license = "MIT"
|
14
|
+
|
15
|
+
gem.files = Dir["{**/}{.*,*}"].select{ |path| File.file?(path) && path !~ /^pkg/ }
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.required_ruby_version = "~> 2.0"
|
21
|
+
gem.add_dependency 'ffi', "~> 1.9"
|
22
|
+
gem.add_development_dependency 'rake', "~> 10.5"
|
23
|
+
gem.add_development_dependency 'rspec', "~> 3.4"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wcswidth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Lelis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
description: Determine terminal display width (columns) of Ruby strings via FFI bindings
|
56
|
+
to libc's wcswidth() function
|
57
|
+
email:
|
58
|
+
- mail@janlelis.de
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CHANGELOG.md
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- Gemfile.lock
|
70
|
+
- MIT-LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- lib/wcswidth.rb
|
74
|
+
- lib/wcswidth/kernel_method.rb
|
75
|
+
- lib/wcswidth/libc.rb
|
76
|
+
- lib/wcswidth/sizes.rb
|
77
|
+
- lib/wcswidth/version.rb
|
78
|
+
- spec/display_width_spec.rb
|
79
|
+
- wcswidth.gemspec
|
80
|
+
homepage: https://github.com/janlelis/wcswidth-ruby
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - "~>"
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '2.0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.1
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: FFI bindings to libc's wcswidth()
|
104
|
+
test_files:
|
105
|
+
- spec/display_width_spec.rb
|