Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --when-visible (focus|swap) option to the summon-workspace command #681

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions Sources/AppBundle/command/impl/SummonWorkspaceCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,34 @@ struct SummonWorkspaceCommand: Command {
func run(_ env: CmdEnv, _ io: CmdIo) -> Bool {
check(Thread.current.isMainThread)
let workspace = Workspace.get(byName: args.target.val.raw)
let monitor = focus.workspace.workspaceMonitor
if monitor.activeWorkspace == workspace {
let focusedMonitor = focus.workspace.workspaceMonitor

if focusedMonitor.activeWorkspace == workspace {
io.err("Workspace '\(workspace.name)' is already visible on the focused monitor. Tip: use --fail-if-noop to exit with non-zero code")
return !args.failIfNoop
}
if monitor.setActiveWorkspace(workspace) {
return workspace.focusWorkspace()

if !workspace.isVisible {
// then we just need to summon the workspace to the focused monitor
if focusedMonitor.setActiveWorkspace(workspace) {
return workspace.focusWorkspace()
} else {
return io.err("Can't move workspace '\(workspace.name)' to monitor '\(focusedMonitor.name)'. workspace-to-monitor-force-assignment doesn't allow it")
}
} else {
return io.err("Can't move workspace '\(workspace.name)' to monitor '\(monitor.name)'. workspace-to-monitor-force-assignment doesn't allow it")
let otherMonitor = workspace.workspaceMonitor
let currentWorkspace = focusedMonitor.activeWorkspace

switch args.whenVisible {
case .swap:
if otherMonitor.setActiveWorkspace(currentWorkspace) && focusedMonitor.setActiveWorkspace(workspace) {
return workspace.focusWorkspace()
} else {
return io.err("Can't swap workspaces due to monitor force assignment restrictions")
}
case .focus:
return workspace.focusWorkspace()
}
}
}
}
21 changes: 21 additions & 0 deletions Sources/Common/cmdArgs/impl/SummonWorkspaceCmdArgs.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
private let actio = "<action>"

public struct SummonWorkspaceCmdArgs: CmdArgs {
public let rawArgs: EquatableNoop<[String]>
public init(rawArgs: [String]) { self.rawArgs = .init(rawArgs) }
Expand All @@ -7,6 +9,7 @@ public struct SummonWorkspaceCmdArgs: CmdArgs {
help: summon_workspace_help_generated,
options: [
"--fail-if-noop": trueBoolFlag(\.failIfNoop),
"--when-visible": ArgParser(\.rawWhenVisibleAction, upcastArgParserFun(parseWhenVisibleAction)),
],
arguments: [newArgParser(\.target, parseWorkspaceName, mandatoryArgPlaceholder: "<workspace>")]
)
Expand All @@ -16,8 +19,26 @@ public struct SummonWorkspaceCmdArgs: CmdArgs {

public var target: Lateinit<WorkspaceName> = .uninitialized
public var failIfNoop: Bool = false
public var rawWhenVisibleAction: WhenVisible? = nil

public enum WhenVisible: String, CaseIterable, Equatable {
case focus = "focus"
case swap = "swap"
}
}

public extension SummonWorkspaceCmdArgs {
var whenVisible: WhenVisible { rawWhenVisibleAction ?? .focus }
}

private func parseWorkspaceName(arg: String, nextArgs: inout [String]) -> Parsed<WorkspaceName> {
WorkspaceName.parse(arg)
}

private func parseWhenVisibleAction(arg: String, nextArgs: inout [String]) -> Parsed<SummonWorkspaceCmdArgs.WhenVisible> {
if let arg = nextArgs.nextNonFlagOrNil() {
return parseEnum(arg, SummonWorkspaceCmdArgs.WhenVisible.self)
} else {
return .failure("\(actio) is mandatory")
}
}
2 changes: 1 addition & 1 deletion Sources/Common/cmdHelpGenerated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ let split_help_generated = """
USAGE: split [-h|--help] [--window-id <window-id>] (horizontal|vertical|opposite)
"""
let summon_workspace_help_generated = """
USAGE: summon-workspace [-h|--help] [--fail-if-noop] <workspace>
USAGE: summon-workspace [-h|--help] [--fail-if-noop] [--when-visible (focus|swap)] <workspace>
"""
let trigger_binding_help_generated = """
USAGE: trigger-binding [-h|--help] <binding> --mode <mode-id>
Expand Down
7 changes: 6 additions & 1 deletion docs/aerospace-summon-workspace.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include::util/man-attributes.adoc[]
== Synopsis
[verse]
// tag::synopsis[]
aerospace summon-workspace [-h|--help] [--fail-if-noop] <workspace>
aerospace summon-workspace [-h|--help] [--fail-if-noop] [--when-visible (focus|swap)] <workspace>

// end::synopsis[]

Expand All @@ -30,6 +30,11 @@ include::./util/conditional-options-header.adoc[]
-h, --help:: Print help
--fail-if-noop:: Exit with non-zero exit code if the workspace already visible on the focused monitor.

--when-visible <action>::
Defines the behavior if the workspace is already visible on another monitor.
`<action>` possible values: `(focus|swap)`. +
The default is: `focus`

// =========================================================== Arguments
include::./util/conditional-arguments-header.adoc[]

Expand Down
Loading