-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsdfopen.zsh
87 lines (77 loc) · 2.55 KB
/
wsdfopen.zsh
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
# reads the contents of a file and puts them at cursor
wsdialog_wsdfopen_msg="Document? "
wsdialog_wsdfopen_modes[1]=eempty
wsdialog_wsdfopen_modes[2]=enotexists
wsdialog_wsdfopen_modes[3]=enotafile
wsdialog_wsdfopen_modes[4]=epermissions
wsdialog_wsdfopen_accept=wsdfopen-accept
wsdialog_wsdfopen_restore=wsdfopen-restore
wsdfopen-run() {
wsdialog-wsdfopen-run
}
wsdfopen-make-eempty-msg() {
wsdialog_wsdfopen_eempty_msg='#Filename empty.# Press Enter to continue.'
}
wsdfopen-make-enotexists-msg() {
local msg='#File "<FN>" does not exist.# Press Enter to continue.'
local filename="$1"
wsdialog_wsdfopen_enotexists_msg="$(echo $msg | sed s:\<FN\>:$1:)"
}
wsdfopen-make-enotafile-msg() {
local msg='#File "<FN>" is not a file.# Press Enter to continue.'
local filename="$1"
wsdialog_wsdfopen_enotafile_msg="$(echo $msg | sed s:\<FN\>:$1:)"
}
wsdfopen-make-epermissions-msg() {
wsdialog_wsdfopen_epermissions_msg='#Permission error.# Press Enter to continue.'
}
wsdialog-add wsdfopen
# If file name empty => filename empty error
# If file does not exist => file not found error
# If file exists and no permission => open with sudo, mark as sudo
# Otherwise, just open normally (without sudo)
wsdfopen-accept() {
local filename="${wsdialog_text// /}"
if [[ -z "$filename" ]]; then
wsdfopen-make-eempty-msg
wsdialog_l4mode=eempty
elif [[ ! -e "$filename" ]]; then
wsdfopen-make-enotexists-msg "$filename"
wsdialog_l4mode=enotexists
elif [[ ! -f "$filename" ]]; then
wsdfopen-make-enotafile-msg "$filename"
wsdialog_l4mode=enotafile
elif [[ -r "$filename" && -w "$filename" ]]; then
wsdfopen-read "$filename"
wsdfopen_fn="$filename"
else # no permissions: TODO: to implement (not working implementation)
if wsdfopen-read "$filename" sudo; then
wsdfopen_fn="$filename"
else
wsdfopen-make-epermissions-msg
wsdialog_l4mode=epermissions
fi
fi
}
wsdfopen-restore() {
if [[ -n $wsdfopen_endfn ]]; then
$wsdfopen_endfn $1
unset wsdfopen_endfn
fi
unset wsdfopen_text
unset wsdfopen_fn
}
# get file contents, file name in first argument, contents in wskr_text
wsdfopen-read() {
local fn="$1"
local sudo="$2"
if [[ "$sudo" = "sudo" ]]; then
wsdfopen_text=$(sudo cat "$fn"; printf x)
wsdfopen_text=${wsdfopen_text%x}
return $?
else
wsdfopen_text=$(cat "$fn"; printf x)
wsdfopen_text=${wsdfopen_text%x}
return 0
fi
}