Module: Tui::Metrics
- Defined in:
- lib/tui.rb
Class Method Summary collapse
-
.char_width(code) ⇒ Object
Simplified width check - we only use known Unicode in this app.
- .truncate(text, max_width, overflow: "…") ⇒ Object
-
.truncate_from_start(text, max_width) ⇒ Object
Truncate from the start, keeping trailing portion (for right-aligned overflow) Preserves leading ANSI escape sequences (like dim/color codes).
-
.visible_width(text) ⇒ Object
Optimized width calculation - avoids per-character method calls.
- .wide?(ch) ⇒ Boolean
- .zero_width?(ch) ⇒ Boolean
Class Method Details
.char_width(code) ⇒ Object
Simplified width check - we only use known Unicode in this app
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/tui.rb', line 123 def char_width(code) # Zero-width: variation selectors (🗑️ = trash + VS16) return 0 if code >= 0xFE00 && code <= 0xFE0F # Emoji range (📁🏠🗑📂 etc) = width 2 return 2 if code >= 0x1F300 && code <= 0x1FAFF # Everything else (ASCII, arrows, box drawing, ellipsis) = width 1 1 end |
.truncate(text, max_width, overflow: "…") ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/tui.rb', line 146 def truncate(text, max_width, overflow: "…") return text if visible_width(text) <= max_width overflow_width = visible_width(overflow) target = [max_width - overflow_width, 0].max truncated = String.new width = 0 in_escape = false escape_buf = String.new text.each_char do |ch| if in_escape escape_buf << ch if ch.match?(/[A-Za-z]/) truncated << escape_buf escape_buf = String.new in_escape = false end next end if ch == "\e" in_escape = true escape_buf = ch next end cw = char_width(ch.ord) break if width + cw > target truncated << ch width += cw end truncated.rstrip + overflow end |
.truncate_from_start(text, max_width) ⇒ Object
Truncate from the start, keeping trailing portion (for right-aligned overflow) Preserves leading ANSI escape sequences (like dim/color codes)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/tui.rb', line 185 def truncate_from_start(text, max_width) vis_width = visible_width(text) return text if vis_width <= max_width # Collect leading escape sequences first leading_escapes = String.new in_escape = false escape_buf = String.new text_start = 0 text.each_char.with_index do |ch, i| if in_escape escape_buf << ch if ch.match?(/[A-Za-z]/) leading_escapes << escape_buf escape_buf = String.new in_escape = false text_start = i + 1 end elsif ch == "\e" in_escape = true escape_buf = ch else # First non-escape character, stop collecting leading escapes break end end # Now skip visible characters to get max_width remaining chars_to_skip = vis_width - max_width skipped = 0 result = String.new in_escape = false text.each_char do |ch| if in_escape result << ch if skipped >= chars_to_skip in_escape = false if ch.match?(/[A-Za-z]/) next end if ch == "\e" in_escape = true result << ch if skipped >= chars_to_skip next end cw = char_width(ch.ord) if skipped < chars_to_skip skipped += cw else result << ch end end # Prepend leading escapes to preserve styling leading_escapes + result end |
.visible_width(text) ⇒ Object
Optimized width calculation - avoids per-character method calls
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/tui.rb', line 100 def visible_width(text) # Fast path: pure ASCII with no escapes if text.bytesize == text.length && !text.include?("\e") return text.length end # Strip ANSI escapes only if present stripped = text.include?("\e") ? text.gsub(/\e\[[0-9;]*[A-Za-z]/, '') : text # Fast path after stripping: pure ASCII if stripped.bytesize == stripped.length return stripped.length end # Slow path: calculate width per codepoint (avoids each_char + ord) width = 0 stripped.each_codepoint do |code| width += char_width(code) end width end |
.wide?(ch) ⇒ Boolean
142 143 144 |
# File 'lib/tui.rb', line 142 def wide?(ch) char_width(ch.ord) == 2 end |
.zero_width?(ch) ⇒ Boolean
134 135 136 137 138 139 140 |
# File 'lib/tui.rb', line 134 def zero_width?(ch) code = ch.ord (code >= 0xFE00 && code <= 0xFE0F) || (code >= 0x200B && code <= 0x200D) || (code >= 0x0300 && code <= 0x036F) || (code >= 0xE0100 && code <= 0xE01EF) end |