]> git.sur5r.net Git - i3/i3status/commitdiff
Implement disk info (%free/%used/%total)
authorMichael Stapelberg <michael@stapelberg.de>
Sun, 11 Oct 2009 21:27:26 +0000 (23:27 +0200)
committerMichael Stapelberg <michael@stapelberg.de>
Sun, 11 Oct 2009 21:27:26 +0000 (23:27 +0200)
i3status.c
i3status.h
src/print_disk_info.c [new file with mode: 0644]

index 141a98477747af7467d979cd825a4e2fae9ba5fd..e72b6779e028a3a92e9b6b619d295bba24eb21c5 100644 (file)
@@ -114,6 +114,11 @@ int main(int argc, char *argv[]) {
                 CFG_END()
         };
 
+        cfg_opt_t disk_opts[] = {
+                CFG_STR("format", "%free", CFGF_NONE),
+                CFG_END()
+        };
+
         cfg_opt_t opts[] = {
                 CFG_STR_LIST("order", "{ipv6,\"run_watch DHCP\",\"wireless wlan0\",\"ethernet eth0\",\"battery 0\",\"cpu_temperature 0\",load,time}", CFGF_NONE),
                 CFG_SEC("general", general_opts, CFGF_NONE),
@@ -122,6 +127,7 @@ int main(int argc, char *argv[]) {
                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
+                CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
                 CFG_SEC("ipv6", ipv6_opts, CFGF_TITLE),
                 CFG_SEC("time", time_opts, CFGF_NONE),
                 CFG_SEC("load", load_opts, CFGF_NONE),
@@ -181,6 +187,9 @@ int main(int argc, char *argv[]) {
                         CASE_SEC_TITLE("run_watch")
                                 print_run_watch(title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
 
+                        CASE_SEC_TITLE("disk")
+                                print_disk_info(title, cfg_getstr(sec, "format"));
+
                         CASE_SEC("load")
                                 print_load(cfg_getstr(sec, "format"));
 
index d00d3189586e8fef2525568c5f888153bd16df23..b6fdd865d389df8be00734d6527e598b6bfd51ab 100644 (file)
@@ -62,6 +62,7 @@ char *color(const char *colorstr);
 char *endcolor() __attribute__ ((pure));
 
 void print_ipv6_info(const char *format);
+void print_disk_info(const char *path, const char *format);
 void print_battery_info(int number, const char *format);
 void print_time(const char *format);
 const char *get_ip_addr();
diff --git a/src/print_disk_info.c b/src/print_disk_info.c
new file mode 100644 (file)
index 0000000..84524c3
--- /dev/null
@@ -0,0 +1,67 @@
+// vim:ts=8:expandtab
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdint.h>
+#include <sys/statvfs.h>
+#include <sys/types.h>
+
+#include "i3status.h"
+
+#define TERABYTE (1024ULL * 1024 * 1024 * 1024)
+#define GIGABYTE (1024ULL * 1024 * 1024)
+#define MEGABYTE (1024ULL * 1024)
+#define KILOBYTE (1024ULL)
+
+void print_bytes_human(uint64_t bytes) {
+        /* 1 TB */
+        if (bytes > TERABYTE)
+                printf("%f TB", (double)bytes / TERABYTE);
+        else if (bytes > GIGABYTE)
+                printf("%.01f GB", (double)bytes / GIGABYTE);
+        else if (bytes > MEGABYTE)
+                printf("%.01f MB", (double)bytes / MEGABYTE);
+        else if (bytes > KILOBYTE)
+                printf("%.01f KB", (double)bytes / KILOBYTE);
+        else {
+                printf("%.01f B", (double)bytes);
+        }
+
+}
+
+/*
+ * Just parses /proc/net/wireless looking for lines beginning with
+ * wlan_interface, extracting the quality of the link and adding the
+ * current IP address of wlan_interface.
+ *
+ */
+void print_disk_info(const char *path, const char *format) {
+        const char *walk;
+        struct statvfs buf;
+
+        if (statvfs(path, &buf) == -1)
+                return;
+
+        for (walk = format; *walk != '\0'; walk++) {
+                if (*walk != '%') {
+                        putchar(*walk);
+                        continue;
+                }
+
+                if (BEGINS_WITH(walk+1, "free")) {
+                        print_bytes_human(buf.f_bsize * buf.f_bfree);
+                        walk += strlen("free");
+                }
+
+                if (BEGINS_WITH(walk+1, "used")) {
+                        print_bytes_human(buf.f_bsize * (buf.f_blocks - buf.f_bfree));
+                        walk += strlen("used");
+                }
+
+                if (BEGINS_WITH(walk+1, "total")) {
+                        print_bytes_human(buf.f_bsize * buf.f_blocks);
+                        walk += strlen("total");
+                }
+        }
+}