summaryrefslogtreecommitdiffstats
path: root/.config/lf/scope
blob: d924019de5bb85a964d393e3b3518d51a53d58c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash

set -C -f
#IFS=$'\n'
IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}"

# ANSI color codes are supported.
# STDIN is disabled, so interactive scripts won't work properly

# This script is considered a configuration file and must be updated manually.

# Meanings of exit codes:
# code | meaning    | action of ranger
# -----+------------+-------------------------------------------
# 0    | success    | Display stdout as preview
# 1    | no preview | Display no preview at all
# 2    | plain text | Display the plain content of the file

# Script arguments
FILE_PATH="${1}"         # Full path of the highlighted file

#FILE_EXTENSION="${FILE_PATH##*.}"
#FILE_EXTENSION_LOWER=$(echo ${FILE_EXTENSION} | tr '[:upper:]' '[:lower:]')

# Settings
export HIGHLIGHT_SIZE_MAX=262143  # 256KiB
export HIGHLIGHT_TABWIDTH=8
export HIGHLIGHT_STYLE='pablo'

image() {
	if [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1; then
		printf '{"action": "add", "identifier": "PREVIEW", "x": "%s", "y": "%s", "width": "%s", "height": "%s", "scaler": "contain", "path": "%s"}\n' "$4" "$5" "$(($2-1))" "$(($3-1))" "$1" > "$FIFO_UEBERZUG"
		exit 1
	else
		mediainfo "$1"
	fi
}
CACHE="$HOME/.cache/lf/thumbnail.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}'))"

case "$(file --dereference --brief --mime-type -- "${FILE_PATH}")" in
	image/*) image "$FILE_PATH" "$2" "$3" "$4" "$5" ;;
	text/html) lynx -display_charset=utf-8 -dump "${FILE_PATH}"  ;;
	text/troff) man ./ "${FILE_PATH}" | col -b ;;
	text/* | */xml) bat -f "${FILE_PATH}" ;;
	application/zip) atool --list -- "${FILE_PATH}" ;;
        video/* | audio/* | application/octet-stream) mediainfo "${FILE_PATH}" || exit 1;;
        */pdf) pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - ;;
        *opendocument*) odt2txt "${FILE_PATH}" ;;
	application/pgp-encrypted) gpg -d -- "${FILE_PATH}" ;;
esac
exit 1