-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclip
More file actions
executable file
·138 lines (112 loc) · 2.79 KB
/
clip
File metadata and controls
executable file
·138 lines (112 loc) · 2.79 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
#!/bin/bash
# Spreadsheets manager
# declare -A DEFAULTS=()
# declare -A FILES=()
declare -A ERROR=(
[11]="No such file"
)
declare -A NAMESPACE=(
[error]=0
[args]="$@"
[debug]=0
[file]=0
[piped]=""
[clipboard_command]='xclip -selection clipboard'
)
#########################################################
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 ;
;;
file? | -f)
NAMESPACE[file]=1 ;
LAST_OPTION='file'
;;
*)
text_or_file "$1" ;
;;
esac
# discard argument
shift
done
}
validate() {
nothing=here
}
dispatch() {
if [[ "${FLAG[error]:-0}" -gt 0 ]]; then
echo "Error: ${ERROR[${FLAG[error]}]}" >&2
exit "${FLAG[error]}"
fi
[[ ! -z "${NAMESPACE[piped]}" ]] && copy_and_save_to_file
[[ "${NAMESPACE[file]}" -eq 1 ]] && copy_file_content
# ...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() {
if [[ "${NAMESPACE[args]}" == "" && -z "${NAMESPACE[piped]}" ]]; then
echo 'Usage:'
echo 'clip [-f] TEXT | FILES'
echo 'echo "" | clip'
fi
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=${NAMESPACE[$key]}"
done
fi
}
text_or_file() {
nothing=here
}
copy_file_content() {
echo "idk what to do :("
}
copy_and_save_to_file() {
timestamp=$(date +%H.%M.%S)
tempfile="/tmp/clip.${timestamp}"
clipped_text="${NAMESPACE[piped]}"
clipboard_command="${NAMESPACE[clipboard_command]}"
# save content to file
echo "$clipped_text" > "$tempfile"
# save content to clipboard
echo "$clipped_text" | $clipboard_command
}
# Capture piped input
[[ ! -t 0 ]] && NAMESPACE[piped]=$(cat)
# Main function
main "$@"