Fix issue previewing less than window height

If there is no scrollback, namely that you have only populated the
window with output, the preview failed due to a lot of trailing
newlines, which messed up the linum calc
This commit is contained in:
Daniel Berg 2022-01-03 17:32:29 +01:00
parent 3931d36040
commit a0d2c3040e
No known key found for this signature in database
GPG key ID: BFB01A06E7FB4F81

View file

@ -33,10 +33,11 @@ preview() {
local linum total half_lines start end total
match="$2"
capture_file="$1"
# head_file=$(get_head_filename)
# tail_file=$(get_tail_filename)
total=$(wc -l < "$capture_file")
# trim trailing newlines
trimmed=$(printf "%s" "$(< "$capture_file")")
total=$(wc -l <<< "$trimmed")
half_lines=$(( FZF_PREVIEW_LINES / 2))
line_rev=$(get_line_number "$match")
linum=$(reverse_n "$total" 1 "$line_rev")
@ -44,7 +45,7 @@ preview() {
[[ $(( linum - half_lines )) -lt 1 ]] && start=1 || start=$(( linum - half_lines ))
[[ $(( linum + half_lines )) -gt $total ]] && end=$total || end=$(( linum + half_lines ))
[[ $start -eq 1 && $end -ne $total ]] && end=$FZF_PREVIEW_LINES
hl=$(highlight_line "$(<"$capture_file")" "$linum")
hl=$(highlight_line "$trimmed" "$linum")
sed -n "${start},${end}p" <<< "$hl"
}