Skip to content

Conversation

@Nuru
Copy link
Contributor

@Nuru Nuru commented Feb 5, 2025

what

  • Add extra safeguards when querying terminal for color status
  • Update demo gif

why

  • Handle more unusual cases
  • Reduce duplication of code

@Nuru Nuru requested a review from a team as a code owner February 5, 2025 23:54
@coderabbitai
Copy link

coderabbitai bot commented Feb 5, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • docs/demo.gif is excluded by !**/*.gif

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The pull request introduces modifications to several terminal-related shell scripts. A new logging function _terminal_trace is added, and existing functions are revised to enhance terminal capability and color detection using tput colors. The changes improve trace logging, error handling, and implement a fallback mechanism to light mode when necessary. Additionally, the scope of a variable in the terminal folding function has been adjusted by removing the local keyword.

Changes

File(s) Change Summary
rootfs/.../_07-term-mode.sh
rootfs/.../_10-colors.sh
Added _terminal_trace for logging; updated functions (_verify_terminal_queries_are_supported, _is_term_dark_mode, _srgb_to_luminance) in _07-term-mode.sh and modified _is_color_term in _10-colors.sh to use tput colors with improved error handling.
rootfs/.../_40-preferences.sh Removed the local keyword from the cols variable in _term_fold, altering its scope while maintaining the function's logic.
demo.tape Updated instructions for running the application, replacing make all with a command that sets DOCKER_BASE_TAG to latest before executing make install.
.github/.../vhs.yaml Changed directory for storing preferences from $HOME/.geodesic to $HOME/.geodesic/defaults, with adjustments in the workflow for file handling.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the shell label Feb 5, 2025
Copy link

@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: 0

🧹 Nitpick comments (1)
rootfs/etc/profile.d/_07-term-mode.sh (1)

38-43: Robust Check for /dev/tty Read/Write Access

The added check for both writable and readable /dev/tty—along with informative trace messages—provides clear guidance when the terminal isn’t fully accessible. This helps prevent misinterpretation of terminal capabilities and informs the user how to address permission issues.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ffb16cc and 4448e20.

📒 Files selected for processing (3)
  • rootfs/etc/profile.d/_07-term-mode.sh (5 hunks)
  • rootfs/etc/profile.d/_10-colors.sh (2 hunks)
  • rootfs/etc/profile.d/_40-preferences.sh (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-push (debian)
🔇 Additional comments (11)
rootfs/etc/profile.d/_40-preferences.sh (1)

40-43: Variable Scope in _term_fold Function

The PR summary mentioned the removal of the local keyword from the cols variable declaration, which would change its scope to global. However, the current diff still shows local cols. Please verify whether this change in variable scoping is intentional. If the intention is to allow other functions or subsequent code to access cols, it may be valid—but if not, retaining local scoping could help prevent unintended side effects.

rootfs/etc/profile.d/_07-term-mode.sh (8)

20-25: Addition of _terminal_trace Logging Function

The new _terminal_trace function is well implemented. It provides early-stage logging (even when color functions aren’t yet available) by leveraging tput for ANSI color control. This enhancement will help in debugging terminal operations during startup.


28-30: Enhanced Terminal Capability Check

Introducing the use of tput colors to determine the number of supported colors (with a fallback to zero) makes the terminal capability check more robust. This change ensures that the script can gracefully handle environments where terminal color support is absent.


57-65: Fallback Output in _is_term_dark_mode Function

The early-return condition based on GEODESIC_TERM_COLOR_AUTO being set to "unsupported" or "disabled" is practical. Mapping the various flags (e.g., -b, -m) to predetermined output values ensures that the function always returns a consistent and expected result when color detection isn’t viable. Please double-check that these fallbacks integrate seamlessly with downstream logic.


77-77: Trace Logging Before OSC Query Execution

The trace message "Checking terminal color scheme..." right before issuing the OSC queries is a useful debugging aid. It clearly indicates when the script begins probing terminal colors.


84-89: Timeout Handling for OSC Queries

Adjusting the timeout duration dynamically—with a longer wait when GEODESIC_TERM_COLOR_SIGNAL is true—ensures that the script can accommodate slower terminal responses (such as when waking from sleep). The use of IFS with a custom delimiter to capture OSC responses is an effective way to parse the terminal’s reply.


102-104: Error Reporting for OSC Query Failure

The trace message issued when no response is received from the OSC queries provides clear diagnostic feedback. Defaulting to “light” mode in this scenario is historically consistent, but ensure that this fallback aligns with overall user expectations.


118-123: Review Error Message Formatting

The error output on line 121 appears to have misformatted command substitutions (e.g. using $(tput set bold) with a subsequent literal ($tput setaf 1)). Please verify that the intended ANSI escape sequences are rendered correctly. A small tweak in the formatting might be needed to ensure the message is both clear and correctly colored.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 121-121: tput is referenced but not assigned (for output from commands, use "$(tput ...)" ).

(SC2154)


182-186: Graceful Handling of Empty Input in _srgb_to_luminance

Checking for an empty $color argument and logging this with _terminal_trace is a good safeguard. This defensive programming ensures that the luminance conversion doesn’t proceed with invalid input.

rootfs/etc/profile.d/_10-colors.sh (2)

63-67: Enhanced Terminal Color Support Detection

Refactoring _is_color_term to capture the number of colors supported via tput colors improves its detection accuracy. Combining this with a check on file descriptor -t 0 ensures a better assessment of whether the terminal can display colors.


76-79: Robust Initialization of Color Defaults in _geodesic_tput_cache_init

Defining a local variable color_off and using a compound condition that retrieves the default colors via tput op—while verifying terminal query support with _verify_terminal_queries_are_supported—adds robustness to the initialization process. This ensures that if terminal color settings can’t be reliably obtained, the cache initialization will safely abort, prompting a fallback.

Copy link

@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: 0

🧹 Nitpick comments (3)
rootfs/etc/profile.d/_07-term-mode.sh (3)

69-92: Enhanced OSC query handling in _is_term_dark_mode
Within the terminal query block, the function now:

  • Leverages _verify_terminal_queries_are_supported to ensure the terminal is capable before proceeding.
  • Uses a dynamic timeout based on the value of GEODESIC_TERM_COLOR_SIGNAL (with 30 seconds for signal-wakeup scenarios and 1 second otherwise).
  • Reads OSC responses for both foreground and background colors and aggregates exit codes correctly.
    These changes improve robustness when terminals respond slowly or not at all.

Consider defaulting the value of GEODESIC_TERM_COLOR_SIGNAL (e.g.,

+GEODESIC_TERM_COLOR_SIGNAL=${GEODESIC_TERM_COLOR_SIGNAL:-false}

) to avoid potential issues if it is undefined.


96-115: Fallback and signal handler messaging in _is_term_dark_mode
The block handling conditions when the terminal does not respond is now clear and caters for both standard and signal-wakeup scenarios. The use of printf for detailed error output is appropriate. This fallback logic ensures a sensible default (light mode) is applied while informing the user about detection issues.

You may consider extracting the fallback messaging and error reporting into a helper function to reduce complexity.


178-219: Improved defensive logging and luminance conversion in _srgb_to_luminance
The update to log a warning via _terminal_trace when the function is called with an empty argument is a good defensive practice. The inner function normalize_and_linearize is used to process each hexadecimal component correctly, and the subsequent calculation of luminance is logically sound.

Be aware that the use of a nested function (i.e. normalize_and_linearize) makes this implementation Bash-specific; if portability beyond Bash is ever needed, consider extracting this to a separate helper.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4448e20 and f2f6c4d.

📒 Files selected for processing (1)
  • rootfs/etc/profile.d/_07-term-mode.sh (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-push (debian)
🔇 Additional comments (4)
rootfs/etc/profile.d/_07-term-mode.sh (4)

20-25: Introduce centralized terminal tracing via _terminal_trace
The new function is a well-isolated way to log terminal-related debug messages, ensuring that color codes are only used when available. This helps reduce code duplication and improves maintainability.


27-47: Robust terminal capability check in _verify_terminal_queries_are_supported
The modifications correctly check for terminal color support by using tput colors and testing for a TTY. Note the intentional use of direct echo (instead of _terminal_trace) in contexts where color output might not be supported. The logic to export GEODESIC_TERM_COLOR_AUTO=unsupported and later unset it (unless already "disabled") is clear and effective.


57-65: Early exit for unsupported or disabled color auto-detection in _is_term_dark_mode
The initial guard clause now correctly detects when GEODESIC_TERM_COLOR_AUTO is set to "unsupported" or "disabled" and immediately returns appropriate default outputs based on the flags. This helps avoid unnecessary terminal queries when color support is already ruled out.


118-123: Error reporting for unexpected color format
The check for an unanticipated color format using parameter expansion is effective. The error message is formatted with tput to highlight the issue. This will help capture non-standard responses from terminals for future enhancements.

Copy link

@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: 0

🧹 Nitpick comments (1)
demo.tape (1)

33-34: Consider making the installation instructions more robust.

While the instructions are clearer now, consider these improvements:

  1. Add a note about potential terminal color detection issues
  2. Consider using a specific version tag instead of 'latest' for reproducibility
-Type "# After having run `DOCKER_BASE_TAG=latest make install`," Enter
-Type "# simply run `make run` (or `geodesic`) to enter the interactive shell" Enter
+Type "# First, install Geodesic (use specific version tag for reproducibility):" Enter
+Type "# DOCKER_BASE_TAG=v1.x.x make install" Enter
+Type "# Note: If you experience terminal color issues, try running with TERM=xterm-256color" Enter
+Type "# Then, enter the interactive shell:" Enter
+Type "# make run (or geodesic)" Enter
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f2f6c4d and 162cf95.

📒 Files selected for processing (1)
  • demo.tape (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: vhs

Copy link

@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: 0

🧹 Nitpick comments (1)
.github/workflows/vhs.yaml (1)

49-50: Updated Preferences Directory Structure:
The workflow now creates a new directory at $HOME/.geodesic/defaults and writes the preferences to $HOME/.geodesic/defaults/preferences. This reorganization helps reduce code duplication and improve configuration management. Please verify that any downstream scripts or processes relying on the old path ($HOME/.geodesic/preferences) are updated to use the new location.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 162cf95 and 231c2fb.

📒 Files selected for processing (2)
  • .github/workflows/vhs.yaml (1 hunks)
  • demo.tape (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • demo.tape
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: vhs

@Nuru Nuru added the patch A minor, backward compatible change label Feb 6, 2025
@Nuru Nuru merged commit 16b16b4 into main Feb 6, 2025
2 checks passed
@Nuru Nuru deleted the color-guards branch February 6, 2025 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

github patch A minor, backward compatible change shell

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants