Skip to content

Feature request: Bash widget support #82

@noahgift

Description

@noahgift

Feature Request

Add a bash-widget command to generate readline integration for Bash users, similar to the existing zsh-widget.

Motivation

Currently only ZSH is supported. Bash remains widely used, especially on servers and in CI environments.

Proposed Implementation

Bash doesn't have ZSH's POSTDISPLAY variable. Instead, use readline's bind -x to hook keystrokes and manipulate READLINE_LINE/READLINE_POINT.

Suggested Widget Code

# aprender-shell Bash widget
# Add this to your ~/.bashrc

_aprender_suggest() {
    local suggestion
    suggestion=$(aprender-shell suggest "$READLINE_LINE" 2>/dev/null | head -1 | cut -f1)
    if [[ -n "$suggestion" && "$suggestion" != "$READLINE_LINE" ]]; then
        local completion="${suggestion#$READLINE_LINE}"
        # Store for accept function
        _APRENDER_SUGGESTION="$completion"
        # Display ghost text (gray)
        echo -ne "\001\e[90m\002${completion}\001\e[0m\002"
    else
        _APRENDER_SUGGESTION=""
    fi
}

_aprender_accept() {
    if [[ -n "$_APRENDER_SUGGESTION" ]]; then
        READLINE_LINE="${READLINE_LINE}${_APRENDER_SUGGESTION}"
        READLINE_POINT=${#READLINE_LINE}
        _APRENDER_SUGGESTION=""
    fi
}

# Bind to keystrokes
bind -x '"\C-i": _aprender_accept'  # Tab to accept

Challenges

  1. No true ghost-text: Bash readline doesn't support overlay text like ZSH's POSTDISPLAY. Options:

    • Print suggestion after cursor, clear on next keystroke (janky)
    • Use ble.sh integration for richer readline features
    • Implement as completion menu via complete -F
  2. Performance: bind -x runs on every keystroke - need fast suggestion latency

  3. Escape code wrapping: Must use \001...\002 around ANSI codes in readline context

Alternatives Considered

  • ble.sh integration: More powerful but adds dependency
  • Completion function only: complete -C "aprender-shell suggest" <cmd> - simpler but less seamless

CLI Addition

Commands:
  ...
  bash-widget   Generate Bash widget code
  ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions