Skip to content

Commit

Permalink
Update wlan script
Browse files Browse the repository at this point in the history
  • Loading branch information
DO1JLR committed Aug 22, 2023
1 parent eeb97cb commit 5283727
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 71 deletions.
78 changes: 78 additions & 0 deletions files/i3blocks/wlan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import re
import subprocess
import sys
import argparse

def find_interface(interfaces):
for iface in interfaces:
if os.path.exists(f"/sys/class/net/{iface}"):
return iface
return None

def get_signal_quality(interface):
try:
iw_output = subprocess.check_output(["/sbin/iw", "dev", interface, "link"], text=True)
signal_match = re.search(r'(\-\d{2}) dBm', iw_output)
return int(signal_match.group(1)) if signal_match else None
except subprocess.CalledProcessError:
return None

def get_ssid(interface):
try:
iw_output = subprocess.check_output(["/sbin/iw", "dev", interface, "link"], text=True)
ssid_match = re.search(r'SSID: (.+)', iw_output)
return ssid_match.group(1).encode("utf-8").decode("unicode-escape") if ssid_match else None
except subprocess.CalledProcessError:
return None

def print_color(quality):
if quality >= 70:
print("#00FF00")
elif quality >= 50:
print("#CCFF00")
elif quality >= 30:
print("#FFFF00")
elif quality >= 10:
print("#FFAA00")
else:
print("#FF0000")

def main():
interfaces = ["wlp3s0", "wlan0"]
parser = argparse.ArgumentParser(description="Display WiFi signal strength and SSID")
parser.add_argument("--interface", help="Specify the wireless interface")
args = parser.parse_args()

if args.interface:
interface = args.interface
else:
interface = find_interface(interfaces)

if not interface:
sys.exit(0)

signal_quality = get_signal_quality(interface)
ssid = get_ssid(interface)

if signal_quality is None:
sys.exit(1)

# quality = int((signal_quality / 70) * 100) # Adjust the calculation as needed
quality = int(signal_quality) # Adjust the calculation as needed

emoji_signal = "📶"

if ssid:
print(f"{emoji_signal} {ssid} {quality}dBm")
else:
print(f"{emoji_signal} {quality}dBm")

print_color(quality)

if __name__ == "__main__":
main()

70 changes: 0 additions & 70 deletions files/i3blocks/wlan.sh

This file was deleted.

2 changes: 1 addition & 1 deletion tasks/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
group: "{{ i3wm_usr.user }}"
with_items:
- 'volume.sh'
- 'wlan.sh'
- 'wlan.py'
- 'battery.py'

- name: Create rofi config folder
Expand Down

0 comments on commit 5283727

Please sign in to comment.