Skip to content

Commit 5e5b932

Browse files
MikaelCarpenternikitabobko
authored andcommitted
feat: allow new list-modes commands
closes #1525 _fixes #1026
1 parent 7f1bf12 commit 5e5b932

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

Sources/AppBundle/command/impl/ListModesCommand.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ struct ListModesCommand: Command {
66
/*conforms*/ var shouldResetClosedWindowsCache = false
77

88
func run(_ env: CmdEnv, _ io: CmdIo) -> Bool {
9-
if args.current {
10-
return io.out(activeMode ?? mainModeId)
11-
} else {
12-
let modeNames: [String] = config.modes.map { $0.key }
13-
return io.out(modeNames)
9+
let modes: [String] = args.current ? [activeMode ?? mainModeId] : config.modes.keys.sorted()
10+
return switch () {
11+
case _ where args.outputOnlyCount:
12+
io.out("\(modes.count)")
13+
case _ where args.json:
14+
JSONEncoder.aeroSpaceDefault.encodeToString(modes.map { ["mode-id": $0] }).map(io.out)
15+
?? io.err("Failed to encode JSON")
16+
default:
17+
io.out(modes)
1418
}
1519
}
1620
}

Sources/AppBundleTests/command/ListModesTest.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,55 @@ final class ListModesTest: XCTestCase {
66
func testParseListModesCommand() {
77
testParseCommandSucc("list-modes", ListModesCmdArgs(rawArgs: []))
88
testParseCommandSucc("list-modes --current", ListModesCmdArgs(rawArgs: []).copy(\.current, true))
9+
testParseCommandSucc("list-modes --json", ListModesCmdArgs(rawArgs: []).copy(\.json, true))
10+
testParseCommandSucc("list-modes --count", ListModesCmdArgs(rawArgs: []).copy(\.outputOnlyCount, true))
11+
testParseCommandSucc("list-modes --current --json", ListModesCmdArgs(rawArgs: []).copy(\.current, true).copy(\.json, true))
12+
}
13+
14+
func testParseListModesCommandConflicts() {
15+
assertEquals(parseCommand("list-modes --json --count").errorOrNil, "ERROR: Conflicting options: --count, --json")
16+
assertEquals(parseCommand("list-modes --current --count").errorOrNil, "ERROR: Conflicting options: --count, --current")
17+
}
18+
19+
@MainActor
20+
func testListModesOutput() async throws {
21+
config.modes = [
22+
"main": Mode(name: nil, bindings: [:]),
23+
"service": Mode(name: nil, bindings: [:]),
24+
"resize": Mode(name: nil, bindings: [:]),
25+
]
26+
27+
let defaultResult = try await ListModesCommand(args: ListModesCmdArgs(rawArgs: [])).run(.defaultEnv, .emptyStdin)
28+
assertEquals(defaultResult.exitCode, 0)
29+
assertEquals(defaultResult.stdout, ["main", "resize", "service"])
30+
assertEquals(defaultResult.stderr, [])
31+
32+
let currentResult = try await ListModesCommand(args: ListModesCmdArgs(rawArgs: []).copy(\.current, true)).run(.defaultEnv, .emptyStdin)
33+
assertEquals(currentResult.exitCode, 0)
34+
assertEquals(currentResult.stdout, ["main"])
35+
assertEquals(currentResult.stderr, [])
36+
37+
let countResult = try await ListModesCommand(args: ListModesCmdArgs(rawArgs: []).copy(\.outputOnlyCount, true)).run(.defaultEnv, .emptyStdin)
38+
assertEquals(countResult.exitCode, 0)
39+
assertEquals(countResult.stdout, ["3"])
40+
assertEquals(countResult.stderr, [])
41+
42+
let jsonResult = try await ListModesCommand(args: ListModesCmdArgs(rawArgs: []).copy(\.json, true)).run(.defaultEnv, .emptyStdin)
43+
let expectedJson = JSONEncoder.aeroSpaceDefault.encodeToString([
44+
["mode-id": "main"],
45+
["mode-id": "resize"],
46+
["mode-id": "service"],
47+
])
48+
assertEquals(jsonResult.exitCode, 0)
49+
assertEquals(jsonResult.stdout, [expectedJson])
50+
assertEquals(jsonResult.stderr, [])
51+
52+
let currentJsonResult = try await ListModesCommand(args: ListModesCmdArgs(rawArgs: []).copy(\.current, true).copy(\.json, true)).run(.defaultEnv, .emptyStdin)
53+
let expectedCurrentJson = JSONEncoder.aeroSpaceDefault.encodeToString([
54+
["mode-id": "main"],
55+
])
56+
assertEquals(currentJsonResult.exitCode, 0)
57+
assertEquals(currentJsonResult.stdout, [expectedCurrentJson])
58+
assertEquals(currentJsonResult.stderr, [])
959
}
1060
}

Sources/Common/cmdArgs/impl/ListModesCmdArgs.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ public struct ListModesCmdArgs: CmdArgs {
88
allowInConfig: false,
99
help: list_modes_help_generated,
1010
options: [
11+
"--count": trueBoolFlag(\.outputOnlyCount),
1112
"--current": trueBoolFlag(\.current),
13+
"--json": trueBoolFlag(\.json),
1214
],
1315
arguments: [],
16+
conflictingOptions: [
17+
["--count", "--current"],
18+
["--count", "--json"],
19+
],
1420
)
1521

1622
/*conforms*/ public var windowId: UInt32?
1723
/*conforms*/ public var workspaceName: WorkspaceName?
1824
public var current: Bool = false
25+
public var json: Bool = false
26+
public var outputOnlyCount: Bool = false
1927
}
2028

2129
public func parseListModesCmdArgs(_ args: [String]) -> ParsedCmd<ListModesCmdArgs> {

Sources/Common/cmdHelpGenerated.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let list_exec_env_vars_help_generated = """
6767
USAGE: list-exec-env-vars [-h|--help]
6868
"""
6969
let list_modes_help_generated = """
70-
USAGE: list-modes [-h|--help] [--current]
70+
USAGE: list-modes [-h|--help] [--current] [--count] [--json]
7171
"""
7272
let list_monitors_help_generated = """
7373
USAGE: list-monitors [-h|--help] [--focused [no]] [--mouse [no]] [--format <output-format>] [--count] [--json]

docs/aerospace-list-modes.adoc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include::util/man-attributes.adoc[]
99
== Synopsis
1010
[verse]
1111
// tag::synopsis[]
12-
aerospace list-modes [-h|--help] [--current]
12+
aerospace list-modes [-h|--help] [--current] [--count] [--json]
1313

1414
// end::synopsis[]
1515

@@ -26,8 +26,14 @@ include::util/conditional-options-header.adoc[]
2626

2727
-h, --help:: Print help
2828

29-
--current::
30-
Only print the currently active mode
29+
--current:: Only print the currently active mode.
30+
Incompatible with `--count`
31+
32+
--count:: Output only the number of modes.
33+
Incompatible with `--current`, `--json`
34+
35+
--json:: Output in JSON format.
36+
Incompatible with `--count`
3137

3238
// end::body[]
3339

grammar/commands-bnf-grammar.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ aerospace -h;
9191

9292
| list-exec-env-vars
9393

94-
| list-modes [--current]
94+
| list-modes [--current|--json|--count]
9595

9696
| list-monitors [--focused [no] | --mouse [no] | --format <output_format> | --count | --json]...
9797

0 commit comments

Comments
 (0)