From 3c905d717a331b9be6d7036624378d0617234a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Wed, 8 Jun 2022 22:01:23 +0200 Subject: Update i3blocks --- config/i3blocks/scripts/iface | 61 +++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'config/i3blocks/scripts/iface') diff --git a/config/i3blocks/scripts/iface b/config/i3blocks/scripts/iface index abac9c0..ffa576e 100755 --- a/config/i3blocks/scripts/iface +++ b/config/i3blocks/scripts/iface @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Copyright (C) 2014 Julien Bonjean # Copyright (C) 2014 Alexander Keller @@ -17,38 +17,61 @@ #------------------------------------------------------------------------ -# Locate current default interface, but use only wlp (wifi) or enp (ethernet) -# TODO probably also add connection over phone -IF=$(ip route | awk '/^default .* (wlp|wlo|enp)/ { print $5 ; exit }') +# Use the provided interface, otherwise the device used for the default route. +IF="${IFACE:-$BLOCK_INSTANCE}" +for version in 4 6; do + IF="${IF:-$(ip -$version route | sed -n 's/^default.*dev \([^ ]*\).*/\1/p')}" +done + +# Exit if there is no default route +[[ -z "$IF" ]] && exit #------------------------------------------------------------------------ # As per #36 -- It is transparent: e.g. if the machine has no battery or wireless # connection (think desktop), the corresponding block should not be displayed. [[ ! -d /sys/class/net/${IF} ]] && exit -# Note this passes if IF is empty! #------------------------------------------------------------------------ -# If no IF is selected then we want see down, not anything else. -if [ -z "$IF" ] || [[ "$(cat /sys/class/net/$IF/operstate)" = 'down' ]]; then +AF=${ADDRESS_FAMILY:-"inet6\?"} +LABEL="${LABEL:-}" + +for flag in "$1" "$2"; do + case "$flag" in + -4) + AF=inet ;; + -6) + AF=inet6 ;; + -L) + if [[ "$IF" = "" ]]; then + LABEL="iface" + else + LABEL="$IF:" + fi ;; + esac +done + +if [[ "$IF" = "" ]] || [[ "$(cat /sys/class/net/$IF/operstate)" = 'down' ]]; then + echo "${LABEL} down" # full text + echo "${LABEL} down" # short text + echo \#FF0000 # color exit fi -case $1 in - -4) - AF=inet ;; - -6) - AF=inet6 ;; - *) - AF=inet6? ;; -esac - -# Use the first global scope address -IPADDR=$(ip addr show $IF | perl -n -e "/$AF ([^\/]+).* scope global/ && print \$1 and exit") +# Use the first global scope address that matches limitation and preffer not local one) +while read -r ipaddr; do + case "$ipaddr" in + ""|169\.*|f*) + IPADDR="${IPADDR:-$ipaddr}" + continue # try another one + esac + IPADDR="$ipaddr" + break +done <<<"$(ip addr show "$IF" | sed -n 's/.*inet6\? \([^/]\+\).* scope global.*/\1/p')" case $BLOCK_BUTTON in - 3) echo -n "$IPADDR" | xclip -q -se c ;; + 3) echo -n "$IPADDR" | xclip -q -se c ;; esac #------------------------------------------------------------------------ -- cgit v1.2.3