Ranger:不要尝试显示大文件(预览)

Ranger:不要尝试显示大文件(预览)

我正在使用 Ranger 作为文件浏览器,顺便说一句,它很棒......

我遇到问题,因为 Ranger 可以显示当前所选文件的预览。这非常有用,但对于大文件来说会出现问题。事实上,对于大文件,创建预览需要花费大量时间和资源。

我的问题是:有没有办法设置 Ranger 不会尝试显示预览的最大尺寸?

答案1

我找到了解决方案,至少对于文本文件,问题在于突出显示... Ranger 试图突出显示长文件... 我找到的解决方法显示在以下摘录中~/.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

这个想法是,仅选择文本文件的第一行,然后 highligh仅调用该部分。

答案2

您可以在 的某些部分包含命令scope.sh来检查文件大小。

首先,添加新函数(将上面的代码粘贴handle_extension()到 中scope.sh):

drop_bigsize() {
    # 51200 == 50 MB * 1024
    # change this number for different sizes
    if [[ `du "${FILE_PATH}" | cut -f1` -gt 51200 ]]; then
        echo '----- TOO BIG FILE -----'
        exit 0
    fi
}

其次,在 中的某个位置调用该函数scope.sh
例如,下面的代码将阻止预览任何大小大于 50MB 的文件(来自 的最后几行scope.sh):

...
MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"

### start of new block ###
drop_bigsize
### end of new block ###

if [[ "${PV_IMAGE_ENABLED}" == 'True' ]]; then
    handle_image "${MIMETYPE}"
fi

handle_extension
handle_mime "${MIMETYPE}"
handle_fallback

exit 1

要对某些特定文件类型(例如某些档案)执行此类操作,您需要将相同的代码块放置在您的不同部分scope.sh

...
handle_extension() {
    case "${FILE_EXTENSION_LOWER}" in
        # Archive
        a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
        rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
            ### start of new block ###
            drop_bigsize
            ### end of new block ###
            atool --list -- "${FILE_PATH}" && exit 5
            bsdtar --list --file "${FILE_PATH}" && exit 5
            exit 1;;
        rar)
            # Avoid password prompt by providing empty password
            unrar lt -p- -- "${FILE_PATH}" && exit 5
            exit 1;;
...

相关内容