Skip to content

Commit 7045d1f

Browse files
committed
zfsbootmenu-lib: show sort key where needed
When fzf supports a border label (>= 0.35.0), show the current sort key that is being used to sort the list. This is only relevant on the main BE selection screen and the Snapshot selection screen. If fzf does not support a border label, fall back to legacy behavior of not showing the value anywhere.
1 parent ff59562 commit 7045d1f

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

zfsbootmenu/lib/zfsbootmenu-lib.sh

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ global_header() {
124124
# returns: 0 on successful selection, 1 if Esc was pressed, 130 if BE list is missing
125125

126126
draw_be() {
127-
local env selected header expects kcl_text kcl_bind blank
127+
local env selected header expects kcl_text kcl_bind blank sort_key preview_label
128128

129129
env="${1}"
130130
if [ -z "${env}" ]; then
@@ -157,12 +157,17 @@ draw_be() {
157157
^[CTRL+R] recovery shell
158158
^[CTRL+H] help" )"
159159

160+
sort_key="$( get_sort_key )"
161+
preview_label="Sorted by: ${sort_key^}"
162+
160163
expects="--expect=alt-e,alt-k,alt-d,alt-s,alt-c,alt-r,alt-p,alt-w,alt-j,alt-o${kcl_bind:+,${kcl_bind}},right"
161164

162165
# shellcheck disable=SC2086
163166
if ! selected="$( ${FUZZYSEL} -0 --prompt "BE > " \
164167
${expects} ${expects//alt-/ctrl-} ${expects//alt-/ctrl-alt-} \
165168
${HAS_BORDER:+--border-label="$( global_header )"} \
169+
${HAS_BORDER:+--preview-label-pos=2:bottom} \
170+
${HAS_BORDER:+--preview-label="$( colorize orange " ${preview_label} " )"} \
166171
--header="${header}" --preview-window="up:${PREVIEW_HEIGHT}${HAS_BORDER:+,border-sharp}" \
167172
--preview="/libexec/zfsbootmenu-preview {} ${BOOTFS}" < "${env}" )"; then
168173
return 1
@@ -230,7 +235,7 @@ draw_kernel() {
230235
# returns: 130 on error, 0 otherwise
231236

232237
draw_snapshots() {
233-
local benv selected header expects sort_key snapshots
238+
local benv selected header expects sort_key snapshots note sorted_by context
234239

235240
benv="${1}"
236241
if [ -z "${benv}" ]; then
@@ -239,8 +244,6 @@ draw_snapshots() {
239244
fi
240245
zdebug "using boot environment: ${benv}"
241246

242-
sort_key="$( get_sort_key )"
243-
244247
header="$( column_wrap "\
245248
^[RETURN] duplicate:[CTRL+C] clone only:[CTRL+X] clone and promote
246249
^[CTRL+D] show diff:[CTRL+R] rollback:[CTRL+N] create new snapshot
@@ -251,7 +254,54 @@ draw_snapshots() {
251254
^[CTRL+D] show diff
252255
^[CTRL+H] help" )"
253256

254-
context="Note: for diff viewer, use tab to select/deselect up to two items"
257+
sort_key="$( get_sort_key )"
258+
259+
sorted_by="Sorted by: ${sort_key^}"
260+
note="Note: for diff viewer, use tab to select/deselect up to two items"
261+
262+
local LEGACY_CONTEXT
263+
if [ -n "${HAS_BORDER}" ] ; then
264+
265+
# Determine how much space should be between the 'sorted by' text and a centered note
266+
# Remove 4 extra characters so that we can put a 1 character pad between strings and
267+
# the horizontal box line
268+
269+
local spacer preview_offset
270+
271+
spacer=$(( ( ( COLUMNS - ${#note} ) / 2 ) - ${#sorted_by} - 4 ))
272+
273+
# preview_offset, if defined, controls the initial preview label text position
274+
# refer to fzf documentation for the --preview-label-pos flag
275+
276+
# if spacer length is non-negative, everything fits
277+
if [ "${spacer}" -gt 0 ]; then
278+
preview_offset="2:"
279+
printf -v spacer "%*s" "${spacer}" ""
280+
# This is a unicode light solid line, U+2500
281+
spacer="${spacer// /─}"
282+
note="$( colorize orange "${note}" )"
283+
sorted_by="$( colorize orange "${sorted_by}" )"
284+
printf -v context " %s %s %s " "${sorted_by}" "${spacer}" "${note}"
285+
# fall back to seeing if the note fits in the available columns
286+
elif [ ${COLUMNS} -gt $(( ${#note} + 2 )) ]; then
287+
printf -v context " %s " "$( colorize orange "${note}" )"
288+
# very few screens will be narrower than this ...
289+
elif [ ${COLUMNS} -gt $(( ${#sorted_by} +2 )) ]; then
290+
preview_offset="2:"
291+
printf -v context " %s " "$( colorize orange "${sorted_by}" )"
292+
# this is a truly narrow screen, skip all preview label text
293+
else
294+
context=""
295+
fi
296+
else
297+
298+
# when defined this controls passing an additional parameter to zfsbootmenu-preview
299+
# as well as extending the preview window height by 1
300+
# when undefined, it triggers adding 0 to the window height, leaving it as-is
301+
302+
LEGACY_CONTEXT=1
303+
context="${note}"
304+
fi
255305

256306
expects="--expect=alt-x,alt-c,alt-j,alt-o,alt-n,alt-r,left,right"
257307

@@ -261,15 +311,6 @@ draw_snapshots() {
261311

262312
zdebug "snapshots: ${snapshots[*]}"
263313

264-
# when defined this controls passing an additional parameter to zfsbootmenu-preview
265-
# as well as extending the preview window height by 1
266-
# when undefined, it triggers added 0 to the window height, leaving it as-is
267-
268-
local LEGACY_CONTEXT
269-
if [ -z "${HAS_BORDER}" ]; then
270-
LEGACY_CONTEXT=1
271-
fi
272-
273314
if ! selected="$(\
274315
HELP_SECTION=snapshot-management ${FUZZYSEL} \
275316
--prompt "Snapshot > " --header="${header}" --tac --multi 2 \
@@ -278,8 +319,8 @@ draw_snapshots() {
278319
--bind="alt-d:execute[ /libexec/zfsbootmenu-diff {+} ]${HAS_REFRESH:++refresh-preview}" \
279320
--bind="ctrl-d:execute[ /libexec/zfsbootmenu-diff {+} ]${HAS_REFRESH:++refresh-preview}" \
280321
--bind="ctrl-alt-d:execute[ /libexec/zfsbootmenu-diff {+} ]${HAS_REFRESH:++refresh-preview}" \
281-
${HAS_BORDER:+--preview-label-pos=bottom} \
282-
${HAS_BORDER:+--preview-label="$( colorize orange " ${context} " )"} \
322+
${HAS_BORDER:+--preview-label-pos=${preview_offset:+${preview_offset}}bottom} \
323+
${HAS_BORDER:+--preview-label="${context}"} \
283324
--preview="/libexec/zfsbootmenu-preview ${benv} ${BOOTFS} ${LEGACY_CONTEXT:+\"${context}\"}" \
284325
--preview-window="up:$(( PREVIEW_HEIGHT + ${LEGACY_CONTEXT:-0} ))${HAS_BORDER:+,border-sharp}" <<<"${snapshots}" )"
285326
then

0 commit comments

Comments
 (0)