William Giokas avatar William Giokas committed a9f74c7

Ability to print to stdout

Added in the ability to print to stdout. status() now takes an int, 0
meaning that it will print to the status bar, anything else making it
print out to stdout. May add in more, though with text output it can
pretty easily be piped to other bars.

get_netusage has a sleep(1) in its function, so having it in the
status() func was making it take 2 seconds to update instead of 1. Fixed
that by changing to sleep(0) in status().

Signed-off-by: William Giokas <1007380@gmail.com>;

Comments (0)

Files changed (2)

 .TH DWMSTATUS 1 dwmstatus\-VERSION
 .SH NAME
-dwmstatus \- simple status bar for dwm
+dwmstatus \- simple status bar
 .SH SYNOPSIS
 .B dwmstatus
-.RB [ \-v ]
+.RB [ \-vo ]
 .SH DESCRIPTION
-dwmstatus is a simple, small status bar for dwm. Unlike large, complex shell
-scripts, this status bar tries to stay clean and concise, has no configuration
-file, and must be recompiled to change settings, similar to dwm. 
+dwmstatus is a simple, small status bar originally for dwm. Unlike large,
+complex shell scripts, this status bar tries to stay clean and concise, has no
+configuration file, and must be recompiled to change settings, similar to dwm.
+It can output to the dwm statusbar (set the xroot label) or to stdout for use
+with other status bars or programs. 
 .P
 The default setup has 5 sections (from left to right): network usage, battery
 status, computer load, temperature, and time. These are separated by pipes, and
 .TP
 .B \-v
 Prints version information to stdout then exits with status 0
+.B \-o
+Print status to stdout instead of updating the status bar
 .SH USAGE
 .TP
 Run dwbstatus in a shell and it will put its output into the dwm status bar
     minutes = seconds / 60;
     seconds -= (minutes *60);
 
-    ret = smprintf("%s: %.2f%% %02d:%02d:%02d", stat, (((float)remcap / (float)descap) * 100), hours, minutes, seconds);
+    if (seconds < 0 || minutes < 0 || hours < 0)
+        ret = smprintf("%s: Calculating...", stat);
+    else
+        ret = smprintf("%s: %.2f%% %02d:%02d:%02d", stat, (((float)remcap / (float)descap) * 100), hours, minutes, seconds);
     if(!stat) { free(stat); }
     return ret;
-
 }
+
 /* END BATTERY USAGE
  *
  * NETWORK STUFF
  * setup the status bar here
  */
 int
-status()
+status(int tostatusbar)
 {
     char *status = NULL;
     char *avgs = NULL;
     time_t count60 = 0;
     time_t count10 = 0;
 
-    if (!(dpy = XOpenDisplay(NULL))) {
+    if (!(dpy = XOpenDisplay(NULL)) && tostatusbar == 0) {
         fprintf(stderr, "dwmstatus: cannot open display.\n");
         return 1;
     }
 
-    for (;;sleep(1)) {
+    for (;;sleep(0)) {
         /* Update every minute */
         if (runevery(&count60, 60)) {
             free(time);
             avgs   = loadavg();
             batt   = getbattery(BATT_PATH);
             temp   = gettemperature(TEMP_SENSOR_PATH, TEMP_SENSOR_UNIT);
-            if(!temp) { free(temp); }
+            if(!temp) free(temp);
         }
         /* Update every second */
         net    = get_netusage(net_device_up);
         /* Format of display */
         status = smprintf("%s (%s) | %s [%s] T %s | %s",
                 net, ipaddr, batt, avgs, temp, time);
-        if(!ipaddr) { free(ipaddr); }
+        if(!ipaddr) free(ipaddr);
         free(net);
-        setstatus(status);
+
+        if(tostatusbar == 0)
+            setstatus(status);
+        else
+            puts(status);
+
         free(status);
     }
     return 0;
                 VERSION);
         return 0;
     }
+    else if(argc == 2 && !strcmp("-o", argv[1])) {
+        status(1);
+        return 0;
+    }
     else if(argc != 1) {
-        printf("usage: dwmstatus [-v]\n");
+        printf("usage: dwmstatus [-v] [-o]\n");
         return 1;
     }
     else
-        status();
+        status(0);
     XCloseDisplay(dpy);
     return 0;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.