Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
peel's tables line up by terminal cells and are written at once
Țugui Dragoș70f2df1d3cbb
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swiftindex 34bf6094..9b4ee242 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift@@ -3,6 +3,9 @@ import Darwinimport Foundationimport PeelCoreimport Synchronization+// `wcwidth_l`, which Swift imports when both are imported, as the SDK's module map says.+import wchar_h+import xlocalestruct CommandFailure: Error, CustomStringConvertible {let description: String@@ -20,12 +23,19 @@ enum Output {/// What a command printed, kept in place of standard output and standard error while a test runs it inside/// `Output.$collected.withValue`, so the test can read what a person or a script would have read.final class Collected: Sendable {- private let printed = Mutex((output: "", notes: ""))+ private let printed = Mutex((output: "", notes: "", writes: 0))var output: String { printed.withLock { $0.output } }var notes: String { printed.withLock { $0.notes } }+ /// How many times standard output was written to.+ var writes: Int { printed.withLock { $0.writes } }- fileprivate func add(output text: String) { printed.withLock { $0.output += text } }+ fileprivate func add(output text: String) {+ printed.withLock {+ $0.output += text+ $0.writes += 1+ }+ }fileprivate func add(note text: String) { printed.withLock { $0.notes += text } }}@@ -91,24 +101,41 @@ enum Output {return String(decoding: try encoder.encode(value), as: UTF8.self) + "\n"}- /// Prints rows as a table, padding every column but the last to the width of its widest cell.+ /// Prints rows as a table, padding every column but the last to the width of its widest cell, in one write.static func table(_ rows: [[String]], indent: String = "") {- for row in paddedCells(rows) {- line(indent + row)- }+ write(paddedCells(rows).map { indent + $0 + "\n" }.joined())}+ /// Returns each row as one line, every cell made `plain` and every cell but the last padded to its column's+ /// width in terminal cells.static func paddedCells(_ rows: [[String]]) -> [String] {+ let rows = rows.map { $0.map(plain) }+ let locale = newlocale(LC_CTYPE_MASK, "UTF-8", nil)+ defer { if let locale { freelocale(locale) } }+ let widths = rows.map { $0.map { cells($0, in: locale) } }let columns = rows.map(\.count).max() ?? 0- let widths = (0..<columns).map { column in rows.map { column < $0.count ? $0[column].count : 0 }.max() ?? 0 }- return rows.map { row in+ let columnWidths = (0..<columns).map { column in widths.map { column < $0.count ? $0[column] : 0 }.max() ?? 0 }+ return zip(rows, widths).map { row, width inrow.enumerated().map { index, cell in- index == row.count - 1 ? cell : cell + String(repeating: " ", count: widths[index] - cell.count)+ index == row.count - 1 ? cell : cell + String(repeating: " ", count: columnWidths[index] - width[index])}.joined(separator: " ")}}+ /// The terminal cells `text` takes: two for a character drawn as an emoji or as a wide East Asian one, none+ /// for a mark that combines with the character before it, and one for anything else.+ static func cells(_ text: String, in locale: locale_t?) -> Int {+ text.reduce(0) { total, character in+ let scalars = character.unicodeScalars+ // A flag, and a character followed by U+FE0F, which asks for its emoji form, are drawn as an emoji.+ if scalars.contains(where: { $0.properties.isEmojiPresentation || $0.value == 0xFE0F }) { return total + 2 }+ let width = scalars.map { Int(wcwidth_l(wchar_t(bitPattern: $0.value), locale)) }.max() ?? 0+ // -1 is a character `wcwidth_l` can't print, which a terminal draws as a box.+ return total + (width < 0 ? 1 : width)+ }+ }+/// Formats `bytes` as a file size. `spellsOutZero` is turned off, since by default zero is spelled out as a word.static func size(_ bytes: Int64) -> String {bytes.formatted(.byteCount(style: .file, spellsOutZero: false).locale(locale))diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swiftindex bfc1ae71..65275e2e 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift@@ -24,8 +24,23 @@ struct CommandLineTests {#expect(ByteSize(argument: "MB") == nil)}- @Test func alignsColumnsByCharacters() {- #expect(Output.paddedCells([["📷 Photos", "1 kB", "x"], ["Café", "10 kB", "y"]]) == ["📷 Photos 1 kB x", "Café 10 kB y"])+ /// A terminal gives an emoji and a wide East Asian character two cells, and a combining mark none.+ @Test func alignsColumnsByTerminalCells() {+ #expect(Output.paddedCells([["📷 Photos", "1 kB", "x"], ["Café", "10 kB", "y"]]) == ["📷 Photos 1 kB x", "Café 10 kB y"])+ #expect(Output.paddedCells([["日本語", "a"], ["abc", "b"], ["e\u{301}", "c"], ["🇷🇴❤️", "d"]]) == [+ "日本語 a", "abc b", "e\u{301} c", "🇷🇴❤️ d",+ ])+ }++ /// A table is written at once, rather than a row at a time: a search lists up to 2,000 rows.+ @Test func writesATableAtOnce() {+ let collected = Output.Collected()+ Output.$collected.withValue(collected) {+ Output.table([["1 kB", "/a"], ["2 kB", "/b"], ["3 kB", "/c"]], indent: " ")+ }++ #expect(collected.writes == 1)+ #expect(collected.output == " 1 kB /a\n 2 kB /b\n 3 kB /c\n")}/// `peel uninstall /applications/example.app`, or a link to the app, finds the installed app itself: never