dot

packages and services management
Log | Files | Refs | README

commit 2f0eee130a92fcd3cb4e5efb5f243b4d9e14c1af
parent 8d7bbd775401c86befb59b2ef57c9162256e144d
Author: josuah <mail@josuah.net>
Date:   Sat, 29 Oct 2016 14:16:33 -0400

Improved buffers structure

Diffstat:
Mbin/buffers | 178++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 94 insertions(+), 84 deletions(-)

diff --git a/bin/buffers b/bin/buffers @@ -12,31 +12,25 @@ help=' Usage: buffers COMMAND COMMAND - r[run] [CMD] Run CMD in an abduco session + r[run] [command] Run command in an abduco session a[ttach] Interactively prompt buffer to attach, display. ' NL=' ' TAB=' ' - - -# FUNCTIONS -#------------------------------------------------------------------------------- +CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/dmenu_run" +COMMAND="$2" # # Update the cache and get the command to run. # -commands() +update_cache() { - local cache="${XDG_CACHE_HOME:-$HOME/.cache}/dmenu_run" + mkdir -p "${CACHE%/*}" - mkdir -p "${cache%/*}" - - # Get the command name from cache or stest + # get the command name from cache or stest IFS=':' - if stest -dqr -n "$cache" $PATH + if stest -dqr -n "$CACHE" $PATH then - stest -flx $PATH | sort -u | tee "$cache" - else - tee < "$cache" + stest -flx $PATH | sort -u > "$CACHE" fi unset IFS } @@ -44,96 +38,110 @@ commands() # # Prompt for a file path. # -path() +get_path() { local type="$1" - find "$HOME/" -type "$type" ! -path '*/.git/*' ! -name '.git' | - while IFS= read -r path - do - printf '%s\n' "${path#$HOME/}" - done | iomenu + find "$HOME/" -type "$type" ! -path '*/.git/*' ! -name '.git' | iomenu } # # Run a command with some arguments in an abduco session # -run() +get_command() { - local cmd="$1" opt path name + local command="$1" - # Overwrite the prompt + # overwrite the prompt printf '\033[1A' 1>&2 - # Get command arguments and the session name - [ -z "$cmd" ] && cmd="$(commands | iomenu -c)" + # get command arguments and the session name + [ -z "$command" ] && command="$(iomenu -c < $CACHE)" - case "$cmd" in + printf %s "$command" +} - # Empty command +# +# Build the name out of the path and set terminal name +# +get_name() +{ + local command="$1" + local path="$(cd "$2"; pwd)" + + path="~${path#$HOME}" + name="$command${path:+ $path}" + + # set terminal's name + printf '\033]0;%s\007' "$name" + + # return the result without forbidden characters + printf %s "$name" | tr / ! +} + +# +# Get the options according to the command and run it +# +run() +{ + local command="$1" + local options + local path + + case "$command" in + + # empty command '' ) exit 1 ;; - # Customs options + # customs options mbsync ) - opt='-a' + options='-a' ;; alsamixer ) - opt='-c 1' + options='-c 1' ;; ping ) - opt='-c 3 www.wikipedia.org' + options='-c 3 www.wikipedia.org' ;; feeds ) - opt='read' + options='read' ;; irc ) - opt='-i' + options='-i' ;; htop | mail | mailx | s-nail | rirc | agenda | music ) - opt='' + options='' ;; - # Single-buffer editor + # single-buffer editor vi | vis | less | ex-vi | ex | ed ) - path="$(path f)" - [ -z "$path" ] && exit 1 + path="$(get_path f)" + options="$path" ;; - # Multi-buffer editor + # multi-buffer editor vim ) - path="$(path d)" + path="$(get_path d)" cd "$path" - [ -z "$path" ] && exit 0 ;; - # Else, prompt for command + # else, prompt for command * ) - read -p "$cmd> " opt + read -p "$command> " options - if [ -z "${opt##*\**}" -a "$opt" ] + if [ -z "${options##*\**}" -a "$options" ] then - path="$(path f)" - opt="${opt##*\*} $path ${opt%%\**}" - [ -z "$path" ] && exit 1 + path="$(get_path f)" + options="${options##*\*} $path ${options%%\**}" fi ;; esac - # Build the name - path="$(cd "$HOME/$path"; pwd)" - [ -z "${path##$HOME*}" ] && path="~${path#$HOME}" - name="$cmd${path:+ $path}" - - # Set terminal's name - printf '\033]0;%s\007' "$name" - - # Remove forbidden characters - name="$(printf '%s' "$name" | tr / !)" - - # Run new or existing abduco session with this name - TERM='screen' ABDUCO="$cmd" abduco -e '^z' -A "$name" $cmd $opt + # run new or existing abduco session with this name + TERM='screen' ABDUCO="$command" abduco -e '^z' \ + -A "$(get_name "$command" "$path")" $command $options } # @@ -143,45 +151,47 @@ attach() { local name - # If already on a session, print its name and exit. + # if already on a session, print its name and exit if [ "$ABDUCO" ] then printf '[ %s ]\n' "$ABDUCO" | tr ! / exit 1 fi - # Overwrite the prompt. - printf '\033[1A' + # overwrite the prompt + printf '\033[A' - # Get the session name by prompting the user. + # get the session name by prompting the user name="$(abduco | tr ! / | sed -r '1d; s/(.*)\t(.*)/\2 # \1/' | sort | iomenu -s '#' | tr / !)" - # Set the name of the terminal. + # set the name of the terminal. printf '\033]0;%s\007' "$name" | tr ! / - # Attach to the session. + # attach to the session. [ "$name" ] && TERM='screen' ABDUCO="$name" abduco -e '^z' -a "$name" } +main() +{ + # parse the arguments + case "$1" in + r | run ) + update_cache + run "${2:-$(get_command)}" + ;; + a | attach ) + attach + ;; + * ) + printf '%s\n' "$help" + exit 0 + ;; + esac -# ALGORITHM -#------------------------------------------------------------------------------- + # reset the screen, print reminder of current sessions + #printf '\033[2J\033[0;0f\033[0m\n' + #abduco | tr ! / | sort -t "$TAB" -k 3 | sed 1d +} -case "$1" in -r | run ) - shift - run "$1" - ;; -a | attach ) - attach - ;; -* ) - printf '%s\n' "$help" - exit 0 - ;; -esac - -# Reset the screen, print reminder of current sessions -printf '\033[2J\033[0;0f\033[0m\n' -abduco | tr ! / | sort -t "$TAB" -k 3 | sed 1d +main $@