changeset 13:f73e67cd0b71

filesel: support spaces and optional regex filter
author David Demelier <markand@malikania.fr>
date Wed, 08 Nov 2017 10:12:22 +0100
parents f004b506c789
children 4c66316076df
files bin/dmenu_filesel
diffstat 1 files changed, 32 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/bin/dmenu_filesel	Wed Nov 08 08:53:04 2017 +0100
+++ b/bin/dmenu_filesel	Wed Nov 08 10:12:22 2017 +0100
@@ -28,30 +28,50 @@
 
 dmt_filesel_list()
 {
+	list="/tmp/dmenutools-$(id -u).filesel_list.txt"
+	result="/tmp/dmenutools-$(id -u).filesel_result.txt"
+
     # Allow '.' only if traget must be directory.
     if [ "${ftype}" = "directory" ]; then
-        list=".\n"
-    fi
-    list="${list}..\n"
-
-    if [ -n "$(ls -A ${root})" ]; then
-        # TODO: Add some filters.
-        for i in ${root}*; do
-            list="${list}$(basename "${i}")\n"
-        done
+		echo "." > ${result}
     fi
 
-    echo -e ${list} | sort
+	echo ".." >> ${result}
+
+	# Avoid basename error in case of empty directories.
+    if [ -n "$(ls -A "${root}")" ]; then
+		find "${root}" -mindepth 1 -maxdepth 1 > ${list}
+
+		# Update the result list.
+		cat ${list} | while read n; do
+			keep=1
+
+			# Check for optional pattern
+			if [ -f "$n" ] && [ -n "${pattern}" ] && ! (echo $n | egrep -q "${pattern}"); then
+				keep=0
+			fi
+
+			if [ ${keep} -eq 1 ]; then
+				echo $(basename "$n") >> ${result}
+			fi
+		done
+    fi
+
+	cat ${result} | sort
+	rm -f ${result} ${list}
 }
 
 ftype="file"
 lines="16"
 
-while getopts "l:t:" opt; do
+while getopts "il:t:p:" opt; do
     case ${opt} in
     l)
         lines=${OPTARG}
         ;;
+	p)
+		pattern="${OPTARG}"
+		;;
     t)
         if [ "${OPTARG}" = "f" ] || [ "${OPTARG}" = "file" ]; then
             ftype="file"
@@ -83,7 +103,7 @@
     fi
 
     if [ "${entry}" = ".." ]; then
-        parent="$(dirname ${root})"
+        parent="$(dirname "${root}")"
 
         if [ "${parent}" = "/" ]; then
             root="/"