console 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/console/console.c +4 -4
- data/test/console.rb +39 -39
- metadata +5 -5
data/ext/console/console.c
CHANGED
@@ -63,7 +63,7 @@ static const char* default_pad_string = " ";
|
|
63
63
|
|
64
64
|
/*
|
65
65
|
* call-seq:
|
66
|
-
*
|
66
|
+
* display_slice(string, start_offset, display_width=1, pad_string=" ")
|
67
67
|
*
|
68
68
|
* Returns a slice of a string based on display width, rather than character or
|
69
69
|
* bytes. I.e, the <code>start_offset</code> and <code>display_width</code>
|
@@ -89,7 +89,7 @@ static const char* default_pad_string = " ";
|
|
89
89
|
* The returned string WILL be in the current locale encoding, regardless of the
|
90
90
|
* encoding of the original string.
|
91
91
|
*/
|
92
|
-
static VALUE
|
92
|
+
static VALUE display_slice(int argc, VALUE *argv, VALUE v_self) {
|
93
93
|
VALUE v_string, v_display_start, v_display_width, v_pad_string;
|
94
94
|
rb_scan_args(argc, argv, "22", &v_string, &v_display_start, &v_display_width, &v_pad_string);
|
95
95
|
Check_Type(v_string, T_STRING);
|
@@ -189,7 +189,7 @@ static VALUE slice(int argc, VALUE *argv, VALUE v_self) {
|
|
189
189
|
*
|
190
190
|
* Console.display_width: get the number of display columns used by a string.
|
191
191
|
*
|
192
|
-
* Console.
|
192
|
+
* Console.display_slice: get a substrig by display column offset and size.
|
193
193
|
*
|
194
194
|
*/
|
195
195
|
|
@@ -198,6 +198,6 @@ void Init_console() {
|
|
198
198
|
|
199
199
|
cConsole = rb_define_class("Console", rb_cObject);
|
200
200
|
rb_define_module_function(cConsole, "display_width", display_width, 1);
|
201
|
-
rb_define_module_function(cConsole, "
|
201
|
+
rb_define_module_function(cConsole, "display_slice", display_slice, -1);
|
202
202
|
}
|
203
203
|
|
data/test/console.rb
CHANGED
@@ -14,76 +14,76 @@ class ConsoleTest < ::Test::Unit::TestCase
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_slice_of_zero_width_is_empty_string
|
17
|
-
assert_equal "", Console.
|
18
|
-
assert_equal "", Console.
|
17
|
+
assert_equal "", Console.display_slice(@s, 0, 0)
|
18
|
+
assert_equal "", Console.display_slice(@s, 1, 0)
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_slice_out_of_bounds_is_nil
|
22
|
-
assert_equal nil, Console.
|
23
|
-
assert_equal nil, Console.
|
22
|
+
assert_equal nil, Console.display_slice(@s, 100, 3)
|
23
|
+
assert_equal nil, Console.display_slice(@s, -100, 3)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_slice_with_negative_offset
|
27
|
-
assert_equal "ê", Console.
|
28
|
-
assert_equal "aê", Console.
|
29
|
-
assert_equal "a", Console.
|
27
|
+
assert_equal "ê", Console.display_slice(@s, -1, 1)
|
28
|
+
assert_equal "aê", Console.display_slice(@s, -2, 2)
|
29
|
+
assert_equal "a", Console.display_slice(@s, -2, 1)
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_slice_width_argument_defaults_to_1
|
33
|
-
assert_equal "ê", Console.
|
34
|
-
assert_equal "a", Console.
|
33
|
+
assert_equal "ê", Console.display_slice(@s, -1)
|
34
|
+
assert_equal "a", Console.display_slice(@s, -2)
|
35
35
|
end
|
36
36
|
|
37
37
|
def test_slice_works_on_chinese_characters
|
38
|
-
assert_equal "能", Console.
|
39
|
-
assert_equal "能吞", Console.
|
40
|
-
assert_equal "能吞a", Console.
|
41
|
-
assert_equal "能吞aê", Console.
|
38
|
+
assert_equal "能", Console.display_slice(@s, 0, 2);
|
39
|
+
assert_equal "能吞", Console.display_slice(@s, 0, 4);
|
40
|
+
assert_equal "能吞a", Console.display_slice(@s, 0, 5);
|
41
|
+
assert_equal "能吞aê", Console.display_slice(@s, 0, 6);
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_slice_with_excessive_width_is_still_cool
|
45
|
-
assert_equal "能吞aê", Console.
|
46
|
-
assert_equal "吞aê", Console.
|
47
|
-
assert_equal "aê", Console.
|
48
|
-
assert_equal "ê", Console.
|
49
|
-
assert_equal "", Console.
|
50
|
-
assert_equal nil, Console.
|
45
|
+
assert_equal "能吞aê", Console.display_slice(@s, 0, 100);
|
46
|
+
assert_equal "吞aê", Console.display_slice(@s, 2, 100);
|
47
|
+
assert_equal "aê", Console.display_slice(@s, 4, 100);
|
48
|
+
assert_equal "ê", Console.display_slice(@s, 5, 100);
|
49
|
+
assert_equal "", Console.display_slice(@s, 6, 100); # yep, we get a non-nil at this value
|
50
|
+
assert_equal nil, Console.display_slice(@s, 7, 100);
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_slice_with_the_biggest_valid_start_offset_behaves_like_String_slice_does
|
54
|
-
assert_equal "", Console.
|
55
|
-
assert_equal "", Console.
|
56
|
-
assert_equal nil, Console.
|
54
|
+
assert_equal "", Console.display_slice(@s, 6, 100);
|
55
|
+
assert_equal "", Console.display_slice(@s, 6, 0);
|
56
|
+
assert_equal nil, Console.display_slice(@s, 6, -1);
|
57
57
|
end
|
58
58
|
|
59
59
|
def test_slice_misaligned_start_offsets_get_padded
|
60
60
|
s = "能吞aê"
|
61
61
|
|
62
|
-
assert_equal "", Console.
|
63
|
-
assert_equal " ", Console.
|
64
|
-
assert_equal "能", Console.
|
65
|
-
assert_equal "能 ", Console.
|
62
|
+
assert_equal "", Console.display_slice(@s, 0, 0)
|
63
|
+
assert_equal " ", Console.display_slice(@s, 0, 1)
|
64
|
+
assert_equal "能", Console.display_slice(@s, 0, 2)
|
65
|
+
assert_equal "能 ", Console.display_slice(@s, 0, 3)
|
66
66
|
|
67
|
-
assert_equal "", Console.
|
68
|
-
assert_equal " ", Console.
|
69
|
-
assert_equal " ", Console.
|
70
|
-
assert_equal " 吞", Console.
|
67
|
+
assert_equal "", Console.display_slice(@s, 1, 0)
|
68
|
+
assert_equal " ", Console.display_slice(@s, 1, 1)
|
69
|
+
assert_equal " ", Console.display_slice(@s, 1, 2)
|
70
|
+
assert_equal " 吞", Console.display_slice(@s, 1, 3)
|
71
71
|
|
72
|
-
assert_equal "", Console.
|
73
|
-
assert_equal " ", Console.
|
74
|
-
assert_equal " a", Console.
|
75
|
-
assert_equal " aê", Console.
|
72
|
+
assert_equal "", Console.display_slice(@s, 3, 0);
|
73
|
+
assert_equal " ", Console.display_slice(@s, 3, 1);
|
74
|
+
assert_equal " a", Console.display_slice(@s, 3, 2);
|
75
|
+
assert_equal " aê", Console.display_slice(@s, 3, 3);
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_slice_misaligned_start_offsets_get_padded_with_specified_character
|
79
|
-
assert_equal "", Console.
|
80
|
-
assert_equal "X", Console.
|
81
|
-
assert_equal "XX", Console.
|
79
|
+
assert_equal "", Console.display_slice(@s, 0, 0, "X")
|
80
|
+
assert_equal "X", Console.display_slice(@s, 0, 1, "X")
|
81
|
+
assert_equal "XX", Console.display_slice(@s, 1, 2, "X")
|
82
82
|
end
|
83
83
|
|
84
84
|
def test_slice_fails_on_nonstrings
|
85
|
-
assert_raises(TypeError) { Console.
|
86
|
-
assert_raises(TypeError) { Console.
|
85
|
+
assert_raises(TypeError) { Console.display_slice :potato, 1, 1 }
|
86
|
+
assert_raises(TypeError) { Console.display_slice 3, 1, 1 }
|
87
87
|
end
|
88
88
|
|
89
89
|
def test_display_width_empty_string_is_zero
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
version: "0.
|
7
|
+
- 2
|
8
|
+
version: "0.2"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- William Morgan
|
@@ -13,11 +13,11 @@ autorequire:
|
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
15
|
|
16
|
-
date: 2010-05-
|
16
|
+
date: 2010-05-11 08:47:32 -04:00
|
17
17
|
default_executable:
|
18
18
|
dependencies: []
|
19
19
|
|
20
|
-
description: Console is a
|
20
|
+
description: Console is a helper class for displaying super-ASCII strings on the console. Console is needed when you want to mix two-column (e.g. Chinese) and single-column (e.g. ASCII) characters and know how much horizontal realestate the result takes on the screen. This is generally necessary when you want to have internationalization support in a console program. Console currently provides utility methods for determining the display width of a string, and for taking a substring based on display position and display width.
|
21
21
|
email: wmorgan-console@masanjin.net
|
22
22
|
executables: []
|
23
23
|
|
@@ -30,7 +30,7 @@ files:
|
|
30
30
|
- ext/console/console.c
|
31
31
|
- test/console.rb
|
32
32
|
has_rdoc: true
|
33
|
-
homepage: http://console.
|
33
|
+
homepage: http://console.rubygems.org
|
34
34
|
licenses: []
|
35
35
|
|
36
36
|
post_install_message:
|