-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet
More file actions
executable file
·192 lines (159 loc) · 4.01 KB
/
sheet
File metadata and controls
executable file
·192 lines (159 loc) · 4.01 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
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
#!/bin/bash
# Spreadsheets manager
# declare -A DEFAULTS=()
declare -A FILES=(
[data]="$SHEETS"
[config_file]="$HOME/.config/my_script/config.yml"
[log_dir]="$HOME/logs/"
)
declare -A FLAG=(
[error]=0
)
declare -A ERROR=(
[11]="CONTRADICTING ARGUMENTS: select only 1 of the following: ls, new, edit, del"
)
declare -A NAMESPACE=(
[args]="$@"
[debug]=0
## mututally exclusive
[select]=1 # TRUE by default
[create]=0
[edit]=0
[delete]=0
## *****************
[uuid]=''
[name]=''
[dir]=0
)
#########################################################
main() {
initialize # setup environment and check for missing files
dissect "$@" # break input for analysis
validate # validate the operation
dispatch # execute the operation
terminate # execute post-script tasks regardless of operation
}
initialize() {
# echo "Setting up environment"
# echo "Checking for missing variables or files"
FLAG[error]=0
}
dissect() {
LAST_OPTION=''
# Iterate over arguments using a while loop
while [[ $# -gt 0 ]]; do
case "$1" in
debug | --debug)
NAMESPACE[debug]=1 ;
;;
list | ls | '')
NAMESPACE[select]=1 ;
;;
new | add | +*)
NAMESPACE[create]=1 ;
NAMESPACE[select]=0 ;
;;
edit | open | mod)
NAMESPACE[edit]=1 ;
NAMESPACE[select]=0 ;
;;
del* | rm | -*)
NAMESPACE[delete]=1 ;
NAMESPACE[select]=0 ;
;;
dir)
NAMESPACE[dir]=1 ;
NAMESPACE[select]=0 ;
;;
*)
uuid_or_filename "$1" ;
;;
esac
# discard argument
shift
done
}
validate() {
check_for_contradiction
# check_for_error2
# check_for_error3
# ...
}
check_for_contradiction() {
local count=0
for key in select create edit delete; do
if [[ "${NAMESPACE[$key]}" -eq 1 ]]; then
((count++))
fi
done
if [[ $count -gt 1 ]]; then FLAG[error]=11; fi
}
dispatch() {
if [[ "${FLAG[error]:-0}" -gt 0 ]]; then
echo "Error: ${ERROR[${FLAG[error]}]}" >&2
exit "${FLAG[error]}"
fi
[[ "${NAMESPACE[select]}" -eq 1 ]] && do_select
[[ "${NAMESPACE[create]}" -eq 1 ]] && do_create
[[ "${NAMESPACE[edit]}" -eq 1 ]] && do_edit
[[ "${NAMESPACE[delete]}" -eq 1 ]] && do_delete
[[ "${NAMESPACE[dir]}" -eq 1 ]] && print_dir
# ...else
# echo "Error: No valid operation specified." >&2
# exit 1
# fi
}
terminate() {
reveal_variables
help_if_no_arg
}
print_dir() {
echo "${FILES[data]}" | xclip -selection clipboard
echo "${FILES[data]}"
echo "(copied)"
}
list_if_no_arg() {
[[ "${NAMESPACE[args]}" == "" ]] && NAMESPACE[select]=1
}
help_if_no_arg() {
[[ "${NAMESPACE[args]}" == "" ]] && echo 'Usage: sheet [new | mod | del] [UUID | FILENAME]' && exit 0
}
reveal_variables() {
if [[ NAMESPACE['debug'] -eq 1 ]]; then
# Loop through the keys of the associative array and print key-value pairs
echo -e "\nDEBUG:"
for key in "${!NAMESPACE[@]}"; do
echo "Key: $key, Value: ${NAMESPACE[$key]}"
done
fi
}
uuid_or_filename() {
echo "uuid or filename? $1"
}
do_select() {
local uuids="${NAMESPACE[uuid]}"
local names="${NAMESPACE[name]}"
local select_all=$([[ -z "$uuids" && -z "$names" ]] && echo 'true' || echo 'false')
if [[ $select_all == "true" ]]; then
list_all_files
else
list_files_by_uuid $uuids
list_files_by_name $names
fi
}
list_all_files() {
echo "listing all"
}
list_files_by_uuid() {
echo "$@"
}
do_create() {
echo "new spreadsheet!"
}
do_edit() {
echo "opening in sc-im..."
}
do_delete() {
echo "removing file..."
}
main "$@"