reline 0.1.8 → 0.2.2

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.
@@ -35,11 +35,16 @@ class Reline::Unicode
35
35
  }
36
36
  EscapedChars = EscapedPairs.keys.map(&:chr)
37
37
 
38
- CSI_REGEXP = /\e\[[\d;]*[ABCDEFGHJKSTfminsuhl]/
39
- OSC_REGEXP = /\e\]\d+(?:;[^;]+)*\a/
40
38
  NON_PRINTING_START = "\1"
41
39
  NON_PRINTING_END = "\2"
42
- WIDTH_SCANNER = /\G(?:#{NON_PRINTING_START}|#{NON_PRINTING_END}|#{CSI_REGEXP}|#{OSC_REGEXP}|\X)/
40
+ CSI_REGEXP = /\e\[[\d;]*[ABCDEFGHJKSTfminsuhl]/
41
+ OSC_REGEXP = /\e\]\d+(?:;[^;]+)*\a/
42
+ WIDTH_SCANNER = /\G(?:(#{NON_PRINTING_START})|(#{NON_PRINTING_END})|(#{CSI_REGEXP})|(#{OSC_REGEXP})|(\X))/o
43
+ NON_PRINTING_START_INDEX = 0
44
+ NON_PRINTING_END_INDEX = 1
45
+ CSI_REGEXP_INDEX = 2
46
+ OSC_REGEXP_INDEX = 3
47
+ GRAPHEME_CLUSTER_INDEX = 4
43
48
 
44
49
  def self.get_mbchar_byte_size_by_first_char(c)
45
50
  # Checks UTF-8 character byte size
@@ -89,15 +94,25 @@ class Reline::Unicode
89
94
  | #{ EastAsianWidth::TYPE_NA }
90
95
  | #{ EastAsianWidth::TYPE_N }
91
96
  )
97
+ | (?<ambiguous_width>
98
+ #{EastAsianWidth::TYPE_A}
99
+ )
92
100
  /x
93
101
 
94
102
  def self.get_mbchar_width(mbchar)
103
+ ord = mbchar.ord
104
+ if (0x00 <= ord and ord <= 0x1F)
105
+ return 2
106
+ elsif (0x20 <= ord and ord <= 0x7E)
107
+ return 1
108
+ end
95
109
  m = mbchar.encode(Encoding::UTF_8).match(MBCharWidthRE)
96
110
  case
97
111
  when m[:width_2_1], m[:width_2_2] then 2
98
112
  when m[:width_3] then 3
99
113
  when m[:width_0] then 0
100
114
  when m[:width_1] then 1
115
+ when m[:ambiguous_width] then Reline.ambiguous_width
101
116
  else
102
117
  nil
103
118
  end
@@ -109,13 +124,14 @@ class Reline::Unicode
109
124
  rest = str.encode(Encoding::UTF_8)
110
125
  in_zero_width = false
111
126
  rest.scan(WIDTH_SCANNER) do |gc|
112
- case gc
113
- when NON_PRINTING_START
127
+ case
128
+ when gc[NON_PRINTING_START_INDEX]
114
129
  in_zero_width = true
115
- when NON_PRINTING_END
130
+ when gc[NON_PRINTING_END_INDEX]
116
131
  in_zero_width = false
117
- when CSI_REGEXP, OSC_REGEXP
118
- else
132
+ when gc[CSI_REGEXP_INDEX], gc[OSC_REGEXP_INDEX]
133
+ when gc[GRAPHEME_CLUSTER_INDEX]
134
+ gc = gc[GRAPHEME_CLUSTER_INDEX]
119
135
  unless in_zero_width
120
136
  width += get_mbchar_width(gc)
121
137
  end
@@ -136,14 +152,17 @@ class Reline::Unicode
136
152
  rest = str.encode(Encoding::UTF_8)
137
153
  in_zero_width = false
138
154
  rest.scan(WIDTH_SCANNER) do |gc|
139
- case gc
140
- when NON_PRINTING_START
155
+ case
156
+ when gc[NON_PRINTING_START_INDEX]
141
157
  in_zero_width = true
142
- when NON_PRINTING_END
158
+ when gc[NON_PRINTING_END_INDEX]
143
159
  in_zero_width = false
144
- when CSI_REGEXP, OSC_REGEXP
145
- lines.last << gc
146
- else
160
+ when gc[CSI_REGEXP_INDEX]
161
+ lines.last << gc[CSI_REGEXP_INDEX]
162
+ when gc[OSC_REGEXP_INDEX]
163
+ lines.last << gc[OSC_REGEXP_INDEX]
164
+ when gc[GRAPHEME_CLUSTER_INDEX]
165
+ gc = gc[GRAPHEME_CLUSTER_INDEX]
147
166
  unless in_zero_width
148
167
  mbchar_width = get_mbchar_width(gc)
149
168
  if (width += mbchar_width) > max_width
@@ -439,8 +458,8 @@ class Reline::Unicode
439
458
  [byte_size, width]
440
459
  end
441
460
 
442
- def self.vi_forward_word(line, byte_pointer)
443
- if (line.bytesize - 1) > byte_pointer
461
+ def self.vi_forward_word(line, byte_pointer, drop_terminate_spaces = false)
462
+ if line.bytesize > byte_pointer
444
463
  size = get_next_mbchar_size(line, byte_pointer)
445
464
  mbchar = line.byteslice(byte_pointer, size)
446
465
  if mbchar =~ /\w/
@@ -455,7 +474,7 @@ class Reline::Unicode
455
474
  else
456
475
  return [0, 0]
457
476
  end
458
- while (line.bytesize - 1) > (byte_pointer + byte_size)
477
+ while line.bytesize > (byte_pointer + byte_size)
459
478
  size = get_next_mbchar_size(line, byte_pointer + byte_size)
460
479
  mbchar = line.byteslice(byte_pointer + byte_size, size)
461
480
  case started_by
@@ -469,7 +488,8 @@ class Reline::Unicode
469
488
  width += get_mbchar_width(mbchar)
470
489
  byte_size += size
471
490
  end
472
- while (line.bytesize - 1) > (byte_pointer + byte_size)
491
+ return [byte_size, width] if drop_terminate_spaces
492
+ while line.bytesize > (byte_pointer + byte_size)
473
493
  size = get_next_mbchar_size(line, byte_pointer + byte_size)
474
494
  mbchar = line.byteslice(byte_pointer + byte_size, size)
475
495
  break if mbchar =~ /\S/
@@ -1,3 +1,3 @@
1
1
  module Reline
2
- VERSION = '0.1.8'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -233,7 +233,9 @@ class Reline::Windows
233
233
 
234
234
  def self.move_cursor_up(val)
235
235
  if val > 0
236
- @@SetConsoleCursorPosition.call(@@hConsoleHandle, (cursor_pos.y - val) * 65536 + cursor_pos.x)
236
+ y = cursor_pos.y - val
237
+ y = 0 if y < 0
238
+ @@SetConsoleCursorPosition.call(@@hConsoleHandle, y * 65536 + cursor_pos.x)
237
239
  elsif val < 0
238
240
  move_cursor_down(-val)
239
241
  end
@@ -241,6 +243,9 @@ class Reline::Windows
241
243
 
242
244
  def self.move_cursor_down(val)
243
245
  if val > 0
246
+ screen_height = get_screen_size.first
247
+ y = cursor_pos.y + val
248
+ y = screen_height - 1 if y > (screen_height - 1)
244
249
  @@SetConsoleCursorPosition.call(@@hConsoleHandle, (cursor_pos.y + val) * 65536 + cursor_pos.x)
245
250
  elsif val < 0
246
251
  move_cursor_up(-val)
@@ -253,10 +258,13 @@ class Reline::Windows
253
258
  cursor = csbi[4, 4].unpack('L').first
254
259
  written = 0.chr * 4
255
260
  @@FillConsoleOutputCharacter.call(@@hConsoleHandle, 0x20, get_screen_size.last - cursor_pos.x, cursor, written)
261
+ @@FillConsoleOutputAttribute.call(@@hConsoleHandle, 0, get_screen_size.last - cursor_pos.x, cursor, written)
256
262
  end
257
263
 
258
264
  def self.scroll_down(val)
259
265
  return if val.zero?
266
+ screen_height = get_screen_size.first
267
+ val = screen_height - 1 if val > (screen_height - 1)
260
268
  scroll_rectangle = [0, val, get_screen_size.last, get_screen_size.first].pack('s4')
261
269
  destination_origin = 0 # y * 65536 + x
262
270
  fill = [' '.ord, 0].pack('SS')
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2009, Park Heesob
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name of Park Heesob nor the names of its contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - aycabta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-09 00:00:00.000000000 Z
11
+ date: 2021-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: io-console
@@ -108,6 +108,7 @@ files:
108
108
  - lib/reline/unicode/east_asian_width.rb
109
109
  - lib/reline/version.rb
110
110
  - lib/reline/windows.rb
111
+ - license_of_rb-readline
111
112
  homepage: https://github.com/ruby/reline
112
113
  licenses:
113
114
  - Ruby
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  - !ruby/object:Gem::Version
128
129
  version: '0'
129
130
  requirements: []
130
- rubygems_version: 3.1.4
131
+ rubygems_version: 3.2.3
131
132
  signing_key:
132
133
  specification_version: 4
133
134
  summary: Alternative GNU Readline or Editline implementation by pure Ruby.