From 0c800a24d5520d5986cf3f5d78dfa667503aeccf Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Mon, 20 Aug 2018 12:54:58 -0500
Subject: [PATCH] Polybar: Add network status module

---
 .config/polybar/amidown.sh | 60 ++++++++++++++++++++++++++++++++++++++
 .config/polybar/config     | 10 ++++++-
 2 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100755 .config/polybar/amidown.sh

diff --git a/.config/polybar/amidown.sh b/.config/polybar/amidown.sh
new file mode 100755
index 00000000..1ed8fd14
--- /dev/null
+++ b/.config/polybar/amidown.sh
@@ -0,0 +1,60 @@
+#! /bin/bash
+#
+# netstatus.sh
+# Copyright (C) 2018 rehashedsalt <salt@lap-th-e560-0>
+#
+# Distributed under terms of the MIT license.
+#
+
+# Global vars
+if_default=""
+ipv4_private=""
+
+# Define our tests
+function test-gateway {
+	if ip r | grep default > /dev/null 2>&1; then
+		ipr="$(ip r | grep default)"
+		ipv4_private="$(echo $ipr | awk '{print $3}')"
+		if_default="$(echo $ipr | awk '{print $5}')"
+		unset ipr
+		return 0
+	else
+		return 1
+	fi
+}
+
+function test-internet {
+	if ping -c 1 8.8.8.8 > /dev/null 2>&1; then
+		return 0
+	else
+		return 1
+	fi
+}
+
+function test-dns {
+	if ping -c 1 google.com > /dev/null 2>&1; then
+		return 0
+	else
+		return 1
+	fi
+}
+
+# Actually go through with them
+if ! test-gateway; then
+	printf "NONE"
+	exit 1
+fi
+
+if ! test-internet; then
+	printf "LOCAL"
+	exit 2
+fi
+
+if ! test-dns; then
+	printf "NO DNS"
+	exit 3
+fi
+
+printf "UP"
+exit 0
+
diff --git a/.config/polybar/config b/.config/polybar/config
index ca694bac..a93f5d3c 100644
--- a/.config/polybar/config
+++ b/.config/polybar/config
@@ -88,7 +88,7 @@ border-top-size = 0
 
 modules-left = ${env:PB_BAR_PRIMARY_MODULES_LEFT:battery cpu memory}
 modules-center = ${env:PB_BAR_PRIMARY_MODULES_CENTER: bspwm}
-modules-right = ${env:PB_BAR_PRIMARY_MODULES_RIGHT:xbacklight volume date}
+modules-right = ${env:PB_BAR_PRIMARY_MODULES_RIGHT:amidown xbacklight volume date}
 
 [bar/primary-2]
 monitor = ${env:PB_MONITOR:eDP-1}
@@ -144,6 +144,14 @@ content = " "
 
 # OTHER
 #====================
+[module/amidown]
+type = custom/script
+format-prefix = "NET: "
+format-prefix-foreground = ${res/colors.alert}
+exec = ~/.config/polybar/amidown.sh
+exec-if = ! ping -c 1 google.com
+interval = 15
+
 [module/battery]
 type = internal/battery
 format-charging = <label-charging>