]> Git repositories of Lucie Scarlet - dotfiles.git/commitdiff
Fixed error when uptime is less than a day.
authorLucie Scarlet <lucie@scarlet.moe>
Sun, 21 Jul 2024 20:28:26 +0000 (22:28 +0200)
committerLucie Scarlet <lucie@scarlet.moe>
Sun, 21 Jul 2024 20:28:26 +0000 (22:28 +0200)
Also updated the formatting a bit and added more typings

i3/uptime.py

index c00aaad9d2843da7acf8d8c2e426b749f4e13d81..01ee9c8af2b056ce5d7c937ac99be8d54c33d0f0 100644 (file)
@@ -6,17 +6,22 @@ def main() -> int:
     with open("/proc/uptime", "r") as uptime:
         raw_uptime = uptime.read()
     text_uptime: str = raw_uptime.split()[0]
-    raw_formatted = timedelta(seconds=(float(text_uptime)))
+    raw_formatted: timedelta = timedelta(seconds=(float(text_uptime)))
     well_formatted: str = str(raw_formatted).split(".")[0][:-3]
     fmt = well_formatted.split(".")[0]
-    f2 = fmt.split(' days, ')[1]
-    days = fmt.split(' days, ')[0]
+    daysfmt = fmt.split(" days, ")
+    if len(daysfmt) > 2:
+        f2: str = daysfmt[1]
+        days: str | None = daysfmt[0]
+    else:
+        f2 = daysfmt[0]
+        days = None
     hours = f2.split(":")[0]
     mins = f2.split(":")[1]
-    output_d = f"{days}d" if days else ""
-    output_h = f"{hours}h"
+    output_d = f"{days}d " if days else ""
+    output_h = f"{hours}h "
     output_m = f"{mins}m"
-    print(f"Uptime: {output_d} {output_h} {output_m}")
+    print(f"Uptime: {output_d}{output_h}{output_m}")
     return 0