scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

dmenu-filepicker (864B)


      1 #!/bin/sh
      2 
      3 # A dmenu-based file picker (prints selected file to stdout)
      4 # Requires: dmenu (or similar)
      5 
      6 # Usage: dmenu-filepicker [-m menu] [path]
      7 
      8 menu="dmenu -i -l 15"
      9 
     10 usage() {
     11 	echo "Usage: dmenu-filepicker [-m MENU] [PATH]"
     12 }
     13 
     14 while getopts "m:" opt; do
     15 	case "$opt" in
     16 		m)
     17 			menu="$OPTARG"
     18 			;;
     19 		*)
     20 			usage
     21 			exit 1
     22 			;;
     23 	esac
     24 done
     25 shift $((OPTIND - 1))
     26 
     27 fullpath=$(realpath "${@:-"$(pwd)"}")
     28 
     29 while true; do
     30 	if [ "$sel" = "." ]; then
     31 		echo "$fullpath" | sed 's|/\.||'
     32 		break
     33 	elif [ -d "$fullpath" ] || [ -z "$fullpath" ]; then
     34 		if [ -z "$fullpath" ]; then fp="/"; else fp="$fullpath"; fi
     35 		sel=$(ls -a "$fp" | sed 's|.*/||' | $menu)
     36 		if [ "$sel" = ".." ]; then
     37 			fullpath=$(echo "$fullpath" | sed "s|/[^/]*$||")
     38 		else
     39 			fullpath=$(echo "$sel" | sed "s|^|$fullpath/|")
     40 		fi
     41 	else
     42 		echo "$fullpath"
     43 		break
     44 	fi
     45 
     46 	[ -z "$sel" ] && break
     47 done