Skip to content

Commit 68ffa6c

Browse files
authored
tart set: support optional "pt" and "px" units for "--display" argument (#1155)
* tart set: support optional "pt" and "px" units for "--display" argument * Don't forget to update "unit" too
1 parent 1b091e9 commit 68ffa6c

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

Sources/tart/Commands/Set.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Set: AsyncParsableCommand {
1414
@Option(help: "VM memory size in megabytes")
1515
var memory: UInt64?
1616

17-
@Option(help: "VM display resolution in a format of <width>x<height>. For example, 1200x800")
17+
@Option(help: "VM display resolution in a format of WIDTHxHEIGHT[pt|px]. For example, 1200x800, 1200x800pt or 1920x1080px. Units are treated as hints and default to \"pt\" (points) for macOS VMs and \"px\" (pixels) for Linux VMs when not specified.")
1818
var display: VMDisplayConfig?
1919

2020
@Flag(inversion: .prefixedNo, help: ArgumentHelp("Whether to automatically reconfigure the VM's display to fit the window"))
@@ -56,6 +56,7 @@ struct Set: AsyncParsableCommand {
5656
if (display.height > 0) {
5757
vmConfig.display.height = display.height
5858
}
59+
vmConfig.display.unit = display.unit
5960
}
6061

6162
vmConfig.displayRefit = displayRefit
@@ -88,12 +89,24 @@ struct Set: AsyncParsableCommand {
8889

8990
extension VMDisplayConfig: ExpressibleByArgument {
9091
public init(argument: String) {
92+
var argument = argument
93+
var unit: Unit? = nil
94+
95+
if argument.hasSuffix(Unit.pixel.rawValue) {
96+
argument = String(argument.dropLast(Unit.pixel.rawValue.count))
97+
unit = Unit.pixel
98+
} else if argument.hasSuffix(Unit.point.rawValue) {
99+
argument = String(argument.dropLast(Unit.point.rawValue.count))
100+
unit = Unit.point
101+
}
102+
91103
let parts = argument.components(separatedBy: "x").map {
92104
Int($0) ?? 0
93105
}
94106
self = VMDisplayConfig(
95107
width: parts[safe: 0] ?? 0,
96-
height: parts[safe: 1] ?? 0
108+
height: parts[safe: 1] ?? 0,
109+
unit: unit,
97110
)
98111
}
99112
}

Sources/tart/Platform/Darwin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct UnsupportedHostOSError: Error, CustomStringConvertible {
8282
func graphicsDevice(vmConfig: VMConfig) -> VZGraphicsDeviceConfiguration {
8383
let result = VZMacGraphicsDeviceConfiguration()
8484

85-
if let hostMainScreen = NSScreen.main {
85+
if (vmConfig.display.unit ?? .point) == .point, let hostMainScreen = NSScreen.main {
8686
let vmScreenSize = NSSize(width: vmConfig.display.width, height: vmConfig.display.height)
8787
result.displays = [
8888
VZMacGraphicsDisplayConfiguration(for: hostMainScreen, sizeInPoints: vmScreenSize)

Sources/tart/VMConfig.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,24 @@ enum CodingKeys: String, CodingKey {
3232
case hardwareModel
3333
}
3434

35-
struct VMDisplayConfig: Codable {
35+
struct VMDisplayConfig: Codable, Equatable {
36+
enum Unit: String, Codable {
37+
case point = "pt"
38+
case pixel = "px"
39+
}
40+
3641
var width: Int = 1024
3742
var height: Int = 768
43+
var unit: Unit?
3844
}
3945

4046
extension VMDisplayConfig: CustomStringConvertible {
4147
var description: String {
42-
"\(width)x\(height)"
48+
if let unit {
49+
"\(width)x\(height)\(unit.rawValue)"
50+
} else {
51+
"\(width)x\(height)"
52+
}
4353
}
4454
}
4555

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import XCTest
2+
@testable import tart
3+
4+
final class VMConfigTests: XCTestCase {
5+
func testVMDisplayConfig() throws {
6+
// Defaults units (points)
7+
var vmDisplayConfig = VMDisplayConfig.init(argument: "1234x5678")
8+
XCTAssertEqual(VMDisplayConfig(width: 1234, height: 5678, unit: nil), vmDisplayConfig)
9+
10+
// Explicit units (points)
11+
vmDisplayConfig = VMDisplayConfig.init(argument: "1234x5678pt")
12+
XCTAssertEqual(VMDisplayConfig(width: 1234, height: 5678, unit: .point), vmDisplayConfig)
13+
14+
// Explicit units (pixels)
15+
vmDisplayConfig = VMDisplayConfig.init(argument: "1234x5678px")
16+
XCTAssertEqual(VMDisplayConfig(width: 1234, height: 5678, unit: .pixel), vmDisplayConfig)
17+
}
18+
}

0 commit comments

Comments
 (0)