Skip to content

Instantly share code, notes, and snippets.

@Balletie
Created April 7, 2016 20:52
Show Gist options
  • Save Balletie/ad58ba7a3b5aa869be3e386f58432ef7 to your computer and use it in GitHub Desktop.
Save Balletie/ad58ba7a3b5aa869be3e386f58432ef7 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Browse files with dmenu.
# usage: set ls arguments with "--ls", and dmenu arguments with "--dm"
# Can be interleaved.
# e.g. "--ls -al --dm -p "Hello" --ls -G" will have the following arguments:
# ls: -al -G
# dmenu: -p "Hello"
# The last argument before the first occurence of "--ls" or "--dm" is the path
# to start browsing at.
target="$(realpath .)"
dm_args=()
ls_args=()
cur_args=""
while [ "$1" != "" ]; do
case $1 in
# All ls arguments
--ls ) cur_args=ls_args
;;
# All dmenu arguments
--dm ) cur_args=dm_args
;;
# Add to current arguments (or set target if cur_args not set yet)
* )
[ "$cur_args" = "" ] && target=$1 || eval "$cur_args+=('$1')"
esac
shift
done
dotdot=""
# If ls_args does not contain "a", we must add ".." to the menu
[ "${ls_args[*]/a/}" = "${ls_args[*]}" ] && dotdot=$'..\n'
while true; do
sel="$(ls "$target" ${ls_args[@]} | grep -v '^\.$' | echo "$dotdot$(cat -)" | dmenu ${dm_args[@]})"
ec=$?
[ "$ec" -ne 0 ] && exit $ec
c="$(echo "$sel" |cut -b1)"
if [ "$c" = "/" ]; then
newt="$sel"
else
newt="$(realpath "${target}/${sel}")"
fi
if [ -e "$newt" ]; then
target="$newt"
if [ ! -d "$target" ]; then
echo "$target"
exit 0
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment