-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirtour
executable file
·51 lines (43 loc) · 1.63 KB
/
dirtour
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
#!/usr/bin/env fish
# dirtour - Get the first (or a random) file from each subdirectory.
argparse --ignore-unknown 'script_name=' 'default_type=' -- $argv
if set -q _flag_script_name
set script "$_flag_script_name"
else
set -- script "$(path basename (status filename))"
end
argparse -n $script 'h/help' 't/type=' 'f/first' 'r/random' 'i/stdin' -- $argv
if set -q _flag_h
echo "$script - Get the first (or a random) file from each subdirectory."
echo "Usage: $script [arguments] [directory]"
echo
echo " -h/--help - Print help and exit."
echo " -t/--type - Specify the media type (image, audio, video, etc) or file extension (including leading period) to get."
echo " -f/--first - Get the first file (alphabetically) from each subdirectory. This is the default behavior."
echo " -r/--random - Get a random file from each subdirectory."
echo " -i/--stdin - Tour each subdirectory passed via stdin. Without this, tour the directories passed as arguments, or the current directory."
exit
end
if set -q _flag_i; or contains -- - $argv
function gen_func
cat
end
else
if test (count $argv) -gt 0
set dirs $argv
else
set dirs .
end
function gen_func
find $dirs -maxdepth 1 -type d | tail -n +2 # FIX: this "tail" only works if only one directory was passed.
end
end
if set -q _flag_r
alias sort_func 'shuf'
else
alias sort_func 'sort'
end
set find_media_args (filter --print=find $_flag_t)
for dir in (gen_func)
find (path resolve $dir) -maxdepth 1 \( $find_media_args \) -not -type d | sort_func | head -1
end # | v -T "dirtourv $path" -