diff --git a/Sources/AppBundle/config/parseOnWindowDetected.swift b/Sources/AppBundle/config/parseOnWindowDetected.swift index 1d407908..679a9aff 100644 --- a/Sources/AppBundle/config/parseOnWindowDetected.swift +++ b/Sources/AppBundle/config/parseOnWindowDetected.swift @@ -77,7 +77,7 @@ private let matcherParsers: [String: any ParserProtocol(_ fun: @escaping @Sendable (Json, ConfigBacktrace) -> ParsedConfig) -> @Sendable (Json, ConfigBacktrace) -> ParsedConfig { - { fun($0, $1).map { $0 } } + { fun($0, $1).map(Optional.init) } } func parseOnWindowDetectedArray(_ raw: Json, _ backtrace: ConfigBacktrace, _ errors: inout [ConfigParseError]) -> [WindowDetectedCallback] { diff --git a/Sources/AppBundle/layout/refresh.swift b/Sources/AppBundle/layout/refresh.swift index 78afc641..60038054 100644 --- a/Sources/AppBundle/layout/refresh.swift +++ b/Sources/AppBundle/layout/refresh.swift @@ -111,7 +111,7 @@ func refreshModel() { private func refresh() async throws { // Garbage collect terminated apps and windows before working with all windows let mapping = try await MacApp.refreshAllAndGetAliveWindowIds(frontmostAppBundleId: NSWorkspace.shared.frontmostApplication?.bundleIdentifier) - let aliveWindowIds = mapping.values.flatMap { $0 }.toSet() + let aliveWindowIds = mapping.values.flatMap(id).toSet() for window in MacWindow.allWindows { if !aliveWindowIds.contains(window.windowId) { diff --git a/Sources/AppBundle/server.swift b/Sources/AppBundle/server.swift index b5346f11..fa8e78bc 100644 --- a/Sources/AppBundle/server.swift +++ b/Sources/AppBundle/server.swift @@ -97,8 +97,8 @@ private func newConnection(_ connection: NWConnection) async { // todo add exit let _answer: Result = await Result { try await runLightSession(.socketServer(command.args), token) { () throws in let env = CmdEnv.init( - windowId: request.windowId.flatMap { $0 }, - workspaceName: request.workspace.flatMap { $0 }, + windowId: request.windowId.flattenOptional(), + workspaceName: request.workspace.flattenOptional(), ) let cmdResult = try await command.run(env, CmdStdin(request.stdin)) return ServerAnswer( diff --git a/Sources/Common/cmdArgs/ArgParser.swift b/Sources/Common/cmdArgs/ArgParser.swift index a6f02dac..a67a5027 100644 --- a/Sources/Common/cmdArgs/ArgParser.swift +++ b/Sources/Common/cmdArgs/ArgParser.swift @@ -103,4 +103,4 @@ func parseCardinalOrDfsDirection(i: PosArgParserInput) -> ParsedCliArgs(_ fun: @escaping ArgParserFun) -> ArgParserFun { { fun($0).map { $0 } } } +func upcastArgParserFun(_ fun: @escaping ArgParserFun) -> ArgParserFun { { fun($0).map(Optional.init) } } diff --git a/Sources/Common/cmdArgs/impl/ConfigCmdArgs.swift b/Sources/Common/cmdArgs/impl/ConfigCmdArgs.swift index 1acc6d7f..45ce51c2 100644 --- a/Sources/Common/cmdArgs/impl/ConfigCmdArgs.swift +++ b/Sources/Common/cmdArgs/impl/ConfigCmdArgs.swift @@ -10,7 +10,7 @@ public struct ConfigCmdArgs: CmdArgs, Equatable { "--major-keys": trueBoolFlag(\.majorKeys), "--all-keys": trueBoolFlag(\.allKeys), "--config-path": trueBoolFlag(\.configPath), - "--get": singleValueSubArgParser(\.keyNameToGet, "") { $0 }, + "--get": singleValueSubArgParser(\.keyNameToGet, "", id), ], posArgs: [], ) diff --git a/Sources/Common/cmdArgs/impl/ListWindowsCmdArgs.swift b/Sources/Common/cmdArgs/impl/ListWindowsCmdArgs.swift index ca59b40f..eb1ef236 100644 --- a/Sources/Common/cmdArgs/impl/ListWindowsCmdArgs.swift +++ b/Sources/Common/cmdArgs/impl/ListWindowsCmdArgs.swift @@ -17,7 +17,7 @@ public struct ListWindowsCmdArgs: CmdArgs { "--monitor": ArgParser(\.filteringOptions.monitors, parseMonitorIds), "--workspace": ArgParser(\.filteringOptions.workspaces, parseWorkspaces), "--pid": singleValueSubArgParser(\.filteringOptions.pidFilter, "", Int32.init), - "--app-bundle-id": singleValueSubArgParser(\.filteringOptions.appIdFilter, "") { $0 }, + "--app-bundle-id": singleValueSubArgParser(\.filteringOptions.appIdFilter, "", id), // Formatting flags "--format": formatParser(\._format, for: .window), diff --git a/Sources/Common/cmdArgs/impl/TriggerBindingCmdArgs.swift b/Sources/Common/cmdArgs/impl/TriggerBindingCmdArgs.swift index a2dc5a49..5991b29d 100644 --- a/Sources/Common/cmdArgs/impl/TriggerBindingCmdArgs.swift +++ b/Sources/Common/cmdArgs/impl/TriggerBindingCmdArgs.swift @@ -5,7 +5,7 @@ public struct TriggerBindingCmdArgs: CmdArgs { allowInConfig: true, help: trigger_binding_help_generated, flags: [ - "--mode": singleValueSubArgParser(\._mode, "") { $0 }, + "--mode": singleValueSubArgParser(\._mode, "", id), ], posArgs: [newMandatoryPosArgParser(\.binding, consumeStrCliArg, placeholder: "")], ) diff --git a/Sources/Common/cmdArgs/subcommandParsers.swift b/Sources/Common/cmdArgs/subcommandParsers.swift index 3dc49d1c..126bb1db 100644 --- a/Sources/Common/cmdArgs/subcommandParsers.swift +++ b/Sources/Common/cmdArgs/subcommandParsers.swift @@ -6,9 +6,7 @@ protocol SubCommandParserProtocol: Sendable { } extension SubCommandParserProtocol { - func parse(args: StrArrSlice) -> ParsedCmd { - _parse(args).map { $0 } - } + func parse(args: StrArrSlice) -> ParsedCmd { _parse(args).map(id) } } struct SubCommandParser: SubCommandParserProtocol, Sendable { diff --git a/Sources/Common/model/clientServer.swift b/Sources/Common/model/clientServer.swift index 63cabc50..20b75fb4 100644 --- a/Sources/Common/model/clientServer.swift +++ b/Sources/Common/model/clientServer.swift @@ -64,8 +64,8 @@ public struct ClientRequest: Codable, Sendable, ConvenienceCopyable, Equatable { var raw = ClientRequest( args: data.args, stdin: data.stdin, - windowId: data.windowId.flatMap { $0 }, - workspace: data.workspace.flatMap { $0 }, + windowId: data.windowId.flattenOptional(), + workspace: data.workspace.flattenOptional(), ) let container = try decoder.container(keyedBy: CodingKeys.self) if !container.contains(.windowId) { raw.windowId = nil } diff --git a/Sources/Common/util/OptionalEx.swift b/Sources/Common/util/OptionalEx.swift index 7c28defc..ea4e6676 100644 --- a/Sources/Common/util/OptionalEx.swift +++ b/Sources/Common/util/OptionalEx.swift @@ -33,6 +33,13 @@ extension Optional { } } + public func flattenOptional() -> T? where Wrapped == T? { + switch self { + case let x?: x + case nil: nil + } + } + public var prettyDescription: String { if let unwrapped = self { return String(describing: unwrapped) diff --git a/Sources/Common/util/SequenceEx.swift b/Sources/Common/util/SequenceEx.swift index bdf917cb..6ba1a4cc 100644 --- a/Sources/Common/util/SequenceEx.swift +++ b/Sources/Common/util/SequenceEx.swift @@ -3,9 +3,7 @@ import AppKit import OrderedCollections extension Sequence { - public func filterNotNil() -> [Unwrapped] where Element == Unwrapped? { - compactMap { $0 } - } + public func filterNotNil() -> [Unwrapped] where Element == Unwrapped? { compactMap(id) } public func filterIsInstance(of _: R.Type) -> [R] { var result: [R] = [] diff --git a/Sources/Common/util/commonUtil.swift b/Sources/Common/util/commonUtil.swift index 31db62c9..795a3473 100644 --- a/Sources/Common/util/commonUtil.swift +++ b/Sources/Common/util/commonUtil.swift @@ -192,6 +192,9 @@ public func exitT(_ exitCode: Int32, out: String? = nil, err: String? = nil) exit(exitCode) } +/// 'id' stands for 'identity'. It's a common name in functional programming +public func id(_ t: T) -> T { t } + @inlinable public func allowOnlyCancellationError(_ block: () async throws -> sending T) async throws -> sending T { do {