summaryrefslogtreecommitdiff
path: root/mac/.config/lf/scope
blob: 2a2970de361eff38a1ca0cc32e6b989918b6b054 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash

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

cache_key() {
  stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" |
    sha256sum | awk '{print $1}'
}

kitty_clear_all() {
  printf '\033_Ga=d,d=a\033\\' >/dev/tty 2>/dev/null
  kitty +kitten icat --silent --stdin no --transfer-mode file --clear </dev/null >/dev/tty 2>/dev/null
}

draw_image() {
  if [ -f "$1" ] && command -v kitty >/dev/null 2>&1; then
    kitty_clear_all
    sleep 0.01
    kitty +kitten icat --silent --stdin no --transfer-mode file \
      --place "${2}x${3}@${4}x${5}" "$1" </dev/null >/dev/tty
  else
    chafa "$1" -s "${2}x${3}"
  fi
}

CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/lf"
mkdir -p "$CACHE_DIR"
mime="$(file --dereference --brief --mime-type -- "$1")"

case "$mime" in
image/avif)
  key=$(cache_key "$1")
  out="$CACHE_DIR/thumb.$key.jpg"
  [ -f "$out" ] || magick "$1" "$out" >/dev/null 2>&1
  draw_image "$out" "$2" "$3" "$4" "$5"
  ;;
image/svg+xml)
  key=$(cache_key "$1")
  out="$CACHE_DIR/thumb.$key.png"
  [ -f "$out" ] || inkscape --convert-dpi-method=none -o "$out" --export-overwrite -D "$1" >/dev/null 2>&1
  draw_image "$out" "$2" "$3" "$4" "$5"
  ;;
image/png | image/jpg | image/jpeg)
  draw_image "$1" "$2" "$3" "$4" "$5"
  ;;
image/x-xcf)
  key=$(cache_key "$1")
  out="$CACHE_DIR/thumb.$key.jpg"
  [ -f "$out" ] || magick "$1[0]" "$out" >/dev/null 2>&1
  draw_image "$out" "$2" "$3" "$4" "$5"
  ;;
image/*)
  draw_image "$1" "$2" "$3" "$4" "$5"
  ;;
text/html)
  lynx -width="$4" -display_charset=utf-8 -dump "$1"
  ;;
text/troff)
  man ./ "$1" | col -b
  ;;
text/* | */xml | application/json | application/x-ndjson)
  bat -p --theme ansi --terminal-width "$(($4 - 2))" -f "$1"
  ;;
audio/*)
  mediainfo "$1" || exit 1
  ;;
video/* | application/octet-stream | application/vnd.rn-realmedia)
  key=$(cache_key "$1")
  jpg="$CACHE_DIR/thumb.$key.jpg"
  [ -f "$jpg" ] || ffmpegthumbnailer -i "$1" -o "$jpg" -s 0 -q 5 >/dev/null 2>&1
  draw_image "$jpg" "$2" "$3" "$4" "$5"
  # 필요 시 메타 한 줄:
  # mediainfo --Output="Video;%Duration/String%" "$1"
  ;;
*/pdf)
  key=$(cache_key "$1")
  base="$CACHE_DIR/thumb.$key"
  [ -f "$base.jpg" ] || pdftoppm -jpeg -f 1 -singlefile "$1" "$base" >/dev/null 2>&1
  draw_image "$base.jpg" "$2" "$3" "$4" "$5"
  ;;
application/vnd.openxmlformats-officedocument.presentationml.presentation)
  key=$(cache_key "$1")
  jpg="$CACHE_DIR/thumb.$key.jpg"
  [ -f "$jpg" ] || unoconv -f jpg -o "$jpg" "$1" >/dev/null 2>&1
  draw_image "$jpg" "$2" "$3" "$4" "$5"
  ;;
application/x-hwp)
  key=$(cache_key "$1")
  jpg="$CACHE_DIR/thumb.$key.jpg"
  outdir="$(dirname "$jpg")"
  [ -f "$jpg" ] || { libreoffice --headless --convert-to jpg --outdir "$outdir" "$1" >/dev/null 2>&1 && mv "$outdir/$(basename "$1" .hwp).jpg" "$jpg"; }
  draw_image "$jpg" "$2" "$3" "$4" "$5"
  ;;
*/epub+zip | */mobi*)
  key=$(cache_key "$1")
  jpg="$CACHE_DIR/thumb.$key.jpg"
  [ -f "$jpg" ] || gnome-epub-thumbnailer "$1" "$jpg" >/dev/null 2>&1
  draw_image "$jpg" "$2" "$3" "$4" "$5"
  ;;
application/*zip)
  atool --list -- "$1"
  ;;
*opendocument*)
  odt2txt "$1"
  ;;
application/pgp-encrypted)
  gpg -d -- "$1"
  ;;
application/vnd.openxmlformats-officedocument.wordprocessingml.document)
  key=$(cache_key "$1")
  txt="$CACHE_DIR/thumb.$key.txt"
  [ -f "$txt" ] || pandoc "$1" -t plain -o "$txt" >/dev/null 2>&1
  bat -p --theme ansi --terminal-width "$(($4 - 2))" -f "$txt"
  ;;
esac

exit 1