#! /bin/sh
# If MAX_ARG_PAGES is 32 and page size is 4KB, then there is only 128kB for environment variables and arguments.
# Since dynamic menu items are passed in via command line, only return about 100kB of data or less.
# Any more, and there will be an error when launching the menu.
FILEMENU_MAXNUMBEROFFILES=60
FILEMENU_MAXAGEINDAYS=180
function listFiles() {
DirPath=$1
ModifyTime=$2
find "$DirPath" -maxdepth 1 -mindepth 1 -type f -mtime "$ModifyTime" -print
}
function listFilesForMenuXarg() {
DirPath=$1
ModifyTime=$2
listFiles "$DirPath" "$ModifyTime" | tail -n $FILEMENU_MAXNUMBEROFFILES | while read filePath; do
echo -e -n "$(basename "$filePath")\0000$(date -r /opt "+%Y-%m-%d") $(cat "$filePath" | wc -c)\0000"
done
}
function fileMenuXarg() {
DirPath=$1
while [ true ]; do
exec 3>&1
exec 4>&1
fileName=$(listFilesForMenuXarg "$DirPath" "-$FILEMENU_MAXAGEINDAYS" 0>&4 | xargs -0 dialog --input-fd 4 --default-item "$fileName" --menu "Xarg Test - Select a file." 0 0 0 2>&1 1>&3)
returnCode=$?
exec 3>&-
exec 4>&-
if [ $returnCode -ne 0 ]; then
return
fi
cat "$DirPath/$fileName" | less
done
}
function listFilesForMenuSet() {
DirPath=$1
ModifyTime=$2
listFiles "$DirPath" "$ModifyTime" | tail -n $FILEMENU_MAXNUMBEROFFILES | while read filePath; do
echo "$(basename "$filePath")"
echo "$(date -r /opt "+%Y-%m-%d") $(cat "$filePath" | wc -c)"
done
}
function fileMenuSet() {
DirPath=$1
while [ true ]; do
IFS=$'\n'
set $(listFilesForMenuSet "$DirPath" "-$FILEMENU_MAXAGEINDAYS")
IFS=$' \t\n'
exec 3>&1
fileName=$(dialog --default-item "$fileName" --menu "Set Test - Select a file." 0 0 0 "$@" 2>&1 1>&3)
exec 3>&-
returnCode=$?
if [ $returnCode -ne 0 ]; then
return
fi
cat "$DirPath/$fileName" | less
done
}
# You must have some files in your home directory for this example to work.
fileMenuSet "$HOME"
fileMenuXarg "$HOME"