fix(help): keep the shortcuts panel inside its dock

The action column of the panel was written with no width limit, so on a narrow
dock a long action name was cut by the edge of the panel in the middle of an
identifier, and a selected row padded with reverse video overflowed. The column
is now bounded by the room the row leaves and ellipsized with a single cell
ellipsis, the key column shrinks on very narrow docks, and the full name of the
selected action stays reachable on the help line.

Fixes #50
This commit is contained in:
BlackAngelTV committed 2026-12-03 10:16:57 +01:00
1 parent d83fbc71e3
commit 801b4a2d89
3 files changed
+145 -23

No files matched your search

+71 -16
View File
@@ -220,28 +220,57 @@ type displayLine struct {
const maxKeyDisplayWidth = 30
// keyWidthFor is the number of cells reserved for the key itself. On a very
// narrow panel it shrinks, so that the action column still has room and the row
// never extends past the edge of the panel.
func keyWidthFor(panelWidth int) int {
return min(maxKeyDisplayWidth, max(panelWidth-10, 1))
}
// keyColumnWidthFor is the number of cells the key column takes: the four space
// indent, the padded key and the separating space.
func keyColumnWidthFor(panelWidth int) int {
return 4 + keyWidthFor(panelWidth) + 1
}
// unmappedLabel is shown in the key column for actions with no keyboard shortcut.
const unmappedLabel = "(unmapped)"
// truncateToWidth truncates s to fit within maxWidth cells, appending "..." if
// truncated and maxWidth > 3. When maxWidth <= 3, the string is simply trimmed
// to fit without appending ellipsis (no room for it).
// ellipsis marks a truncated string. It is a single cell, so that as much of the
// original text as possible is kept inside the panel.
const ellipsis = "…"
// truncateToWidth truncates s to fit within maxWidth cells, appending "…" if
// truncated and maxWidth > 1. When maxWidth <= 1, the string is simply trimmed
// to fit without appending the ellipsis (no room for it).
func truncateToWidth(s string, maxWidth int) string {
if wcswidth.Stringwidth(s) <= maxWidth {
return s
}
ellipsisWidth := wcswidth.Stringwidth(ellipsis)
runes := []rune(s)
if maxWidth <= 3 {
// Not enough room for ellipsis; just trim to fit
if maxWidth <= ellipsisWidth {
// Not enough room for the ellipsis; just trim to fit
for len(runes) > 0 && wcswidth.Stringwidth(string(runes)) > maxWidth {
runes = runes[:len(runes)-1]
}
return string(runes)
}
for len(runes) > 0 && wcswidth.Stringwidth(string(runes))+3 > maxWidth {
for len(runes) > 0 && wcswidth.Stringwidth(string(runes))+ellipsisWidth > maxWidth {
runes = runes[:len(runes)-1]
}
return string(runes) + "..."
return string(runes) + ellipsis
}
// actionColumnWidth is the number of cells left for the action column of a row,
// so that a long action name is ellipsized inside the panel instead of being cut
// by its edge. The category suffix, only shown in search results, is reserved.
func (h *Handler) actionColumnWidth(width int, b *Binding) int {
used := keyColumnWidthFor(width)
if h.query != "" && h.leader == nil {
used += categorySuffixWidth(b)
}
return max(width-used, 1)
}
// sectionHeader returns a separator line like " ── label ─────────".
@@ -642,11 +671,22 @@ func (h *Handler) draw_screen() {
h.drawFlatResults(resultsStartY, resultsHeight, width)
}
// Draw help text for selected binding
// Draw help text for selected binding, falling back to the full action name
// when the action column had to ellipsize it
h.lp.MoveCursorTo(1, helpY)
if b := h.selectedBinding(); b != nil && b.Help != "" {
helpStr := truncateToWidth(b.Help, max(width-2, 3))
h.lp.QueueWriteString(h.lp.SprintStyled("dim italic", " "+helpStr))
if b := h.selectedBinding(); b != nil {
detail := b.Help
if full := h.selectedActionText(); wcswidth.Stringwidth(full) > h.actionColumnWidth(width, b) {
if detail == "" {
detail = full
} else {
detail = full + " " + detail
}
}
if detail != "" {
helpStr := truncateToWidth(detail, max(width-2, 1))
h.lp.QueueWriteString(h.lp.SprintStyled("dim italic", " "+helpStr))
}
}
// Draw key hints footer
@@ -863,8 +903,9 @@ func (h *Handler) drawBindingLine(filteredIdx, width int, isSelected bool) {
b := &h.all_items[idx].binding
actionDisplay := h.all_items[idx].actionText
// Build the key display
keyDisplay := keyDisplayText(b)
// Build the key display, bounded by the room the panel leaves as well
keyWidth := keyWidthFor(width)
keyDisplay := truncateToWidth(keyDisplayText(b), keyWidth)
// Determine match info for highlighting (only set when a query is active)
var mi *matchInfo
@@ -893,8 +934,8 @@ func (h *Handler) drawBindingLine(filteredIdx, width int, isSelected bool) {
return s
}
// Render key column (4-space indent + key padded to maxKeyDisplayWidth + space)
paddingLen := max(0, maxKeyDisplayWidth-wcswidth.Stringwidth(keyDisplay))
// Render key column (4-space indent + key padded to the key column width)
paddingLen := max(0, keyWidth-wcswidth.Stringwidth(keyDisplay))
pad := strings.Repeat(" ", paddingLen) + " "
ks := keyStyle
if b.Key == "" {
@@ -908,7 +949,8 @@ func (h *Handler) drawBindingLine(filteredIdx, width int, isSelected bool) {
h.lp.QueueWriteString(h.lp.SprintStyled(ks, " "+keyDisplay+pad))
}
// Render action display column
// Render action display column, bounded by the room the panel leaves
actionDisplay = truncateToWidth(actionDisplay, h.actionColumnWidth(width, b))
if mi != nil && len(mi.actionPositions) > 0 {
h.lp.QueueWriteString(h.highlightMatchedChars(actionDisplay, mi.actionPositions, baseStyle, matchStyle))
} else {
@@ -932,6 +974,19 @@ func (h *Handler) drawBindingLine(filteredIdx, width int, isSelected bool) {
}
}
// selectedActionText returns the action text of the selected row, which is the
// full name even when the row has to ellipsize it.
func (h *Handler) selectedActionText() string {
if h.selected_idx < 0 || h.selected_idx >= len(h.filtered_idx) {
return ""
}
idx := h.filtered_idx[h.selected_idx]
if idx < 0 || idx >= len(h.all_items) {
return ""
}
return h.all_items[idx].actionText
}
// rowToFilteredIdx converts a 0-indexed cell Y coordinate to a filtered item
// index, or -1 if the cell is not over a clickable item. Internally converts
// to 1-indexed screen rows (matching the MoveCursorTo convention) to compare