diff options
author | Stan Lo <[email protected]> | 2023-03-28 13:49:44 +0100 |
---|---|---|
committer | git <[email protected]> | 2023-03-28 12:49:49 +0000 |
commit | 1e9a218ade3af90c18f42e3fea08e2fcea81222a (patch) | |
tree | 33531b5ca2e7d304f64a2947b1cf7717c683c60b /lib/reline/unicode.rb | |
parent | 417b1a36447cb2c650de55b433ba623541fb8bb3 (diff) |
[ruby/reline] Expand the scanned array to later case statement more
straightforward
(https://github.com/ruby/reline/pull/526)
* Improve test coverage on Unicode.take_range
* Add test for Unicode.calculate_width
* Expand the scanned array to later case statement more straightforward
Diffstat (limited to 'lib/reline/unicode.rb')
-rw-r--r-- | lib/reline/unicode.rb | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/lib/reline/unicode.rb b/lib/reline/unicode.rb index de22d08581..1f0f6432c8 100644 --- a/lib/reline/unicode.rb +++ b/lib/reline/unicode.rb @@ -40,11 +40,6 @@ class Reline::Unicode CSI_REGEXP = /\e\[[\d;]*[ABCDEFGHJKSTfminsuhl]/ OSC_REGEXP = /\e\]\d+(?:;[^;]+)*\a/ WIDTH_SCANNER = /\G(?:(#{NON_PRINTING_START})|(#{NON_PRINTING_END})|(#{CSI_REGEXP})|(#{OSC_REGEXP})|(\X))/o - NON_PRINTING_START_INDEX = 0 - NON_PRINTING_END_INDEX = 1 - CSI_REGEXP_INDEX = 2 - OSC_REGEXP_INDEX = 3 - GRAPHEME_CLUSTER_INDEX = 4 def self.get_mbchar_byte_size_by_first_char(c) # Checks UTF-8 character byte size @@ -132,15 +127,14 @@ class Reline::Unicode width = 0 rest = str.encode(Encoding::UTF_8) in_zero_width = false - rest.scan(WIDTH_SCANNER) do |gc| + rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc| case - when gc[NON_PRINTING_START_INDEX] + when non_printing_start in_zero_width = true - when gc[NON_PRINTING_END_INDEX] + when non_printing_end in_zero_width = false - when gc[CSI_REGEXP_INDEX], gc[OSC_REGEXP_INDEX] - when gc[GRAPHEME_CLUSTER_INDEX] - gc = gc[GRAPHEME_CLUSTER_INDEX] + when csi, osc + when gc unless in_zero_width width += get_mbchar_width(gc) end @@ -161,22 +155,21 @@ class Reline::Unicode rest = str.encode(Encoding::UTF_8) in_zero_width = false seq = String.new(encoding: encoding) - rest.scan(WIDTH_SCANNER) do |gc| + rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc| case - when gc[NON_PRINTING_START_INDEX] + when non_printing_start in_zero_width = true lines.last << NON_PRINTING_START - when gc[NON_PRINTING_END_INDEX] + when non_printing_end in_zero_width = false lines.last << NON_PRINTING_END - when gc[CSI_REGEXP_INDEX] - lines.last << gc[CSI_REGEXP_INDEX] - seq << gc[CSI_REGEXP_INDEX] - when gc[OSC_REGEXP_INDEX] - lines.last << gc[OSC_REGEXP_INDEX] - seq << gc[OSC_REGEXP_INDEX] - when gc[GRAPHEME_CLUSTER_INDEX] - gc = gc[GRAPHEME_CLUSTER_INDEX] + when csi + lines.last << csi + seq << csi + when osc + lines.last << osc + seq << osc + when gc unless in_zero_width mbchar_width = get_mbchar_width(gc) if (width += mbchar_width) > max_width @@ -204,18 +197,17 @@ class Reline::Unicode total_width = 0 rest = str.encode(Encoding::UTF_8) in_zero_width = false - rest.scan(WIDTH_SCANNER) do |gc| + rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc| case - when gc[NON_PRINTING_START_INDEX] + when non_printing_start in_zero_width = true - when gc[NON_PRINTING_END_INDEX] + when non_printing_end in_zero_width = false - when gc[CSI_REGEXP_INDEX] - chunk << gc[CSI_REGEXP_INDEX] - when gc[OSC_REGEXP_INDEX] - chunk << gc[OSC_REGEXP_INDEX] - when gc[GRAPHEME_CLUSTER_INDEX] - gc = gc[GRAPHEME_CLUSTER_INDEX] + when csi + chunk << csi + when osc + chunk << osc + when gc if in_zero_width chunk << gc else |