Added io-mblaze and removed io-ii.1
[iomenu.git] / io-run
blob7b21f1aa8193d9f7202b8673c3c44c2cd8cfa62d
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         IFS=':' u=0
24         for dir in $PATH
25         do
26                 [ "$CACHE/dmenu_run" -ot "$dir" ] && u=1
27         done
29         [ "$u" -eq 1 ] && find -L $PATH -type f -exec test -x {} \; -print |
30                 sed 's|.*/||' | sort -u > "$CACHE/dmenu_run"
35 # Prompt for options for a given command and log it to an history file
37 get_options()
39         local command="$1"
41         printf '%s ' "$command" >> "$CACHE/iomenu/run"
43         while read -r cmd opt
44         do
45                 [ "$command" = "$cmd" ] && printf '%s\n' "$opt"
46         done < "$CACHE/iomenu/run" |
47                 iomenu -p "$command" | tee -a "$CACHE/iomenu/run"
49         sort -u "$CACHE/iomenu/run" -o "$CACHE/iomenu/run"
54 # Prompt for a file path in $HOME and print it.
56 get_path()
58         find "$HOME" ! -path "$CACHE" ! -path '*/.git/*' |
59                 sed -r "s/.{${#HOME}}/~/" | iomenu -l 256 | sed 's/^~//'
64 # Get the options according to the command and run it
66 run()
68         command="${1:-$(iomenu -l 256 -s '#' < "$CACHE/dmenu_run")}"
70         [ -z "$command" ] && exit 1
72         options="$(get_options "$command")"
74         if [ "$options" ] && [ -z "${options%%*+}" ]
75         then
76                 path="$(get_path)" options="${options%+}"
77         fi
79         if [ "$path" ]
80         then exec $command $options "$path"
81         else exec $command $options
82         fi
86 main()
88         mkdir -p "$CACHE/iomenu"
90         if [ $# -gt 0 ] && [ -z "${1##-*}" ]
91         then
92                 usage
93         else
94                 update_cache
95                 run "$@"
96         fi
100 main "$@"