-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.sh
176 lines (149 loc) · 5.2 KB
/
misc.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
#!/usr/bin/env bash
# broot: a better tree optimizing for the height of the screen
function br_tree() {
br -c :pt "$@"
}
# br with git status
function br_git_status() {
if [[ -s "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/conf/broot.toml" ]]; then
br --conf "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/conf/broot.toml" --git-status
else
br --git-status
fi
}
# Send desktop notifications and reminders from Linux terminal
# https://opensource.com/article/22/1/linux-desktop-notifications
function remind() {
local COUNT="$#"
local COMMAND="$1"
local MESSAGE="$1"
local OP="$2"
shift 2
local WHEN="$*"
# Display help if no parameters or help command
if [[ $COUNT -eq 0 || "$COMMAND" == "help" || "$COMMAND" == "--help" || "$COMMAND" == "-h" ]]; then
echo "COMMAND"
echo " remind <message> <time>"
echo " remind <command>"
echo
echo "DESCRIPTION"
echo " Displays notification at specified time"
echo
echo "EXAMPLES"
echo ' remind "Hi there" now'
echo ' remind "Time to wake up" in 5 minutes'
echo ' remind "Dinner" in 1 hour'
echo ' remind "Take a break" at noon'
echo ' remind "Are you ready?" at 13:00'
echo ' remind list'
echo ' remind clear'
echo ' remind help'
echo
return
fi
# Install notify-send, AT
PackagesList=("notify-send" "at")
InstallSystemPackages "" "${PackagesList[@]}"
# Check presence of notify-send command
if ! which notify-send >/dev/null; then
echo "remind: notify-send is required but not installed on your system. Install it with your package manager of choice, for example 'sudo apt install notify-send'."
return
fi
# Check presence of AT command
if ! which at >/dev/null; then
echo "remind: AT utility is required but not installed on your system. Install it with your package manager of choice, for example 'sudo apt install at'."
return
fi
# Run commands: list, clear
if [[ $COUNT -eq 1 ]]; then
if [[ "$COMMAND" == "list" ]]; then
at -l
elif [[ "$COMMAND" == "clear" ]]; then
at -r "$(atq | cut -f1)"
else
echo "remind: unknown command $COMMAND. Type 'remind' without any parameters to see syntax."
fi
return
fi
# Determine time of notification
if [[ "$OP" == "in" ]]; then
local TIME="now + $WHEN"
elif [[ "$OP" == "at" ]]; then
local TIME="$WHEN"
elif [[ "$OP" == "now" ]]; then
local TIME="now"
else
echo "remind: invalid time operator $OP"
return
fi
# Schedule the notification
echo "notify-send '$MESSAGE' 'Reminder' -u critical" | at "$TIME" 2>/dev/null
echo "Notification scheduled at $TIME"
}
# https://github.com/chubin/wttr.in
function get_weather() {
local wttr_city=${1:-""}
local wttr_format=${2:-""}
local wttr_lang=${3:-"zh-cn"}
local wttr_url
if [[ -z "${wttr_format}" ]]; then
wttr_url="wttr.in/${wttr_city}"
else
wttr_url="wttr.in/${wttr_city}?format=${wttr_format}"
fi
curl -fsL --connect-timeout 3 --max-time 10 \
--noproxy '*' -H "Accept-Language: ${wttr_lang}" --compressed \
"${wttr_url}"
}
function get_weather_custom() {
local wttr_city=${1:-""}
local wttr_format=${2:-""}
local wttr_lang=${3:-"zh-cn"}
local wttr_url
local wttr_weather
if [[ -z "${wttr_format}" ]]; then
wttr_format="%l:+%c%C,+%F0%9F%8C%A1%t,+%E2%9B%86%h,+%F0%9F%8E%8F%w,+%E2%98%94%p+%o,+%P"
fi
wttr_url="wttr.in/${wttr_city}?format=${wttr_format}"
wttr_weather=$(curl -fsL --connect-timeout 3 --max-time 10 \
--noproxy '*' -H "Accept-Language: ${wttr_lang}" --compressed \
"${wttr_url}")
[[ -n "${wttr_weather}" ]] && colorEcho "${YELLOW}${wttr_weather}"
}
# Bash Function To Rename Files Without Typing Full Name Twice
function mv_rename() {
if [ "$#" -ne 1 ] || [ ! -e "$1" ]; then
command mv "$@"
return
fi
read -rei "$1" newfilename
command mv -v -- "$1" "$newfilename"
}
## Dateutils
# http://www.fresse.org/dateutils/
# apt install -y dateutils
# dateutils.dadd 2018-05-22 +120d
# Usage: date_diff 20201208 20180522
function date_diff() {
if [[ $# -eq 2 ]]; then
echo $(( ($(date -d "$1" +%s) - $(date -d "$2" +%s) )/(60*60*24) ))
fi
}
# Usage: get_zone_time Asia/Shanghai America/Los_Angeles America/New_York
function get_zone_time() {
local TZONES CURRENT_UTC_TIME DISPLAY_FORMAT UTC_TIME LOCAL_TIME ZONE_TIME tz
TZONES=("$@")
[[ -z "${TZONES[*]}" ]] && TZONES=("Asia/Shanghai")
# /usr/share/zoneinfo
# Asia/Shanghai America/Los_Angeles America/New_York
CURRENT_UTC_TIME=$(date -u)
DISPLAY_FORMAT="%F %T %Z %z"
UTC_TIME=$(date -u -d "$CURRENT_UTC_TIME" +"$DISPLAY_FORMAT")
colorEcho "${YELLOW}UTC Time: ${UTC_TIME}"
LOCAL_TIME=$(date -d "$CURRENT_UTC_TIME" +"$DISPLAY_FORMAT")
colorEcho "${FUCHSIA}Local Time: ${LOCAL_TIME}"
for tz in "${TZONES[@]}"; do
ZONE_TIME=$(TZ="$tz" date -d "$CURRENT_UTC_TIME" +"$DISPLAY_FORMAT")
colorEcho "${BLUE}${tz}: ${ZONE_TIME}"
done
}