Ranger : do not try to display large files (preview)

ranger

I am using Ranger as a file explorer, and by the way it's great…

I have an issue because Ranger can display a preview of the file currently
selected. Which is quite useful but becomes problematic for large files.
Indeed, for large files, it takes a lot of time and ressources to do create a
preview.

My question is : is there a way to set the maximal size for which Ranger won't
try to display a preview ?

Best Answer

I found the solution, at least for text files, the problem was in the highlighting... Ranger was trying to highlight long files... the workaround I found is shown in the following excerpt of ~/.config/ranger/scope.sh

#!/usr/bin/env sh

path="$1"    # Full path of the selected file
width="$2"   # Width of the preview pane (number of fitting characters)
height="$3"  # Height of the preview pane (number of fitting characters)
maxln=54    # Stop after $maxln lines.  Can be used like ls | head -n $maxln

# Find out something about the file:
mimetype=$(file --mime-type -Lb "$path")
extension=${path##*.}

try() { output=$(eval '"$@"'); }
dump() { echo "$output"; }
trim() { head -n "$maxln"; }
hl() { command head -n "$maxln" "$path" | highlight --syntax="$extension" --out-format=ansi; test $? = 0 -o $? = 141; }

case "$mimetype" in
    # Syntax highlight for text files:
    text/* | */xml)
        try hl && { dump | trim; exit 5; } || exit 2;;
esac
exit 1

The idea, it to select only the first lines of the textfile and then call highligh only on that portion.

Related Question