Skip to content

Commit

Permalink
Add/use border label feature flag
Browse files Browse the repository at this point in the history
fzf 0.35 added the ability to added labels to borders - both for the
entire application window and for the preview window. This can now be
used to add a global header, present on all fzf instances. The snapshot
screen can now optionally move context text to the bottom border around
the preview window instead of being in it.
  • Loading branch information
zdykstra committed Nov 14, 2022
1 parent 562b14a commit 00ef434
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 8 deletions.
8 changes: 8 additions & 0 deletions zfsbootmenu/bin/zfsbootmenu
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ fuzzy_default_options=(
"--bind" "\"ctrl-alt-l:execute[ /bin/zlogtail ]${HAS_REFRESH:++refresh-preview}\""
)

if [ -n "${HAS_BORDER_LABEL}" ]; then
# shellcheck disable=SC2016
fuzzy_default_options+=(
"--border-label-pos=top" "--border=top"
"--color=border:white" "--separator=''"
)
fi

# shellcheck disable=SC2016,SC2086
if [ ${loglevel:-4} -eq 7 ] ; then
fuzzy_default_options+=(
Expand Down
11 changes: 11 additions & 0 deletions zfsbootmenu/bin/zlogtail
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# shellcheck disable=SC1091
source /lib/zfsbootmenu-lib.sh >/dev/null 2>&1 || exit 1

[ -f "${BASE}/have_errors" ] && rm "${BASE}/have_errors"
[ -f "${BASE}/have_warnings" ] && rm "${BASE}/have_warnings"

Expand All @@ -16,6 +19,14 @@ if [ -n "${HAS_DISABLED}" ]; then
)
fi

if [ -n "${HAS_BORDER_LABEL}" ]; then
fuzzy_default_options+=(
"--border-label-pos=top" "--border=top"
"--border-label=\"$( global_header zlogtail )\""
"--color=border:white"
)
fi

export FZF_DEFAULT_OPTS="${fuzzy_default_options[*]}"

# Try to use feature flags found on dmesg from util-linux
Expand Down
8 changes: 8 additions & 0 deletions zfsbootmenu/install-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ create_zbm_conf() {
has_disabled=1
fi

# Check if fuzzy finder supports border labels
# Added in fzf 0.35.0
local has_border_label
if echo "abc" | fzf -f "abc" --border-label=test --exit-0 >/dev/null 2>&1; then
has_border_label=1
fi

local has_column
if command -v column >/dev/null 2>&1 ; then
has_column=1
Expand All @@ -113,6 +120,7 @@ create_zbm_conf() {
export BYTE_ORDER="${endian:-le}"
export HAS_REFRESH="${has_refresh}"
export HAS_DISABLED="${has_disabled}"
export HAS_BORDER_LABEL="${has_border_label}"
export HAS_COLUMN="${has_column}"
EOF
}
Expand Down
79 changes: 72 additions & 7 deletions zfsbootmenu/lib/zfsbootmenu-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,53 @@ column_wrap() {
echo -e "${footer//^/${pad}}"
}

# arg1: optional page to mark as active
# prints: a header string with the active page highlighted yellow
# returns: nothing

# shellcheck disable=SC2120
global_header() {
local header page tab

# Accept a parameter to explicitly set a page to be shown
if [ -n "${1}" ]; then
page="${1}"
else
# Name of the calling function
page="${FUNCNAME[1]}"
fi

# Set the entire string to one color
header="\\033[0;37m Boot Environments | Snapshots | Kernels | Pool Status | Logs | Help \\033[0m"

case "${page}" in
draw_be)
tab="Boot Environments"
;;
draw_kernel)
tab="Kernels"
;;
draw_snapshots|draw_diff)
tab="Snapshots"
;;
draw_pool_status)
tab="Pool Status"
;;
help_pager)
tab="Help"
;;
zlogtail)
tab="Logs"
;;
*)
zdebug "Called from unknown function: ${page}"
;;
esac

# change the name of the selected tab to be yellow
echo -n -e "${header/${tab}/\\033[1;33m${tab}\\033[0;37m}"
}

# arg1: Path to file with detected boot environments, 1 per line
# prints: key pressed, boot environment on successful selection
# returns: 0 on successful selection, 1 if Esc was pressed, 130 if BE list is missing
Expand Down Expand Up @@ -109,7 +156,8 @@ draw_be() {

if ! selected="$( ${FUZZYSEL} -0 --prompt "BE > " \
${expects} ${expects//alt-/ctrl-} ${expects//alt-/ctrl-alt-} \
--header="${header}" --preview-window="up:${PREVIEW_HEIGHT}" \
${HAS_BORDER_LABEL:+--border-label="$( global_header )"} \
--header="${header}" --preview-window="up:${PREVIEW_HEIGHT},border-sharp" \
--preview="/libexec/zfsbootmenu-preview {} ${BOOTFS}" < "${env}" )"; then
return 1
fi
Expand Down Expand Up @@ -156,9 +204,10 @@ draw_kernel() {

if ! selected="$( HELP_SECTION=kernel-management ${FUZZYSEL} \
--prompt "${benv} > " --tac --with-nth=2 --header="${header}" \
${HAS_BORDER_LABEL:+--border-label="$( global_header )"} \
${expects} ${expects//alt-/ctrl-} ${expects//alt-/ctrl-alt-} \
--preview="/libexec/zfsbootmenu-preview ${benv} ${BOOTFS}" \
--preview-window="up:${PREVIEW_HEIGHT}" < "${_kernels}" )"; then
--preview-window="up:${PREVIEW_HEIGHT},border-sharp" < "${_kernels}" )"; then
return 1
fi

Expand Down Expand Up @@ -206,14 +255,28 @@ draw_snapshots() {

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

if ! selected="$( HELP_SECTION=snapshot-management ${FUZZYSEL} \
# when defined this controls passing an additional parameter to zfsbootmenu-preview
# as well as extending the preview window height by 1
# when undefined, it triggers added 0 to the window height, leaving it as-is

local LEGACY_CONTEXT
if [ -z "${HAS_BORDER_LABEL}" ]; then
LEGACY_CONTEXT=1
fi

if ! selected="$(\
HELP_SECTION=snapshot-management ${FUZZYSEL} \
--prompt "Snapshot > " --header="${header}" --tac --multi 2 \
${HAS_BORDER_LABEL:+--border-label="$( global_header )"} \
${expects} ${expects//alt-/ctrl-} ${expects//alt-/ctrl-alt-} \
--bind='alt-d:execute[ /libexec/zfunc draw_diff {+} ]' \
--bind='ctrl-d:execute[ /libexec/zfunc draw_diff {+} ]' \
--bind='ctrl-alt-d:execute[ /libexec/zfunc draw_diff {+} ]' \
--preview="/libexec/zfsbootmenu-preview ${benv} ${BOOTFS} '${context}'" \
--preview-window="up:$(( PREVIEW_HEIGHT + 1 ))" <<<"${snapshots}" )"; then
${HAS_BORDER_LABEL:+--preview-label-pos=bottom} \
${HAS_BORDER_LABEL:+--preview-label="$( colorize orange " ${context} " )"} \
--preview="/libexec/zfsbootmenu-preview ${benv} ${BOOTFS} ${LEGACY_CONTEXT:+\"${context}\"}" \
--preview-window="up:$(( PREVIEW_HEIGHT + ${LEGACY_CONTEXT:-0} )),border-sharp" <<<"${snapshots}" )"
then
return 1
fi

Expand Down Expand Up @@ -291,8 +354,9 @@ draw_diff() {
line_two="${left_pad}$( colorize green "+++${diff_target}" )"

sed "s,${mnt},," <&3 | HELP_SECTION=diff-viewer ${FUZZYSEL} --prompt "> " \
${HAS_BORDER_LABEL:+--border-label="$( global_header )"} \
--preview="echo -e '${line_one}\n${line_two}'" --no-sort \
--preview-window="up:${PREVIEW_HEIGHT}"
--preview-window="up:${PREVIEW_HEIGHT},border-sharp"

[ -n "${zfs_diff_PID}" ] && kill "${zfs_diff_PID}"

Expand Down Expand Up @@ -322,7 +386,8 @@ draw_pool_status() {
if ! selected="$( zpool list -H -o name |
HELP_SECTION=zpool-health ${FUZZYSEL} \
--prompt "Pool > " --tac --expect=alt-r,ctrl-r,ctrl-alt-r \
--preview-window="right:${psize}" \
${HAS_BORDER_LABEL:+--border-label="$( global_header )"} \
--preview-window="right:${psize},border-sharp" \
--preview="zpool status -v {}" --header="${header}" )"; then
return 1
fi
Expand Down
3 changes: 2 additions & 1 deletion zfsbootmenu/libexec/zfsbootmenu-help
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ help_pager() {
--with-nth=2.. \
--bind pgup:preview-up,pgdn:preview-down \
--preview="$0 -s {1}" \
--preview-window="right:${PREVIEW_SIZE}:wrap" \
--preview-window="right:${PREVIEW_SIZE}:wrap,border-sharp" \
--header="${header}" \
--border=top --border-label="$( global_header )" \
--tac --inline-info --ansi --layout="reverse-list"
}

Expand Down

0 comments on commit 00ef434

Please sign in to comment.