- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite. Split CommandMutableState to more granular CmdIo, CmdEnv and…
… CmdResult This commit lays out the foundation for the following issues - #186 - #278 - #20 Also the commit changes how focused window is tracked throught the chain of executed commands. - CommandMutableState was a mutable state that tracked the focused window, and the track had to be updated throught the chain of executed commands (for example, take a look at the `focus` command) - CmdEnv is simplier. It just forces a particular window to be percieved as "focused". CmdEnv approach makes things easier to understand and describe in the docs (which I'm going to do later, CmdEnv will be exposed as AEROSPACE_FOCUSED_WORKSPACE and AEROSPACE_FOCUSED_WINDOW_ID environment variables) Unlike CommandMutableState, CmdEnv approach disallows to change focused window in on-window-detected, on-focus-changed, and other callbacks. Which I think is not an issue at all. It maybe even considered a safety mechanism. If a user uses `close` in one of the mentioned callbacks, previously, a random window could become focused and it would receive all the following commands. Now, all the commands that go after `close` will fail with "Invalid <window-id> \(windowId) specified in AEROSPACE_FOCUSED_WINDOW_ID env variable" - This commit is not a breaking change for on-window-detected thanks to limitations in #20 - But this commit is technically a breaking change for other mentioned callbacks, since no limitations were impoosed on those callbacks. But I don't believe that anyone in sane mind relied on it. And the docs are explicit that changing focus in those callbacks is a bad idea: > Changing the focus within these callbacks is a bad idea anyway, and the way it’s handled will probably change in future versions. Currently, the "force focused state" in CmdEnv is immutable, and I hope it will stay so. But hypothetically, it can be mutable in future versions if we decide that the embedded language #278 should allow chaning environment variables.
- Loading branch information
1 parent
5d58c02
commit 61594ee
Showing
53 changed files
with
423 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Common | ||
|
||
struct CmdEnv: Copyable { // todo forward env from cli to server | ||
var windowId: UInt32? | ||
var workspaceName: String? | ||
var pwd: String? | ||
|
||
static var defaultEnv: CmdEnv { CmdEnv(windowId: nil, workspaceName: nil, pwd: nil) } | ||
public init( | ||
windowId: UInt32?, | ||
workspaceName: String?, | ||
pwd: String? | ||
) { | ||
self.windowId = windowId | ||
self.workspaceName = workspaceName | ||
self.pwd = pwd | ||
} | ||
|
||
func withFocus(_ focus: LiveFocus) -> CmdEnv { | ||
switch focus.asLeaf { | ||
case .window(let wd): .defaultEnv.copy(\.windowId, wd.windowId) | ||
case .emptyWorkspace(let ws): .defaultEnv.copy(\.workspaceName, ws.name) | ||
} | ||
} | ||
|
||
var asMap: [String: String] { | ||
var result = config.execConfig.envVariables | ||
if let pwd { | ||
result["PWD"] = pwd | ||
} | ||
if let windowId { | ||
result[AEROSPACE_FOCUSED_WINDOW_ID] = windowId.description | ||
} | ||
if let workspaceName { | ||
result[AEROSPACE_FOCUSED_WORKSPACE] = workspaceName.description | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class CmdStdin { | ||
private var input: String = "" | ||
init(_ input: String) { | ||
self.input = input | ||
} | ||
static var emptyStdin: CmdStdin { .init("") } | ||
|
||
func readAll() -> String { | ||
let result = input | ||
input = "" | ||
return result | ||
} | ||
} | ||
|
||
class CmdIo { | ||
private var stdin: CmdStdin | ||
var stdout: [String] = [] | ||
var stderr: [String] = [] | ||
|
||
init(stdin: CmdStdin) { self.stdin = stdin } | ||
|
||
@discardableResult func out(_ msg: String) -> Bool { stdout.append(msg); return true } | ||
@discardableResult func err(_ msg: String) -> Bool { stderr.append(msg); return false } | ||
@discardableResult func out(_ msg: [String]) -> Bool { stdout += msg; return true } | ||
@discardableResult func err(_ msg: [String]) -> Bool { stderr += msg; return false } | ||
|
||
func readStdin() -> String { stdin.readAll() } | ||
} | ||
|
||
struct CmdResult { | ||
let stdout: [String] | ||
let stderr: [String] | ||
let exitCode: Int32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Sources/AppBundle/command/cmdResolveFocusOrReportError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Common | ||
|
||
extension CmdArgs { | ||
var workspace: Workspace? { | ||
if let workspaceName { Workspace.get(byName: workspaceName) } else { nil } | ||
} | ||
|
||
func resolveFocusOrReportError(_ env: CmdEnv, _ io: CmdIo) -> LiveFocus? { | ||
// Flags | ||
if let windowId { | ||
if let wi = MacWindow.allWindowsMap[windowId] { | ||
return wi.toLiveFocusOrReportError(io) | ||
} else { | ||
io.err("Invalid <window-id> \(windowId) passed to --window-id") | ||
return nil | ||
} | ||
} | ||
if let workspace { | ||
return workspace.toLiveFocus() | ||
} | ||
// Env | ||
if let windowId = env.windowId { | ||
if let wi = MacWindow.allWindowsMap[windowId] { | ||
return wi.toLiveFocusOrReportError(io) | ||
} else { | ||
io.err("Invalid <window-id> \(windowId) specified in \(AEROSPACE_FOCUSED_WINDOW_ID) env variable") | ||
return nil | ||
} | ||
} | ||
if let wsName = env.workspaceName { | ||
return Workspace.get(byName: wsName).toLiveFocus() | ||
} | ||
// Real Focus | ||
return focus | ||
} | ||
} | ||
|
||
extension Window { | ||
func toLiveFocusOrReportError(_ io: CmdIo) -> LiveFocus? { | ||
if let result = toLiveFocusOrNil() { | ||
return result | ||
} else { | ||
io.err("Window \(windowId) doesn't belong to any monitor. And thus can't even define a focused workspace") | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.