Skip to content

Commit

Permalink
Add option to filter by hidden apps in list-windows and list-apps com…
Browse files Browse the repository at this point in the history
…mands

#18
  • Loading branch information
nikitabobko committed Feb 17, 2024
1 parent 77833db commit a4e2be8
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 13 deletions.
4 changes: 0 additions & 4 deletions LocalPackage/Sources/Common/cmdArgs/noCmdArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public struct WorkspaceBackAndForthCmdArgs: RawCmdArgs, CmdArgs {
public init() {}
public static let parser: CmdParser<Self> = noArgsParser(.workspaceBackAndForth, allowInConfig: true)
}
public struct ListAppsCmdArgs: RawCmdArgs, CmdArgs {
public init() {}
public static let parser: CmdParser<Self> = noArgsParser(.listApps, allowInConfig: false)
}
public struct ServerVersionInternalCommandCmdArgs: RawCmdArgs, CmdArgs {
public init() {}
public static let parser: CmdParser<Self> = noArgsParser(.serverVersionInternalCommand, allowInConfig: false)
Expand Down
2 changes: 1 addition & 1 deletion LocalPackage/Sources/Common/cmdArgs/parseCmdArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private func initSubcommands() -> [String: any SubCommandParserProtocol] {
case .layout:
result[kind.rawValue] = SubCommandParser(parseLayoutCmdArgs)
case .listApps:
result[kind.rawValue] = noArgsSubCommandParser(ListAppsCmdArgs())
result[kind.rawValue] = SubCommandParser(parseListAppsCmdArgs)
case .listMonitors:
result[kind.rawValue] = SubCommandParser(parseListMonitorsCmdArgs)
case .listWindows:
Expand Down
24 changes: 24 additions & 0 deletions LocalPackage/Sources/Common/cmdArgs/query/ListAppsCmdArgs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public struct ListAppsCmdArgs: RawCmdArgs, CmdArgs {
public init() {}
public static let parser: CmdParser<Self> = cmdParser(
kind: .listApps,
allowInConfig: false,
help: """
USAGE: list-apps [-h|--help] [--macos-hidden [no]]
OPTIONS:
-h, --help Print help
--macos-hidden [no] Filter results to only print (not) hidden applications
""",
options: [
"--macos-hidden": boolFlag(\.macosHidden),
],
arguments: []
)

public var macosHidden: Bool?
}

public func parseListAppsCmdArgs(_ args: [String]) -> ParsedCmd<ListAppsCmdArgs> {
parseRawCmdArgs(ListAppsCmdArgs(), args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ private struct RawListWindowsCmdArgs: RawCmdArgs, Equatable {
allowInConfig: false,
help: """
USAGE: list-windows [-h|--help] --workspace \(_workspaces) [--monitor \(_monitors)]
[--pid <pid>] [--app-id <app-id>]
[--pid <pid>] [--app-id <app-id>] [--macos-hidden-app [no]]
OR: list-windows [-h|--help] --monitor \(_monitors) [--workspace \(_workspaces)]
[--pid <pid>] [--app-id <app-id>]
[--pid <pid>] [--app-id <app-id>] [--macos-hidden-app [no]]
OR: list-windows [-h|--help] --all
OR: list-windows [-h|--help] --focused
Expand All @@ -21,6 +21,7 @@ private struct RawListWindowsCmdArgs: RawCmdArgs, Equatable {
--monitor \(_monitors) Filter results to only print the windows that are attached to specified monitors
--pid <pid> Filter results to only print windows that belong to the Application with specified <pid>
--app-id <app-id> Filter results to only print windows that belong to the Application with specified Bundle ID
--macos-hidden-app [no] Filter results to only print windows that belong to (not) hidden applications
""",
options: [
"--focused": trueBoolFlag(\.focused),
Expand All @@ -29,6 +30,7 @@ private struct RawListWindowsCmdArgs: RawCmdArgs, Equatable {
"--monitor": ArgParser(\.manual.monitors, parseMonitorIds),
"--workspace": ArgParser(\.manual.workspaces, parseWorkspaces),
"--pid": singleValueOption(\.manual.pidFilter, "<pid>", Int32.init),
"--macos-hidden-app": boolFlag(\.manual.macosHiddenApp),
"--app-id": singleValueOption(\.manual.appIdFilter, "<app-id>", { $0 })
],
arguments: []
Expand Down Expand Up @@ -68,6 +70,7 @@ public enum ListWindowsCmdArgs: CmdArgs {
public var workspaces: [WorkspaceFilter] = []
public var pidFilter: Int32?
public var appIdFilter: String?
public var macosHiddenApp: Bool?
}
}

Expand Down
6 changes: 5 additions & 1 deletion docs/aerospace-list-apps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include::util/man-attributes.adoc[]

== Synopsis
// tag::synopsis[]
list-apps [-h|--help]
list-apps [-h|--help] [--macos-hidden [no]]
// end::synopsis[]

== Description
Expand Down Expand Up @@ -42,4 +42,8 @@ include::util/conditional-options-header.adoc[]

-h, --help:: Print help

--macos-hidden [no]::
Filter results to only print hidden applications.
`[no]` inverts the condition

include::util/man-footer.adoc[]
8 changes: 6 additions & 2 deletions docs/aerospace-list-windows.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ include::util/man-attributes.adoc[]
[verse]
// tag::synopsis[]
list-windows [-h|--help] --workspace <workspace>... [--monitor <monitor>...]
[--pid <pid>] [--app-id <app-id>]
[--pid <pid>] [--app-id <app-id>] [--macos-hidden-app [no]]
list-windows [-h|--help] --monitor <monitor>... [--workspace <workspace>...]
[--pid <pid>] [--app-id <app-id>]
[--pid <pid>] [--app-id <app-id>] [--macos-hidden-app [no]]
list-windows [-h|--help] --all
list-windows [-h|--help] --focused

Expand Down Expand Up @@ -65,6 +65,10 @@ include::util/monitor-option.adoc[]
--pid <pid>:: Filter results to only print windows that belong to the Application with specified `<pid>`
--app-id <app-id>:: Filter results to only print windows that belong to the Application with specified Bundle ID

--macos-hidden-app [no]::
Filter results to only print windows that belong to hidden applications.
`[no]` inverts the condition

// end::body[]

include::util/man-footer.adoc[]
2 changes: 1 addition & 1 deletion src/command/parseCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension CmdArgs {
case .layout:
command = LayoutCommand(args: self as! LayoutCmdArgs)
case .listApps:
command = ListAppsCommand()
command = ListAppsCommand(args: self as! ListAppsCmdArgs)
case .listMonitors:
command = ListMonitorsCommand(args: self as! ListMonitorsCmdArgs)
case .listWindows:
Expand Down
8 changes: 6 additions & 2 deletions src/command/query/ListAppsCommand.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Common

struct ListAppsCommand: Command {
let args = ListAppsCmdArgs()
let args: ListAppsCmdArgs

func _run(_ state: CommandMutableState, stdin: String) -> Bool {
check(Thread.current.isMainThread)
state.stdout += apps
var result = apps
if let hidden = args.macosHidden {
result = result.filter { $0.asMacApp().nsApp.isHidden == hidden }
}
state.stdout += result
.map { app in
let pid = String(app.pid)
let appId = app.id ?? "NULL-APP-ID"
Expand Down
3 changes: 3 additions & 0 deletions src/command/query/ListWindowsCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct ListWindowsCommand: Command {
if let pid = manual.pidFilter {
windows = windows.filter { $0.app.pid == pid }
}
if let macosHiddenApp = manual.macosHiddenApp {
windows = windows.filter { $0.macAppUnsafe.nsApp.isHidden == macosHiddenApp }
}
if let appId = manual.appIdFilter {
windows = windows.filter { $0.app.id == appId }
}
Expand Down
8 changes: 8 additions & 0 deletions src/tree/AbstractApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ class AbstractApp: Hashable {
var name: String? { nil }
func detectNewWindowsAndGetAll(startup: Bool) -> [Window] { error("Not implemented") }
}

extension AbstractApp {
func asMacApp() -> MacApp { self as! MacApp }
}

extension Window {
var macAppUnsafe: MacApp { app.asMacApp() }
}

0 comments on commit a4e2be8

Please sign in to comment.