-
Notifications
You must be signed in to change notification settings - Fork 0
/
rofi-filebrowser.sh
executable file
·201 lines (177 loc) · 6.77 KB
/
rofi-filebrowser.sh
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
##################################################################################
TMPDIR="/dev/shm/${UID}/rofi-filebrowser-$$"
WD="${ROFI_INFO}"
declare -A cols
eval $( dircolors )
eval 'cols=('$( echo -ne $LS_COLORS | sed -e 's/:$/\"/; y/:/\ /; s/^/[\\\"/g; s/=/\\\"]=\"/g; s/\ /\"\ [\\\"/g' )' )'
COLORS_FG=( [30]=black [31]=red [32]=green [33]=yellow [34]=blue [35]=magenta [36]=cyan [37]=white )
COLORS_BG=( [40]=black [41]=red [42]=green [43]=yellow [44]=blue [45]=magenta [46]=cyan [47]=white )
_exit()
{
rm "${lskey}" "${lsstr}"
rm -r "${TMPDIR}"
exit
}
lsdir()
{
lskey="${TMPDIR}/lskey"; mkfifo "${lskey}"
lsstr="${TMPDIR}/lsstr"; mkfifo "${lsstr}"
cd "${1}"
ls --color=never -a -1 "${1}" >"${lskey}" &
ls --color=never -a -lph "${1}" >"${lsstr}" &
if [ "x${ROFI_FB_SHOW_ICONS}" == "x1" ]
then
lsmim="${TMPDIR}/lsmim"; mkfifo "${lsmim}"
ls --color=never -a -1 "${1}" | file --mime-type -nNb --files-from - | tr '/' '-' >"${lsmim}" &
exec 12< "${lsmim}"
fi
exec 10< "${lskey}"
exec 11< "${lsstr}"
read -u11 -t0.2 sum
while read -u10 -t0.2 key
do
read -u11 -t0.2 str
fg=
bg=
style=
if [ "x${ROFI_FB_COLORS}" == "x1" ]
then
case "${str:0:1}" in
b) style="bd" ;;
c) style="cd" ;;
C) style="ca" ;;
d) style='di' ;;
D) style='do' ;;
l) if readlink -e "${1}/${key}"
then style="ln"
else style="or"
fi
;;
p) style="pi" ;;
s) style="so" ;;
*) if [ "${str:3:1}" == 's' ] || [ "${str:3:1}" == 'S' ]
then style="su"
elif [ "${str:6:1}" == 's' ] || [ "${str:6:1}" == 'S' ]
then style="sg"
elif [ "${str:3:1}" == 'x' ] || [ "${str:6:1}" == 'x' ] || [ "${str:9:1}" == 'x' ]
then style="ex"
fi
if [ "${str:8:1}" == 'wt' ] || [ "${str:8:1}" == 'wT' ]
then style="tw"
elif [ "${str:9:1}" == 't' ] || [ "${str:9:1}" == 'T' ]
then style="st"
elif [ "${str:8:1}" == 'w' ]
then style="ow"
fi
;;
esac
if [ -z "${style}" ] ; then
style='*.'"${key##*.}"
fi
if [ -n "${style}" ]
then
props=( $( tr ';' ' ' <<< ${cols[\"${style}\"]} ) )
if [ -n "${props[*]}" ]
then
for p in "${props[@]}"
do
if [ -z "${fg}" ]; then
fg="${COLORS_FG["${p}"]}"
fi
if [ -z "${bg}" ]; then
bg="${COLORS_BG["${p}"]}"
fi
done
fi
fi
if [ -n "${fg}${bg}" ]
then
echo -en "<span"
if [ -n "${fg}" ] ; then
echo -en " foreground=\"${fg}\""
fi
if [ -n "${bg}" ] ; then
echo -en " background=\"${bg}\""
fi
echo -en ">${str}</span>"
else
echo -en "${str}"
fi
else
echo -en "${str}"
fi
echo -en "\x00info\x1f${1}/${key}"
if [ "x${ROFI_FB_SHOW_ICONS}" == "x1" ]
then
read -u12 icon
if [ -z "${icon}" ] ; then
icon=$( mimetype --output-format="%m" "${key}" | tr '/' '-' )
fi
echo -en "\x1ficon\x1f${icon}"
fi
echo
done
echo -en "\0active\x1f${pos}\n"
}
use_parent()
{
local parent="$(dirname "${path}")"
pos=2
for f in "${parent}"/*
do
if [ "${f}" == "${path}" ]; then
break;
fi
(( pos++ ))
done
path="${parent}"
}
# Main #
shopt -s dotglob
mkdir -p "${TMPDIR}"
if ! [ -v ROFI_FB_SHOW_ICONS ]; then
ROFI_FB_SHOW_ICONS=0
fi
if ! [ -v ROFI_FB_COLORS ]; then
ROFI_FB_COLORS=0
fi
pos=0
msg=
path="${HOME}"
if [ $# != 0 ]; then
path="$(realpath "${WD}")"
fi
if [ -d "${path}" ]
then
if ! [ -r "${path}" ] || ! [ -x "${path}" ]
then
msg="Permission denied"
use_parent
fi
else
xdg-open "${path}" > /dev/null 2>&1 &
use_parent
fi
echo -en "\0markup-rows\x1ftrue\n"
echo -en "\0message\x1f${path}"
[ -n "${msg}" ] && \
echo -en "\r<b>${msg}</b>"
echo
echo -en "\0no-custom\x1ftrue\n"
lsdir "${path}"
_exit