terminal-layout 0.1.0 → 0.1.1
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/lib/ansi_string.rb +19 -5
- data/spec/ansi_string_spec.rb +65 -1
- data/terminal-layout.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69a8c9c2bf542621e42aaefcd221d552b507a333
|
4
|
+
data.tar.gz: 029d171fa3c85d36edd1312e6a1ba45b1f77c4b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad0a729599af474de93c42ef4b704b326514e7515e703a1377f583b6e018185de12c0bcb87fe26dd323bf1ddb70f2429c0f4f19230737d26aff56117cfd6ee73
|
7
|
+
data.tar.gz: 69d6c4ca7dac9d55941bf94404b780fdc07e8918363dc3b8b5b7124fcba9b82644d986ad249bffbd4d92526b56e807359da21ccf3a5f3135baa84ce530e0cbed
|
data/lib/ansi_string.rb
CHANGED
@@ -36,8 +36,14 @@ class ANSIString
|
|
36
36
|
# convert numeric position to a range
|
37
37
|
range = (range..range) if range.is_a?(Integer)
|
38
38
|
|
39
|
-
|
40
|
-
|
39
|
+
range_begin = range.begin
|
40
|
+
range_end = range.exclude_end? ? range.end - 1 : range.end
|
41
|
+
|
42
|
+
range_begin = @without_ansi.length - range.begin.abs if range.begin < 0
|
43
|
+
range_end = @without_ansi.length - range.end.abs if range.end < 0
|
44
|
+
|
45
|
+
updated_string = replace_in_string(range_begin..range_end, replacement_str)
|
46
|
+
process_string raw_string_for(updated_string)
|
41
47
|
self
|
42
48
|
end
|
43
49
|
|
@@ -55,6 +61,11 @@ class ANSIString
|
|
55
61
|
@without_ansi.rindex(*args)
|
56
62
|
end
|
57
63
|
|
64
|
+
def replace(str)
|
65
|
+
process_string raw_string_for(str)
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
58
69
|
def reverse
|
59
70
|
str = @ansi_sequence_locations.reverse.map do |location|
|
60
71
|
[location[:start_ansi_sequence], location[:text].reverse, location[:end_ansi_sequence]].join
|
@@ -88,6 +99,10 @@ class ANSIString
|
|
88
99
|
raw.split(*args).map { |s| ANSIString.new(s) }
|
89
100
|
end
|
90
101
|
|
102
|
+
def strip
|
103
|
+
ANSIString.new raw.strip
|
104
|
+
end
|
105
|
+
|
91
106
|
def length
|
92
107
|
@without_ansi.length
|
93
108
|
end
|
@@ -218,7 +233,6 @@ class ANSIString
|
|
218
233
|
|
219
234
|
range = range.begin..(range.end - 1) if range.exclude_end?
|
220
235
|
str = ""
|
221
|
-
|
222
236
|
@ansi_sequence_locations.each_with_index do |location, j|
|
223
237
|
# If the given range encompasses part of the location, then we want to
|
224
238
|
# include the whole location
|
@@ -255,9 +269,9 @@ class ANSIString
|
|
255
269
|
str << [
|
256
270
|
location[:start_ansi_sequence],
|
257
271
|
location[:text][location[:begins_at]...(location[:begins_at]+start_index)],
|
258
|
-
|
259
|
-
replacement_str.to_s,
|
272
|
+
replacement_str,
|
260
273
|
location[:text][end_index..-1],
|
274
|
+
location[:end_ansi_sequence],
|
261
275
|
].join
|
262
276
|
|
263
277
|
if location=@ansi_sequence_locations[j+1]
|
data/spec/ansi_string_spec.rb
CHANGED
@@ -219,6 +219,20 @@ describe 'ANSIString' do
|
|
219
219
|
expect(ansi_string).to eq ANSIString.new(blue("tZYXs is blue"))
|
220
220
|
end
|
221
221
|
|
222
|
+
it "supports replacing with negative indexes at the front of the string" do
|
223
|
+
ansi_string[0..-1] = "abc"
|
224
|
+
expect(ansi_string).to eq ANSIString.new(blue("abc"))
|
225
|
+
end
|
226
|
+
|
227
|
+
it "supports replacing with negative indexes in the middle of the string" do
|
228
|
+
ansi_string = ANSIString.new(blue("abc"))
|
229
|
+
ansi_string[1..-2] = red("*")
|
230
|
+
|
231
|
+
# Do not preserve reset sequences (e.g. "\e[0m") when inserting/replacing.
|
232
|
+
# So no \e[0m before the replacement '*'
|
233
|
+
expect(ansi_string).to eq ANSIString.new("\e[34ma\e[31m*\e[0mc\e[0m")
|
234
|
+
end
|
235
|
+
|
222
236
|
it "preserves coloring when part of the text with a String" do
|
223
237
|
ansi_string[0..3] = "that"
|
224
238
|
expect(ansi_string).to eq ANSIString.new(blue("that is blue"))
|
@@ -270,10 +284,43 @@ describe 'ANSIString' do
|
|
270
284
|
context "replacing a substring that goes across ANSI sequence boundaries" do
|
271
285
|
subject(:ansi_string){ ANSIString.new "this#{blue('that')}" }
|
272
286
|
|
273
|
-
it "
|
287
|
+
it "moves the boundaries when using positive indexes and a regular String replacement" do
|
274
288
|
ansi_string[3..4] = yellow("SORRY")
|
275
289
|
expect(ansi_string).to eq ANSIString.new("thi#{yellow('SORRY')}#{blue('hat')}")
|
276
290
|
end
|
291
|
+
|
292
|
+
it "moves the boundaries when using positive indexes and an ANSIString replacement" do
|
293
|
+
ansi_string[3..4] = yellow("SORRY")
|
294
|
+
expect(ansi_string).to eq ANSIString.new("thi#{yellow('SORRY')}#{blue('hat')}")
|
295
|
+
end
|
296
|
+
|
297
|
+
it "moves the boundaries when using negatives indexes and a regular String replacement" do
|
298
|
+
ansi_string[-5..4] = "SORRY"
|
299
|
+
expect(ansi_string).to eq ANSIString.new("thiSORRY#{blue('hat')}")
|
300
|
+
end
|
301
|
+
|
302
|
+
it "moves the boundaries when using negatives indexes and an ANSIString replacement" do
|
303
|
+
ansi_string[-5..4] = yellow("SORRY")
|
304
|
+
expect(ansi_string).to eq ANSIString.new("thi#{yellow('SORRY')}#{blue('hat')}")
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
context "clearing the string" do
|
309
|
+
subject(:ansi_string){ ANSIString.new "this\nthat" }
|
310
|
+
|
311
|
+
it "clears the string" do
|
312
|
+
ansi_string[0..-1] = ""
|
313
|
+
expect(ansi_string).to eq ANSIString.new("")
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
context "expanding a string" do
|
318
|
+
subject(:ansi_string){ ANSIString.new "" }
|
319
|
+
|
320
|
+
it "expands the string" do
|
321
|
+
ansi_string[0..-1] = ANSIString.new(blue("HI"))
|
322
|
+
expect(ansi_string).to eq ANSIString.new(blue("HI"))
|
323
|
+
end
|
277
324
|
end
|
278
325
|
|
279
326
|
it "raises an error out of index" do
|
@@ -365,6 +412,15 @@ describe 'ANSIString' do
|
|
365
412
|
end
|
366
413
|
end
|
367
414
|
|
415
|
+
describe "#replace" do
|
416
|
+
it "replaces the contents of the current string with the new string" do
|
417
|
+
ansi_string = ANSIString.new("abc")
|
418
|
+
original_object_id = ansi_string.object_id
|
419
|
+
expect(ansi_string.replace("def")).to eq ANSIString.new("def")
|
420
|
+
expect(ansi_string.object_id).to eq(original_object_id)
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
368
424
|
describe "#reverse" do
|
369
425
|
it "reverses the string" do
|
370
426
|
ansi_string = ANSIString.new("abc")
|
@@ -463,6 +519,14 @@ describe 'ANSIString' do
|
|
463
519
|
end
|
464
520
|
end
|
465
521
|
|
522
|
+
describe "#strip" do
|
523
|
+
it 'returns a copy of the string with leading and trailing whitespace removed' do
|
524
|
+
ansi_string = ANSIString.new " this is his #{blue('pig')} "
|
525
|
+
expect(ansi_string.strip).to eq ANSIString.new "this is his #{blue('pig')}"
|
526
|
+
expect(ansi_string).to eq ANSIString.new " this is his #{blue('pig')} "
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
466
530
|
describe "#sub" do
|
467
531
|
subject(:ansi_string){ ANSIString.new blue(string) }
|
468
532
|
let(:string){ "this is blue" }
|
data/terminal-layout.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "terminal-layout"
|
7
|
-
spec.version = "0.1.
|
7
|
+
spec.version = "0.1.1"
|
8
8
|
spec.authors = ["Zach Dennis"]
|
9
9
|
spec.email = ["zach.dennis@gmail.com"]
|
10
10
|
spec.summary = %q{A terminal layout manager}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: terminal-layout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Dennis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-terminfo
|