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
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/tui.rb', line 131 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
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 182 183 184 185 186 187 188 189 190 |
# File 'lib/tui.rb', line 154 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?(ESCAPE_TERMINATOR_RE) truncated << escape_buf escape_buf.clear in_escape = false end next end if ch == "\e" in_escape = true escape_buf.clear 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)
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 243 244 245 246 247 248 249 250 |
# File 'lib/tui.rb', line 194 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.each_char do |ch| if in_escape escape_buf << ch if ch.match?(ESCAPE_TERMINATOR_RE) leading_escapes << escape_buf escape_buf.clear in_escape = false end elsif ch == "\e" in_escape = true escape_buf.clear 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?(ESCAPE_TERMINATOR_RE) 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/tui.rb', line 106 def visible_width(text) has_escape = text.include?("\e") # Fast path: pure ASCII with no escapes if !has_escape && text.bytesize == text.length return text.length end # Strip ANSI escapes only if present stripped = has_escape ? text.gsub(ANSI_STRIP_RE, '') : 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
150 151 152 |
# File 'lib/tui.rb', line 150 def wide?(ch) char_width(ch.ord) == 2 end |
.zero_width?(ch) ⇒ Boolean
142 143 144 145 146 147 148 |
# File 'lib/tui.rb', line 142 def zero_width?(ch) code = ch.ord (code >= 0xFE00 && code <= 0xFE0F) || (code >= 0x200B && code <= 0x200D) || (code >= 0x0300 && code <= 0x036F) || (code >= 0xE0100 && code <= 0xE01EF) end |