From 5e612ad62dfd2f2db350597bf19a2d572cf71366 Mon Sep 17 00:00:00 2001 From: josuah Date: Tue, 22 Nov 2016 21:52:02 +0100 Subject: [PATCH] Moved added io-abduco, and typo. --- README | 2 +- io-abduco | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100755 io-abduco diff --git a/README b/README index 1b8a83c..121b434 100644 --- a/README +++ b/README @@ -6,7 +6,7 @@ -------------------------------------------------------------------------------- iomenu 2016-11-22 -------------------------------------------------------------------------------- -Filter lines form stdin with an interactive menu +Filter lines from stdin with an interactive menu iomenu is a terminal tool to interactively select lines from stdin, and print them out to the standard output. diff --git a/io-abduco b/io-abduco new file mode 100755 index 0000000..b83247c --- /dev/null +++ b/io-abduco @@ -0,0 +1,159 @@ +# Detacheable buffer for programs and files + + +CACHE="${XDG_CACHE_HOME:-$HOME/.cache}" + + +usage() +{ + printf '%s\n' "${0##*/} [-w] [cmd [args...]] + + -w update command description using whatis + cmd do not prompt for a command and run CMD right away + args do not prompt for arguments neither and use ARG" +} + + +# +# Update the cache and get the command to run. +# +update_commands_cache() +{ + # get the command name from cache or stest + IFS=':' + if stest -dqr -n "$CACHE/dmenu_run" $PATH + then + stest -flx $PATH | sort -u > "$CACHE/dmenu_run" + cp "$CACHE/dmenu_run" "$CACHE/buffer/run" + fi + unset IFS +} + + +# +# Generate descriptions of commands from whatis(1). This takes a lot of time. +# +update_descriptions_cache() +{ + local whatis + + update_commands_cache + + while IFS= read -r command + do + whatis="$(whatis -s 1 "$command" 2> /dev/null | tail -n 1)" + if [ "$whatis" ] + then + printf '%-20s # %s\n' \ + "$command" "${whatis#* - }" + else + printf '%s\n' "$command" + fi + done < $CACHE/dmenu_run > "$CACHE/buffer/run" +} + + +# +# Prompt for options for a given command and log it to an history file +# +get_options() +{ + local command="$1" + local history + + printf '%s ' "$command" >> "$CACHE/buffer/history" + + while read -r cmd options + do + [ "$command" = "$cmd" -a "$options" ] && printf '%s\n' "$options" + done < $CACHE/buffer/history | + iomenu -c -p "$command" | + tee -a "$CACHE/buffer/history" + + sort -u "$CACHE/buffer/history" -o "$CACHE/buffer/history" +} + + +# +# Prompt for a file path in $HOME and print it. +# +get_path() +{ + printf %s "$HOME" + + find ~ -type f ! -path '*/.cache/*' ! -path '*/.git/*' | + while IFS='' read -r path + do + printf '~%s\n' "${path#$HOME}" + done | iomenu -l 256 | sed 's/^~//' +} + + +# +# Get the options according to the command and run it +# +run() +{ + local command="$1" options="" + local path name + + [ $# -gt 0 ] && shift && options="$*" + + command="${command:-$( + iomenu -l 256 -s '#' << EOF | tr / ! +# +# $(abduco | tr ! / | sed -r '2,$ s/(.*)\t(.*)/\2 # \1/') +# +# Commands +$(cat "$CACHE/buffer/run") +EOF + )}" + + [ -z "$command" ] && exit 1 + + # prompt for option if needed + if [ "${command##* *}" ] + then + [ -z "$options" ] && options="$( get_options "$command")" + + # if '+' is at the end of the options then prompt for a path + if [ -z "${options%%*+}" -a "$options" ] + then + path="$(get_path)" options="${options%+}" + fi + fi + + name="$command${options:+ $options}${path:+ ~${path#$HOME}}" + name="$(printf %s "$name" | tr / !)" + + export TERM='screen' ABDUCO="$command" + + # run new or existing session with this name + if [ "$path" ] + then + exec abduco -e ^Z -A "$name" $command $options "$path" + else + exec abduco -e ^Z -A "$name" $command $options + fi +} + + +main() +{ + mkdir -p "$CACHE/buffer" + + if [ "$1" = -w ] + then + update_descriptions_cache + elif [ $# -gt 0 -a -z "${1##-*}" ] + then + usage + else + update_commands_cache + run $@ + fi + + stty sane +} + +main $@ -- 2.11.4.GIT