-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReferencePreset.swift
82 lines (68 loc) · 2.38 KB
/
ReferencePreset.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Cocoa
import Foundation
func presetStrings(_ display: MPDisplay) -> [String] {
display.presets.filter(\.isValid).map { preset in
"\(preset.presetIndex). \(preset.presetName ?? "NO NAME")"
}
}
func printDisplays(_ displays: [MPDisplay]) {
for panel in displays {
print("""
\(panel.displayName ?? "Unknown display")
ID: \(panel.displayID)
UUID: \(panel.uuid?.uuidString ?? "")
Preset: \(panel.activePreset?.presetName ?? "No preset")
Presets:
\t\(presetStrings(panel).joined(separator: "\n\t"))
""")
}
}
func setReferencePreset(display: MPDisplay, presetFilter: String) {
let displayName = display.displayName ?? "display"
if let index = Int(presetFilter) {
guard let preset = display.presets.first(where: { $0.presetIndex == index }) else {
print("No preset with index \(index) for \(displayName)")
return
}
print("Activating preset \"\(preset.presetName ?? presetFilter)\" for \(displayName)")
display.setActivePreset(preset)
return
}
guard let preset = display.presets.first(where: { $0.presetName == presetFilter }) else {
print("No preset with name \(presetFilter) for \(displayName)")
return
}
print("Activating preset \"\(preset.presetName ?? presetFilter)\" for \(displayName)")
display.setActivePreset(preset)
}
func main() {
guard let mgr = MPDisplayMgr(), let displays = mgr.displays else {
print("No displays")
return
}
guard CommandLine.arguments.count >= 3 else {
printDisplays(displays)
print("\nUsage: \(CommandLine.arguments[0]) <id/uuid/name/all> <preset-name-or-index>")
return
}
defer {
print("")
printDisplays(displays)
}
let display = CommandLine.arguments[1]
let preset = CommandLine.arguments[2]
// Example: `ReferencePreset all 2`
if display.lowercased() == "all" {
for display in displays.filter(\.hasPresets) {
setReferencePreset(display: display, presetFilter: preset)
}
return
}
// Example: `ReferencePreset DELL 2`
guard let display = mgr.matchDisplay(filter: display) else {
print("No display found for query: \(display)")
return
}
setReferencePreset(display: display, presetFilter: preset)
}
main()