Skip to content

Commit 79c75d2

Browse files
committed
Add support for --json flag to format output as json to list-modes command
Implemented this for #1026
1 parent a8cd536 commit 79c75d2

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Sources/AppBundle/command/impl/ListModesCommand.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ struct ListModesCommand: Command {
77
func run(_ env: CmdEnv, _ io: CmdIo) -> Bool {
88
if args.current {
99
return io.out(activeMode ?? mainModeId)
10+
} else if args.json {
11+
let modeNames: [String] = config.modes.map { $0.key }
12+
return switch JSONEncoder.aeroSpaceDefault.encodeToString(modeNames).map(Result.success)
13+
?? .failure("Can't encode '\(modeNames)' to JSON")
14+
{
15+
case .success(let json): io.out(json)
16+
case .failure(let msg): io.err(msg)
17+
}
1018
} else {
1119
let modeNames: [String] = config.modes.map { $0.key }
1220
return io.out(modeNames)

Sources/AppBundleTests/command/ListModesTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ 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))
910
}
1011
}

Sources/Common/cmdArgs/impl/ListModesCmdArgs.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ public struct ListModesCmdArgs: CmdArgs {
99
help: list_modes_help_generated,
1010
options: [
1111
"--current": trueBoolFlag(\.current),
12+
"--json": trueBoolFlag(\.json),
1213
],
1314
arguments: []
1415
)
1516

1617
public var windowId: UInt32? // unused
1718
public var workspaceName: WorkspaceName? // unused
1819
public var current: Bool = false
20+
public var json: Bool = false
1921
}
2022

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

0 commit comments

Comments
 (0)