tuile 0.2.0 → 0.3.0
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/CHANGELOG.md +11 -0
- data/README.md +4 -1
- data/examples/sampler.rb +33 -0
- data/lib/tuile/ansi.rb +14 -0
- data/lib/tuile/component/label.rb +64 -26
- data/lib/tuile/component/list.rb +155 -69
- data/lib/tuile/component/text_area.rb +1 -3
- data/lib/tuile/component/text_field.rb +1 -4
- data/lib/tuile/component/text_view.rb +351 -0
- data/lib/tuile/component/window.rb +2 -2
- data/lib/tuile/styled_string.rb +761 -0
- data/lib/tuile/version.rb +1 -1
- data/sig/tuile.rbs +640 -85
- metadata +4 -2
- data/lib/tuile/truncate.rb +0 -83
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tuile
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Martin Vysny
|
|
@@ -133,6 +133,7 @@ files:
|
|
|
133
133
|
- examples/hello_world.rb
|
|
134
134
|
- examples/sampler.rb
|
|
135
135
|
- lib/tuile.rb
|
|
136
|
+
- lib/tuile/ansi.rb
|
|
136
137
|
- lib/tuile/component.rb
|
|
137
138
|
- lib/tuile/component/button.rb
|
|
138
139
|
- lib/tuile/component/has_content.rb
|
|
@@ -145,6 +146,7 @@ files:
|
|
|
145
146
|
- lib/tuile/component/popup.rb
|
|
146
147
|
- lib/tuile/component/text_area.rb
|
|
147
148
|
- lib/tuile/component/text_field.rb
|
|
149
|
+
- lib/tuile/component/text_view.rb
|
|
148
150
|
- lib/tuile/component/window.rb
|
|
149
151
|
- lib/tuile/event_queue.rb
|
|
150
152
|
- lib/tuile/fake_event_queue.rb
|
|
@@ -156,7 +158,7 @@ files:
|
|
|
156
158
|
- lib/tuile/screen.rb
|
|
157
159
|
- lib/tuile/screen_pane.rb
|
|
158
160
|
- lib/tuile/size.rb
|
|
159
|
-
- lib/tuile/
|
|
161
|
+
- lib/tuile/styled_string.rb
|
|
160
162
|
- lib/tuile/version.rb
|
|
161
163
|
- lib/tuile/vertical_scroll_bar.rb
|
|
162
164
|
- sig/tuile.rbs
|
data/lib/tuile/truncate.rb
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Tuile
|
|
4
|
-
# Truncates a string to a given column width, preserving ANSI escape
|
|
5
|
-
# sequences and accounting for Unicode display width. Truncated output is
|
|
6
|
-
# suffixed with an ellipsis (`…`).
|
|
7
|
-
#
|
|
8
|
-
# Extracted from `strings-truncation` 0.1.0 (MIT, Piotr Murach) — only the
|
|
9
|
-
# default end-position, default-omission, no-separator path Tuile uses.
|
|
10
|
-
module Truncate
|
|
11
|
-
# @return [Regexp]
|
|
12
|
-
ANSI_REGEXP = /(\[)?\033(\[)?[;?\d]*[\dA-Za-z]([\];])?/
|
|
13
|
-
private_constant :ANSI_REGEXP
|
|
14
|
-
|
|
15
|
-
# @return [String]
|
|
16
|
-
RESET = "\e[0m"
|
|
17
|
-
private_constant :RESET
|
|
18
|
-
|
|
19
|
-
# @return [Regexp]
|
|
20
|
-
RESET_REGEXP = /#{Regexp.escape(RESET)}/
|
|
21
|
-
private_constant :RESET_REGEXP
|
|
22
|
-
|
|
23
|
-
# @return [Regexp]
|
|
24
|
-
END_REGEXP = /\A(#{ANSI_REGEXP})*\z/
|
|
25
|
-
private_constant :END_REGEXP
|
|
26
|
-
|
|
27
|
-
# @return [String]
|
|
28
|
-
OMISSION = "…"
|
|
29
|
-
private_constant :OMISSION
|
|
30
|
-
|
|
31
|
-
# @return [Integer]
|
|
32
|
-
OMISSION_WIDTH = 1
|
|
33
|
-
private_constant :OMISSION_WIDTH
|
|
34
|
-
|
|
35
|
-
module_function
|
|
36
|
-
|
|
37
|
-
# Truncate `text` to at most `length` display columns. ANSI escape
|
|
38
|
-
# sequences pass through without consuming budget; when characters are
|
|
39
|
-
# dropped, an ellipsis (`…`) is appended (and counts toward `length`).
|
|
40
|
-
#
|
|
41
|
-
# @param text [String]
|
|
42
|
-
# @param length [Integer, nil] target column width. A `nil` returns
|
|
43
|
-
# `text` unchanged.
|
|
44
|
-
# @return [String]
|
|
45
|
-
def truncate(text, length:)
|
|
46
|
-
return text if length.nil? || text.bytesize <= length
|
|
47
|
-
return "" if length.zero?
|
|
48
|
-
|
|
49
|
-
budget = length - OMISSION_WIDTH
|
|
50
|
-
scanner = StringScanner.new(text)
|
|
51
|
-
out = +""
|
|
52
|
-
visible = 0
|
|
53
|
-
ansi_open = false
|
|
54
|
-
stop = false
|
|
55
|
-
|
|
56
|
-
until scanner.eos? || stop
|
|
57
|
-
if scanner.scan(RESET_REGEXP)
|
|
58
|
-
unless scanner.eos?
|
|
59
|
-
out << scanner.matched
|
|
60
|
-
ansi_open = false
|
|
61
|
-
end
|
|
62
|
-
elsif scanner.scan(ANSI_REGEXP)
|
|
63
|
-
out << scanner.matched
|
|
64
|
-
ansi_open = true
|
|
65
|
-
else
|
|
66
|
-
char = scanner.getch
|
|
67
|
-
new_visible = visible + Unicode::DisplayWidth.of(char)
|
|
68
|
-
|
|
69
|
-
if new_visible <= budget || (scanner.check(END_REGEXP) && new_visible <= length)
|
|
70
|
-
out << char
|
|
71
|
-
visible = new_visible
|
|
72
|
-
else
|
|
73
|
-
stop = true
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
out << RESET if ansi_open
|
|
79
|
-
out << OMISSION if stop
|
|
80
|
-
out
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
end
|