mirror of
https://github.com/phranck/TUIkit.git
synced 2026-06-20 09:54:37 +00:00
- 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
35 lines
947 B
Swift
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)
|
|
}
|
|
}
|