reline 0.3.2 → 0.5.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,7 @@
1
1
  begin
2
+ # Ignore warning `Add fiddle to your Gemfile or gemspec` in Ruby 3.4.
3
+ # terminfo.rb and ansi.rb supports fiddle unavailable environment.
4
+ verbose, $VERBOSE = $VERBOSE, nil
2
5
  require 'fiddle'
3
6
  require 'fiddle/import'
4
7
  rescue LoadError
@@ -7,6 +10,8 @@ rescue LoadError
7
10
  false
8
11
  end
9
12
  end
13
+ ensure
14
+ $VERBOSE = verbose
10
15
  end
11
16
 
12
17
  module Reline::Terminfo
@@ -31,21 +36,7 @@ module Reline::Terminfo
31
36
  @curses_dl = false
32
37
  def self.curses_dl
33
38
  return @curses_dl unless @curses_dl == false
34
- if RUBY_VERSION >= '3.0.0'
35
- # Gem module isn't defined in test-all of the Ruby repository, and
36
- # Fiddle in Ruby 3.0.0 or later supports Fiddle::TYPE_VARIADIC.
37
- fiddle_supports_variadic = true
38
- elsif Fiddle.const_defined?(:VERSION,false) and Gem::Version.create(Fiddle::VERSION) >= Gem::Version.create('1.0.1')
39
- # Fiddle::TYPE_VARIADIC is supported from Fiddle 1.0.1.
40
- fiddle_supports_variadic = true
41
- else
42
- fiddle_supports_variadic = false
43
- end
44
- if fiddle_supports_variadic and not Fiddle.const_defined?(:TYPE_VARIADIC)
45
- # If the libffi version is not 3.0.5 or higher, there isn't TYPE_VARIADIC.
46
- fiddle_supports_variadic = false
47
- end
48
- if fiddle_supports_variadic
39
+ if Fiddle.const_defined?(:TYPE_VARIADIC)
49
40
  curses_dl_files.each do |curses_name|
50
41
  result = Fiddle::Handle.new(curses_name)
51
42
  rescue Fiddle::DLError
@@ -94,23 +85,11 @@ module Reline::Terminfo
94
85
  def self.setupterm(term, fildes)
95
86
  errret_int = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
96
87
  ret = @setupterm.(term, fildes, errret_int)
97
- errret = errret_int[0, Fiddle::SIZEOF_INT].unpack1('i')
98
88
  case ret
99
89
  when 0 # OK
100
- 0
90
+ @term_supported = true
101
91
  when -1 # ERR
102
- case errret
103
- when 1
104
- raise TerminfoError.new('The terminal is hardcopy, cannot be used for curses applications.')
105
- when 0
106
- raise TerminfoError.new('The terminal could not be found, or that it is a generic type, having too little information for curses applications to run.')
107
- when -1
108
- raise TerminfoError.new('The terminfo database could not be found.')
109
- else # unknown
110
- -1
111
- end
112
- else # unknown
113
- -2
92
+ @term_supported = false
114
93
  end
115
94
  end
116
95
 
@@ -162,9 +141,14 @@ module Reline::Terminfo
162
141
  num
163
142
  end
164
143
 
144
+ # NOTE: This means Fiddle and curses are enabled.
165
145
  def self.enabled?
166
146
  true
167
147
  end
148
+
149
+ def self.term_supported?
150
+ @term_supported
151
+ end
168
152
  end if Reline::Terminfo.curses_dl
169
153
 
170
154
  module Reline::Terminfo
@@ -1,6 +1,6 @@
1
1
  class Reline::Unicode::EastAsianWidth
2
2
  # This is based on EastAsianWidth.txt
3
- # EastAsianWidth.txt
3
+ # UNICODE_VERSION = '15.1.0'
4
4
 
5
5
  # Fullwidth
6
6
  TYPE_F = /^[#{ %W(
@@ -60,14 +60,14 @@ class Reline::Unicode::EastAsianWidth
60
60
  \u{2E80}-\u{2E99}
61
61
  \u{2E9B}-\u{2EF3}
62
62
  \u{2F00}-\u{2FD5}
63
- \u{2FF0}-\u{2FFB}
63
+ \u{2FF0}-\u{2FFF}
64
64
  \u{3001}-\u{303E}
65
65
  \u{3041}-\u{3096}
66
66
  \u{3099}-\u{30FF}
67
67
  \u{3105}-\u{312F}
68
68
  \u{3131}-\u{318E}
69
69
  \u{3190}-\u{31E3}
70
- \u{31F0}-\u{321E}
70
+ \u{31EF}-\u{321E}
71
71
  \u{3220}-\u{3247}
72
72
  \u{3250}-\u{4DBF}
73
73
  \u{4E00}-\u{A48C}
@@ -84,8 +84,13 @@ class Reline::Unicode::EastAsianWidth
84
84
  \u{17000}-\u{187F7}
85
85
  \u{18800}-\u{18CD5}
86
86
  \u{18D00}-\u{18D08}
87
- \u{1B000}-\u{1B11E}
87
+ \u{1AFF0}-\u{1AFF3}
88
+ \u{1AFF5}-\u{1AFFB}
89
+ \u{1AFFD}-\u{1AFFE}
90
+ \u{1B000}-\u{1B122}
91
+ \u{1B132}
88
92
  \u{1B150}-\u{1B152}
93
+ \u{1B155}
89
94
  \u{1B164}-\u{1B167}
90
95
  \u{1B170}-\u{1B2FB}
91
96
  \u{1F004}
@@ -119,21 +124,21 @@ class Reline::Unicode::EastAsianWidth
119
124
  \u{1F6CC}
120
125
  \u{1F6D0}-\u{1F6D2}
121
126
  \u{1F6D5}-\u{1F6D7}
127
+ \u{1F6DC}-\u{1F6DF}
122
128
  \u{1F6EB}-\u{1F6EC}
123
129
  \u{1F6F4}-\u{1F6FC}
124
130
  \u{1F7E0}-\u{1F7EB}
131
+ \u{1F7F0}
125
132
  \u{1F90C}-\u{1F93A}
126
133
  \u{1F93C}-\u{1F945}
127
- \u{1F947}-\u{1F978}
128
- \u{1F97A}-\u{1F9CB}
129
- \u{1F9CD}-\u{1F9FF}
130
- \u{1FA70}-\u{1FA74}
131
- \u{1FA78}-\u{1FA7A}
132
- \u{1FA80}-\u{1FA86}
133
- \u{1FA90}-\u{1FAA8}
134
- \u{1FAB0}-\u{1FAB6}
135
- \u{1FAC0}-\u{1FAC2}
136
- \u{1FAD0}-\u{1FAD6}
134
+ \u{1F947}-\u{1F9FF}
135
+ \u{1FA70}-\u{1FA7C}
136
+ \u{1FA80}-\u{1FA88}
137
+ \u{1FA90}-\u{1FABD}
138
+ \u{1FABF}-\u{1FAC5}
139
+ \u{1FACE}-\u{1FADB}
140
+ \u{1FAE0}-\u{1FAE8}
141
+ \u{1FAF0}-\u{1FAF8}
137
142
  \u{20000}-\u{2FFFD}
138
143
  \u{30000}-\u{3FFFD}
139
144
  ).join }]/
@@ -403,8 +408,7 @@ class Reline::Unicode::EastAsianWidth
403
408
  \u{0591}-\u{05C7}
404
409
  \u{05D0}-\u{05EA}
405
410
  \u{05EF}-\u{05F4}
406
- \u{0600}-\u{061C}
407
- \u{061E}-\u{070D}
411
+ \u{0600}-\u{070D}
408
412
  \u{070F}-\u{074A}
409
413
  \u{074D}-\u{07B1}
410
414
  \u{07C0}-\u{07FA}
@@ -413,9 +417,9 @@ class Reline::Unicode::EastAsianWidth
413
417
  \u{0840}-\u{085B}
414
418
  \u{085E}
415
419
  \u{0860}-\u{086A}
416
- \u{08A0}-\u{08B4}
417
- \u{08B6}-\u{08C7}
418
- \u{08D3}-\u{0983}
420
+ \u{0870}-\u{088E}
421
+ \u{0890}-\u{0891}
422
+ \u{0898}-\u{0983}
419
423
  \u{0985}-\u{098C}
420
424
  \u{098F}-\u{0990}
421
425
  \u{0993}-\u{09A8}
@@ -493,11 +497,12 @@ class Reline::Unicode::EastAsianWidth
493
497
  \u{0C0E}-\u{0C10}
494
498
  \u{0C12}-\u{0C28}
495
499
  \u{0C2A}-\u{0C39}
496
- \u{0C3D}-\u{0C44}
500
+ \u{0C3C}-\u{0C44}
497
501
  \u{0C46}-\u{0C48}
498
502
  \u{0C4A}-\u{0C4D}
499
503
  \u{0C55}-\u{0C56}
500
504
  \u{0C58}-\u{0C5A}
505
+ \u{0C5D}
501
506
  \u{0C60}-\u{0C63}
502
507
  \u{0C66}-\u{0C6F}
503
508
  \u{0C77}-\u{0C8C}
@@ -509,10 +514,10 @@ class Reline::Unicode::EastAsianWidth
509
514
  \u{0CC6}-\u{0CC8}
510
515
  \u{0CCA}-\u{0CCD}
511
516
  \u{0CD5}-\u{0CD6}
512
- \u{0CDE}
517
+ \u{0CDD}-\u{0CDE}
513
518
  \u{0CE0}-\u{0CE3}
514
519
  \u{0CE6}-\u{0CEF}
515
- \u{0CF1}-\u{0CF2}
520
+ \u{0CF1}-\u{0CF3}
516
521
  \u{0D00}-\u{0D0C}
517
522
  \u{0D0E}-\u{0D10}
518
523
  \u{0D12}-\u{0D44}
@@ -542,7 +547,7 @@ class Reline::Unicode::EastAsianWidth
542
547
  \u{0EA7}-\u{0EBD}
543
548
  \u{0EC0}-\u{0EC4}
544
549
  \u{0EC6}
545
- \u{0EC8}-\u{0ECD}
550
+ \u{0EC8}-\u{0ECE}
546
551
  \u{0ED0}-\u{0ED9}
547
552
  \u{0EDC}-\u{0EDF}
548
553
  \u{0F00}-\u{0F47}
@@ -577,9 +582,8 @@ class Reline::Unicode::EastAsianWidth
577
582
  \u{13F8}-\u{13FD}
578
583
  \u{1400}-\u{169C}
579
584
  \u{16A0}-\u{16F8}
580
- \u{1700}-\u{170C}
581
- \u{170E}-\u{1714}
582
- \u{1720}-\u{1736}
585
+ \u{1700}-\u{1715}
586
+ \u{171F}-\u{1736}
583
587
  \u{1740}-\u{1753}
584
588
  \u{1760}-\u{176C}
585
589
  \u{176E}-\u{1770}
@@ -587,8 +591,7 @@ class Reline::Unicode::EastAsianWidth
587
591
  \u{1780}-\u{17DD}
588
592
  \u{17E0}-\u{17E9}
589
593
  \u{17F0}-\u{17F9}
590
- \u{1800}-\u{180E}
591
- \u{1810}-\u{1819}
594
+ \u{1800}-\u{1819}
592
595
  \u{1820}-\u{1878}
593
596
  \u{1880}-\u{18AA}
594
597
  \u{18B0}-\u{18F5}
@@ -607,9 +610,9 @@ class Reline::Unicode::EastAsianWidth
607
610
  \u{1A7F}-\u{1A89}
608
611
  \u{1A90}-\u{1A99}
609
612
  \u{1AA0}-\u{1AAD}
610
- \u{1AB0}-\u{1AC0}
611
- \u{1B00}-\u{1B4B}
612
- \u{1B50}-\u{1B7C}
613
+ \u{1AB0}-\u{1ACE}
614
+ \u{1B00}-\u{1B4C}
615
+ \u{1B50}-\u{1B7E}
613
616
  \u{1B80}-\u{1BF3}
614
617
  \u{1BFC}-\u{1C37}
615
618
  \u{1C3B}-\u{1C49}
@@ -617,8 +620,7 @@ class Reline::Unicode::EastAsianWidth
617
620
  \u{1C90}-\u{1CBA}
618
621
  \u{1CBD}-\u{1CC7}
619
622
  \u{1CD0}-\u{1CFA}
620
- \u{1D00}-\u{1DF9}
621
- \u{1DFB}-\u{1F15}
623
+ \u{1D00}-\u{1F15}
622
624
  \u{1F18}-\u{1F1D}
623
625
  \u{1F20}-\u{1F45}
624
626
  \u{1F48}-\u{1F4D}
@@ -653,7 +655,7 @@ class Reline::Unicode::EastAsianWidth
653
655
  \u{2090}-\u{209C}
654
656
  \u{20A0}-\u{20A8}
655
657
  \u{20AA}-\u{20AB}
656
- \u{20AD}-\u{20BF}
658
+ \u{20AD}-\u{20C0}
657
659
  \u{20D0}-\u{20F0}
658
660
  \u{2100}-\u{2102}
659
661
  \u{2104}
@@ -767,9 +769,7 @@ class Reline::Unicode::EastAsianWidth
767
769
  \u{2B51}-\u{2B54}
768
770
  \u{2B5A}-\u{2B73}
769
771
  \u{2B76}-\u{2B95}
770
- \u{2B97}-\u{2C2E}
771
- \u{2C30}-\u{2C5E}
772
- \u{2C60}-\u{2CF3}
772
+ \u{2B97}-\u{2CF3}
773
773
  \u{2CF9}-\u{2D25}
774
774
  \u{2D27}
775
775
  \u{2D2D}
@@ -784,14 +784,16 @@ class Reline::Unicode::EastAsianWidth
784
784
  \u{2DC8}-\u{2DCE}
785
785
  \u{2DD0}-\u{2DD6}
786
786
  \u{2DD8}-\u{2DDE}
787
- \u{2DE0}-\u{2E52}
787
+ \u{2DE0}-\u{2E5D}
788
788
  \u{303F}
789
789
  \u{4DC0}-\u{4DFF}
790
790
  \u{A4D0}-\u{A62B}
791
791
  \u{A640}-\u{A6F7}
792
- \u{A700}-\u{A7BF}
793
- \u{A7C2}-\u{A7CA}
794
- \u{A7F5}-\u{A82C}
792
+ \u{A700}-\u{A7CA}
793
+ \u{A7D0}-\u{A7D1}
794
+ \u{A7D3}
795
+ \u{A7D5}-\u{A7D9}
796
+ \u{A7F2}-\u{A82C}
795
797
  \u{A830}-\u{A839}
796
798
  \u{A840}-\u{A877}
797
799
  \u{A880}-\u{A8C5}
@@ -823,11 +825,11 @@ class Reline::Unicode::EastAsianWidth
823
825
  \u{FB3E}
824
826
  \u{FB40}-\u{FB41}
825
827
  \u{FB43}-\u{FB44}
826
- \u{FB46}-\u{FBC1}
827
- \u{FBD3}-\u{FD3F}
828
- \u{FD50}-\u{FD8F}
828
+ \u{FB46}-\u{FBC2}
829
+ \u{FBD3}-\u{FD8F}
829
830
  \u{FD92}-\u{FDC7}
830
- \u{FDF0}-\u{FDFD}
831
+ \u{FDCF}
832
+ \u{FDF0}-\u{FDFF}
831
833
  \u{FE20}-\u{FE2F}
832
834
  \u{FE70}-\u{FE74}
833
835
  \u{FE76}-\u{FEFC}
@@ -861,10 +863,20 @@ class Reline::Unicode::EastAsianWidth
861
863
  \u{104D8}-\u{104FB}
862
864
  \u{10500}-\u{10527}
863
865
  \u{10530}-\u{10563}
864
- \u{1056F}
866
+ \u{1056F}-\u{1057A}
867
+ \u{1057C}-\u{1058A}
868
+ \u{1058C}-\u{10592}
869
+ \u{10594}-\u{10595}
870
+ \u{10597}-\u{105A1}
871
+ \u{105A3}-\u{105B1}
872
+ \u{105B3}-\u{105B9}
873
+ \u{105BB}-\u{105BC}
865
874
  \u{10600}-\u{10736}
866
875
  \u{10740}-\u{10755}
867
876
  \u{10760}-\u{10767}
877
+ \u{10780}-\u{10785}
878
+ \u{10787}-\u{107B0}
879
+ \u{107B2}-\u{107BA}
868
880
  \u{10800}-\u{10805}
869
881
  \u{10808}
870
882
  \u{1080A}-\u{10835}
@@ -906,13 +918,14 @@ class Reline::Unicode::EastAsianWidth
906
918
  \u{10E80}-\u{10EA9}
907
919
  \u{10EAB}-\u{10EAD}
908
920
  \u{10EB0}-\u{10EB1}
909
- \u{10F00}-\u{10F27}
921
+ \u{10EFD}-\u{10F27}
910
922
  \u{10F30}-\u{10F59}
923
+ \u{10F70}-\u{10F89}
911
924
  \u{10FB0}-\u{10FCB}
912
925
  \u{10FE0}-\u{10FF6}
913
926
  \u{11000}-\u{1104D}
914
- \u{11052}-\u{1106F}
915
- \u{1107F}-\u{110C1}
927
+ \u{11052}-\u{11075}
928
+ \u{1107F}-\u{110C2}
916
929
  \u{110CD}
917
930
  \u{110D0}-\u{110E8}
918
931
  \u{110F0}-\u{110F9}
@@ -922,7 +935,7 @@ class Reline::Unicode::EastAsianWidth
922
935
  \u{11180}-\u{111DF}
923
936
  \u{111E1}-\u{111F4}
924
937
  \u{11200}-\u{11211}
925
- \u{11213}-\u{1123E}
938
+ \u{11213}-\u{11241}
926
939
  \u{11280}-\u{11286}
927
940
  \u{11288}
928
941
  \u{1128A}-\u{1128D}
@@ -954,11 +967,11 @@ class Reline::Unicode::EastAsianWidth
954
967
  \u{11600}-\u{11644}
955
968
  \u{11650}-\u{11659}
956
969
  \u{11660}-\u{1166C}
957
- \u{11680}-\u{116B8}
970
+ \u{11680}-\u{116B9}
958
971
  \u{116C0}-\u{116C9}
959
972
  \u{11700}-\u{1171A}
960
973
  \u{1171D}-\u{1172B}
961
- \u{11730}-\u{1173F}
974
+ \u{11730}-\u{11746}
962
975
  \u{11800}-\u{1183B}
963
976
  \u{118A0}-\u{118F2}
964
977
  \u{118FF}-\u{11906}
@@ -974,7 +987,8 @@ class Reline::Unicode::EastAsianWidth
974
987
  \u{119DA}-\u{119E4}
975
988
  \u{11A00}-\u{11A47}
976
989
  \u{11A50}-\u{11AA2}
977
- \u{11AC0}-\u{11AF8}
990
+ \u{11AB0}-\u{11AF8}
991
+ \u{11B00}-\u{11B09}
978
992
  \u{11C00}-\u{11C08}
979
993
  \u{11C0A}-\u{11C36}
980
994
  \u{11C38}-\u{11C45}
@@ -996,19 +1010,23 @@ class Reline::Unicode::EastAsianWidth
996
1010
  \u{11D93}-\u{11D98}
997
1011
  \u{11DA0}-\u{11DA9}
998
1012
  \u{11EE0}-\u{11EF8}
1013
+ \u{11F00}-\u{11F10}
1014
+ \u{11F12}-\u{11F3A}
1015
+ \u{11F3E}-\u{11F59}
999
1016
  \u{11FB0}
1000
1017
  \u{11FC0}-\u{11FF1}
1001
1018
  \u{11FFF}-\u{12399}
1002
1019
  \u{12400}-\u{1246E}
1003
1020
  \u{12470}-\u{12474}
1004
1021
  \u{12480}-\u{12543}
1005
- \u{13000}-\u{1342E}
1006
- \u{13430}-\u{13438}
1022
+ \u{12F90}-\u{12FF2}
1023
+ \u{13000}-\u{13455}
1007
1024
  \u{14400}-\u{14646}
1008
1025
  \u{16800}-\u{16A38}
1009
1026
  \u{16A40}-\u{16A5E}
1010
1027
  \u{16A60}-\u{16A69}
1011
- \u{16A6E}-\u{16A6F}
1028
+ \u{16A6E}-\u{16ABE}
1029
+ \u{16AC0}-\u{16AC9}
1012
1030
  \u{16AD0}-\u{16AED}
1013
1031
  \u{16AF0}-\u{16AF5}
1014
1032
  \u{16B00}-\u{16B45}
@@ -1025,10 +1043,14 @@ class Reline::Unicode::EastAsianWidth
1025
1043
  \u{1BC80}-\u{1BC88}
1026
1044
  \u{1BC90}-\u{1BC99}
1027
1045
  \u{1BC9C}-\u{1BCA3}
1046
+ \u{1CF00}-\u{1CF2D}
1047
+ \u{1CF30}-\u{1CF46}
1048
+ \u{1CF50}-\u{1CFC3}
1028
1049
  \u{1D000}-\u{1D0F5}
1029
1050
  \u{1D100}-\u{1D126}
1030
- \u{1D129}-\u{1D1E8}
1051
+ \u{1D129}-\u{1D1EA}
1031
1052
  \u{1D200}-\u{1D245}
1053
+ \u{1D2C0}-\u{1D2D3}
1032
1054
  \u{1D2E0}-\u{1D2F3}
1033
1055
  \u{1D300}-\u{1D356}
1034
1056
  \u{1D360}-\u{1D378}
@@ -1055,17 +1077,27 @@ class Reline::Unicode::EastAsianWidth
1055
1077
  \u{1D7CE}-\u{1DA8B}
1056
1078
  \u{1DA9B}-\u{1DA9F}
1057
1079
  \u{1DAA1}-\u{1DAAF}
1080
+ \u{1DF00}-\u{1DF1E}
1081
+ \u{1DF25}-\u{1DF2A}
1058
1082
  \u{1E000}-\u{1E006}
1059
1083
  \u{1E008}-\u{1E018}
1060
1084
  \u{1E01B}-\u{1E021}
1061
1085
  \u{1E023}-\u{1E024}
1062
1086
  \u{1E026}-\u{1E02A}
1087
+ \u{1E030}-\u{1E06D}
1088
+ \u{1E08F}
1063
1089
  \u{1E100}-\u{1E12C}
1064
1090
  \u{1E130}-\u{1E13D}
1065
1091
  \u{1E140}-\u{1E149}
1066
1092
  \u{1E14E}-\u{1E14F}
1093
+ \u{1E290}-\u{1E2AE}
1067
1094
  \u{1E2C0}-\u{1E2F9}
1068
1095
  \u{1E2FF}
1096
+ \u{1E4D0}-\u{1E4F9}
1097
+ \u{1E7E0}-\u{1E7E6}
1098
+ \u{1E7E8}-\u{1E7EB}
1099
+ \u{1E7ED}-\u{1E7EE}
1100
+ \u{1E7F0}-\u{1E7FE}
1069
1101
  \u{1E800}-\u{1E8C4}
1070
1102
  \u{1E8C7}-\u{1E8D6}
1071
1103
  \u{1E900}-\u{1E94B}
@@ -1142,8 +1174,8 @@ class Reline::Unicode::EastAsianWidth
1142
1174
  \u{1F6D3}-\u{1F6D4}
1143
1175
  \u{1F6E0}-\u{1F6EA}
1144
1176
  \u{1F6F0}-\u{1F6F3}
1145
- \u{1F700}-\u{1F773}
1146
- \u{1F780}-\u{1F7D8}
1177
+ \u{1F700}-\u{1F776}
1178
+ \u{1F77B}-\u{1F7D9}
1147
1179
  \u{1F800}-\u{1F80B}
1148
1180
  \u{1F810}-\u{1F847}
1149
1181
  \u{1F850}-\u{1F859}
@@ -38,41 +38,18 @@ class Reline::Unicode
38
38
  NON_PRINTING_START = "\1"
39
39
  NON_PRINTING_END = "\2"
40
40
  CSI_REGEXP = /\e\[[\d;]*[ABCDEFGHJKSTfminsuhl]/
41
- OSC_REGEXP = /\e\]\d+(?:;[^;]+)*\a/
41
+ OSC_REGEXP = /\e\]\d+(?:;[^;\a\e]+)*(?:\a|\e\\)/
42
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
48
-
49
- def self.get_mbchar_byte_size_by_first_char(c)
50
- # Checks UTF-8 character byte size
51
- case c.ord
52
- # 0b0xxxxxxx
53
- when ->(code) { (code ^ 0b10000000).allbits?(0b10000000) } then 1
54
- # 0b110xxxxx
55
- when ->(code) { (code ^ 0b00100000).allbits?(0b11100000) } then 2
56
- # 0b1110xxxx
57
- when ->(code) { (code ^ 0b00010000).allbits?(0b11110000) } then 3
58
- # 0b11110xxx
59
- when ->(code) { (code ^ 0b00001000).allbits?(0b11111000) } then 4
60
- # 0b111110xx
61
- when ->(code) { (code ^ 0b00000100).allbits?(0b11111100) } then 5
62
- # 0b1111110x
63
- when ->(code) { (code ^ 0b00000010).allbits?(0b11111110) } then 6
64
- # successor of mbchar
65
- else 0
66
- end
67
- end
68
43
 
69
44
  def self.escape_for_print(str)
70
45
  str.chars.map! { |gr|
71
- escaped = EscapedPairs[gr.ord]
72
- if escaped && gr != -"\n" && gr != -"\t"
73
- escaped
74
- else
46
+ case gr
47
+ when -"\n"
75
48
  gr
49
+ when -"\t"
50
+ -' '
51
+ else
52
+ EscapedPairs[gr.ord] || gr
76
53
  end
77
54
  }.join
78
55
  end
@@ -132,15 +109,14 @@ class Reline::Unicode
132
109
  width = 0
133
110
  rest = str.encode(Encoding::UTF_8)
134
111
  in_zero_width = false
135
- rest.scan(WIDTH_SCANNER) do |gc|
112
+ rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc|
136
113
  case
137
- when gc[NON_PRINTING_START_INDEX]
114
+ when non_printing_start
138
115
  in_zero_width = true
139
- when gc[NON_PRINTING_END_INDEX]
116
+ when non_printing_end
140
117
  in_zero_width = false
141
- when gc[CSI_REGEXP_INDEX], gc[OSC_REGEXP_INDEX]
142
- when gc[GRAPHEME_CLUSTER_INDEX]
143
- gc = gc[GRAPHEME_CLUSTER_INDEX]
118
+ when csi, osc
119
+ when gc
144
120
  unless in_zero_width
145
121
  width += get_mbchar_width(gc)
146
122
  end
@@ -154,30 +130,40 @@ class Reline::Unicode
154
130
  end
155
131
  end
156
132
 
157
- def self.split_by_width(str, max_width, encoding = str.encoding)
133
+ def self.split_by_width(str, max_width, encoding = str.encoding, offset: 0)
158
134
  lines = [String.new(encoding: encoding)]
159
135
  height = 1
160
- width = 0
136
+ width = offset
161
137
  rest = str.encode(Encoding::UTF_8)
162
138
  in_zero_width = false
163
- rest.scan(WIDTH_SCANNER) do |gc|
139
+ seq = String.new(encoding: encoding)
140
+ rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc|
164
141
  case
165
- when gc[NON_PRINTING_START_INDEX]
142
+ when non_printing_start
166
143
  in_zero_width = true
167
- when gc[NON_PRINTING_END_INDEX]
144
+ lines.last << NON_PRINTING_START
145
+ when non_printing_end
168
146
  in_zero_width = false
169
- when gc[CSI_REGEXP_INDEX]
170
- lines.last << gc[CSI_REGEXP_INDEX]
171
- when gc[OSC_REGEXP_INDEX]
172
- lines.last << gc[OSC_REGEXP_INDEX]
173
- when gc[GRAPHEME_CLUSTER_INDEX]
174
- gc = gc[GRAPHEME_CLUSTER_INDEX]
147
+ lines.last << NON_PRINTING_END
148
+ when csi
149
+ lines.last << csi
150
+ unless in_zero_width
151
+ if csi == -"\e[m" || csi == -"\e[0m"
152
+ seq.clear
153
+ else
154
+ seq << csi
155
+ end
156
+ end
157
+ when osc
158
+ lines.last << osc
159
+ seq << osc
160
+ when gc
175
161
  unless in_zero_width
176
162
  mbchar_width = get_mbchar_width(gc)
177
163
  if (width += mbchar_width) > max_width
178
164
  width = mbchar_width
179
165
  lines << nil
180
- lines << String.new(encoding: encoding)
166
+ lines << seq.dup
181
167
  height += 1
182
168
  end
183
169
  end
@@ -194,34 +180,79 @@ class Reline::Unicode
194
180
  end
195
181
 
196
182
  # Take a chunk of a String cut by width with escape sequences.
197
- def self.take_range(str, start_col, max_width, encoding = str.encoding)
198
- chunk = String.new(encoding: encoding)
183
+ def self.take_range(str, start_col, max_width)
184
+ take_mbchar_range(str, start_col, max_width).first
185
+ end
186
+
187
+ def self.take_mbchar_range(str, start_col, width, cover_begin: false, cover_end: false, padding: false)
188
+ chunk = String.new(encoding: str.encoding)
189
+
190
+ end_col = start_col + width
199
191
  total_width = 0
200
192
  rest = str.encode(Encoding::UTF_8)
201
193
  in_zero_width = false
202
- rest.scan(WIDTH_SCANNER) do |gc|
194
+ chunk_start_col = nil
195
+ chunk_end_col = nil
196
+ has_csi = false
197
+ rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc|
203
198
  case
204
- when gc[NON_PRINTING_START_INDEX]
199
+ when non_printing_start
205
200
  in_zero_width = true
206
- when gc[NON_PRINTING_END_INDEX]
201
+ chunk << NON_PRINTING_START
202
+ when non_printing_end
207
203
  in_zero_width = false
208
- when gc[CSI_REGEXP_INDEX]
209
- chunk << gc[CSI_REGEXP_INDEX]
210
- when gc[OSC_REGEXP_INDEX]
211
- chunk << gc[OSC_REGEXP_INDEX]
212
- when gc[GRAPHEME_CLUSTER_INDEX]
213
- gc = gc[GRAPHEME_CLUSTER_INDEX]
204
+ chunk << NON_PRINTING_END
205
+ when csi
206
+ has_csi = true
207
+ chunk << csi
208
+ when osc
209
+ chunk << osc
210
+ when gc
214
211
  if in_zero_width
215
212
  chunk << gc
213
+ next
214
+ end
215
+
216
+ mbchar_width = get_mbchar_width(gc)
217
+ prev_width = total_width
218
+ total_width += mbchar_width
219
+
220
+ if (cover_begin || padding ? total_width <= start_col : prev_width < start_col)
221
+ # Current character haven't reached start_col yet
222
+ next
223
+ elsif padding && !cover_begin && prev_width < start_col && start_col < total_width
224
+ # Add preceding padding. This padding might have background color.
225
+ chunk << ' '
226
+ chunk_start_col ||= start_col
227
+ chunk_end_col = total_width
228
+ next
229
+ elsif (cover_end ? prev_width < end_col : total_width <= end_col)
230
+ # Current character is in the range
231
+ chunk << gc
232
+ chunk_start_col ||= prev_width
233
+ chunk_end_col = total_width
234
+ break if total_width >= end_col
216
235
  else
217
- mbchar_width = get_mbchar_width(gc)
218
- total_width += mbchar_width
219
- break if (start_col + max_width) < total_width
220
- chunk << gc if start_col < total_width
236
+ # Current character exceeds end_col
237
+ if padding && end_col < total_width
238
+ # Add succeeding padding. This padding might have background color.
239
+ chunk << ' '
240
+ chunk_start_col ||= prev_width
241
+ chunk_end_col = end_col
242
+ end
243
+ break
221
244
  end
222
245
  end
223
246
  end
224
- chunk
247
+ chunk_start_col ||= start_col
248
+ chunk_end_col ||= start_col
249
+ if padding && chunk_end_col < end_col
250
+ # Append padding. This padding should not include background color.
251
+ chunk << "\e[0m" if has_csi
252
+ chunk << ' ' * (end_col - chunk_end_col)
253
+ chunk_end_col = end_col
254
+ end
255
+ [chunk, chunk_start_col, chunk_end_col - chunk_start_col]
225
256
  end
226
257
 
227
258
  def self.get_next_mbchar_size(line, byte_pointer)
@@ -1,3 +1,3 @@
1
1
  module Reline
2
- VERSION = '0.3.2'
2
+ VERSION = '0.5.9'
3
3
  end