diff --git a/API.md b/API.md index f62c004..b3a6c2d 100644 --- a/API.md +++ b/API.md @@ -30,10 +30,6 @@ own version there. failure if the user does not press 'y'. - `bbpause`: Display a "press any key to continue" message and wait for a keypress. - `bbpick [prompt]`: Select one of `NULL`-delimited multiple inputs and print it. -- `bbtargets "$BBCMD" "$@"`: If `$BB` is not currently among `$@` (the - selected files), this script prompts the user to ask whether they want to - perform an action on the selected files, or on the cursor. The result is - printed as `cursor` or `selected`. - `bbunscroll`: Print text to the screen *above* the cursor instead of below it. ## Environment Variables diff --git a/scripts/bbactions b/scripts/bbactions new file mode 100755 index 0000000..962aaf0 --- /dev/null +++ b/scripts/bbactions @@ -0,0 +1,90 @@ +#!/bin/sh +# This file defines file actions for bb. Each command will be run with files in +# the $@ arguments. +# +# The format is: ## Action \n { action } + +## Edit file(s) +{ + $EDITOR "$@" +} + +## Delete file(s) +{ + printf "\033[1mDeleting the following:\n\033[33m$(printf ' %s\n' "$@")\033[0m" | bbunscroll | more + bbconfirm + rm -rf "$@" + bbcmd deselect refresh +} + +## Page through file(s) +{ + less -XK "$@" +} + +## Rename file(s) +{ + for f; do + newname="$(bbask "Rename $(printf "\033[33m%s\033[39m" "${f##*/}"): " "${f##*/}")" || break + r="$(dirname "$f")/$newname" + [ "$r" = "$f" ] && continue + [ -e "$r" ] && printf "\033[31;1m$r already exists! It will be overwritten.\033[0m " + && bbconfirm && { rm -rf "$r" || { bbpause; exit; }; } + mv "$f" "$r" || { bbpause; exit; } + bbcmd deselect:"$f" select:"$r" + [ "$f" = "$BB" ] && bbcmd goto:"$r" + done + bbcmd refresh +} + +## Rename file(s) with regex +{ + if ! command -v rename >/dev/null; then + printf '\033[31;1mThe `rename` command is not installed. Please install it to use this key binding.\033[0m\n' + bbpause; exit 1 + fi + patt="$(bbask "Replace pattern: ")" + rep="$(bbask "Replacement: ")" + printf "\033[1mRenaming:\n\033[33m$(rename -nv "$patt" "$rep" "$@")\033[0m" | bbunscroll | more + bbconfirm + rename -i "$patt" "$rep" "$@" + bbcmd deselect refresh +} + +## Pass file(s) as arguments to a command +{ + cmd="$(bbask '@')" + sh -c "$cmd \"\$@\"" -- "$@" || true + bbpause + bbcmd refresh +} + +## Pipe file(s) to a command +{ + cmd="$(bbask '|')" + printf '%s\n' "$@" | sh -c "$cmd" || true + bbpause + bbcmd refresh +} + + +## Run file(s) +{ + for f; do "$f"; done + bbpause +} + +## Open file(s) +{ + if [ "$(uname)" = "Darwin" ]; then + for f; do open "$f"; done + else + xdg-open "$f" + fi +} + +## Print filename(s) +{ + echo "$@" + bbpause +} diff --git a/scripts/bbkeys b/scripts/bbkeys index 97bad28..644e007 100755 --- a/scripts/bbkeys +++ b/scripts/bbkeys @@ -180,16 +180,6 @@ fi ## e: Edit file in $EDITOR $EDITOR "$BB" -## d,Delete: Delete -case "$(bbtargets "$BB" "$@")" in - cursor) set -- "$BB" ;; - both) set -- "$BB" "$@" ;; -esac -printf "\033[1mDeleting the following:\n\033[33m$(printf ' %s\n' "$@")\033[0m" | bbunscroll | more -bbconfirm -rm -rf "$@" -bbcmd deselect refresh - ## Ctrl-v: Move files here printf "\033[1mMoving the following to here:\n\033[33m$(printf ' %s\n' "$@")\033[0m" | bbunscroll | more bbconfirm @@ -225,40 +215,6 @@ case "$(printf '%s\0' File Directory | bbpick "Create new: ")" in esac bbcmd goto:"$name" refresh -## p: Page through a file with `less` -less -XK "$BB" - -## r,F2: Rename files -case "$(bbtargets "$BB" "$@")" in - cursor) set -- "$BB";; - both) set -- "$BB" "$@";; -esac -for f; do - newname="$(bbask "Rename $(printf "\033[33m%s\033[39m" "${f##*/}"): " "${f##*/}")" || break - r="$(dirname "$f")/$newname" - [ "$r" = "$f" ] && continue - [ -e "$r" ] && printf "\033[31;1m$r already exists! It will be overwritten.\033[0m " - && bbconfirm && { rm -rf "$r" || { bbpause; exit; }; } - mv "$f" "$r" || { bbpause; exit; } - bbcmd deselect:"$f" select:"$r" - [ "$f" = "$BB" ] && bbcmd goto:"$r" -done -bbcmd refresh - -## ~: Regex rename files -if ! command -v rename >/dev/null; then - printf '\033[31;1mThe `rename` command is not installed. Please install it to use this key binding.\033[0m\n' - bbpause; exit 1 -fi -if [ $# -eq 0 ]; then set -- $BBGLOB; fi -patt="$(bbask "Replace pattern: ")" -rep="$(bbask "Replacement: ")" -printf "\033[1mRenaming:\n\033[33m$(rename -nv "$patt" "$rep" "$@")\033[0m" | bbunscroll | more -bbconfirm -rename -i "$patt" "$rep" "$@" -bbcmd deselect refresh - - ## Section: Shell Commands ## Colon: Run a command cmd="$(bbask ':')" @@ -322,3 +278,26 @@ key="$(bbask -1 "Press key to bind...")" echo script="$(bbask "Bind script: ")" bbcmd bind:"$key":"{ $script; } || bbpause" + +## 1: Run action on files +missing_cursor() { + for f; do + if [ "$f" = "$BB" ]; then return 1; fi + done +} +if [ $# -gt 0 ]; then + if missing_cursor "$@"; then + intended="$(printf '%s\0' 'Cursor file' 'Selected files' 'Both' | bbpick 'Which do you mean? ')" + case "$intended" in + Cursor*) set -- "$BB";; + Selected*) ;; + Both) set -- "$BB" "$@" ;; + *) exit 1;; + esac + fi +else + set -- "$BB" +fi +picked=$(sed -n 's/^## *//p' /etc/bb/bbactions | fzf --height=1) +[ -n "$picked" ] || exit 1 +sh -c "$(bp "$picked"'\(\n braces)' /etc/bb/bbactions)" -- "$@" || bbpause diff --git a/scripts/bbtargets b/scripts/bbtargets deleted file mode 100755 index 3b47cc7..0000000 --- a/scripts/bbtargets +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh -# If the user is doing something ambiguous, like selecting a bunch of files, -# moving the cursor off of those files, then doing an action, this will ask -# what they mean to target, then output either 'cursor' or 'selected'. -# Usage: targets "$BB" "$@" -cursor="$1" -shift -if [ $# -gt 0 ]; then - for f in "$@"; do - if [ "$f" = "$cursor" ]; then - intended='Selected files' - break - fi - done -else - intended='Cursor file' -fi - -[ -z "$intended" ] && intended="$(printf '%s\0' 'Cursor file' 'Selected files' 'Both' | bbpick 'Which do you mean? ')" - -case "$intended" in - Cursor*) echo cursor ;; - Selected*) echo selected ;; - Both) echo both ;; -esac