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