~mil/f_scripts

ref: f0f7c5f784415cf86cf64eed26cdd7e2fc80a777 f_scripts/scripts/f_files -rwxr-xr-x 2.9 KiB
f0f7c5f7Miles Alan f_files: Handle .opus files 6 months ago
                                                                                
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
#!/usr/bin/env osh
shopt -s strict:all
DEP="vim mpv fbida-fbi fbida-fbgs"
DEC="Browse files on your filesystem"
DOC="
  An interactive prompt-driven filebrowser. Enter the name of the
  folder to enter to cd or enter a file to open it.

  Set \$F_FILES_HANDLERS to customize handlers to use to open files.
  Note that sequence matters. Each line in \$F_FILES_HANDLERS has 4
  parts seperated by the ^ character, the first field is a regex
  for grep, the second field is fbp fields to populate, and the
  third is the command to run which will be suffixed with "\$@".
  The fourth field is an optional arbitrary shell code to be eval'd
  after terminating the program. This can be helpful to run cleanup
  code for buggy applications like fbi which don't always properly
  on C-c or for any other terminal restoration code.
"
VAR="
  F_FILES_HANDLERS='
    [.](mp4|mov|avi)$                    ^ 9 0 [ ] p q                        ^ mpv --vo=tct   ^
    [.](webm|ogg|mp3|m4a|wav|flac|opus)$ ^ 9 0 [ ] p q                        ^ mpv --no-video ^
    [.](jpg|jpeg|gif|webm|png)$          ^ q j k a + - 100s 150s 200s v h i q ^ fbi            ^ pkill -9 fbi
    [.](pdf)$                            ^ q j k a + - 100s 150s 200s v h i q ^ fbgs -r 200    ^ pkill -9 fbi
    .                                    ^ Z X Q :q :w :wq  j k h l           ^ vim            ^
  '
  F_FILES_FBSET_FBPRATIO='2 / 3'
  F_FILES_FBSET_YRES='1440'
"

terminate() {
  reset
  exit 0
}

handle() {
  local EXT FBKEYS CMD CLEANUP

  IFS=$'\n'
  for HANDLER in $F_FILES_HANDLERS; do
    echo "$HANDLER" | grep -q . || continue
    IFS=$' '
    EXT="$(echo "$HANDLER" | cut -d^ -f1 | tr -d ' ')"
    FBPKEYS="$(echo "$HANDLER" | cut -d^ -f2)"
    CMD="$(echo "$HANDLER" | cut -d^ -f3 | awk '{$1=$1};1')"
    CLEANUP="$(echo "$HANDLER" | cut -d^ -f4)"

    if echo "$1" | grep -qiE "$EXT"; then
      [ -p /tmp/fbp.fifo ] && {
        echo -e "\b\f\e"
        echo "$FBPKEYS" | tr " " "\n"
      } > /tmp/fbp.fifo
      clear
      # Note, custom fbsetting applicable because fbi
      fbset -yres "$(echo "$F_FILES_FBSET_YRES * $F_FILES_FBSET_FBPRATIO" | bc)"
      $CMD "$@"
      fbset -yres "$F_FILES_FBSET_YRES"
      [ -p /tmp/fbp.fifo ] && echo -e "\v" > /tmp/fbp.fifo
      eval "$CLEANUP"
      clear
      return
    fi
  done
  [ -p /tmp/fbp.fifo ] && echo -e "\r" > /tmp/fbp.fifo
}

promptfolder() {
  local FOLDER OPTIONS PICKED

  FOLDER="$1"
  echo "Listing folder: $PWD"
  ls "$FOLDER"
  [ -p /tmp/fbp.fifo ] && { echo -e '\b\f\r../\n*';  ls -1p "$FOLDER"; } > /tmp/fbp.fifo
  read -p "Select: " PICKED; PICKED="$(echo "$PICKED" | tr -d '[:cntrl:]')"

  echo "$PICKED" | grep -E '^[*]$' && handle ./*
  [ -d "$PICKED" ] && cd "$PICKED" && return
  [ -f "$PICKED" ] && handle "$PICKED"
}

main() {
  trap terminate HUP TERM INT

  env | grep -q "^$(basename "$0" | tr '[a-z]' '[A-Z]')=" || eval "$VAR"
  PWD="$HOME"
  while true; do
      promptfolder "$PWD"
  done
}

if [ -n "$1" ]; then "$@"; else main; fi