-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.zsh
205 lines (187 loc) · 5.91 KB
/
functions.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
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
202
203
204
# ----------------------------------------------------------------------
# ~/.dotfiles/functions.sh
# Mark Spain
# ----------------------------------------------------------------------
# get operating system
# --------------------------------------
OS=$(uname)
# qfind - used to quickly find files that contain a string in a directory
# --------------------------------------
qfind() {
if [[ -z $1 ]] || [[ $1 == "--help" ]] || [[ $1 == "-h" ]]; then
echo "qfind - used to quickly find files that contain a string in a directory"
echo "Usage: qfind DIRECTORY STRING"
echo " or: qfind STRING"
echo ""
echo "Searches for files containing STRING recursively from DIRECTORY,"
echo " or the current directory if DIRECOTRY is omitted"
return 0
fi
dir=$1
str=$2
if [[ -z $2 ]]; then
dir=.
str=$1
fi
find $dir -type f -print0 | xargs -0 grep -l $str;
}
# loop - run 1 or more commands in a loop with a specified sleep duration
# --------------------------------------
loop() {
if [[ -z $1 ]] || [[ -z $2 ]]; then
echo "loop - run 1 or more commands in a loop with a specfied sleep duration"
echo "USAGE: loop \"COMMAND\" \"SLEEP_DURATION\""
echo ""
echo "Runs the COMMAND (include double quotes) indefinitely every SLEEP_DURATION seconds"
return 0;
fi
COMMAND=$1
SLEEP_DURATION=$2
while true; do eval $COMMAND; sleep $SLEEP_DURATION; done
}
# ff - search current directory for specified pattern
# --------------------------------------
unalias ff 2>/dev/null
ff() {
fd $1 .
}
# psgrep - search for the specified process
# --------------------------------------
psgrep() {
input=$1
out=$(ps aux | grep -i --color=always "$input" | grep -v 'grep')
[[ -z "$out" ]] && out="No process named $input found."
echo $out
}
# clearfile - clear the contents of a specified file
# --------------------------------------
clearfile() {
echo -n > $1
}
# weather - prints the day's weather forcast using wttr.in
# --------------------------------------
weather() {
# if current number of terminal columns is greater or equal to 126
if [[ $(tput cols) -ge 126 ]]; then
# motd script version (long) (>= 126 cols)
curl -m 10 'wttr.in/Edmonds,%20Washington,%20United%20States?u1qF'
else
# motd script version (short)
curl -m 10 'wttr.in/Edmonds,%20Washington,%20United%20States?u1qFn'
fi
}
# print out the current terminal colors
# --------------------------------------
palette() {
for i in {0..255}; do
print -Pn "%K{$i} %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$'\n'};
done
}
# print the escape sequence for the color code
# --------------------------------------
printc() {
local color="%F{$1}"
echo -E ${(qqqq)${(%)color}}
}
# update_fastfetch - installs/updates fastfetch by downloading the latest .deb from github
# --------------------------------------
update_fastfetch() {
OS=$(uname)
if [[ $OS == Linux ]]; then
arch=$(arch)
case $arch in
"x86_64" | "amd64") arch="amd64" ;;
"aarch64" | "arm64") arch="aarch64" ;;
"armv7l" | "armhf") arch="armv7l" ;;
*) arch="" ;;
esac
if [ -z $arch ]; then
echo "Incompatible architecture: $(arch)"
else
echo "Installing latest version of fastfetch for architecture: $arch"
package="fastfetch-linux-$arch.deb"
url="https://github.com/fastfetch-cli/fastfetch/releases/latest/download/$package"
wget -O /tmp/$package $url
sudo apt install -y /tmp/$package
rm -f /tmp/$package
echo "Installed $(fastfetch --version)"
fi
else
echo "Only applicable on Linux systems"
fi
}
# update_starship - installs/updates starship
# --------------------------------------
update_starship() {
OS=$(uname)
if [[ $OS == Linux ]]; then
curl -sS https://starship.rs/install.sh | sh
else
echo "Only applicable on Linux systems"
fi
}
# update_fzf - updates fzf by pulling the latest and running the installation script
# --------------------------------------
update_fzf() {
if (( $+commands[fzf] )); then
current_dir=$(pwd)
cd $HOME/.fzf
git pull
$HOME/.fzf/install --all --key-bindings --completion --no-update-rc --no-bash --no-zsh --no-fish
cd $current_dir
fi
}
# updatea_fzf_git - updates fzf-git.sh by pulling the latest and sourcing the file
# --------------------------------------
update_fzf_git() {
current_dir=$(pwd)
cd $HOME/.fzf-git.sh
git pull
source $HOME/.fzf-git.sh/fzf-git.sh
cd $current_dir
}
# update_fd - installs/updates fd by downloading the latest .deb from github
# --------------------------------------
update_fd() {
OS=$(uname)
if [[ $OS == Linux ]]; then
arch=$(arch)
case $arch in
"x86_64" | "amd64") arch="amd64" ;;
"aarch64" | "arm64") arch="arm64" ;;
"armv7l" | "armhf") arch="armhf" ;;
*) arch="" ;;
esac
if [ -z $arch ]; then
echo "Incompatible architecture: $(arch)"
else
echo "Checking latest version of fd for architecture: $arch"
current_version=$(fd --version | cut -c4-)
latest_version=$(curl https://api.github.com/repos/sharkdp/fd/releases/latest -s | jq .name -r | cut -c2-)
echo "Current version is $current_version"
echo "Lastest version is $latest_version"
if [[ $current_version != $latest_version ]]; then
echo "Installing latest version"
package="fd-musl_${latest_version}_${arch}.deb"
url="https://github.com/sharkdp/fd/releases/latest/download/$package"
wget -O /tmp/$package $url
sudo apt install -y /tmp/$package
rm -f /tmp/$package
echo "Installed $(fd --version)"
else
echo "Latest version already installed"
fi
fi
else
echo "Only applicable on Linux systems"
fi
}
# update_all - runs all the update functions in this file
# --------------------------------------
update_all() {
update_fastfetch
update_starship
update_fzf
update_fzf_git
update_fd
}