Skip to content

Commit

Permalink
Create waybar config
Browse files Browse the repository at this point in the history
  • Loading branch information
DO1JLR committed Jul 22, 2024
1 parent 7e988b5 commit 932c081
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 4 deletions.
1 change: 1 addition & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sway__keyboard_settings: true
sway__keyboard_lang: 'de'
sway__lock: 'swaylock --color 000000 --image /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png'
sway__waybar: true
sway__waybar_modules_right: ["idle_inhibitor", "custom/weather", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "keyboard-state", "sway/language", "battery", "clock", "tray"]

Check failure on line 21 in defaults/main.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

yaml[line-length]

Line too long (197 > 180 characters)

Check warning on line 21 in defaults/main.yml

View workflow job for this annotation

GitHub Actions / Yamllint

21:181 [line-length] line too long (197 > 180 characters)

sway__keybindings:
- keybinding:

Check failure on line 24 in defaults/main.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

yaml[indentation]

Wrong indentation: expected 2 but found 1

Check failure on line 24 in defaults/main.yml

View workflow job for this annotation

GitHub Actions / Yamllint

24:2 [indentation] wrong indentation: expected 2 but found 1
Expand Down
9 changes: 9 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
ansible.builtin.import_tasks:
file: "config.yml"

- name: Create waybar config
ansible.builtin.include_tasks:
file: "waybar.yml"
loop: "{{ sway__user_list | flatten(1) }}"
loop_control:
loop_var: swayusr
label: "{{ swayusr.home }}/.config/waybar/"
when: sway__waybar | bool

# - name: Ensure i3 packages are installed [Archlinux}
# ansible.builtin.include_tasks:
# file: setup-Archlinux.yml
Expand Down
36 changes: 36 additions & 0 deletions tasks/waybar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- name: Create waybar config folder
become: true
ansible.builtin.file:
path: "{{ swayusr.home }}/.config/waybr/"
state: directory
mode: '0750'
owner: "{{ swayusr.user }}"
group: "{{ swayusr.user }}"

- name: Deploy waybar weather script
become: true
ansible.builtin.template:
src: 'templates/wttr.py.j2'
dest: "{{ swayusr.home }}/.config/waybar/wttr.py"
mode: '0750'
owner: "{{ swayusr.user }}"
group: "{{ swayusr.user }}"

- name: Deploy waybar stylesheet
become: true
ansible.builtin.template:
src: 'templates/waybar.css.j2'
dest: "{{ swayusr.home }}/.config/waybar/style.css"
mode: '0640'
owner: "{{ swayusr.user }}"
group: "{{ swayusr.user }}"

- name: Deploy waybar config
become: true
ansible.builtin.template:
src: 'templates/waybar.json.j2'
dest: "{{ swayusr.home }}/.config/waybar/config"
mode: '0640'
owner: "{{ swayusr.user }}"
group: "{{ swayusr.user }}"
13 changes: 10 additions & 3 deletions templates/waybar.json.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// "width": 1280, // Waybar width
"spacing": 4, // Gaps between modules (4px)
// Choose the order of the modules
"modules-left": ["sway/workspaces", "sway/mode", "sway/scratchpad", "custom/media"],
"modules-left": ["sway/workspaces", "sway/mode", "sway/scratchpad"],
"modules-center": ["sway/window"],
"modules-right": ["idle_inhibitor", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "keyboard-state", "sway/language", "battery", "clock", "tray"],
"modules-right": {{ sway__waybar_modules_right | tojson }},
// Modules configuration
"sway/workspaces": {
"disable-scroll": false,
Expand Down Expand Up @@ -151,6 +151,13 @@
"escape": true,
"exec": "$HOME/.config/waybar/mediaplayer.py 2> /dev/null" // Script in resources folder
// "exec": "$HOME/.config/waybar/mediaplayer.py --player spotify 2> /dev/null" // Filter player based on name
}
},
"custom/weather": {
"format": "{}",
"tooltip": true,
"interval": 900,
"exec": "{{ swayusr.home }}/.config/waybar/wttr.py",
"return-type": "json"
},
}

194 changes: 194 additions & 0 deletions templates/wttr.py.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#!/usr/bin/env python
"""
wttr.in waybar widget
{{ ansible_managed }}
"""
import json
from datetime import datetime
import requests

localization = {
"en": {
"feels_like": "Feels like",
"wind": "Wind",
"humidity": "Humidity",
"today": "Today",
"tomorrow": "Tomorrow",
"weatherDesc": "weatherDesc",
"chanceoffog": "Fog",
"chanceoffrost": "Frost",
"chanceofovercast": "Overcast",
"chanceofrain": "Rain",
"chanceofsnow": "Snow",
"chanceofsunshine": "Sunshine",
"chanceofthunder": "Thunder",
"chanceofwindy": "Wind",
"weatherfor": "Weather for"
},
"de": {
"feels_like": "Gefühlt wie",
"wind": "Wind",
"humidity": "Luftfeuchtigkeit",
"today": "Heute",
"tomorrow": "Morgen",
"weatherDesc": "lang_de",
"chanceoffog": "Nebel",
"chanceoffrost": "Frost",
"chanceofovercast": "Bewölkt",
"chanceofrain": "Regen",
"chanceofsnow": "Schnee",
"chanceofsunshine": "Sonne",
"chanceofthunder": "Gewitter",
"chanceofwindy": "Wind",
"weatherfor": "Wetter für"
},
"el": {
"feels_like": "Πραγματική αίσθηση",
"wind": "Άνεμος",
"humidity": "Υγρασία",
"today": "Σήμερα",
"tomorrow": "Αύριο",
"day_after_tomorrow": "Μεθαύριο",
"weatherDesc": "lang_el",
"chanceoffog": "Ομίχλη",
"chanceoffrost": "Παγετός",
"chanceofovercast": "Πυκνή νέφωση",
"chanceofrain": "Βροχή",
"chanceofsnow": "Χιόνι",
"chanceofsunshine": "Λιακάδα",
"chanceofthunder": "Κεραυνοί",
"chanceofwindy": "Άνεμος",
"weatherfor": "Καιρός για"
}
}

LANG = "de"
text = localization[LANG]

WEATHER_CODES = {
'113': '☀️',
'116': '⛅️',
'119': '☁️',
'122': '☁️',
'143': '🌫',
'176': '🌦',
'179': '🌧',
'182': '🌧',
'185': '🌧',
'200': '⛈',
'227': '🌨',
'230': '❄️',
'248': '🌫',
'260': '🌫',
'263': '🌦',
'266': '🌦',
'281': '🌧',
'284': '🌧',
'293': '🌦',
'296': '🌦',
'299': '🌧',
'302': '🌧',
'305': '🌧',
'308': '🌧',
'311': '🌧',
'314': '🌧',
'317': '🌧',
'320': '🌨',
'323': '🌨',
'326': '🌨',
'329': '❄️',
'332': '❄️',
'335': '❄️',
'338': '❄️',
'350': '🌧',
'353': '🌦',
'356': '🌧',
'359': '🌧',
'362': '🌧',
'365': '🌧',
'368': '🌨',
'371': '❄️',
'374': '🌧',
'377': '🌧',
'386': '⛈',
'389': '🌩',
'392': '⛈',
'395': '❄️'
}

data = {}


weather = requests.get(f"https://{LANG}.wttr.in/?format=j1", timeout=10).json()


def format_time(time):
"""
Make sure times get properly formated...
"""
return time.replace("00", "").zfill(2)


def format_temp(temp):
"""
Make sure temperature get properly formated...
"""
return (f"{temp}°").ljust(3)

def format_chances(format_hour):
"""
Make sure weather wil be properly ranslated
"""
chances = [
"chanceoffog",
"chanceoffrost",
"chanceofovercast",
"chanceofrain",
"chanceofsnow",
"chanceofsunshine",
"chanceofthunder",
"chanceofwindy"
]

probs = {text[e]: int(prb) for e, prb in format_hour.items() if e in chances and int(prb) > 0}
sorted_probs = {e: probs[e] for e in sorted(probs, key=probs.get, reverse=True)}
conditions = [f"{event} {prb}%" for event, prb in sorted_probs.items()]
return ", ".join(conditions)

data['text'] = f"{weather['nearest_area'][0]['areaName'][0]['value']}: "
data['text'] += f"{WEATHER_CODES[weather['current_condition'][0]['weatherCode']]} "
data['text'] += f"{weather['current_condition'][0]['FeelsLikeC']}°C"

WEATHER_DESC = text['weatherDesc']
data['tooltip'] = f"<b>{text['weatherfor']} "
data['tooltip'] += f"{weather['nearest_area'][0]['areaName'][0]['value']}, "
data['tooltip'] += f"{weather['nearest_area'][0]['country'][0]['value']}:</b>\n\n"
data['tooltip'] += f"<b>{weather['current_condition'][0][WEATHER_DESC][0]['value']}"
data['tooltip'] += f"{weather['current_condition'][0]['temp_C']}°</b>\n"
data['tooltip'] += f"{text['feels_like']}: "
data['tooltip'] += f"{weather['current_condition'][0]['FeelsLikeC']}°\n"
data['tooltip'] += f"{text['wind']}: "
data['tooltip'] += f"{weather['current_condition'][0]['windspeedKmph']}Km/h\n"
data['tooltip'] += f"{text['humidity']}: {weather['current_condition'][0]['humidity']}%\n"
for i, day in enumerate(weather['weather']):
data['tooltip'] += "\n<b>"
if i == 0:
data['tooltip'] += f"{text['today']}, "
if i == 1:
data['tooltip'] += f"{text['tomorrow']}, "
if i == 2 and 'day_after_tomorrow' in text:
data['tooltip'] += f"{text['day_after_tomorrow']}, "
data['tooltip'] += f"{day['date']}</b>\n"
data['tooltip'] += f"⬆️ {day['maxtempC']}° ⬇️ {day['mintempC']}° "
data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n"
for hour in day['hourly']:
if i == 0:
if int(format_time(hour['time'])) < datetime.now().hour-2:
continue
data['tooltip'] += f"{format_time(hour['time'])} "
data['tooltip'] += f"{WEATHER_CODES[hour['weatherCode']]} "
data['tooltip'] += f"{format_temp(hour['FeelsLikeC'])} "
data['tooltip'] += f"{hour[WEATHER_DESC][0]['value']}, {format_chances(hour)}\n"


print(json.dumps(data))
2 changes: 1 addition & 1 deletion vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
playbook_version_number: 4 # should be int
playbook_version_number: 5 # should be int
playbook_version_path: 'role-roles-ansible.sway.version'

0 comments on commit 932c081

Please sign in to comment.