-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpickers.sh
More file actions
157 lines (139 loc) · 4.77 KB
/
pickers.sh
File metadata and controls
157 lines (139 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# Multi-UI Scripting - File/Folder Picker Functions
# https://github.com/lunarcloud/script-dialog
# LGPL-2.1 license
#######################################
# Display a file selector dialog
# GLOBALS:
# GUI_ICON
# GUI_TITLE
# XDG_ICO_SAVE
# XDG_ICO_FILE_OPEN
# DOCUMENT_SYMBOL
# INTERFACE
# RECMD_LINES
# RECMD_COLS
# RECMD_SCROLL
# APP_NAME
# ACTIVITY
# ZENITY_ICON_ARG
# ZENITY_HEIGHT (optional)
# ZENITY_WIDTH (optional)
# ARGUMENTS:
# The starting folder
# "save" or "open" (assume "open" if omitted)
# OUTPUTS:
# Path to selected file
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function filepicker() {
if [ -z ${GUI_ICON+x} ]; then
if [ "$2" == "save" ]; then
GUI_ICON=$XDG_ICO_SAVE
else
GUI_ICON=$XDG_ICO_FILE_OPEN
fi
fi
_calculate-gui-title
local exit_status=0
if [ "$INTERFACE" == "whiptail" ]; then
# shellcheck disable=SC2012
read -r -d '' -a files < <(ls -lBhpa "$1" | awk -F ' ' ' { print $9 " " $5 } ')
SELECTED=$(whiptail --clear --backtitle "$APP_NAME" --title "$GUI_TITLE" --cancel-button Cancel --ok-button Select --menu "$ACTIVITY" $((8+RECMD_LINES)) $((6+RECMD_COLS)) "$RECMD_LINES" "${files[@]}" 3>&1 1>&2 2>&3)
exit_status=$?
FILE="$1/$SELECTED"
elif [ "$INTERFACE" == "dialog" ]; then
FILE=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --stdout --fselect "$1"/ 14 48)
exit_status=$?
elif [ "$INTERFACE" == "zenity" ]; then
FILE=$(zenity --title "$GUI_TITLE" "$ZENITY_ICON_ARG" "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --file-selection --filename "$1"/ )
exit_status=$?
elif [ "$INTERFACE" == "kdialog" ]; then
if [ "$2" == "save" ]; then
FILE=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --getsavefilename "$1"/ )
else #elif [ "$2" == "open" ]; then
FILE=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --getopenfilename "$1"/ )
fi
exit_status=$?
else
read -erp "${DOCUMENT_SYMBOL}You need to $2 a file in $1/. Hit enter to browse this folder"
ls -lBhpa "$1" 3>&1 1>&2 2>&3 #| less
read -erp "Enter name of file to $2 in $1/: " SELECTED
exit_status=$?
# TODO: Add validation - handle empty SELECTED or when SELECTED is a folder
FILE=$1/$SELECTED
fi
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
# Ignore choice and relaunch dialog
if [[ "$SELECTED" == "./" ]]; then
FILE=$(filepicker "$1" "$2")
fi
# Drill into folder
if [ -d "$FILE" ]; then
FILE=$(filepicker "$FILE" "$2")
fi
echo "$FILE"
}
#######################################
# Display a folder selector dialog
# GLOBALS:
# GUI_ICON
# GUI_TITLE
# XDG_ICO_FOLDER_OPEN
# FOLDER_SYMBOL
# INTERFACE
# RECMD_LINES
# RECMD_COLS
# RECMD_SCROLL
# APP_NAME
# ACTIVITY
# ZENITY_ICON_ARG
# ZENITY_HEIGHT (optional)
# ZENITY_WIDTH (optional)
# ARGUMENTS:
# The starting folder
# OUTPUTS:
# Path to selected folder
# RETURN:
# 0 if success, non-zero otherwise.
#######################################
function folderpicker() {
if [ -z ${GUI_ICON+x} ]; then
GUI_ICON=$XDG_ICO_FOLDER_OPEN
fi
_calculate-gui-title
local exit_status=0
if [ "$INTERFACE" == "whiptail" ]; then
# shellcheck disable=SC2010
read -r -d '' -a files < <(ls -lBhpa "$1" | grep "^d" | awk -F ' ' ' { print $9 " " $5 } ')
SELECTED=$(whiptail --clear --backtitle "$APP_NAME" --title "$GUI_TITLE" --cancel-button Cancel --ok-button Select --menu "$ACTIVITY" $((8+RECMD_LINES)) $((6+RECMD_COLS)) "$RECMD_LINES" "${files[@]}" 3>&1 1>&2 2>&3)
exit_status=$?
FILE="$1/$SELECTED"
elif [ "$INTERFACE" == "dialog" ]; then
FILE=$(dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --stdout --dselect "$1"/ 14 48)
exit_status=$?
elif [ "$INTERFACE" == "zenity" ]; then
FILE=$(zenity --title "$GUI_TITLE" "$ZENITY_ICON_ARG" "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --file-selection --directory --filename "$1"/ )
exit_status=$?
elif [ "$INTERFACE" == "kdialog" ]; then
FILE=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --getexistingdirectory "$1"/ )
exit_status=$?
else
read -erp "${FOLDER_SYMBOL}You need to select a folder in $1/. Hit enter to browse this folder"
# shellcheck disable=SC2010
ls -lBhpa "$1" | grep "^d" 3>&1 1>&2 2>&3 #| less
read -erp "Enter name of folder in $1/: " SELECTED
exit_status=$?
# TODO: Add validation - handle empty SELECTED or parent directory (..)
FILE=$1/$SELECTED
fi
# Exit script if dialog was cancelled
if [ $exit_status -ne 0 ]; then
exit "$SCRIPT_DIALOG_CANCEL_EXIT_CODE"
fi
echo "$FILE"
}