Files
TUIkit/Sources/TUIkit/Extensions/String+ANSI.swift
T
phranck 95029184e7 Chore: Rename TUIKit to TUIkit
- Rename module, targets and products in Package.swift
- Rename directories: Sources/TUIkit, TUIkitExample, Tests/TUIkitTests
- Rename files: TUIkit.swift, TUIkit.docc/TUIkit.md
- Update all import statements, module-qualified calls, file headers
- Update doc comments, string literals and DocC articles
- Update CI workflow, README, .swiftlint.yml and markdown docs
- Merge platform badges into single combined badge in README
- Add "Example App auslagern" task to to-dos.md
2026-01-30 20:29:29 +01:00

35 lines
947 B
Swift

//
// String+ANSI.swift
// TUIkit
//
// ANSI-aware String helpers for visible-length calculation and padding.
//
// MARK: - ANSI String Helpers
extension String {
/// The visible length of the string, excluding ANSI escape codes.
var strippedLength: Int {
stripped.count
}
/// The string with all ANSI escape codes removed.
var stripped: String {
replacing(ANSIRenderer.ansiRegex, with: "")
}
/// Pads the string to the specified visible width using spaces.
///
/// ANSI codes are excluded from the width calculation.
///
/// - Parameter targetWidth: The desired visible width.
/// - Returns: The padded string.
func padToVisibleWidth(_ targetWidth: Int) -> String {
let currentWidth = strippedLength
if currentWidth >= targetWidth {
return self
}
return self + String(repeating: " ", count: targetWidth - currentWidth)
}
}