Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(lume): print VM details as labeled fields in terminals (#5358)

injaneityd9998fb4af5d
diff --git a/libs/lume/src/VM/VMDetailsPrinter.swift b/libs/lume/src/VM/VMDetailsPrinter.swift
index 6bc2268ad4..2200688e5e 100644
--- a/libs/lume/src/VM/VMDetailsPrinter.swift
+++ b/libs/lume/src/VM/VMDetailsPrinter.swift
@@ -1,6 +1,7 @@
+import Darwin
import Foundation
-/// Prints VM status information in a formatted table
+/// Prints labeled VM fields in terminals and a table when redirected.
enum VMDetailsPrinter {
/// Represents a column in the VM status table
private struct Column: Sendable {
@@ -41,7 +42,8 @@ enum VMDetailsPrinter {
getValue: { vm in
// Only show shared directories if the VM is running
if vm.status == "running", let dirs = vm.sharedDirectories, !dirs.isEmpty {
- return dirs.map { "\($0.hostPath) (\($0.readOnly ? "ro" : "rw"))" }.joined(separator: ", ")
+ return dirs.map { "\($0.hostPath) (\($0.readOnly ? "ro" : "rw"))" }.joined(
+ separator: ", ")
} else {
return "-"
}
@@ -66,10 +68,12 @@ enum VMDetailsPrinter {
}),
]
- /// Prints the status of all VMs in a formatted table
- /// - Parameter vms: Array of VM status objects to display
+ /// Prints all fields without truncation in terminals.
+ /// JSON and redirected text retain their existing formats.
+ /// - Parameter isTerminal: Whether standard output is a terminal; detected by default.
static func printStatus(
- _ vms: [VMDetails], format: FormatOption, print: (String) -> Void = { print($0) }
+ _ vms: [VMDetails], format: FormatOption, isTerminal: Bool = isatty(STDOUT_FILENO) == 1,
+ print: (String) -> Void = { print($0) }
) throws {
if format == .json {
let jsonEncoder = JSONEncoder()
@@ -77,24 +81,24 @@ enum VMDetailsPrinter {
let jsonData = try jsonEncoder.encode(vms)
let jsonString = String(data: jsonData, encoding: .utf8)!
print(jsonString)
+ } else if isTerminal {
+ let labelWidth = columns.reduce(0) { max($0, $1.header.count) } + 2
+ for (index, vm) in vms.enumerated() {
+ if index > 0 {
+ print("")
+ }
+ for column in columns {
+ let label = "\(column.header):".paddedToWidth(labelWidth)
+ print(label + column.getValue(vm))
+ }
+ }
} else {
- printHeader(print: print)
- vms.forEach({ vm in
- printVM(vm, print: print)
- })
- }
- }
-
- private static func printHeader(print: (String) -> Void = { print($0) }) {
- let paddedHeaders = columns.map { $0.header.paddedToWidth($0.width) }
- print(paddedHeaders.joined())
- }
-
- private static func printVM(_ vm: VMDetails, print: (String) -> Void = { print($0) }) {
- let paddedColumns = columns.map { column in
- column.getValue(vm).paddedToWidth(column.width)
+ print(columns.map { $0.header.paddedToWidth($0.width) }.joined())
+ for vm in vms {
+ let values = columns.map { $0.getValue(vm).paddedToWidth($0.width) }
+ print(values.joined())
+ }
}
- print(paddedColumns.joined())
}
}
diff --git a/libs/lume/tests/VM/VMDetailsPrinterTests.swift b/libs/lume/tests/VM/VMDetailsPrinterTests.swift
index b13725eb42..4813d9cce2 100644
--- a/libs/lume/tests/VM/VMDetailsPrinterTests.swift
+++ b/libs/lume/tests/VM/VMDetailsPrinterTests.swift
@@ -4,88 +4,83 @@ import Testing
@testable import lume
struct VMDetailsPrinterTests {
+ private let vms = ["first", "second"].map { name in
+ VMDetails(
+ name: name + String(repeating: "-long", count: 10),
+ os: "macOS",
+ cpuCount: 4,
+ memorySize: 8 * 1024 * 1024 * 1024,
+ diskSize: .init(allocated: 24, total: 30),
+ display: "1024x768",
+ status: "running",
+ vncUrl: "vnc://:[email protected]:59000",
+ ipAddress: nil,
+ locationName: "home",
+ sharedDirectories: [
+ SharedDirectory(
+ hostPath: String(repeating: "/shared", count: 10), tag: "shared", readOnly: true
+ )
+ ])
+ }
- @Test func printStatus_whenJSON() throws {
- // Given
- let vms: [VMDetails] = [
- VMDetails(
- name: "name",
- os: "os",
- cpuCount: 2,
- memorySize: 1024,
- diskSize: .init(allocated: 24, total: 30),
- display: "1024x768",
- status: "status",
- vncUrl: "vncUrl",
- ipAddress: "0.0.0.0",
- locationName: "mockLocation")
- ]
- let jsonEncoder = JSONEncoder()
- jsonEncoder.outputFormatting = .prettyPrinted
- let expectedOutput = try String(data: jsonEncoder.encode(vms), encoding: .utf8)!
-
- // When
- var printedStatus: String?
- try VMDetailsPrinter.printStatus(vms, format: .json, print: { printedStatus = $0 })
-
- // Then
- // Decode both JSONs and compare the actual data structures
- let jsonDecoder = JSONDecoder()
- let printedVMs = try jsonDecoder.decode(
- [VMDetails].self, from: printedStatus!.data(using: .utf8)!)
- let expectedVMs = try jsonDecoder.decode(
- [VMDetails].self, from: expectedOutput.data(using: .utf8)!)
+ @Test(arguments: [true, false])
+ func printStatus_whenJSON(isTerminal: Bool) throws {
+ var output = ""
+ try VMDetailsPrinter.printStatus(
+ vms, format: .json, isTerminal: isTerminal, print: { output = $0 })
- #expect(printedVMs.count == expectedVMs.count)
- for (printed, expected) in zip(printedVMs, expectedVMs) {
- #expect(printed.name == expected.name)
- #expect(printed.os == expected.os)
- #expect(printed.cpuCount == expected.cpuCount)
- #expect(printed.memorySize == expected.memorySize)
- #expect(printed.diskSize.allocated == expected.diskSize.allocated)
- #expect(printed.diskSize.total == expected.diskSize.total)
- #expect(printed.status == expected.status)
- #expect(printed.vncUrl == expected.vncUrl)
- #expect(printed.ipAddress == expected.ipAddress)
- }
+ let actual = try #require(JSONSerialization.jsonObject(with: Data(output.utf8)) as? NSArray)
+ let expected = try JSONSerialization.jsonObject(with: JSONEncoder().encode(vms)) as? NSArray
+ #expect(actual == expected)
}
- @Test func printStatus_whenNotJSON() throws {
- // Given
- let vms: [VMDetails] = [
- VMDetails(
- name: "name",
- os: "os",
- cpuCount: 2,
- memorySize: 1024,
- diskSize: .init(allocated: 24, total: 30),
- display: "1024x768",
- status: "status",
- vncUrl: "vncUrl",
- ipAddress: "0.0.0.0",
- locationName: "mockLocation")
- ]
+ @Test func printStatus_whenTerminal() throws {
+ var lines: [String] = []
+ try VMDetailsPrinter.printStatus(
+ vms, format: .text, isTerminal: true, print: { lines.append($0) })
- // When
- var printedLines: [String] = []
- try VMDetailsPrinter.printStatus(vms, format: .text, print: { printedLines.append($0) })
+ let expected = vms.map { vm in
+ """
+ name: \(vm.name)
+ os: macOS
+ cpu: 4
+ memory: 8.00G
+ disk: 24.0B/30.0B
+ display: 1024x768
+ status: running
+ network: nat
+ storage: home
+ shared_dirs: \(vm.sharedDirectories![0].hostPath) (ro)
+ ip: -
+ ssh: -
+ vnc: \(vm.vncUrl!)
+ """
+ }.joined(separator: "\n\n")
+ #expect(lines.joined(separator: "\n") == expected)
+ }
- // Then
- #expect(printedLines.count == 2)
+ @Test func printStatus_whenRedirected() throws {
+ var lines: [String] = []
+ try VMDetailsPrinter.printStatus(
+ vms, format: .text, isTerminal: false, print: { lines.append($0) })
- let headerParts = printedLines[0].split(whereSeparator: \.isWhitespace)
+ #expect(lines.count == 3)
+ #expect(lines.allSatisfy { $0.count == 274 })
#expect(
- headerParts == [
- "name", "os", "cpu", "memory", "disk", "display", "status", "network", "storage", "shared_dirs", "ip", "ssh", "vnc",
- ])
-
- #expect(
- printedLines[1].split(whereSeparator: \.isWhitespace).map(String.init) == [
- "name", "os", "2", "0.00G", "24.0B/30.0B", "1024x768", "status", "nat", "mockLocation",
- "-",
- "0.0.0.0",
- "-",
- "vncUrl",
+ lines[0].split(whereSeparator: \.isWhitespace) == [
+ "name", "os", "cpu", "memory", "disk", "display", "status", "network", "storage",
+ "shared_dirs", "ip", "ssh", "vnc",
])
+ for (line, vm) in zip(lines.dropFirst(), vms) {
+ let expected = [
+ (vm.name, 40), ("macOS", 8), ("4", 8), ("8.00G", 8), ("24.0B/30.0B", 16),
+ ("1024x768", 12), ("running", 28), ("nat", 12), ("home", 16),
+ ("\(vm.sharedDirectories![0].hostPath) (ro)", 54), ("-", 16), ("-", 6),
+ (vm.vncUrl!, 50),
+ ].map { value, width in
+ value.padding(toLength: width, withPad: " ", startingAt: 0)
+ }.joined()
+ #expect(line == expected)
+ }
}
}