Skip to content

Add --json and --count flag to list-modes command #1525

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

Closed

Conversation

MikaelCarpenter
Copy link
Contributor

I've tried to implement the --json command as specified in the issue. I was looking at list-apps as a reference and decided to add --count as well, because it seemed simple enough.

Happy to make any adjustments necessary, just let me know!

🧪 Testing Outputs

❯ ./.debug/aerospace list-modes
main
service

❯ ./.debug/aerospace list-modes --current
main

❯ ./.debug/aerospace list-modes --json
[
  {
    "mode-id" : "main"
  },
  {
    "mode-id" : "service"
  }
]

❯ ./.debug/aerospace list-modes --count
2

❯ ./.debug/aerospace list-modes --current --json
[
  {
    "mode-id" : "main"
  }
]

❯ ./.debug/aerospace list-modes --count --json
ERROR: Conflicting options: --count, --json

❯ ./.debug/aerospace list-modes --count --current
ERROR: Conflicting options: --count, --current

@nikitabobko
Copy link
Owner

The PR is functional, thanks.

Merged with the following cosmetic + shell completion changes from me:

diff --git a/Sources/AppBundle/command/impl/ListModesCommand.swift b/Sources/AppBundle/command/impl/ListModesCommand.swift
index 74f1ebcf..c154aea0 100644
--- a/Sources/AppBundle/command/impl/ListModesCommand.swift
+++ b/Sources/AppBundle/command/impl/ListModesCommand.swift
@@ -6,23 +6,15 @@ struct ListModesCommand: Command {
     /*conforms*/ var shouldResetClosedWindowsCache = false
 
     func run(_ env: CmdEnv, _ io: CmdIo) -> Bool {
-        if args.outputOnlyCount {
-            let count = args.current ? 1 : config.modes.count
-            return io.out("\(count)")
-        }
-
-        if args.json {
-            let modeNames = args.current ? [activeMode ?? mainModeId] : config.modes.keys.sorted()
-            let modeData = modeNames.map { ["mode-id": $0] }
-            return JSONEncoder.aeroSpaceDefault.encodeToString(modeData).map(io.out)
-                ?? io.err("Failed to encode JSON")
-        }
-
-        if args.current {
-            return io.out(activeMode ?? mainModeId)
-        } else {
-            let modeNames: [String] = config.modes.keys.sorted()
-            return io.out(modeNames)
+        let modes: [String] = args.current ? [activeMode ?? mainModeId] : config.modes.keys.sorted()
+        return switch () {
+            case _ where args.outputOnlyCount:
+                io.out("\(modes.count)")
+            case _ where args.json:
+                JSONEncoder.aeroSpaceDefault.encodeToString(modes.map { ["mode-id": $0] }).map(io.out)
+                    ?? io.err("Failed to encode JSON")
+            default:
+                io.out(modes)
         }
     }
 }
diff --git a/grammar/commands-bnf-grammar.txt b/grammar/commands-bnf-grammar.txt
index 31ee5963..5cb61dcd 100644
--- a/grammar/commands-bnf-grammar.txt
+++ b/grammar/commands-bnf-grammar.txt
@@ -91,7 +91,7 @@ aerospace -h;
 
     | list-exec-env-vars
 
-    | list-modes [--current]
+    | list-modes [--current|--json|--count]
 
     | list-monitors [--focused [no] | --mouse [no] | --format <output_format> | --count | --json]...
 

@nikitabobko nikitabobko added the pr-merged Pull Request is merged label Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-merged Pull Request is merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants