Skip to content

Conversation

@amatsuda
Copy link
Member

Summary

This patch adds support for displaying a right-aligned prompt (rprompt) similar to zsh's RPROMPT feature.

  • The rprompt is displayed at the right edge of the terminal
  • Automatically hides when the input line gets too long (prevents overlap)
  • Persists during line editing (backspace, Ctrl-U, etc.) because it's part of Reline's render cycle

Usage

Reline.rprompt = "[%H:%M]"
Reline.readline("> ")

Implementation

  • Added rprompt attribute to Reline::Core (reader + setter)
  • Pass rprompt to LineEditor in inner_readline
  • In LineEditor#render, add rprompt as a segment on the first visible line at [right_col, width, text]
  • Uses the same segment-based rendering as dialogs

Motivation

I wanted to have this feature for my app that I'm building on top of Reline, and found it difficult to implement without patching Reline itself.

Testing

Behavior of the RPROMPT feature can be confirmed with something like this.

ruby -rreline -Ilib -e 'Reline.rprompt = "[#{Dir.pwd}]"; print Reline.readline("> ")'

This adds support for displaying a right-aligned prompt (rprompt)
similar to zsh's RPROMPT feature. The rprompt is displayed at the
right edge of the terminal and automatically hides when the input
line gets too long.

Usage:
  Reline.rprompt = "[%H:%M]"
  Reline.readline("> ")

The rprompt is rendered as part of Reline's normal render cycle,
so it persists during line editing unlike workarounds using
pre_input_hook.
content_end = first_line.sum { |_, width, _| width }
# Only show rprompt if there's at least 1 char gap between content and rprompt
if right_col > content_end
first_line << [right_col, rprompt_width, @rprompt]
Copy link
Member

Choose a reason for hiding this comment

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

This part adds segment of rpromt to new_lines[idx][2]. This part is OK, but segments added by dialog is now problematic.

Can you also change the dialog segment adding part?

# index 0 is for prompt, index 1 is for line, index 2.. is for dialog
dialog_rows[index + 2] = [x_range.begin, dialog.width, dialog.contents[row - y_range.begin]]

index 2 is now used for rpromt, so dialogs should use index 3..
This is a current restriction of differential calculation assumption: same index represents logically same thing. Sorry for the inconvenience of this restriction.

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