terminal_rb 1.0.8 → 1.0.10
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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/bin/bbcode +24 -28
- data/examples/8bit-colors.rb +1 -1
- data/examples/attributes.rb +1 -1
- data/examples/bbcode.rb +13 -13
- data/lib/terminal/ansi/screen_viewer.rb +9 -8
- data/lib/terminal/ansi.rb +357 -257
- data/lib/terminal/input/ansi.rb +1 -1
- data/lib/terminal/input/dumb.rb +2 -2
- data/lib/terminal/input/key_event.rb +4 -4
- data/lib/terminal/input.rb +19 -18
- data/lib/terminal/output/ansi.rb +13 -19
- data/lib/terminal/output/dumb.rb +6 -10
- data/lib/terminal/output.rb +2 -3
- data/lib/terminal/rspec/helper.rb +5 -4
- data/lib/terminal/text/char_width.rb +44 -21
- data/lib/terminal/text/formatter.rb +142 -56
- data/lib/terminal/text.rb +22 -5
- data/lib/terminal/version.rb +1 -1
- data/lib/terminal.rb +38 -13
- metadata +3 -3
|
@@ -9,15 +9,15 @@ module Terminal
|
|
|
9
9
|
# @see Terminal::Text
|
|
10
10
|
#
|
|
11
11
|
# @example Basic word-wrapping
|
|
12
|
-
#
|
|
13
|
-
#
|
|
12
|
+
# formatter = Terminal::Text::Formatter.new('Hello World, this is a test')
|
|
13
|
+
# formatter.lines(width: 12)
|
|
14
14
|
# # => ["Hello World,", "this is a", "test"]
|
|
15
15
|
#
|
|
16
16
|
# @example Formatted output with alignment
|
|
17
17
|
# Terminal::Text::Formatter.format(
|
|
18
18
|
# 'Hello', align: :center, width: 20
|
|
19
19
|
# )
|
|
20
|
-
# # => ["
|
|
20
|
+
# # => [" Hello "]
|
|
21
21
|
class Formatter
|
|
22
22
|
# Parse text into lines, optionally with display widths.
|
|
23
23
|
#
|
|
@@ -55,7 +55,7 @@ module Terminal
|
|
|
55
55
|
#
|
|
56
56
|
# @param (see #initialize)
|
|
57
57
|
# @param (see #format)
|
|
58
|
-
# @
|
|
58
|
+
# @return (see #format)
|
|
59
59
|
# @raise (see #format)
|
|
60
60
|
def self.format(
|
|
61
61
|
*str,
|
|
@@ -63,23 +63,48 @@ module Terminal
|
|
|
63
63
|
bbcode: true,
|
|
64
64
|
spaces: true,
|
|
65
65
|
eol: true,
|
|
66
|
-
align: nil,
|
|
67
66
|
width: nil,
|
|
68
67
|
height: nil,
|
|
68
|
+
align: nil,
|
|
69
69
|
padding: nil,
|
|
70
70
|
prefix: nil,
|
|
71
71
|
suffix: nil
|
|
72
72
|
)
|
|
73
73
|
new(*str, ansi:, bbcode:, spaces:, eol:).format(
|
|
74
|
-
align:,
|
|
75
74
|
width:,
|
|
76
75
|
height:,
|
|
76
|
+
align:,
|
|
77
77
|
padding:,
|
|
78
78
|
prefix:,
|
|
79
79
|
suffix:
|
|
80
80
|
)
|
|
81
81
|
end
|
|
82
82
|
|
|
83
|
+
# Format text with alignment.
|
|
84
|
+
#
|
|
85
|
+
# @see #fmt
|
|
86
|
+
#
|
|
87
|
+
# @example
|
|
88
|
+
# Terminal::Text::Formatter.fmt('Hi', align: :center, width: 10)
|
|
89
|
+
# # => [" Hi "]
|
|
90
|
+
#
|
|
91
|
+
# @param (see #initialize)
|
|
92
|
+
# @param (see #fmt)
|
|
93
|
+
# @return (see #fmt)
|
|
94
|
+
# @raise (see #fmt)
|
|
95
|
+
def self.fmt(
|
|
96
|
+
*str,
|
|
97
|
+
ansi: true,
|
|
98
|
+
bbcode: true,
|
|
99
|
+
spaces: true,
|
|
100
|
+
eol: true,
|
|
101
|
+
width: nil,
|
|
102
|
+
height: nil,
|
|
103
|
+
align: nil
|
|
104
|
+
)
|
|
105
|
+
new(*str, ansi:, bbcode:, spaces:, eol:).fmt(width:, height:, align:)
|
|
106
|
+
end
|
|
107
|
+
|
|
83
108
|
# Create a new formatter with given text and options.
|
|
84
109
|
#
|
|
85
110
|
# @param str [Array<#to_s>] text to process
|
|
@@ -95,6 +120,7 @@ module Terminal
|
|
|
95
120
|
return if str.empty?
|
|
96
121
|
ansi ? _generate_ansi(str, bbcode) : _generate(str, bbcode)
|
|
97
122
|
return if @lex.empty?
|
|
123
|
+
@sp_cache = SPACE_CACHE.dup
|
|
98
124
|
@keep_paragraph = true
|
|
99
125
|
@lex = _ignore_whitespace unless spaces
|
|
100
126
|
case eol
|
|
@@ -108,8 +134,8 @@ module Terminal
|
|
|
108
134
|
# Word-wrap and return lines with their display widths.
|
|
109
135
|
#
|
|
110
136
|
# @example
|
|
111
|
-
#
|
|
112
|
-
#
|
|
137
|
+
# formatter = Terminal::Text::Formatter.new('Hello World')
|
|
138
|
+
# formatter.lines_with_size(width: 5)
|
|
113
139
|
# # => [["Hello", 5], ["World", 5]]
|
|
114
140
|
#
|
|
115
141
|
# @param width [#to_i, nil] maximum line width in columns;
|
|
@@ -127,8 +153,8 @@ module Terminal
|
|
|
127
153
|
# Word-wrap and return lines as strings.
|
|
128
154
|
#
|
|
129
155
|
# @example
|
|
130
|
-
#
|
|
131
|
-
#
|
|
156
|
+
# formatter = Terminal::Text::Formatter.new('Hello World')
|
|
157
|
+
# formatter.lines(width: 5)
|
|
132
158
|
# # => ["Hello", "World"]
|
|
133
159
|
#
|
|
134
160
|
# @param width [#to_i, nil] maximum line width in columns
|
|
@@ -138,16 +164,22 @@ module Terminal
|
|
|
138
164
|
|
|
139
165
|
# Format lines with alignment, padding, and decorations.
|
|
140
166
|
#
|
|
141
|
-
# @
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
167
|
+
# @see #fmt
|
|
168
|
+
#
|
|
169
|
+
# @example Prefix each line
|
|
170
|
+
# formatter = Terminal::Text::Formatter.new("Hello\nWorld")
|
|
171
|
+
# formatter.format(prefix: '> ')
|
|
172
|
+
# # => ["> Hello", "> World"]
|
|
173
|
+
# @example Framed box with padding
|
|
174
|
+
# formatter = Terminal::Text::Formatter.new('Hi')
|
|
175
|
+
# formatter.format(width: 6, padding: [1, 2], prefix: '|', suffix: '|')
|
|
176
|
+
# # => ["| |", "| Hi |", "| |"]
|
|
145
177
|
#
|
|
146
|
-
# @param align [Symbol, nil] text alignment:
|
|
147
|
-
# `:left`, `:right`, `:center`, or `nil` (no fill)
|
|
148
178
|
# @param width [#to_i, nil] output line width in columns
|
|
149
179
|
# @param height [#to_i, nil] number of output lines;
|
|
150
|
-
# negative values take lines from the end
|
|
180
|
+
# negative values take lines from the end; `0` returns `[""]`
|
|
181
|
+
# @param align [Symbol, nil] text alignment:
|
|
182
|
+
# `:left`, `:right`, `:center`, or `nil` (no fill)
|
|
151
183
|
# @param padding [#to_i, Array, nil] CSS-style padding
|
|
152
184
|
# - 1 value: all sides;
|
|
153
185
|
# - 2 values: [vertical, horizontal];
|
|
@@ -158,13 +190,17 @@ module Terminal
|
|
|
158
190
|
# @return [Array<String>] formatted output lines
|
|
159
191
|
# @raise [ArgumentError] if `width` is zero or negative
|
|
160
192
|
def format(
|
|
161
|
-
align: nil,
|
|
162
193
|
width: nil,
|
|
163
194
|
height: nil,
|
|
195
|
+
align: nil,
|
|
164
196
|
padding: nil,
|
|
165
197
|
prefix: nil,
|
|
166
198
|
suffix: nil
|
|
167
199
|
)
|
|
200
|
+
if padding.nil? && prefix.nil? && suffix.nil?
|
|
201
|
+
return fmt(align:, width:, height:)
|
|
202
|
+
end
|
|
203
|
+
|
|
168
204
|
top, right, bottom, left = _padding(padding)
|
|
169
205
|
|
|
170
206
|
if height
|
|
@@ -178,59 +214,105 @@ module Terminal
|
|
|
178
214
|
return [] if height < 1
|
|
179
215
|
end
|
|
180
216
|
|
|
181
|
-
space = SPACE_CACHE.dup
|
|
182
|
-
|
|
183
217
|
if width
|
|
184
218
|
w = (width = width.to_i) - left - right
|
|
185
219
|
raise(ArgumentError, "invalid width - #{width.inspect}") if w < 1
|
|
186
220
|
lws = _limited(w)
|
|
187
221
|
lws = tail ? lws.last(height) : lws.take(height) if height
|
|
188
|
-
empty = "#{prefix}#{
|
|
222
|
+
empty = "#{prefix}#{@sp_cache[width]}#{suffix}" if top + bottom > 0
|
|
189
223
|
else
|
|
190
224
|
lws = @unlimited || _unlimited
|
|
191
225
|
lws = tail ? lws.last(height) : lws.take(height) if height
|
|
192
226
|
w = lws.empty? ? 0 : lws.max_by(&:last)[-1]
|
|
193
227
|
if top + bottom > 0
|
|
194
|
-
empty = "#{prefix}#{
|
|
228
|
+
empty = "#{prefix}#{@sp_cache[w + left + right]}#{suffix}"
|
|
195
229
|
end
|
|
196
230
|
end
|
|
197
231
|
|
|
198
232
|
ret = []
|
|
199
233
|
ret.fill(empty, 0, top) if top > 0
|
|
200
|
-
left = left < 1 ? prefix.to_s : "#{prefix}#{
|
|
201
|
-
right = right < 1 ? suffix.to_s : "#{
|
|
202
|
-
|
|
203
|
-
# TODO: styling
|
|
234
|
+
left = left < 1 ? prefix.to_s : "#{prefix}#{@sp_cache[left]}"
|
|
235
|
+
right = right < 1 ? suffix.to_s : "#{@sp_cache[right]}#{suffix}"
|
|
204
236
|
|
|
205
237
|
case align
|
|
206
|
-
when
|
|
207
|
-
lws.each { |l,
|
|
238
|
+
when nil, false
|
|
239
|
+
lws.each { |l, _| ret << "#{left}#{l}#{right}" }
|
|
208
240
|
when :right
|
|
209
|
-
lws.each { |l, s| ret << "#{left}#{
|
|
241
|
+
lws.each { |l, s| ret << "#{left}#{@sp_cache[w - s]}#{l}#{right}" }
|
|
210
242
|
when :center
|
|
211
243
|
lws.each do |l, s|
|
|
212
244
|
rs = ((s = w - s) / 2.0).round
|
|
213
245
|
next ret << "#{left}#{l}#{right}" if rs < 1
|
|
214
|
-
ret << "#{left}#{
|
|
246
|
+
ret << "#{left}#{@sp_cache[s - rs]}#{l}#{@sp_cache[rs]}#{right}"
|
|
215
247
|
end
|
|
216
248
|
else
|
|
217
|
-
lws.each { |l,
|
|
249
|
+
lws.each { |l, s| ret << "#{left}#{l}#{@sp_cache[w - s]}#{right}" }
|
|
218
250
|
end
|
|
219
251
|
|
|
220
252
|
return ret.fill(empty, ret.size, bottom) if bottom > 0
|
|
221
253
|
ret.empty? ? ret << '' : ret
|
|
222
254
|
end
|
|
223
255
|
|
|
256
|
+
# Format lines with alignment.
|
|
257
|
+
#
|
|
258
|
+
# Like {#format} without padding, prefix, and suffix.
|
|
259
|
+
#
|
|
260
|
+
# @see #format
|
|
261
|
+
#
|
|
262
|
+
# @example
|
|
263
|
+
# Terminal::Text::Formatter.new('Hi').fmt(align: :right, width: 6)
|
|
264
|
+
# # => [" Hi"]
|
|
265
|
+
#
|
|
266
|
+
# @param width (see #format)
|
|
267
|
+
# @param height (see #format)
|
|
268
|
+
# @param align (see #format)
|
|
269
|
+
# @return (see #format)
|
|
270
|
+
# @raise (see #format)
|
|
271
|
+
def fmt(width: nil, height: nil, align: nil)
|
|
272
|
+
if height
|
|
273
|
+
return [] if (height = height.to_i) == 0
|
|
274
|
+
height = -height if (tail = (height < 0))
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
lines =
|
|
278
|
+
if width
|
|
279
|
+
w = (width = width.to_i)
|
|
280
|
+
raise(ArgumentError, "invalid width - #{width.inspect}") if w < 1
|
|
281
|
+
_limited(w)
|
|
282
|
+
else
|
|
283
|
+
@unlimited || _unlimited
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
return [+''] if lines.empty?
|
|
287
|
+
|
|
288
|
+
lines = tail ? lines.last(height) : lines.take(height) if height
|
|
289
|
+
w ||= lines.max_by(&:last)[-1] # unless width
|
|
290
|
+
|
|
291
|
+
return lines.map(&:first) unless align
|
|
292
|
+
|
|
293
|
+
return lines.map { |l, s| "#{@sp_cache[w - s]}#{l}" } if align == :right
|
|
294
|
+
if align != :center
|
|
295
|
+
return lines.map { |l, s| "#{l}#{@sp_cache[w - s]}" }
|
|
296
|
+
end
|
|
297
|
+
lines.map do |l, s|
|
|
298
|
+
rs = ((s = w - s) / 2.0).round
|
|
299
|
+
rs < 1 ? l : "#{@sp_cache[s - rs]}#{l}#{@sp_cache[rs]}"
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
|
|
224
303
|
# Whether the formatter has any content.
|
|
225
304
|
#
|
|
305
|
+
# @attribute [r] empty?
|
|
226
306
|
# @return [true, false]
|
|
227
307
|
def empty? = @lex.empty?
|
|
228
308
|
|
|
229
309
|
# Maximum display width of any line.
|
|
230
310
|
#
|
|
231
311
|
# @example
|
|
232
|
-
#
|
|
233
|
-
#
|
|
312
|
+
# formatter = Terminal::Text::Formatter.new("short\na longer line")
|
|
313
|
+
# formatter.max_line_width # => 13
|
|
314
|
+
#
|
|
315
|
+
# @attribute [r] max_line_width
|
|
234
316
|
#
|
|
235
317
|
# @return [Integer] widest line in columns, `0` when empty
|
|
236
318
|
def max_line_width
|
|
@@ -352,9 +434,7 @@ module Terminal
|
|
|
352
434
|
next if current.is_a?(Space) # ignore trailing space
|
|
353
435
|
|
|
354
436
|
# handle Word...
|
|
355
|
-
splitted = current.split(width)
|
|
356
|
-
last = splitted.pop
|
|
357
|
-
|
|
437
|
+
*splitted, last = current.split(width)
|
|
358
438
|
if csi.empty?
|
|
359
439
|
splitted.each { ret << [it.to_str, it.size] }
|
|
360
440
|
else
|
|
@@ -368,7 +448,10 @@ module Terminal
|
|
|
368
448
|
size = last.size
|
|
369
449
|
end
|
|
370
450
|
|
|
371
|
-
ret.
|
|
451
|
+
if ret.size > 1
|
|
452
|
+
str, width = ret[-1]
|
|
453
|
+
ret.pop if width == 0 && str.empty?
|
|
454
|
+
end
|
|
372
455
|
ret
|
|
373
456
|
end
|
|
374
457
|
|
|
@@ -421,8 +504,9 @@ module Terminal
|
|
|
421
504
|
|
|
422
505
|
next @lex << EOL if b == 0x0a || b == 0x0d
|
|
423
506
|
|
|
424
|
-
# escape sequences are dropped without `ansi
|
|
425
|
-
# character is text
|
|
507
|
+
# escape sequences are dropped without `ansi`
|
|
508
|
+
# any other control character is text
|
|
509
|
+
|
|
426
510
|
next if b == 0x1b && ((c = token.getbyte(1)) == 0x5b || c == 0x5d)
|
|
427
511
|
|
|
428
512
|
(last = @lex[-1]).is_a?(Word) and next last.add(token)
|
|
@@ -476,6 +560,7 @@ module Terminal
|
|
|
476
560
|
if (c = token.getbyte(1)) == 0x5d # Osc
|
|
477
561
|
next @lex << Osc.new(token)
|
|
478
562
|
end
|
|
563
|
+
|
|
479
564
|
if c != 0x5b # Csi
|
|
480
565
|
# a bare escape is just text
|
|
481
566
|
(last = @lex[-1]).is_a?(Word) and next last.add(token)
|
|
@@ -515,18 +600,17 @@ module Terminal
|
|
|
515
600
|
def _ignore_whitespace
|
|
516
601
|
ret = []
|
|
517
602
|
last = nil
|
|
518
|
-
space = Space.new.freeze
|
|
519
603
|
|
|
520
604
|
@lex.each do |current|
|
|
521
605
|
if current.is_a?(Space)
|
|
522
606
|
next if last.nil? || last.is_a?(NewLine) # skip leading space
|
|
523
|
-
next ret << (last =
|
|
607
|
+
next ret << (last = SPACE)
|
|
524
608
|
end
|
|
525
609
|
|
|
526
610
|
if current.is_a?(NewLine) # remove trailing space
|
|
527
|
-
if last ==
|
|
611
|
+
if last == SPACE
|
|
528
612
|
ret.pop
|
|
529
|
-
elsif last == CsiEnd && ret[-2] ==
|
|
613
|
+
elsif last == CsiEnd && ret[-2] == SPACE
|
|
530
614
|
ret.delete_at(-2)
|
|
531
615
|
end
|
|
532
616
|
end
|
|
@@ -564,6 +648,9 @@ module Terminal
|
|
|
564
648
|
def inspect = @size == 1 ? '<Space>' : "<Space #{@size}>"
|
|
565
649
|
end
|
|
566
650
|
|
|
651
|
+
# @private
|
|
652
|
+
SPACE = Space.new.freeze
|
|
653
|
+
|
|
567
654
|
# @private
|
|
568
655
|
# A chunk is either a single grapheme cluster or a run of printable
|
|
569
656
|
# ASCII, for which the display width equals the character count.
|
|
@@ -596,11 +683,10 @@ module Terminal
|
|
|
596
683
|
@chunks.each_with_index do |chunk, idx|
|
|
597
684
|
if chunk.bytesize > 1 && chunk.getbyte(0) < 0x80 &&
|
|
598
685
|
chunk.ascii_only?
|
|
599
|
-
chunk.each_char { chars << it and sizes << 1 }
|
|
600
|
-
else
|
|
601
|
-
chars << chunk
|
|
602
|
-
sizes << @sizes[idx]
|
|
686
|
+
next chunk.each_char { chars << it and sizes << 1 }
|
|
603
687
|
end
|
|
688
|
+
chars << chunk
|
|
689
|
+
sizes << @sizes[idx]
|
|
604
690
|
end
|
|
605
691
|
else
|
|
606
692
|
chars = @chunks
|
|
@@ -656,27 +742,26 @@ module Terminal
|
|
|
656
742
|
|
|
657
743
|
ENCODING = Encoding::UTF_8
|
|
658
744
|
|
|
659
|
-
|
|
660
|
-
Hash.new { |h, s|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
SPACE_CACHE
|
|
745
|
+
SPACE_CACHE =
|
|
746
|
+
Hash.new { |h, s| s < 1 ? '' : h[s] = ' ' * s }.compare_by_identity
|
|
747
|
+
SPACE_CACHE[0] = ''
|
|
748
|
+
SPACE_CACHE[1] = ' '
|
|
749
|
+
SPACE_CACHE[2] = ' '
|
|
750
|
+
SPACE_CACHE.freeze
|
|
665
751
|
|
|
666
752
|
# A run of printable ASCII is only taken when the following character is
|
|
667
753
|
# ASCII too: everything which can extend a grapheme cluster (combining
|
|
668
754
|
# marks, ZWJ, variation selectors, ...) is non-ASCII, so a run can never
|
|
669
755
|
# swallow the base of a cluster `\X` would have kept together.
|
|
670
756
|
SCAN_REGEX =
|
|
671
|
-
/
|
|
672
|
-
[\x21-\x7e]+(?=[\x00-\x7f]|\z)
|
|
757
|
+
/ [\x21-\x7e]+(?=[\x00-\x7f]|\z)
|
|
673
758
|
| \x20+
|
|
674
759
|
| \r?\n
|
|
675
760
|
| \e\[[\x30-\x3f]*[\x20-\x2f]*[a-zA-Z]
|
|
676
761
|
| \e\]\d+(?:;[^\a\e]+)*(?:\a|\e\\)
|
|
677
762
|
| \s
|
|
678
763
|
| \X
|
|
679
|
-
|
|
764
|
+
/x
|
|
680
765
|
|
|
681
766
|
SPACE_BYTE =
|
|
682
767
|
Hash
|
|
@@ -694,6 +779,7 @@ module Terminal
|
|
|
694
779
|
private_constant(
|
|
695
780
|
:SPACE_BYTE,
|
|
696
781
|
:Space,
|
|
782
|
+
:SPACE,
|
|
697
783
|
:Word,
|
|
698
784
|
:Osc,
|
|
699
785
|
:Csi,
|
data/lib/terminal/text.rb
CHANGED
|
@@ -42,7 +42,7 @@ module Terminal
|
|
|
42
42
|
# @example
|
|
43
43
|
# Terminal::Text.width('Hello') # => 5
|
|
44
44
|
# Terminal::Text.width('[bold]Hi[/bold]') # => 2
|
|
45
|
-
# Terminal::Text.width('[bold]Hi[/bold]', bbcode: false) # =>
|
|
45
|
+
# Terminal::Text.width('[bold]Hi[/bold]', bbcode: false) # => 15
|
|
46
46
|
#
|
|
47
47
|
# @param str [#to_s] the string to measure
|
|
48
48
|
# @param bbcode (see Formatter#initialize)
|
|
@@ -106,11 +106,30 @@ module Terminal
|
|
|
106
106
|
#
|
|
107
107
|
# @see Formatter.format
|
|
108
108
|
#
|
|
109
|
+
# @example
|
|
110
|
+
# Terminal::Text.format(
|
|
111
|
+
# 'Hello World', width: 9, align: :left, prefix: '| ', suffix: ' |'
|
|
112
|
+
# )
|
|
113
|
+
# # => ["| Hello |", "| World |"]
|
|
114
|
+
#
|
|
109
115
|
# @param (see Formatter.format)
|
|
110
116
|
# @return (see Formatter.format)
|
|
111
117
|
# @raise (see Formatter.format)
|
|
112
118
|
def format(...) = Formatter.format(...)
|
|
113
119
|
|
|
120
|
+
# Format text with alignment.
|
|
121
|
+
#
|
|
122
|
+
# @see Formatter.fmt
|
|
123
|
+
#
|
|
124
|
+
# @example
|
|
125
|
+
# Terminal::Text.fmt('Hi', align: :center, width: 10)
|
|
126
|
+
# # => [" Hi "]
|
|
127
|
+
#
|
|
128
|
+
# @param (see Formatter.fmt)
|
|
129
|
+
# @return (see Formatter.fmt)
|
|
130
|
+
# @raise (see Formatter.fmt)
|
|
131
|
+
def fmt(...) = Formatter.fmt(...)
|
|
132
|
+
|
|
114
133
|
# @deprecated Use {.lines} and iterate over the result.
|
|
115
134
|
#
|
|
116
135
|
# @comment @param str [Array<#to_s>] text to process
|
|
@@ -163,7 +182,7 @@ module Terminal
|
|
|
163
182
|
)
|
|
164
183
|
warn(
|
|
165
184
|
'[DEPRECATED] `each_line_with_size` is deprecated ' \
|
|
166
|
-
'– use `.
|
|
185
|
+
'– use `.lines_with_size.each` instead',
|
|
167
186
|
category: :deprecated,
|
|
168
187
|
uplevel: 1
|
|
169
188
|
)
|
|
@@ -212,7 +231,6 @@ module Terminal
|
|
|
212
231
|
def char_width(char)
|
|
213
232
|
ord = char.ord
|
|
214
233
|
return @ctrlchar_width[ord] if ord < 0x20
|
|
215
|
-
# CharWidth knows this rule too - a comparison just beats a memo hit
|
|
216
234
|
return ord < 0xa1 ? 1 : CharWidth[ord] if char.bytesize == 1
|
|
217
235
|
return 2 if ord == 0x1f3f3 && @very_special_flags.include?(char)
|
|
218
236
|
sum = 0
|
|
@@ -220,7 +238,7 @@ module Terminal
|
|
|
220
238
|
char.each_codepoint do |cp|
|
|
221
239
|
next skip = false if skip
|
|
222
240
|
next skip = true if cp == 0x200d # zero width joiner
|
|
223
|
-
sum += CharWidth[cp]
|
|
241
|
+
sum += cp < 0xa1 ? 1 : CharWidth[cp]
|
|
224
242
|
end
|
|
225
243
|
sum
|
|
226
244
|
end
|
|
@@ -231,7 +249,6 @@ module Terminal
|
|
|
231
249
|
"🏳\u{FE0F}\u{200D}⚧\u{FE0F}" # transgender flag
|
|
232
250
|
].freeze
|
|
233
251
|
|
|
234
|
-
# indexed by ordinal, only ever asked for control characters
|
|
235
252
|
@ctrlchar_width = [
|
|
236
253
|
0,
|
|
237
254
|
1,
|
data/lib/terminal/version.rb
CHANGED
data/lib/terminal.rb
CHANGED
|
@@ -72,28 +72,52 @@ module Terminal
|
|
|
72
72
|
|
|
73
73
|
# Execute a shell command.
|
|
74
74
|
#
|
|
75
|
-
#
|
|
76
|
-
#
|
|
75
|
+
# Output lines are collected or yielded without their trailing newline.
|
|
76
|
+
#
|
|
77
|
+
# @overload sh(*cmd, env: {}, shell: false, input: nil, **options)
|
|
78
|
+
# Run a command and capture its output.
|
|
79
|
+
#
|
|
80
|
+
# @example
|
|
81
|
+
# status, output, error = Terminal.sh('echo', 'hello')
|
|
82
|
+
# output # => ["hello"]
|
|
83
|
+
#
|
|
77
84
|
# @param cmd [Array<String>] command and arguments
|
|
78
|
-
# @param
|
|
85
|
+
# @param env [Hash] environment variables for the command
|
|
86
|
+
# @param shell [true, false]
|
|
87
|
+
# when true the `cmd` is executed via the shell,
|
|
88
|
+
# when false, the executable is direct executed
|
|
89
|
+
# (see `Process.spawn` for this)
|
|
90
|
+
# @param input [String, IO, Array<String>, Enumerable, nil]
|
|
91
|
+
# data written to the command's standard input
|
|
92
|
+
# @param options [Hash] passed to `Process.spawn`
|
|
93
|
+
# (`:in`, `:out`, `:err` are ignored)
|
|
79
94
|
# @return [Array<Process::Status, Array<String>, Array<String>>]
|
|
80
95
|
# `[status, stdout_lines, stderr_lines]`
|
|
96
|
+
# @return [nil] when the command can not be executed
|
|
97
|
+
#
|
|
98
|
+
# @overload sh(*cmd, env: {}, shell: false, input: nil, **options, &block)
|
|
99
|
+
# Run a command and stream its output to a block.
|
|
100
|
+
#
|
|
81
101
|
# @example
|
|
82
|
-
#
|
|
83
|
-
#
|
|
102
|
+
# Terminal.sh('make') do |line, type|
|
|
103
|
+
# Terminal.puts(type == :error ? "[red]#{line}[/]" : line)
|
|
104
|
+
# end
|
|
84
105
|
#
|
|
85
|
-
# @overload sh(*cmd, **options, &block)
|
|
86
|
-
# Run a command and stream output to a block.
|
|
87
106
|
# @param cmd [Array<String>] command and arguments
|
|
88
|
-
# @param
|
|
107
|
+
# @param env [Hash] environment variables for the command
|
|
108
|
+
# @param shell [true, false]
|
|
109
|
+
# when true the `cmd` is executed via the shell,
|
|
110
|
+
# when false, the executable is direct executed
|
|
111
|
+
# (see `Process.spawn` for this)
|
|
112
|
+
# @param input [String, IO, Array<String>, Enumerable, nil]
|
|
113
|
+
# data written to the command's standard input
|
|
114
|
+
# @param options [Hash] passed to `Process.spawn`
|
|
115
|
+
# (`:in`, `:out`, `:err` are ignored)
|
|
89
116
|
# @yield [line, type] called for each output line
|
|
90
117
|
# @yieldparam line [String] the output line
|
|
91
118
|
# @yieldparam type [Symbol] `:output` or `:error`
|
|
92
119
|
# @return [Process::Status] the exit status
|
|
93
|
-
# @
|
|
94
|
-
# Terminal.sh('make') do |line, type|
|
|
95
|
-
# Terminal.puts(type == :error ? "[red]#{line}[/]" : line)
|
|
96
|
-
# end
|
|
120
|
+
# @return [nil] when the command can not be executed
|
|
97
121
|
#
|
|
98
122
|
# @raise [ArgumentError] if no command is given
|
|
99
123
|
def sh(*cmd, **, &)
|
|
@@ -101,7 +125,8 @@ module Terminal
|
|
|
101
125
|
return Shell.run(*cmd, **, &) if block_given?
|
|
102
126
|
out = []
|
|
103
127
|
err = []
|
|
104
|
-
|
|
128
|
+
ret = Shell.run(*cmd, **) { |l, t| (t == :error ? err : out) << l }
|
|
129
|
+
[ret, out, err] if ret
|
|
105
130
|
end
|
|
106
131
|
|
|
107
132
|
#
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: terminal_rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.10
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mike Blumtritt
|
|
@@ -60,7 +60,7 @@ metadata:
|
|
|
60
60
|
yard.run: yard
|
|
61
61
|
source_code_uri: https://codeberg.org/mblumtritt/Terminal.rb
|
|
62
62
|
bug_tracker_uri: https://codeberg.org/mblumtritt/Terminal.rb/issues
|
|
63
|
-
documentation_uri: https://rubydoc.info/gems/terminal_rb/1.0.
|
|
63
|
+
documentation_uri: https://rubydoc.info/gems/terminal_rb/1.0.10
|
|
64
64
|
rdoc_options: []
|
|
65
65
|
require_paths:
|
|
66
66
|
- lib
|
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
75
75
|
- !ruby/object:Gem::Version
|
|
76
76
|
version: '0'
|
|
77
77
|
requirements: []
|
|
78
|
-
rubygems_version: 4.0.
|
|
78
|
+
rubygems_version: 4.0.20
|
|
79
79
|
specification_version: 4
|
|
80
80
|
summary: Fast terminal access with ANSI, CSIu, mouse events, BBCode, word-wise line
|
|
81
81
|
break support and much more.
|