Repository navigation
Expand file tree
/
Copy pathRightSidebarRemoteCommand.swift
More file actions
164 lines (152 loc) · 7.61 KB
/
Copy pathRightSidebarRemoteCommand.swift
File metadata and controls
164 lines (152 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import Foundation
nonisolated struct RightSidebarRemoteTarget: Equatable, Sendable {
var windowId: UUID? = nil
var workspaceId: UUID? = nil
var isActiveTarget: Bool {
windowId == nil && workspaceId == nil
}
}
nonisolated enum RightSidebarRemoteCommand: Equatable, Sendable {
case toggle
case show
case hide
case focus
case setMode(RightSidebarMode, focus: Bool)
case getState
}
nonisolated struct RightSidebarRemoteRequest: Equatable, Sendable {
let command: RightSidebarRemoteCommand
let target: RightSidebarRemoteTarget
}
nonisolated struct RightSidebarRemoteParseError: Error, Equatable, Sendable {
let message: String
}
nonisolated struct RightSidebarRemoteState: Equatable, Sendable {
let visible: Bool
let mode: RightSidebarMode
}
nonisolated enum RightSidebarRemoteApplyResult: Equatable, Sendable {
case ok
case state(RightSidebarRemoteState)
case failure(String)
}
extension RightSidebarRemoteRequest {
static func parse(tokens: [String]) -> Result<RightSidebarRemoteRequest, RightSidebarRemoteParseError> {
var positional: [String] = []
var target = RightSidebarRemoteTarget()
var noFocus = false
var index = 0
while index < tokens.count {
let token = tokens[index]
if token == "--no-focus" {
noFocus = true
index += 1
continue
}
if token == "--workspace" || token == "--tab" || token == "--window" {
guard index + 1 < tokens.count else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.optionRequiresID", defaultValue: "ERROR: \(token) requires an id")))
}
let value = tokens[index + 1]
if let error = parseTargetOption(name: String(token.dropFirst(2)), value: value, target: &target) {
return .failure(error)
}
index += 2
continue
}
if token.hasPrefix("--workspace=") {
let value = String(token.dropFirst("--workspace=".count))
if let error = parseTargetOption(name: "workspace", value: value, target: &target) {
return .failure(error)
}
index += 1
continue
}
if token.hasPrefix("--tab=") {
let value = String(token.dropFirst("--tab=".count))
if let error = parseTargetOption(name: "tab", value: value, target: &target) {
return .failure(error)
}
index += 1
continue
}
if token.hasPrefix("--window=") {
let value = String(token.dropFirst("--window=".count))
if let error = parseTargetOption(name: "window", value: value, target: &target) {
return .failure(error)
}
index += 1
continue
}
if token.hasPrefix("--") {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.unknownOption", defaultValue: "ERROR: Unknown right sidebar option '\(token)'")))
}
positional.append(token)
index += 1
}
guard let action = positional.first?.lowercased() else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.usage", defaultValue: "ERROR: Usage: right_sidebar <toggle|show|hide|focus|set|mode> [mode] [--workspace=<workspace-id>] [--window=<window-id>] [--no-focus]")))
}
switch action {
case "toggle":
guard positional.count == 1, !noFocus else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.usage.toggle", defaultValue: "ERROR: Usage: right_sidebar toggle [--workspace=<workspace-id>] [--window=<window-id>]")))
}
return .success(.init(command: .toggle, target: target))
case "show":
guard positional.count == 1, !noFocus else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.usage.show", defaultValue: "ERROR: Usage: right_sidebar show [--workspace=<workspace-id>] [--window=<window-id>]")))
}
return .success(.init(command: .show, target: target))
case "hide":
guard positional.count == 1, !noFocus else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.usage.hide", defaultValue: "ERROR: Usage: right_sidebar hide [--workspace=<workspace-id>] [--window=<window-id>]")))
}
return .success(.init(command: .hide, target: target))
case "focus":
guard positional.count == 1, !noFocus else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.usage.focus", defaultValue: "ERROR: Usage: right_sidebar focus [--workspace=<workspace-id>] [--window=<window-id>]")))
}
return .success(.init(command: .focus, target: target))
case "mode", "state":
guard positional.count == 1, !noFocus else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.usage.mode", defaultValue: "ERROR: Usage: right_sidebar mode [--workspace=<workspace-id>] [--window=<window-id>]")))
}
return .success(.init(command: .getState, target: target))
case "set":
guard positional.count == 2 else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.usage.set", defaultValue: "ERROR: Usage: right_sidebar set <files|find|vault|sessions|feed|dock> [--no-focus] [--workspace=<workspace-id>] [--window=<window-id>]")))
}
guard let mode = RightSidebarMode.from(cliArgument: positional[1]) else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.unknownMode", defaultValue: "ERROR: Unknown right sidebar mode '\(positional[1])'")))
}
return .success(.init(command: .setMode(mode, focus: !noFocus), target: target))
default:
guard !noFocus else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.noFocusOnlySet", defaultValue: "ERROR: --no-focus is only valid with right_sidebar set")))
}
guard positional.count == 1, let mode = RightSidebarMode.from(cliArgument: action) else {
return .failure(.init(message: String(localized: "rightSidebar.remote.error.unknownCommand", defaultValue: "ERROR: Unknown right sidebar command '\(action)'")))
}
return .success(.init(command: .setMode(mode, focus: true), target: target))
}
}
private static func parseTargetOption(
name: String,
value: String,
target: inout RightSidebarRemoteTarget
) -> RightSidebarRemoteParseError? {
guard let uuid = UUID(uuidString: value.trimmingCharacters(in: .whitespacesAndNewlines)) else {
return .init(message: String(localized: "rightSidebar.remote.error.invalidTargetID", defaultValue: "ERROR: Invalid right sidebar --\(name) id '\(value)'"))
}
switch name {
case "window":
target.windowId = uuid
case "workspace", "tab":
target.workspaceId = uuid
default:
return .init(message: String(localized: "rightSidebar.remote.error.unknownTargetOption", defaultValue: "ERROR: Unknown right sidebar target option '\(name)'"))
}
return nil
}
}