tuile 0.2.0 → 0.4.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.
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.2.0
4
+ version: 0.4.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,8 @@ 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_input.rb
150
+ - lib/tuile/component/text_view.rb
148
151
  - lib/tuile/component/window.rb
149
152
  - lib/tuile/event_queue.rb
150
153
  - lib/tuile/fake_event_queue.rb
@@ -156,7 +159,7 @@ files:
156
159
  - lib/tuile/screen.rb
157
160
  - lib/tuile/screen_pane.rb
158
161
  - lib/tuile/size.rb
159
- - lib/tuile/truncate.rb
162
+ - lib/tuile/styled_string.rb
160
163
  - lib/tuile/version.rb
161
164
  - lib/tuile/vertical_scroll_bar.rb
162
165
  - sig/tuile.rbs
@@ -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