├── README.md ├── LICENSE └── bandwidth.py /README.md: -------------------------------------------------------------------------------- 1 | A waybar custom module for showing current internet speed, inspired by [this issue](https://github.com/Alexays/Waybar/issues/2162) 2 | 3 | ![image](https://github.com/user-attachments/assets/bbcd94b3-e41b-4292-8ebd-48fa5b1a9a83) 4 | 5 | Blue for download speed, red for upload. 6 | 7 | # Usage: 8 | 1. Download the file `bandwidth.py` 9 | 2. Add it to the config file: 10 | ```json 11 | "modules-right": [ 12 | "custom/bandwidth", 13 | ], 14 | "custom/bandwidth": { 15 | "exec": "$HOME/.config/waybar/bandwidth.py" 16 | }, 17 | ``` 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 aj3423 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bandwidth.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import subprocess 4 | from time import sleep 5 | 6 | ### Configuration 7 | 8 | refresh_interval = 500 # in milliseconds 9 | rx_min = 200 * 1024 # 200 KB, the min download speed 10 | rx_max = 12 * 1024**2 # 12 MB, the max download speed 11 | tx_min = 200 * 1024 # 200 KB, the min upload speed 12 | tx_max = 5 * 1024**2 # 5 MB, the max upload speed 13 | rx_color = "#0080c0" # blue, the color for download speed 14 | tx_color = "#fa7070" # red, the color for upload speed 15 | 16 | ### End Configuration 17 | 18 | 19 | def default_interface(): 20 | process = subprocess.run( 21 | ["ip", "route"], check=True, text=True, capture_output=True 22 | ) 23 | for line in process.stdout.splitlines(): 24 | if line.startswith("default via"): 25 | return line.split()[4] 26 | raise RuntimeError("No default interface found") 27 | 28 | 29 | def get_rx_tx_bytes(iface): 30 | with open("/proc/net/dev") as f: 31 | for line in f: 32 | line = line.strip() 33 | if not line.startswith(f"{iface}:"): 34 | continue 35 | rx_bytes = int(line.split()[1]) 36 | tx_bytes = int(line.split()[9]) 37 | return rx_bytes, tx_bytes 38 | raise RuntimeError("Interface not found") 39 | 40 | 41 | def bar(current, min, max, color): 42 | # don't show anything if the current value is less than `min` 43 | if current < min: 44 | return " " 45 | 46 | levels = 7 47 | 48 | labels = [ 49 | "▁", # 0 50 | "▂", # 1 51 | "▄", # 2 52 | "▅", # 3 53 | "▆", # 4 54 | "▇", # 5 55 | "█", # 6 56 | ] 57 | 58 | level_size = max // levels 59 | level = current // level_size 60 | if level >= levels: 61 | level = levels - 1 62 | 63 | label = labels[level] 64 | 65 | return f"{label}" # blue 66 | 67 | 68 | def main(): 69 | iface = default_interface() 70 | 71 | rx_bytes, tx_bytes = get_rx_tx_bytes(iface) 72 | 73 | while True: 74 | prev_rx_bytes, prev_tx_bytes = rx_bytes, tx_bytes 75 | rx_bytes, tx_bytes = get_rx_tx_bytes(iface) 76 | rx_current = (rx_bytes - prev_rx_bytes) * 1000 // refresh_interval 77 | tx_current = (tx_bytes - prev_tx_bytes) * 1000 // refresh_interval 78 | 79 | rx_bar = bar(rx_current, rx_min, rx_max, rx_color) 80 | tx_bar = bar(tx_current, tx_min, tx_max, tx_color) 81 | line = f"{rx_bar} {tx_bar}" 82 | print(line, flush=True) 83 | sleep(refresh_interval / 1000) # Convert ms to seconds 84 | 85 | 86 | if __name__ == "__main__": 87 | main() 88 | --------------------------------------------------------------------------------