Skip to content

Commit aea1c1e

Browse files
author
Robert Atkins
committed
Fix optional string interpolation to tidy string generation
1 parent a5fd604 commit aea1c1e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

Sources/autokbisw/IOKeyEventMonitor.swift

+20-13
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,20 @@ internal final class IOKeyEventMonitor {
7474
let selfPtr = Unmanaged<IOKeyEventMonitor>.fromOpaque(context!).takeUnretainedValue()
7575
let senderDevice = Unmanaged<IOHIDDevice>.fromOpaque(sender!).takeUnretainedValue()
7676

77-
let vendorId = String(describing: IOHIDDeviceGetProperty(senderDevice, kIOHIDVendorIDKey as CFString))
78-
let productId = String(describing: IOHIDDeviceGetProperty(senderDevice, kIOHIDProductIDKey as CFString))
79-
let product = String(describing: IOHIDDeviceGetProperty(senderDevice, kIOHIDProductKey as CFString))
80-
let manufacturer = String(describing: IOHIDDeviceGetProperty(senderDevice, kIOHIDManufacturerKey as CFString))
81-
let serialNumber = String(describing: IOHIDDeviceGetProperty(senderDevice, kIOHIDSerialNumberKey as CFString))
82-
let locationId = String(describing: IOHIDDeviceGetProperty(senderDevice, kIOHIDLocationIDKey as CFString))
83-
let uniqueId = String(describing: IOHIDDeviceGetProperty(senderDevice, kIOHIDUniqueIDKey as CFString))
84-
85-
let keyboard =
86-
selfPtr.useLocation ?
87-
"\(product)-[\(vendorId)-\(productId)-\(manufacturer)-\(serialNumber)-\(locationId)]" :
88-
"\(product)-[\(vendorId)-\(productId)-\(manufacturer)-\(serialNumber)]"
77+
let vendorId = IOHIDDeviceGetProperty(senderDevice, kIOHIDVendorIDKey as CFString) ??? "unknown"
78+
let productId = IOHIDDeviceGetProperty(senderDevice, kIOHIDProductIDKey as CFString) ??? "unknown"
79+
let product = IOHIDDeviceGetProperty(senderDevice, kIOHIDProductKey as CFString) ??? "unknown"
80+
let manufacturer = IOHIDDeviceGetProperty(senderDevice, kIOHIDManufacturerKey as CFString) ??? "unknown"
81+
let serialNumber = IOHIDDeviceGetProperty(senderDevice, kIOHIDSerialNumberKey as CFString) ??? "unknown"
82+
let locationId = IOHIDDeviceGetProperty(senderDevice, kIOHIDLocationIDKey as CFString) ??? "unknown"
83+
let uniqueId = IOHIDDeviceGetProperty(senderDevice, kIOHIDUniqueIDKey as CFString) ??? "unknown"
84+
85+
let keyboard = selfPtr.useLocation
86+
? "\(product)-[\(vendorId)-\(productId)-\(manufacturer)-\(serialNumber)-\(locationId)]"
87+
: "\(product)-[\(vendorId)-\(productId)-\(manufacturer)-\(serialNumber)]"
8988

9089
if selfPtr.verbosity >= TRACE {
91-
print("received event from keyboard \(keyboard) - \(locationId) - \(uniqueId)")
90+
print("received event from keyboard \(keyboard) - \(locationId) - \(uniqueId)")
9291
}
9392
selfPtr.onKeyboardEvent(keyboard: keyboard)
9493
}
@@ -171,3 +170,11 @@ extension IOKeyEventMonitor {
171170
}
172171
}
173172
}
173+
174+
// Nicer string interpolation of optional strings, see: https://oleb.net/blog/2016/12/optionals-string-interpolation/
175+
176+
infix operator ???: NilCoalescingPrecedence
177+
178+
public func ???<T>(optional: T?, defaultValue: @autoclosure () -> String) -> String {
179+
return optional.map { String(describing: $0) } ?? defaultValue()
180+
}

Sources/autokbisw/main.swift

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ do {
3535
let useLocation = parsedArguments.get(locationOption) ?? false
3636
let verbosity = parsedArguments.get(verboseOption) ?? 0
3737

38+
if verbosity > 0 {
39+
print("Starting with useLocation: \(useLocation) - verbosity: \(verbosity)");
40+
}
3841
let monitor = IOKeyEventMonitor(usagePage: 0x01, usage: 6, useLocation: useLocation, verbosity: verbosity)
3942
monitor?.start()
4043
CFRunLoopRun()

0 commit comments

Comments
 (0)