TUI: a selected row no longer turns the whole screen reverse
The frame cut each line to the terminal's width counting ANSI codes as characters, so the padded, reversed selected row lost its reset code and the reverse video ran over the rest of the screen (Models, Dashboard, model picker). TextLayout.truncate / fit now count visible characters, and a cut styled line ends with a reset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
802366e parent: 228c5b0 modified
Sources/RazorbillTUI/Style.swift +2 -2 | @@ -26,9 +26,9 @@ public struct Style: Sendable, Equatable { | ||
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | extension TextLayout { |
| 29 | - /// `text` cut or padded with spaces to exactly `width` characters. | |
| 29 | + /// `text` cut or padded with spaces to exactly `width` visible characters. | |
| 30 | 30 | public static func fit(_ text: String, width: Int) -> String { |
| 31 | 31 | let cut = truncate(text, width: max(width, 0)) |
| 32 | - return cut + String(repeating: " ", count: max(0, width - cut.count)) | |
| 32 | + return cut + String(repeating: " ", count: max(0, width - visibleCount(cut))) | |
| 33 | 33 | } |
| 34 | 34 | } |
| @@ -26,9 +26,9 @@ public struct Style: Sendable, Equatable { | |||
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | extension TextLayout { | 28 | extension TextLayout { |
| 29 | - /// `text` cut or padded with spaces to exactly `width` characters. | 29 | + /// `text` cut or padded with spaces to exactly `width` visible characters. |
| 30 | public static func fit(_ text: String, width: Int) -> String { | 30 | public static func fit(_ text: String, width: Int) -> String { |
| 31 | let cut = truncate(text, width: max(width, 0)) | 31 | let cut = truncate(text, width: max(width, 0)) |
| 32 | - return cut + String(repeating: " ", count: max(0, width - cut.count)) | 32 | + return cut + String(repeating: " ", count: max(0, width - visibleCount(cut))) |
| 33 | } | 33 | } |
| 34 | } | 34 | } |
modified
Sources/RazorbillTUI/TextLayout.swift +42 -4 | @@ -28,10 +28,48 @@ public enum TextLayout { | ||
| 28 | 28 | return lines |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - /// `text` cut to `width` characters, with `…` when cut. | |
| 31 | + /// The characters `text` shows on screen: ANSI escape sequences (`ESC [ … letter`) count for | |
| 32 | + /// nothing. | |
| 33 | + public static func visibleCount(_ text: String) -> Int { | |
| 34 | + var count = 0 | |
| 35 | + var inEscape = false | |
| 36 | + for character in text { | |
| 37 | + if inEscape { | |
| 38 | + if character.isLetter { inEscape = false } | |
| 39 | + } else if character == "\u{1B}" { | |
| 40 | + inEscape = true | |
| 41 | + } else { | |
| 42 | + count += 1 | |
| 43 | + } | |
| 44 | + } | |
| 45 | + return count | |
| 46 | + } | |
| 47 | + | |
| 48 | + /// `text` cut to `width` visible characters, with `…` when cut. Escape sequences are kept and | |
| 49 | + /// not counted, and a cut styled text ends with a reset, so a style never runs past the line. | |
| 32 | 50 | public static func truncate(_ text: String, width: Int) -> String { |
| 33 | - guard text.count > width else { return text } | |
| 34 | - guard width > 1 else { return String(text.prefix(width)) } | |
| 35 | - return String(text.prefix(width - 1)) + "…" | |
| 51 | + guard visibleCount(text) > width else { return text } | |
| 52 | + let kept = max(0, width > 1 ? width - 1 : width) | |
| 53 | + var result = "" | |
| 54 | + var count = 0 | |
| 55 | + var inEscape = false | |
| 56 | + var styled = false | |
| 57 | + for character in text { | |
| 58 | + if inEscape { | |
| 59 | + result.append(character) | |
| 60 | + if character.isLetter { inEscape = false } | |
| 61 | + } else if character == "\u{1B}" { | |
| 62 | + inEscape = true | |
| 63 | + styled = true | |
| 64 | + result.append(character) | |
| 65 | + } else if count < kept { | |
| 66 | + result.append(character) | |
| 67 | + count += 1 | |
| 68 | + } else { | |
| 69 | + break | |
| 70 | + } | |
| 71 | + } | |
| 72 | + if width > 1 { result += "…" } | |
| 73 | + return styled ? result + "\u{1B}[0m" : result | |
| 36 | 74 | } |
| 37 | 75 | } |
| @@ -28,10 +28,48 @@ public enum TextLayout { | |||
| 28 | return lines | 28 | return lines |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | - /// `text` cut to `width` characters, with `…` when cut. | 31 | + /// The characters `text` shows on screen: ANSI escape sequences (`ESC [ … letter`) count for |
| 32 | + /// nothing. | ||
| 33 | + public static func visibleCount(_ text: String) -> Int { | ||
| 34 | + var count = 0 | ||
| 35 | + var inEscape = false | ||
| 36 | + for character in text { | ||
| 37 | + if inEscape { | ||
| 38 | + if character.isLetter { inEscape = false } | ||
| 39 | + } else if character == "\u{1B}" { | ||
| 40 | + inEscape = true | ||
| 41 | + } else { | ||
| 42 | + count += 1 | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + return count | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + /// `text` cut to `width` visible characters, with `…` when cut. Escape sequences are kept and | ||
| 49 | + /// not counted, and a cut styled text ends with a reset, so a style never runs past the line. | ||
| 32 | public static func truncate(_ text: String, width: Int) -> String { | 50 | public static func truncate(_ text: String, width: Int) -> String { |
| 33 | - guard text.count > width else { return text } | 51 | + guard visibleCount(text) > width else { return text } |
| 34 | - guard width > 1 else { return String(text.prefix(width)) } | 52 | + let kept = max(0, width > 1 ? width - 1 : width) |
| 35 | - return String(text.prefix(width - 1)) + "…" | 53 | + var result = "" |
| 54 | + var count = 0 | ||
| 55 | + var inEscape = false | ||
| 56 | + var styled = false | ||
| 57 | + for character in text { | ||
| 58 | + if inEscape { | ||
| 59 | + result.append(character) | ||
| 60 | + if character.isLetter { inEscape = false } | ||
| 61 | + } else if character == "\u{1B}" { | ||
| 62 | + inEscape = true | ||
| 63 | + styled = true | ||
| 64 | + result.append(character) | ||
| 65 | + } else if count < kept { | ||
| 66 | + result.append(character) | ||
| 67 | + count += 1 | ||
| 68 | + } else { | ||
| 69 | + break | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + if width > 1 { result += "…" } | ||
| 73 | + return styled ? result + "\u{1B}[0m" : result | ||
| 36 | } | 74 | } |
| 37 | } | 75 | } |
modified
Tests/RazorbillTUITests/AppShellTests.swift +25 -0 | @@ -185,3 +185,28 @@ import Testing | ||
| 185 | 185 | #expect(decoder.decode([27, 91, 90]) == [.backtab]) |
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | + | |
| 189 | +@Suite struct StyledLinesTests { | |
| 190 | + /// The selected row of a list, drawn in reverse video at the full width, must keep its reset | |
| 191 | + /// code once the frame cuts lines to the terminal's width: otherwise the reverse video runs | |
| 192 | + /// over the rest of the screen. | |
| 193 | + @Test func selectedRowKeepsItsResetInTheFrame() { | |
| 194 | + let style = Style(colors: true) | |
| 195 | + var list = ListView(count: 3) | |
| 196 | + list.handle(.arrowDown, pageSize: 3) | |
| 197 | + let rows = list.render(["a", "b", "c"], width: 40, height: 3, style: style) | |
| 198 | + let screen = AppRenderer(style: style).render( | |
| 199 | + AppState(serverURL: "u"), body: rows, rows: 6, columns: 40) | |
| 200 | + let selected = screen.lines[2] | |
| 201 | + #expect(selected.hasPrefix("\u{1B}[7m") && selected.hasSuffix("\u{1B}[0m")) | |
| 202 | + #expect(TextLayout.visibleCount(selected) == 40) | |
| 203 | + } | |
| 204 | + | |
| 205 | + @Test func truncateCountsOnlyVisibleCharactersAndClosesStyles() { | |
| 206 | + let styled = "\u{1B}[1mhello world\u{1B}[0m" | |
| 207 | + #expect(TextLayout.truncate(styled, width: 20) == styled) | |
| 208 | + #expect(TextLayout.truncate(styled, width: 6) == "\u{1B}[1mhello…\u{1B}[0m") | |
| 209 | + #expect(TextLayout.visibleCount(styled) == 11) | |
| 210 | + #expect(TextLayout.fit("\u{1B}[2mab\u{1B}[0m", width: 4) == "\u{1B}[2mab\u{1B}[0m ") | |
| 211 | + } | |
| 212 | +} | |
| @@ -185,3 +185,28 @@ import Testing | |||
| 185 | #expect(decoder.decode([27, 91, 90]) == [.backtab]) | 185 | #expect(decoder.decode([27, 91, 90]) == [.backtab]) |
| 186 | } | 186 | } |
| 187 | } | 187 | } |
| 188 | + | ||
| 189 | +@Suite struct StyledLinesTests { | ||
| 190 | + /// The selected row of a list, drawn in reverse video at the full width, must keep its reset | ||
| 191 | + /// code once the frame cuts lines to the terminal's width: otherwise the reverse video runs | ||
| 192 | + /// over the rest of the screen. | ||
| 193 | + @Test func selectedRowKeepsItsResetInTheFrame() { | ||
| 194 | + let style = Style(colors: true) | ||
| 195 | + var list = ListView(count: 3) | ||
| 196 | + list.handle(.arrowDown, pageSize: 3) | ||
| 197 | + let rows = list.render(["a", "b", "c"], width: 40, height: 3, style: style) | ||
| 198 | + let screen = AppRenderer(style: style).render( | ||
| 199 | + AppState(serverURL: "u"), body: rows, rows: 6, columns: 40) | ||
| 200 | + let selected = screen.lines[2] | ||
| 201 | + #expect(selected.hasPrefix("\u{1B}[7m") && selected.hasSuffix("\u{1B}[0m")) | ||
| 202 | + #expect(TextLayout.visibleCount(selected) == 40) | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + @Test func truncateCountsOnlyVisibleCharactersAndClosesStyles() { | ||
| 206 | + let styled = "\u{1B}[1mhello world\u{1B}[0m" | ||
| 207 | + #expect(TextLayout.truncate(styled, width: 20) == styled) | ||
| 208 | + #expect(TextLayout.truncate(styled, width: 6) == "\u{1B}[1mhello…\u{1B}[0m") | ||
| 209 | + #expect(TextLayout.visibleCount(styled) == 11) | ||
| 210 | + #expect(TextLayout.fit("\u{1B}[2mab\u{1B}[0m", width: 4) == "\u{1B}[2mab\u{1B}[0m ") | ||
| 211 | + } | ||
| 212 | +} | ||