Skip to content

Commit 63c97ee

Browse files
author
M
authored
First upload
1 parent f849772 commit 63c97ee

File tree

4 files changed

+358
-0
lines changed

4 files changed

+358
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# my zshrc file
2+
## Description
3+
This is the zshrc file I use for the moment.
4+
I'll modify it through time because I might not like what it looks like for now.
5+
It uses geolocation to find when will be sunset and sunrise and then print a nice ascii art according to the time in the day. It also shows the the weather for the place you're in right now.
6+
I adapted a few scripts I found from here and there so you might recognize things you've seen somewhere else. If I didn't quote you in the list please notify me and I'll add you ;)
7+
8+
![Screenshot from my terminal - I use kitty.](http://www.enlightenment.org/ss/e-65749a9c89d601.08580631.jpg)
9+
10+
### Future
11+
In the future I'd like to add a mailutils wrapper that would show if I have unread mails. I might want to add a RSS feed also.
12+
13+
## Requirements
14+
### Programs
15+
You'll need to use :
16+
- _curl_: to fetch infos on geolocation and weather,
17+
- _toilet_: to write date in a nice format,
18+
- _iwctl_: it's my network manager on systemd, I use it to get the ip adress,
19+
- _zsh-autosuggestion_: it allows to do autocomplete in zsh, it's really nice,
20+
- _oh-my-zsh_: plenty of different features, among them zsh theme management,
21+
- _lolcat_: to generate nice looking DNA loops around my screen,
22+
- _jq_: it allows to open json files directly from the shell.
23+
### API and websites
24+
To access the location, we call to [_tools.keycdn.com_](https://tools.keycdn.com), if you want to use another tool you can change that.
25+
You'll need to make an account on [_openweathermap_](http://openweathermap.com) to have the api key you'll be able to use in the zshrc file.
26+
27+
## File description and what you might want to modify
28+
### zshrc
29+
The zshrc file where everything is stored.
30+
You can modify the folders where everything is stored in by modifying the main variables on lines.
31+
`$CACHE` : the **~/.cache/** file where the json files are stored by the _weather-v2.0.sh_ script.
32+
`$ScriptFolder`: the **/usr/src/** folder where the weather-v2.0.sh script is stored.
33+
34+
### weather-v2.0.sh
35+
The main script that allows to find location and weather.
36+
`$CACHE` same as for zshrc. The must be similar to enable the two scripts to work together.
37+
38+
### bash_profile
39+
The profile folder where all the aliases are stored.
40+
41+
## Credit
42+
I need to give credit to [@closebox73](https://github.com/closebox73) that made most of the weather script.

bash_profile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#aliases
2+
alias brightenkbd="brightnessctl --device=\"asus::kbd_backlight\" set 2"
3+
alias dimkbd="brightnessctl --device=\"asus::kbd_backlight\" set 1"
4+
alias cutkbd="brightnessctl --device=\"asus::kbd_backlight\" set 0"
5+
alias dir="ls -lt"
6+
alias calculator="bc"

weather-v2.0.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/zsh
2+
3+
#Variables
4+
CACHE=~/.cache
5+
#We catch the ip from iwctl with this regex
6+
ip=$(iwctl station wlan0 show| grep -oE '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))')
7+
##Openweather api variables
8+
# We will use the openweather api to check on the weather and sunset/sunrise time, you need an api key for this.
9+
#############################################################################
10+
api_key= #Put your openweather api key here, you can obtain it for free ! #
11+
#############################################################################
12+
# choose between metric for Celcius or imperial for fahrenheit
13+
unit=metric
14+
# i'm not sure it will support all languange,
15+
lang=fr
16+
17+
#Functions
18+
weatherUpdate(){
19+
# If the file isn't up to date or doesn't exist we need to update it :
20+
#We need to update the main file to have the following:
21+
#wind speed in km/h
22+
#sunrise/sunset time in date format HH:MM
23+
#Updating Wind value
24+
windval=$(bc -l <<<$(cat $CACHE/weather.json | jq '.wind.speed')*3.6) #we want to put the wind speed in km/h : m/s->km/h we need to multiply the value by 3.6
25+
26+
echo $windval # we echo the windval to a temp file
27+
tmp=$(mktemp)
28+
jq --arg wind $windval '.wind.speed=($wind|tonumber|floor)' $CACHE/weather.json >> "$tmp" && mv "$tmp" $CACHE/weather.json #we update the file with the temp
29+
30+
#Sunrise value
31+
sunriseval=$(date -d @$(cat $CACHE/weather.json | jq '.sys.sunrise') | cut -c 12-16) # we use the date command to evaluate the unix format retrieved from openweather map and write it as date format.
32+
echo $sunriseval
33+
tmp=$(mktemp)
34+
jq --arg sunrise $sunriseval '.sys.sunrise=($sunrise)' ~/.cache/weather.json >> "$tmp" && mv "$tmp" ~/.cache/weather.json
35+
36+
#Sunset value
37+
sunseteval=$(date -d @$(cat ~/.cache/weather.json | jq '.sys.sunset') | cut -c 12-16) # we use the date command to evaluate the unix format retrieved from openweather map and write it as date format.
38+
echo $sunsetval
39+
tmp1=$(mktemp)
40+
jq --arg sunset $sunseteval '.sys.sunset=($sunset)' ~/.cache/weather.json >> "$tmp1" && mv "$tmp1" ~/.cache/weather.json
41+
42+
#Longitude value
43+
echo $LONGITUDE
44+
tmp2=$(mktemp)
45+
jq --arg lon $LONGITUDE '.sys.longitude=($lon)' ~/.cache/weather.json >> "$tmp2" && mv "$tmp2" ~/.cache/weather.json
46+
47+
#Latitude value
48+
echo $LATITUDE
49+
tmp3=$(mktemp)
50+
jq --arg lat $LATITUDE '.sys.latitude=($lat)' ~/.cache/weather.json >> "$tmp3" && mv "$tmp3" ~/.cache/weather.json
51+
}
52+
53+
54+
#Program begins here
55+
if test -f $CACHE/location.json #First we check for the existence of the location file where the Long and Lat are stored
56+
then # if the file exists we can retrieve everything directly
57+
Filedate=$(stat $CACHE/location.json --format="%y" | cut -c 1-10) # We store the complete date to check
58+
Filehour=$(stat $CACHE/location.json --format="%y" | cut -c 12-13) # We will want to check the hour also
59+
if [[ $Filedate!=$(date +"%Y-%m-%d") ]] # if the date is not the same we want to retrieve the file
60+
then
61+
curl -sH "User-Agent: keycdn-tools:https://github.com" https://tools.keycdn.com/geo.json\?host=$ip -o $CACHE/location.json
62+
elif [$Filedate=$(date +"%Y-%m-%d")] & [$(date +%H)!=$Filehour ] #if the date is the same but the time is different we update the file
63+
then
64+
curl -sH "User-Agent: keycdn-tools:https://github.com" https://tools.keycdn.com/geo.json\?host=$ip -o $CACHE/location.json
65+
fi
66+
else # if the file doesn't exist we download it
67+
curl -sH "User-Agent: keycdn-tools:https://github.com" https://tools.keycdn.com/geo.json\?host=$ip -o $CACHE/location.json
68+
fi
69+
#We store the lon and lat in two variables
70+
LATITUDE=$(cat $CACHE/location.json | jq '.data.geo.latitude')
71+
LONGITUDE=$(cat $CACHE/location.json | jq '.data.geo.longitude')
72+
73+
# Main command
74+
url="api.openweathermap.org/data/2.5/weather?lat=$LATITUDE&lon=$LONGITUDE&appid=${api_key}&cnt=5&units=${unit}&lang=${lang}"
75+
# We check on the existence of the weather.json file the same way we did with location.json
76+
if test -f $CACHE/weather.json
77+
then # if the file exists we need to check date/hour
78+
Filedate2=$(stat $CACHE/weather.json --format="%y" | cut -c 1-10)
79+
Filehour2=$(stat $CACHE/weather.json --format="%y" | cut -c 12-13)
80+
if [[ $Filedate2!=$(date +"%Y-%m-%d") ]] #We check the date. if it's different, we need to update the file
81+
then
82+
curl ${url} -s -o $CACHE/weather.json
83+
weatherUpdate #We then update the weather
84+
elif [ $Filedate2=$(date +"%Y-%m-%d")] & [$(date +%H)!=$Filehour2 ]
85+
then
86+
curl ${url} -s -o $CACHE/weather.json
87+
weatherUpdate #We then update the weather
88+
else #File exists for less than an hour, we don't have anything to do
89+
exit
90+
fi
91+
else # if the file doesn't exist we download it and update windval
92+
curl ${url} -s -o $CACHE/weather.json
93+
weatherUpdate #We then need to update the weather
94+
fi
95+
96+
exit
97+
98+
#We can safely exit the program
99+
100+
101+
102+

zshrc

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
#Main variables and environment
2+
ZSH_THEME=mortalscumbag
3+
CACHE=~/.cache
4+
ScriptFolder=/usr/src
5+
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
6+
source ~/.bash_profile
7+
source ~/.oh-my-zsh/oh-my-zsh.sh
8+
9+
#Main script
10+
## First we need to check if we're online to see if we can then keep going on receiving the weather
11+
res=`ping google.com -c 1 -q -W 2 -w 2 | grep '1 packets transmitted, 1 received, 0% packet loss' | wc -l`
12+
if [ "$res" -eq "1" ]
13+
then
14+
# We retrieve everything we need: we store it in weather.json
15+
#First we check for the existence of the file
16+
$ScriptFolder/weather-v2.0.sh &>/dev/null
17+
18+
# We fetch the sunrise/set time
19+
sunrise=$(cat $CACHE/weather.json |jq '.sys.sunrise')
20+
sunset=$(cat $CACHE/weather.json|jq '.sys.sunset' )
21+
#We need the minutes and hours as numeric values to keep going
22+
sunriseMins=$(echo $sunrise | cut -c 5-6)
23+
sunsetMins=$(echo $sunset | cut -c 5-6)
24+
sunriseHours=$(echo $sunrise | cut -c 2-3)
25+
sunsetHours=$(echo $sunset | cut -c 2-3)
26+
sunriseMins=$(echo $sunriseMins|sed 's/^0*//')
27+
sunsetMins=$(echo $sunsetMins|sed 's/^0*//')
28+
#We need to parse them to the hour, that is the only thing we check
29+
if [[ sunriseMins -ge 30 ]] #if the minutes are above half an hour, then we round it above
30+
then
31+
sunriseHours=$(echo $sunriseHours|sed 's/^0*//')
32+
sunrise=$(($sunriseHours+1))
33+
else #if the minutes are below 30, we just take the normal hour
34+
sunrise=$sunriseHours
35+
fi
36+
37+
# We do the same for the sunset time
38+
if [[ sunsetMins -ge 30 ]] #if the minutes are above half an hour, then we round it above
39+
then
40+
sunsetHours=$(echo $sunsetHours|sed 's/^0*//')
41+
sunset=$(($sunsetHours+1))
42+
else #if the minutes are below 30, we just take the normal hour
43+
sunset=$sunsetHours
44+
fi
45+
midday=$(($sunrise+($sunset-$sunrise)/2))
46+
VAR=$(date +%H)
47+
48+
49+
#Then we begin to print text with lolcat to have the beautiful colorful DNA strands
50+
lolcat << 'END_DNA'
51+
O o O o O oO o O o O o
52+
| O o | | O o | | O o || O o | | O o | | O o |
53+
| | O | | | | O | | | | O | || | O | | | | O | | | | O | |
54+
| o O | | o O | | o O || o O | | o O | | o O |
55+
o O o O o Oo O o O o O
56+
END_DNA
57+
echo -e "\n"
58+
59+
if [[ $VAR -ge $sunrise ]] && [[ $VAR -le $(($midday-1)) ]]
60+
then
61+
cat << 'END_MSG'
62+
* * ▌ ▌ ▜ ▌ ▌
63+
* * * ▌▖▌▞▀▖▐ ▞▀▖▞▀▖▛▚▀▖▞▀▖ ▛▀▖▝▀▖▞▀▖▌▗▘
64+
* * * * * ▙▚▌▛▀ ▐ ▌ ▖▌ ▌▌▐ ▌▛▀ ▌ ▌▞▀▌▌ ▖▛▚
65+
* * * * * ▘ ▘▝▀▘ ▘▝▀ ▝▀ ▘▝ ▘▝▀▘ ▀▀ ▝▀▘▝▀ ▘ ▘
66+
* * * * * * *
67+
* * * * * .# * *
68+
* * * #. .# * *
69+
* "#. #: #" * * *
70+
* * * "#. ##" *
71+
* "###
72+
"##
73+
##. \ " ' ' " /
74+
.##: \ @@@@@@@@@ /
75+
:##: \ @@@@@@@@@@@@@ /
76+
;### @@@@@@@@@@@@@@@@@
77+
,####. " @@@@@@@@@@@@@@@@@@@@ "
78+
/\/\/\/\/\/.######.\/\/\/\/\/\/\@@@@@@@@@@@@@@@@@@@@@@/\/\/\/\
79+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&&&&&&&&&&&&&&&&&&&&&&~~~~~~~~~~
80+
END_MSG
81+
82+
elif [[ $VAR -ge $(($midday-1)) ]] && [[ $VAR -le $(($sunset-1)) ]]
83+
then
84+
cat << 'END_MSG'
85+
* *
86+
* * *
87+
* * * * *
88+
* * * * * ; : ;
89+
* * * * * * * . \_,!,_/ ,
90+
* * * * * .# * * `.,' `.,'
91+
* * * #. .# * * / \
92+
* "#. #: #" * * * ~ -- : : -- ~
93+
* * * "#. ##" * \ /
94+
* "### ,'`._ _.'`.
95+
"## ' / `!` \ `
96+
##. ; : ;
97+
.##:
98+
:##:
99+
;### Welcome Back !
100+
,####.
101+
/\/\/\/\/\/.######.\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
102+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
END_MSG
104+
105+
elif [[ $VAR -ge $(($sunset-1)) ]] && [[ $VAR -le $(($sunset+1)) ]]
106+
then
107+
cat << 'END_MSG'
108+
* *
109+
* * *
110+
* * * * *
111+
* * * * *
112+
* * * * * * *
113+
* * * * * .# * * Welcome back !
114+
* * * #. .# * *
115+
* "#. #: #" * * *
116+
* * * "#. ##" *
117+
* "###
118+
"##
119+
##. ~ " ' ' " ~
120+
.##: ~ @@@@@@@@@ ~
121+
:##: ~ ~@@@@@@@@@@@@@~ ~
122+
;### ~~@@@@@@@@@@@@@@@@@~~
123+
,####. " @@@@@@@@@@@@@@@@@@@@ "
124+
/\/\/\/\/\/.######.\/\/\/\/\/\/\@@@@@@@@@@@@@@@@@@@@@@/\/\/\/\
125+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&&&&&&&&&&&&&&&&&&&&&&~~~~~~~~~~
126+
END_MSG
127+
128+
129+
elif [[ $VAR -ge $(($sunset+1)) ]] || [[ $VAR -le $sunrise ]]
130+
then
131+
cat << 'END_MSG'
132+
* *
133+
* * * * _.-'''-._ *
134+
* * * * * .' .-'``|'.
135+
* * * * * / / -*- \
136+
* * * * * * * ; <{ | ;
137+
* * * * * .# * | _\ | |
138+
* * * #. .# * * ; _\ -*- | ; *
139+
* "#. #: #" * * * \ \ | -*- /
140+
* * * "#. ##" * '._ '.__ |_.'
141+
* "### '-----'
142+
"## *
143+
* ##. *
144+
.##: * *
145+
* :##: *
146+
;### Welcome Back ! *
147+
,####.
148+
/\/\/\/\/\/.######.\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
149+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150+
END_MSG
151+
fi
152+
153+
echo -e "\n"
154+
echo -e "\r $USER, today is $(date +"%A, %d-%m-%y")"
155+
toilet -f mono9 $(date +%H:%M)
156+
echo -e "The weather today in $(cat $CACHE/weather.json | jq -r '.name' | sed "s|\<.|\U&|g") is : \n"
157+
#smblock might be nice too
158+
#toilet -f term "$(figlet -n -f term " Main temperature will be of: $(cat $CACHE/weather.json | jq '.main.temp' | awk '{print int($1+0.5)}')°C
159+
#(min: $(cat $CACHE/weather.json | jq -r '.main.temp_min' | awk '{print int($1+0.5)}')°C ; max: $(cat $CACHE/weather.json | jq -r '.main.temp_max' | awk '{print int($1+0.5)}')°C)
160+
#Humidity: $(cat $CACHE/weather.json | jq '.main.humidity')%
161+
#Wind speed: $(cat $CACHE/weather.json | jq '.wind.speed')km/h")"
162+
echo -e "\e[32mMain temperature will be of: $(cat $CACHE/weather.json | jq '.main.temp' | awk '{print int($1+0.5)}')°C\n
163+
(min: $(cat $CACHE/weather.json | jq -r '.main.temp_min' | awk '{print int($1+0.5)}')°C max: $(cat $CACHE/weather.json | jq -r '.main.temp_max' | awk '{print int($1+0.5)}')°C) \n
164+
Humidity: $(cat $CACHE/weather.json | jq '.main.humidity')% \n
165+
Wind speed: $(cat $CACHE/weather.json | jq '.wind.speed')km/h \n"
166+
167+
168+
lolcat << 'END_DNA'
169+
O o O o O oO o O o O o
170+
| O o | | O o | | O o || O o | | O o | | O o |
171+
| | O | | | | O | | | | O | || | O | | | | O | | | | O | |
172+
| o O | | o O | | o O || o O | | o O | | o O |
173+
o O o O o Oo O o O o O
174+
END_DNA
175+
else
176+
echo -e "\n"
177+
cat << 'END_MSG'
178+
* *
179+
* * *
180+
* * * * *
181+
* * * * *
182+
* * * * * * *
183+
* * * * * .# * * Welcome back !
184+
* * * #. .# * * Your computer is not connected to any network =(
185+
* "#. #: #" * * *
186+
* * * "#. ##" *
187+
* "###
188+
"##
189+
##.
190+
.##:
191+
:##:
192+
;###
193+
,####.
194+
/\/\/\/\/\/.######.\/\/\/\/\/\
195+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
196+
END_MSG
197+
echo -e "\r $USER, today is $(date +"%A, %d-%m-%y")"
198+
toilet -f mono12 $(date +%H:%M)
199+
200+
201+
lolcat << 'END_DNA'
202+
O o O o O oO o O o O o
203+
| O o | | O o | | O o || O o | | O o | | O o |
204+
| | O | | | | O | | | | O | || | O | | | | O | | | | O | |
205+
| o O | | o O | | o O || o O | | o O | | o O |
206+
o O o O o Oo O o O o O
207+
END_DNA
208+
fi

0 commit comments

Comments
 (0)