scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

commit 9ef68618736c13f8153c6dd267598f2cab8903fa
parent 2f89d94c228b01ed9a28c75918f2e35f73e6378b
Author: Claudio Alessi <smoppy@gmail.com>
Date:   Sat, 25 Jun 2022 16:12:31 +0200

[dbrowse] Add multi selection support

Diffstat:
Msrc/dbrowse | 66++++++++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 46 insertions(+), 20 deletions(-)

diff --git a/src/dbrowse b/src/dbrowse @@ -1,29 +1,55 @@ #!/bin/sh -# TODO: multisel -target="$1" -[ -z "$target" ] && target="$(realpath .)" -prompt="$2" +NEWLINE=" +" -while true; do - p="$prompt" - [ -z "$p" ] && p="$target" - sel="$(ls -1a "$target" |grep -v '^\.$' | dmenu -p "$p" -l 25)" - ec=$? - [ "$ec" -ne 0 ] && exit $ec +fullpath() { + name="$1" + path="$2" + c="$(echo "$name" |cut -b1)" - c="$(echo "$sel" |cut -b1)" if [ "$c" = "/" ]; then - newt="$sel" + full="$name" else - newt="$(realpath "${target}/${sel}")" + full="$path/$name" fi + realpath "$full" - if [ -e "$newt" ]; then - target="$newt" - if [ ! -d "$target" ]; then - echo "$target" - exit 0 +} + +main() { + target="$1" + [ -z "$target" ] && target="$(realpath .)" + prompt="$2" + + while true; do + p="$prompt" + [ -z "$p" ] && p="$target" + items="$(ls -1a "$target" |grep -v '^\.$' | dmenu -p "$p" -l 25)" + ec=$? + [ "$ec" -ne 0 ] && exit $ec + + # ignore duplicates + items="$(echo "$items" |sort -u)" + + nitems=$(echo "$items" |wc -l) + if [ $nitems -eq 1 ]; then + newt="$(fullpath "$items" "$target")" + [ ! -e "$newt" ] && continue + if [ -d "$newt" ]; then + target="$newt" + continue + fi fi - fi -done + IFS="$NEWLINE" + for item in $items; do + item="$(fullpath "$item" "$target")" + [ ! -e "$item" ] && continue + echo "$item" + done + unset IFS + exit 0 + done +} + +main "$@"