Added dvtm-support to io-abduco
[iomenu.git] / io-abduco
blob92277ced0afc79b8dd208e1354227628158a172b
1 # detacheable buffer for programs and files using abduco
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 +       While at the end of the arguments, prompt for a file\n' "${0##*/}"
18 # Update the cache and get the command to run.
20 update_cache()
22         local IFS=':'
24         if stest -dqr -n "$CACHE/dmenu_run" $PATH
25         then
26                 stest -flx $PATH | sort -u > "$CACHE/dmenu_run"
27         fi
32 # Prompt for options for a given command and log it to an history file
34 get_options()
36         local command="$1"
37         local history
39         printf '%s ' "$command" >> "$CACHE/iomenu/history"
41         while read -r cmd options
42         do
43                 [ "$command" = "$cmd" -a "$options" ] && printf '%s\n' "$options"
44         done < $CACHE/iomenu/history |
45                 iomenu -p "$command" |
46                 tee -a "$CACHE/iomenu/history"
48         sort -u "$CACHE/iomenu/history" -o "$CACHE/iomenu/history"
53 # Prompt for a file path in $HOME and print it.
55 get_path()
57         find "$HOME" -type f ! -path '*/.cache/*' ! -path '*/.git/*' |
58                 sed -r "s/.{${#HOME}}/~/" | iomenu -l 256 | sed 's/^~//'
63 # Get the options according to the command and run it
65 run()
67         local command="$1" options=""
68         local path name
70         [ $# -gt 0 ] && shift && options="$*"
71         
72         command="${command:-$(
73                 iomenu -l 256 -s '#' << EOF | tr / !
75 # $(abduco | tr ! / | sed -r '2,$ s/(.*)\t(.*)/\2 # \1/')
77 # Commands
78 $(cat "$CACHE/dmenu_run")
79 EOF
80         )}"
82         if [ -z "$command" -o "$ABDUCO" -a -z "${name##*dvtm*}" ]
83         then
84                 exit 1
86         elif [ -z "$ABDUCO" -a -z "${command##*dvtm*}" ]
87         then
88                 TERM='screen' ABDUCO='dvtm' exec abduco -A 'dvtm' dvtm-status
90         elif [ "${command##* *}" ]
91         then
92                 [ -z "$options" ] && options="$(get_options "$command")"
94                 # if '+' is at the end of options, prompt for a path
95                 if [ "$options" -a -z "${options%%*+}" ]
96                 then
97                         path="$(get_path)" options="${options%+}"
98                 fi
99         fi
101         name="$command${options:+ $options}${path:+ ~${path#$HOME}}"
102         name="$(printf %s "$name" | tr / !)"
104         export TERM='screen' ABDUCO="$command"
106         if [ "$path" ]
107         then
108                 exec abduco -e ^Z -A "$name" $command $options "$path"
109         else
110                 exec abduco -e ^Z -A "$name" $command $options
111         fi
115 main()
117         mkdir -p "$CACHE"
119         if [ $# -gt 0 -a -z "${1##-*}" ]
120         then
121                 usage
122         else
123                 update_cache
124                 run $@
125         fi
128 main $@