Moved added io-abduco, and typo.
[iomenu.git] / io-abduco
blobb83247c5ef61604dd1ad3ab69b6a91009ee0b5ed
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/buffer/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="$(whatis -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/buffer/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/buffer/history"
66         while read -r cmd options
67         do
68                 [ "$command" = "$cmd" -a "$options" ] && printf '%s\n' "$options"
69         done < $CACHE/buffer/history |
70                 iomenu -c -p "$command" |
71                 tee -a "$CACHE/buffer/history"
73         sort -u "$CACHE/buffer/history" -o "$CACHE/buffer/history"
78 # Prompt for a file path in $HOME and print it.
80 get_path()
82         printf %s "$HOME"
84         find ~ -type f ! -path '*/.cache/*' ! -path '*/.git/*' |
85                 while IFS='' read -r path
86                 do
87                         printf '~%s\n' "${path#$HOME}"
88                 done | iomenu -l 256 | sed 's/^~//'
93 # Get the options according to the command and run it
95 run()
97         local command="$1" options=""
98         local path name
100         [ $# -gt 0 ] && shift && options="$*"
101         
102         command="${command:-$(
103                 iomenu -l 256 -s '#' << EOF | tr / !
105 # $(abduco | tr ! / | sed -r '2,$ s/(.*)\t(.*)/\2 # \1/')
107 # Commands
108 $(cat "$CACHE/buffer/run")
110         )}"
112         [ -z "$command" ] && exit 1
114         # prompt for option if needed
115         if [ "${command##* *}" ]
116         then
117                 [ -z "$options" ] && options="$( get_options "$command")"
119                 # if '+' is at the end of the options then prompt for a path
120                 if [ -z "${options%%*+}" -a "$options" ]
121                 then
122                         path="$(get_path)" options="${options%+}"
123                 fi
124         fi
126         name="$command${options:+ $options}${path:+ ~${path#$HOME}}"
127         name="$(printf %s "$name" | tr / !)"
129         export TERM='screen' ABDUCO="$command"
131         # run new or existing session with this name
132         if [ "$path" ]
133         then
134                 exec abduco -e ^Z -A "$name" $command $options "$path"
135         else
136                 exec abduco -e ^Z -A "$name" $command $options
137         fi
141 main()
143         mkdir -p "$CACHE/buffer"
145         if [ "$1" = -w ]
146         then
147                 update_descriptions_cache
148         elif [ $# -gt 0 -a -z "${1##-*}" ]
149         then
150                 usage
151         else
152                 update_commands_cache
153                 run $@
154         fi
156         stty sane
159 main $@