Skip to content

add terminal display to ease parsing for command line#93

Open
pantelm wants to merge 1 commit intoopenhue:mainfrom
pantelm:terminal-display
Open

add terminal display to ease parsing for command line#93
pantelm wants to merge 1 commit intoopenhue:mainfrom
pantelm:terminal-display

Conversation

@pantelm
Copy link
Contributor

@pantelm pantelm commented Nov 16, 2025

Print without header and blank line to ease parsing with cli tools

Summary by CodeRabbit

  • New Features
    • Added --terminal (short -c) flag to the get command, providing a terminal-friendly output format option alongside the existing JSON format for displaying light information.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 16, 2025

Walkthrough

The changes introduce a --terminal flag to the get command for alternative output formatting. A new generic utility function PrintTerminal is added to enable terminal-based output, and the light command implementation conditionally routes non-JSON output through this terminal formatter when the flag is active.

Changes

Cohort / File(s) Summary
Command Configuration
cmd/get/get.go
Added Terminal bool field to CmdGetOptions struct and wired a new --terminal (-c) command-line flag to control terminal-based output formatting.
Command Implementation
cmd/get/get_light.go
Added conditional logic to route non-JSON output to PrintLight terminal formatter when Terminal flag is set, replacing the previous unconditional table format behavior for this code path.
Output Utilities
util/utils.go
Added new generic function PrintTerminal[T any] that accepts an I/O stream, a table slice, and a line formatter function to print each element without table formatting.

Sequence Diagram

sequenceDiagram
    participant User
    participant get_light
    participant utils
    participant IO
    
    User->>get_light: RunGetLightCmd(--terminal)
    
    rect rgb(200, 220, 255)
    Note over get_light: JSON format check
    get_light->>get_light: o.JSON == true?
    end
    
    alt JSON output
        get_light->>IO: PrintJSON()
    else Terminal output (NEW)
        rect rgb(220, 255, 220)
        get_light->>utils: PrintTerminal()
        utils->>IO: Println() per light
        end
    else Table output (default)
        get_light->>IO: PrintTable()
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Focus on the conditional routing logic in get_light.go to ensure the Terminal flag correctly gates the formatter selection
  • Verify the new PrintTerminal generic function handles edge cases (empty slices, nil streams)
  • Confirm the --terminal flag registration in get.go uses the correct option name and doesn't conflict with existing flags

Poem

🐰 A terminal flag hops into view,
New formatting paths for a colorful brew,
PrintTerminal springs with generic delight,
Output now routes to the terminal light! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'add terminal display to ease parsing for command line' accurately reflects the main change: adding a new terminal display mode with a new --terminal flag and PrintTerminal utility function to enable simpler CLI output parsing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 262260d and ccc231d.

📒 Files selected for processing (3)
  • cmd/get/get.go (2 hunks)
  • cmd/get/get_light.go (1 hunks)
  • util/utils.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
util/utils.go (1)
openhue/io.go (1)
  • IOStreams (21-29)
cmd/get/get_light.go (1)
util/utils.go (1)
  • PrintTerminal (48-52)
🔇 Additional comments (3)
cmd/get/get.go (1)

11-12: LGTM!

The Terminal field addition follows the same pattern as the existing Json field and integrates cleanly into the options struct.

util/utils.go (1)

48-52: LGTM!

The PrintTerminal function implementation is clean and minimal, correctly achieving the PR goal of printing output without headers or blank lines. The generic signature allows reuse across different data types.

cmd/get/get_light.go (1)

67-68: Verify that tab-separated format aligns with your actual use case needs.

The code review comment is factually accurate: PrintLight does use tab-separated values (confirmed at lines 88-93). The --terminal flag correctly passes this function to util.PrintTerminal, which prints each light as a tab-delimited string.

However, the design question raised is valid: tab-separated values are a standard CLI format and your implementation already achieves the stated goal by removing headers and blank lines. Whether tab-separated output is the most appropriate choice depends on your actual usage patterns. If you expect users to pipe this to other tools, TSV is reasonable; if you need maximum simplicity, other formats exist (JSON, newline-separated fields, etc.).

Review the actual usage patterns to confirm tab-separated output meets your users' needs for CLI tool integration.

Comment on lines 37 to +38
cmd.PersistentFlags().BoolVarP(&o.Json, "json", "j", false, "Format output as JSON")
cmd.PersistentFlags().BoolVarP(&o.Terminal, "terminal", "c", false, "Format from Terminal use")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

Verify behavior when both --json and --terminal flags are set.

There's no mutual exclusion check between the --json and --terminal flags. Users could potentially set both flags simultaneously, which may lead to unclear behavior (the first condition wins, so JSON would be used).

Consider adding validation to ensure these flags are mutually exclusive. Run the following script to check if there are any existing patterns for flag mutual exclusion in the codebase:


🏁 Script executed:

#!/bin/bash
# Search for mutual exclusion patterns between flags
rg -nP --type=go -C3 'MarkFlagsMutuallyExclusive|cobra\.ExactArgs|OnlyOneOf'

Length of output: 1328


I need to examine the full context of cmd/get/get.go to verify whether the mutual exclusion validation is indeed missing.

</parameter>
</invoke>

---

🏁 Script executed:

```shell
#!/bin/bash
# View the full get.go file to see if mutual exclusion is set anywhere
cat cmd/get/get.go

Length of output: 1809


🏁 Script executed:

#!/bin/bash
# Search for any validation logic or usage of Json and Terminal flags in get.go
rg -nP --type=go -C5 'o\.Json|o\.Terminal' cmd/get/get.go

Length of output: 806


🏁 Script executed:

#!/bin/bash
# Check if MarkFlagsMutuallyExclusive is used anywhere in get.go
rg -n 'MarkFlagsMutuallyExclusive' cmd/get/get.go

Length of output: 0


Now let me verify if the o.Terminal flag is actually used in the sub-commands:


🏁 Script executed:

#!/bin/bash
# Search for Terminal usage in all get-related command files
rg -n 'o\.Terminal' cmd/get/

Length of output: 162


Let me examine the usage context in get_light.go:


🏁 Script executed:

#!/bin/bash
# View the context around line 67 in get_light.go to see the flag usage logic
sed -n '55,75p' cmd/get/get_light.go

Length of output: 526


Add mutual exclusion validation for --json and --terminal flags

The concern is valid. The if/else-if/else logic in cmd/get/get_light.go:60-67 confirms that when both flags are set, JSON format takes precedence and Terminal formatting is ignored. The codebase already establishes the pattern for this via MarkFlagsMutuallyExclusive in cmd/set/common.go.

Add after line 38 in cmd/get/get.go:

cmd.MarkFlagsMutuallyExclusive("json", "terminal")
🤖 Prompt for AI Agents
In cmd/get/get.go around lines 37-38, add a mutual exclusion declaration for the
--json and --terminal flags by calling cmd.MarkFlagsMutuallyExclusive("json",
"terminal") immediately after those flag definitions; follow the existing
project pattern for handling the returned error (check the error and
handle/log/propagate it the same way as in cmd/set/common.go) so the CLI will
surface a clear validation error when both flags are provided.


// persistence flags
cmd.PersistentFlags().BoolVarP(&o.Json, "json", "j", false, "Format output as JSON")
cmd.PersistentFlags().BoolVarP(&o.Terminal, "terminal", "c", false, "Format from Terminal use")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix grammatical error in flag help text.

The help text "Format from Terminal use" is grammatically incorrect.

Apply this diff:

-	cmd.PersistentFlags().BoolVarP(&o.Terminal, "terminal", "c", false, "Format from Terminal use")
+	cmd.PersistentFlags().BoolVarP(&o.Terminal, "terminal", "c", false, "Format for terminal use")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cmd.PersistentFlags().BoolVarP(&o.Terminal, "terminal", "c", false, "Format from Terminal use")
cmd.PersistentFlags().BoolVarP(&o.Terminal, "terminal", "c", false, "Format for terminal use")
🤖 Prompt for AI Agents
In cmd/get/get.go around line 38, the flag help text "Format from Terminal use"
is grammatically incorrect; update the BoolVarP help string to a correct phrase
such as "Format for terminal use" or "Format output for terminal use" so the
flag description is clear and idiomatic.

@thibauult
Copy link
Contributor

Salut @pantelm - I was wondering if using the --json flag + jq would help you solve this problem?

As example, I usually use:

openhue get lights --json | jq '.[].Name'

to list all my lights

@pantelm
Copy link
Contributor Author

pantelm commented Dec 31, 2025

Salut @thibauult , I'll give it a try, I'm not really familiar with jq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants