console 0.2 → 0.2.1
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.
- data/ext/console/console.c +9 -3
- data/lib/console/string.rb +39 -0
- data/test/console.rb +4 -0
- metadata +4 -2
data/ext/console/console.c
CHANGED
@@ -49,7 +49,9 @@ static VALUE display_width(VALUE v_self, VALUE v_string) {
|
|
49
49
|
if(num_bytes == 0) rb_raise(rb_eArgError, "malformed string: NULL byte at position %ld", RSTRING_LEN(v_string) - remaining_bytes);
|
50
50
|
|
51
51
|
int width = wcwidth(wc);
|
52
|
-
|
52
|
+
/* sometimes this seems to happen! maybe it's not bad...
|
53
|
+
if(width == 0) rb_raise(rb_eArgError, "bad string: non-printable character at position %ld", RSTRING_LEN(v_string) - remaining_bytes);
|
54
|
+
*/
|
53
55
|
|
54
56
|
display_width += width;
|
55
57
|
remaining_bytes -= num_bytes;
|
@@ -126,7 +128,9 @@ static VALUE display_slice(int argc, VALUE *argv, VALUE v_self) {
|
|
126
128
|
if(num_bytes == 0) rb_raise(rb_eArgError, "malformed string: NULL byte at position %ld", RSTRING_LEN(v_string) - remaining_bytes);
|
127
129
|
|
128
130
|
int width = wcwidth(wc);
|
129
|
-
|
131
|
+
/* sometimes this seems to happen! maybe it's not bad...
|
132
|
+
if(width == 0) rb_raise(rb_eArgError, "bad string: non-printable character at position %ld", RSTRING_LEN(v_string) - remaining_bytes);
|
133
|
+
*/
|
130
134
|
|
131
135
|
current_width += width;
|
132
136
|
remaining_bytes -= num_bytes;
|
@@ -152,7 +156,9 @@ static VALUE display_slice(int argc, VALUE *argv, VALUE v_self) {
|
|
152
156
|
if(num_bytes == 0) rb_raise(rb_eArgError, "malformed string: NULL byte at position %ld", RSTRING_LEN(v_string) - remaining_bytes);
|
153
157
|
|
154
158
|
int width = wcwidth(wc);
|
155
|
-
|
159
|
+
/* sometimes this seems to happen! maybe it's not bad...
|
160
|
+
if(width == 0) rb_raise(rb_eArgError, "bad string: non-printable character at position %ld", RSTRING_LEN(v_string) - remaining_bytes);
|
161
|
+
*/
|
156
162
|
|
157
163
|
if((current_width + width) > display_width) break; // have to stop here
|
158
164
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
## lib/console/string.rb -- include Console methods into String
|
4
|
+
## Author: William Morgan (mailto: wmorgan-ruby-console@masanjin.net)
|
5
|
+
## Copyright: Copyright 2010 William Morgan
|
6
|
+
## License: same terms as Ruby itself
|
7
|
+
|
8
|
+
require 'console'
|
9
|
+
|
10
|
+
## reopen the String class and add #display_width and
|
11
|
+
## #display_slice methods directly to strings.
|
12
|
+
##
|
13
|
+
## If you include "console/string", you can call
|
14
|
+
## "能吞a".display_width
|
15
|
+
## instead of
|
16
|
+
## Console.display_width "能吞a"
|
17
|
+
##
|
18
|
+
## and
|
19
|
+
##
|
20
|
+
## "能吞a".display_slice 0, 2
|
21
|
+
## instead of
|
22
|
+
## Console.display_slice "能吞a", 0, 2
|
23
|
+
##
|
24
|
+
|
25
|
+
class String
|
26
|
+
## Returns the display width of the string. See Console.display_width for details.
|
27
|
+
def display_width; Console.display_width self end
|
28
|
+
|
29
|
+
## Returns a substring according to display-based start and offset values. See
|
30
|
+
## Console.display_slice for what this means.
|
31
|
+
##
|
32
|
+
## Stupid rdoc is not interpreting this next directive, and I'm tired of wasting
|
33
|
+
## my life figuring out why.
|
34
|
+
##
|
35
|
+
## :call-seq:
|
36
|
+
## display_slice(start, offset=1, pad_string=" ")
|
37
|
+
##
|
38
|
+
def display_slice(*a); Console.display_slice self, *a end
|
39
|
+
end
|
data/test/console.rb
CHANGED
@@ -119,4 +119,8 @@ class ConsoleTest < ::Test::Unit::TestCase
|
|
119
119
|
assert_raises(TypeError) { Console.display_width :potato }
|
120
120
|
assert_raises(TypeError) { Console.display_width 3 }
|
121
121
|
end
|
122
|
+
|
123
|
+
def test_display_width_of_a_big_crazy_string
|
124
|
+
assert_equal 154, Console.display_width("我能吞下玻璃而不傷身體。Góa ē-tàng chia̍h po-lê, mā bē tio̍h-siong.私はガラスを食べられます。それは私を傷つけません。I can eat glass and it doesn't hurt me.")
|
125
|
+
end
|
122
126
|
end
|
metadata
CHANGED
@@ -5,7 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
9
10
|
platform: ruby
|
10
11
|
authors:
|
11
12
|
- William Morgan
|
@@ -13,7 +14,7 @@ autorequire:
|
|
13
14
|
bindir: bin
|
14
15
|
cert_chain: []
|
15
16
|
|
16
|
-
date: 2010-05-11
|
17
|
+
date: 2010-05-11 13:23:26 -04:00
|
17
18
|
default_executable:
|
18
19
|
dependencies: []
|
19
20
|
|
@@ -29,6 +30,7 @@ files:
|
|
29
30
|
- ext/console/extconf.rb
|
30
31
|
- ext/console/console.c
|
31
32
|
- test/console.rb
|
33
|
+
- lib/console/string.rb
|
32
34
|
has_rdoc: true
|
33
35
|
homepage: http://console.rubygems.org
|
34
36
|
licenses: []
|