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

Allow specifying relative DFS index for focus command #682

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 15 additions & 2 deletions Sources/AppBundle/command/impl/FocusCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,23 @@
return io.err("Can't find window with ID \(windowId)")
}
case .dfsIndex(let dfsIndex):
if let windowToFocus = target.workspace.rootTilingContainer.allLeafWindowsRecursive.getOrNil(atIndex: Int(dfsIndex)) {
guard let window = target.windowOrNil else {
return io.err("Can't focus window by DFS index without a window")
}

let allWindows = target.workspace.rootTilingContainer.allLeafWindowsRecursive

guard let dfsAbsoluteIndex = switch dfsIndex {
case .absolute(let index): index
case .relative(let index): allWindows.firstIndex(of: window).map { $0 + index }

Check warning on line 43 in Sources/AppBundle/command/impl/FocusCommand.swift

View workflow job for this annotation

GitHub Actions / Build (macos-15)

trailing closure in this context is confusable with the body of the statement; pass as a parenthesized argument to silence this warning

Check warning on line 43 in Sources/AppBundle/command/impl/FocusCommand.swift

View workflow job for this annotation

GitHub Actions / Build (macos-15)

trailing closure in this context is confusable with the body of the statement; pass as a parenthesized argument to silence this warning
} else {
return io.err("Can't find window in allLeafWindowsRecursive")
}

if let windowToFocus = allWindows.get(wrappingIndex: dfsAbsoluteIndex) {
return windowToFocus.focusWindow()
} else {
return io.err("Can't find window with DFS index \(dfsIndex)")
return io.err("Can't find window with DFS index \(dfsAbsoluteIndex)")
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Common/cmdArgs/cmdArgsStringArrayEx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ extension [String] {
first?.starts(with: "-") == true ? nil : nextOrNil()
}

mutating func nextNonFullFlagOrNil() -> String? {
first?.starts(with: "--") == true ? nil : nextOrNil()
}

mutating func allNextNonFlagArgs() -> [String] {
var args: [String] = []
while let nextArg = nextNonFlagOrNil() {
Expand Down
29 changes: 25 additions & 4 deletions Sources/Common/cmdArgs/impl/FocusCmdArgs.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
private let boundar = "<boundary>"
private let actio = "<action>"
private let dfsinde = "<dfs index>"

public struct FocusCmdArgs: CmdArgs {
public let rawArgs: EquatableNoop<[String]>
Expand All @@ -13,14 +14,14 @@ public struct FocusCmdArgs: CmdArgs {
"--boundaries": ArgParser(\.rawBoundaries, upcastArgParserFun(parseBoundaries)),
"--boundaries-action": ArgParser(\.rawBoundariesAction, upcastArgParserFun(parseBoundariesAction)),
"--window-id": ArgParser(\.windowId, upcastArgParserFun(parseArgWithUInt32)),
"--dfs-index": ArgParser(\.dfsIndex, upcastArgParserFun(parseArgWithUInt32)),
"--dfs-index": ArgParser(\.dfsIndex, upcastArgParserFun(parseDfsIndex)),
],
arguments: [ArgParser(\.direction, upcastArgParserFun(parseCardinalDirectionArg))]
)

public var rawBoundaries: Boundaries? = nil // todo cover boundaries wrapping with tests
public var rawBoundariesAction: WhenBoundariesCrossed? = nil
public var dfsIndex: UInt32? = nil
public var dfsIndex: DfsIndex? = nil
public var direction: CardinalDirection? = nil
public var floatingAsTiling: Bool = true
public var windowId: UInt32?
Expand All @@ -36,7 +37,7 @@ public struct FocusCmdArgs: CmdArgs {
self.windowId = windowId
}

public init(rawArgs: [String], dfsIndex: UInt32) {
public init(rawArgs: [String], dfsIndex: DfsIndex) {
self.rawArgs = .init(rawArgs)
self.dfsIndex = dfsIndex
}
Expand All @@ -55,7 +56,7 @@ public struct FocusCmdArgs: CmdArgs {
public enum FocusCmdTarget {
case direction(CardinalDirection)
case windowId(UInt32)
case dfsIndex(UInt32)
case dfsIndex(DfsIndex)
}

public extension FocusCmdArgs {
Expand Down Expand Up @@ -109,3 +110,23 @@ private func parseBoundaries(arg: String, nextArgs: inout [String]) -> Parsed<Fo
return .failure("\(boundar) is mandatory")
}
}

private func parseDfsIndex(arg: String, nextArgs: inout [String]) -> Parsed<DfsIndex> {
guard let arg = nextArgs.nextNonFullFlagOrNil() else {
return .failure("\(dfsinde) is mandatory")
}

// Handle relative indices with + or - prefix
if arg.hasPrefix("+") || arg.hasPrefix("-") {
if let value = Int(arg) {
return .success(.relative(value))
}
return .failure("Invalid relative DFS index format")
}

// Handle absolute indices (no prefix)
if let value = Int(arg) {
return .success(.absolute(value))
}
return .failure("Invalid DFS index format")
}
4 changes: 4 additions & 0 deletions Sources/Common/model/dfsIndex.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum DfsIndex: Equatable {
case absolute(Int)
case relative(Int)
}
Loading