Added a convenient and nice file picker script
[iomenu.git] / io-run
blob997025bd128bdd425f8f0d2f5651bd17b2c76f61
1 # Prompt for a programs to run
4 CACHE="${XDG_CACHE_HOME:-$HOME/.cache}"
7 usage()
9         printf 'Usage: %s [cmd [args...] [+]]
10         
11 cmd     do not prompt for a command and run cmd right away
12 args    do not prompt for arguments neither and use arg
13 +       if present after the arguments, prompt for a path\n' "${0##*/}"
18 # Update the cache and get the command to run.
20 update_cache()
22         local IFS=':' update=''
24         for dir in $PATH
25         do
26                 [ "$CACHE/iomenu/run" -ot "$dir" ] && update=1
27         done
29         [ "$update" ] && find -L $PATH -type f -exec test -x {} \; -print |
30                 sed 's|.*/||' | sort -u > "$CACHE/iomenu/run"
35 # Prompt for options for a given command and log it to an history file
37 get_options()
39         local command="$1"
40         local history
42         printf '%s ' "$command" >> "$CACHE/iomenu/history"
44         while read -r cmd options
45         do
46                 [ "$command" = "$cmd" -a "$options" ] && printf '%s\n' "$options"
47         done < $CACHE/iomenu/history |
48                 iomenu -p "$command" |
49                 tee -a "$CACHE/iomenu/history"
51         sort -u "$CACHE/iomenu/history" -o "$CACHE/iomenu/history"
56 # Prompt for a file path in $HOME and print it.
58 get_path()
60         find "$HOME" ! -path '*/.cache/*' ! -path '*/.git/*' |
61                 sed -r "s/.{${#HOME}}/~/" | iomenu -l 256 | sed 's/^~//'
66 # Get the options according to the command and run it
68 run()
70         local command="$1" options=""
71         local path name
73         [ $# -gt 0 ] && shift && options="$*"
74         
75         command="${command:-$(
76                 iomenu -l 256 -s '#' < "$CACHE/iomenu/run"
77         )}"
79         [ -z "$command" ] && exit 1
81         if [ "${command##* *}" ]
82         then
83                 [ -z "$options" ] && options="$(get_options "$command")"
85                 # if '+' at the end of options, prompt for a path
86                 if [ "$options" -a -z "${options%%*+}" ]
87                 then
88                         path="$(get_path)" options="${options%+}"
89                 fi
90         fi
92         [ "$path" ] && exec $command $options "$path" || exec $command $options
96 main()
98         mkdir -p "$CACHE/iomenu"
100         if [ $# -gt 0 -a -z "${1##-*}" ]
101         then
102                 usage
103         else
104                 update_cache
105                 run $@
106         fi
109 main $@