scripts/preview: Add support for multiple files

master
Pierre Neidhardt 2017-05-23 18:32:20 +02:00
parent b8794f4f1a
commit 4b247de6f0
1 changed files with 44 additions and 30 deletions

View File

@ -2,9 +2,9 @@
usage () {
cat <<EOF>&2
Usage: ${0##*/} FILE
Usage: ${0##*/} FILES
Preview FILE:
Preview FILES:
- If folder, list it.
@ -16,8 +16,8 @@ Preview FILE:
EOF
}
[ $# -ne 1 ] && usage && exit 1
[ "$1" = "-h" ] && usage && exit
[ $# -lt 1 ] && usage && return 1
[ "$1" = "-h" ] && usage && return
[ "$1" = "--" ] && shift
ls="ls -1 --color=always"
@ -28,12 +28,8 @@ if [ "$(uname -o)" = "GNU/Linux" ]; then
ls="$ls --group-directories-first"
fi
## Folders.
[ -d "$1" ] && $ls "$1" && exit
## scope.sh.
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-~/.config}
scope="$XDG_CONFIG_HOME/ranger/scope.sh"
if [ ! -x "$scope" ]; then
XDG_DATA_DIRS=${XDG_DATA_DIRS:-"/usr/local/share"}
@ -43,27 +39,45 @@ if [ ! -x "$scope" ]; then
fi
fi
buf=$("$scope" "$1" $(tput cols) $(tput lines) 2>/dev/null)
status=$?
## Several files must share the screen space.
lines=$(expr $(tput lines) / $#)
[ $lines -lt 1 ] && lines=1
echo HHH $lines
case $status in
1)
break ;;
2)
## Text files
sed $(tput lines)q "$1"
exit ;;
*)
echo "$buf" | sed $(tput lines)q
exit ;;
esac
_preview() {
## Folders.
[ -d "$1" ] && $ls "$1" && return
## All files.
path="$1"
[ ! -e "$path" ] && path=$(command -v "$1")
[ ! -e "$path" ] && exit 1
$ls -l "$path"
file "$path" | cut -d':' -f2 | cut -b 2-
if command -v pacman >/dev/null 2>&1; then
pacman -Qo "$path" 2>/dev/null
fi
buf=$("$scope" "$1" $(tput cols) $lines 2>/dev/null)
status=$?
case $status in
1)
break ;;
2)
## Text files
sed ${lines}q "$1"
return ;;
*)
echo "$buf" | sed ${lines}q
return ;;
esac
## All files.
path="$1"
[ ! -e "$path" ] && path=$(command -v "$1")
[ ! -e "$path" ] && return
$ls -l "$path"
file "$path" | cut -d':' -f2 | cut -b 2-
if command -v pacman >/dev/null 2>&1; then
pacman -Qo "$path" 2>/dev/null
fi
}
[ $# -eq 1 ] && _preview $1 && exit
for i; do
## GNU head uses this echo when outputting multiple files.
echo "==> $i <=="
_preview "$i"
done