Quite working, just need one little fix...
[iomenu.git] / io-abduco
blob9b29df08729f2a740b3efb64c818c4943dc64145
1 # Detacheable buffer for programs and files
4 CACHE="${XDG_CACHE_HOME:-$HOME/.cache}"
7 usage()
9         printf '%s\n' "${0##*/} [-w] [cmd [args...]]
11         -w      update command description using whatis
12         cmd     do not prompt for a command and run CMD right away
13         args    do not prompt for arguments neither and use ARG"
18 # Update the cache and get the command to run.
20 update_commands_cache()
22         # get the command name from cache or stest
23         IFS=':'
24         if stest -dqr -n "$CACHE/dmenu_run" $PATH
25         then
26                 stest -flx $PATH | sort -u > "$CACHE/dmenu_run"
27                 cp "$CACHE/dmenu_run" "$CACHE/iomenu/run"
28         fi
29         unset IFS
34 # Generate descriptions of commands from whatis(1).  This takes a lot of time.
36 update_descriptions_cache()
38         local whatis
40         update_commands_cache
42         while IFS= read -r command
43         do
44                 whatis="$(man -f -s 1 "$command" 2> /dev/null | tail -n 1)"
45                 if [ "$whatis" ]
46                 then
47                         printf '%-20s # %s\n' \
48                                 "$command" "${whatis#* - }"
49                 else
50                         printf '%s\n' "$command"
51                 fi
52         done < $CACHE/dmenu_run > "$CACHE/iomenu/run"
57 # Prompt for options for a given command and log it to an history file
59 get_options()
61         local command="$1"
62         local history
64         printf '%s ' "$command" >> "$CACHE/iomenu/history"
66         while read -r cmd options
67         do
68                 [ "$command" = "$cmd" -a "$options" ] && printf '%s\n' "$options"
69         done < $CACHE/iomenu/history |
70                 iomenu -c -p "$command" |
71                 tee -a "$CACHE/iomenu/history"
73         sort -u "$CACHE/iomenu/history" -o "$CACHE/iomenu/history"
78 # Prompt for a file path in $HOME and print it.
80 get_path()
82         printf %s "$HOME"
84         find "$HOME" -type f ! -path '*/.cache/*' ! -path '*/.git/*' |
85                 sed -r "s/.{${#HOME}}/~/" |
86                 iomenu -l 256             |
87                 sed 's/^~//'
92 # Get the options according to the command and run it
94 run()
96         local command="$1" options=""
97         local path name
99         [ $# -gt 0 ] && shift && options="$*"
100         
101         command="${command:-$(
102                 iomenu -l 256 -s '#' << EOF | tr / !
104 # $(abduco | tr ! / | sed -r '2,$ s/(.*)\t(.*)/\2 # \1/')
106 # Commands
107 $(cat "$CACHE/iomenu/run")
109         )}"
111         [ -z "$command" ] && exit 1
113         # prompt for option if needed
114         if [ "${command##* *}" ]
115         then
116                 [ -z "$options" ] && options="$( get_options "$command")"
118                 # if '+' is at the end of the options then prompt for a path
119                 if [ -z "${options%%*+}" -a "$options" ]
120                 then
121                         path="$(get_path)" options="${options%+}"
122                 fi
123         fi
125         name="$command${options:+ $options}${path:+ ~${path#$HOME}}"
126         name="$(printf %s "$name" | tr / !)"
128         export TERM='screen' ABDUCO="$command"
130         # run new or existing session with this name
131         if [ "$path" ]
132         then
133                 exec abduco -e ^Z -A "$name" $command $options "$path"
134         else
135                 exec abduco -e ^Z -A "$name" $command $options
136         fi
140 main()
142         mkdir -p "$CACHE/iomenu"
144         if [ "$1" = -w ]
145         then
146                 update_descriptions_cache
147         elif [ $# -gt 0 -a -z "${1##-*}" ]
148         then
149                 usage
150         else
151                 update_commands_cache
152                 run $@
153         fi
155         stty sane
158 main $@