vid: Add caching .vidlist for faster searching.

This commit is contained in:
Ole Tange 2018-11-24 23:49:47 +01:00
parent b2ed8d9d73
commit 82f6abbe09

63
vid/vid
View file

@ -20,6 +20,9 @@ filters out the names matching all the B<grep> expressions.
There can be multiple B<grep> expressions. There can be multiple B<grep> expressions.
The searching is cached in B<.vidlist> in a parent dir or in the
current dir if no parents contain B<.vidlist>.
=head1 EXAMPLE =head1 EXAMPLE
@ -64,12 +67,64 @@ B<G>
=cut =cut
find_find_vidlist() {
dir="$1"
if [ -f "$1"/.vidlist ] ; then
echo "$1"/.vidlist
return 0
else
if [ "/" == $(readlink -f "$dir") ]; then
echo ./.vidlist
return 0
fi
find_find_vidlist "../$dir"
return $?
fi
}
update_list() {
# update_list(vidlist_file, full_path_vidlist_dir)
# Update vidlist
# Update .vidlist with content from full_path_vidlist_dir
vidlist="$1"
full_path_vidlist_dir="$2"
# Subshell to ignore 'cd'
(cd "$full_path_vidlist_dir" &&
find . -iregex '.*\(webm\|rm\|mov\|mpg\|mpeg\|avi\|wmv\|flv\|mp4\|3gp\)$' \
-type f -printf '%s\t%p\n') |
# Sort by size
sort -rn |
# Remove size
perl -pe 's/^\S+\t//' > "$vidlist".$$
mv "$vidlist".$$ "$vidlist"
}
cat_list() {
vidlist="$(find_find_vidlist .)"
full_path_vidlist_dir="$(dirname $(readlink -f "$vidlist") )"
full_path_thisdir="$(readlink -f .)"
if [ -f "$vidlist" ] ; then
# find background (>/dev/null to detach from tty)
update_list "$vidlist" "$full_path_vidlist_dir" >/dev/null &
else
# find foreground
update_list "$vidlist" "$full_path_vidlist_dir"
fi
subdir="$(echo "$full_path_thisdir" |
perl -pe 's|\Q'"$full_path_vidlist_dir"'\E||')"
# if not empty append /
if [ -n "$subdir" ] ; then
subdir="$subdir/"
fi
# cat "$vidlist" | grep matching this dir + remove dirs
cat "$vidlist" |
perl -ne 's|^(\./)?\Q'"$subdir"'\E|| and print'
}
stdin() { stdin() {
if tty -s ; then if tty -s ; then
# STDIN is terminal # STDIN is terminal
find . -iregex '.*\(webm\|rm\|mov\|mpg\|mpeg\|avi\|wmv\|flv\|mp4\|3gp\)$' -type f | cat_list
# Sort by size - descending
perl -e 'print map {"$_\n"} sort { chomp $a;chomp $b; -s $b <=> -s $a } <>'
else else
# STDIN redir'ed - read it # STDIN redir'ed - read it
cat cat
@ -82,7 +137,7 @@ stdout() {
# start VLC # start VLC
parallel -Xj1 vlc parallel -Xj1 vlc
else else
parallel -kXj1 ls -dS cat
fi fi
} }