Fix #516: Shortcut/Feature: Select random image
[geeqie.git] / plugins / random-image / geeqie-random-image
1 #!/bin/bash
2
3 # Select and display a random image from a list of all images
4 # in Collections and the currently displayed folder
5
6 # get list of images in all collections
7 collection_list=$(geeqie --remote --get-collection-list)
8 OLDIFS=$IFS
9 while IFS= read -r line
10 do
11         collection=$(geeqie --remote --get-collection:"$line")
12         list="$list""$collection"$'\n'
13 done <<< "$collection_list"
14 IFS=$OLDIFS
15
16 # get list of images in current folder
17 file_list=$(geeqie --remote --get-filelist:)
18 OLDIFS=$IFS
19 while IFS= read -r line
20 do
21         class_whitespace="${line##*Class:}"
22         class="${class_whitespace#"${class_whitespace%%[![:space:]]*}"}"
23
24         if [ "$class" == "Image" ]
25         then
26                 list="$list""${line%%Class*}"$'\n'
27         fi
28
29         if [ "$class" == "RAW Image" ]
30         then
31                 list="$list""${line%%Class*}"$'\n'
32         fi
33 done <<< "$file_list"
34 IFS=$OLDIFS
35
36 # remove blank lines
37 files_no_blanks=$(echo "$list" | sed -e 's/^[[:blank:]]*$//')
38 # remove leading trailing whitespace
39 files_no_spaces=$(echo "$files_no_blanks" | sed 's/^[ \t]*//;s/[ \t]*$//')
40
41 # remove duplicate lines and select random line
42 display_image="$(echo "$files_no_spaces" | sort --uniq | shuf -n 1)"
43
44 # get image currently displayed
45 current_image_collection=$(geeqie --remote --tell)
46 # remove collection name, if it is there
47 current_image_spaces="${current_image_collection%%Collection:*}"
48 # remove leading trailing whitespace
49 curent_image=$(echo "$current_image_spaces" | sed 's/^[ \t]*//;s/[ \t]*$//')
50
51 # if the selected random image is currently displayed, try again
52 if [ "$current_image" == "$display_image" ]
53 then
54         display_image="$(echo "$files_no_spaces" | sort --uniq | shuf -n 1)"
55 fi
56
57 geeqie --remote file:"$display_image"