aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgotmor <gotmor@f2baff5b-bf2c-0410-a398-912abdc3d8b2>2008-01-04 21:57:27 +0000
committergotmor <gotmor@f2baff5b-bf2c-0410-a398-912abdc3d8b2>2008-01-04 21:57:27 +0000
commited61be747386029fcf009002c5bf02126aebd835 (patch)
tree39c931a580f8b6521e96421fc33537cbe3a49e87
parentfacf378ae7400168a95bdfec8126cd251d25a02e (diff)
downloaddzen-ed61be747386029fcf009002c5bf02126aebd835.tar.gz
dzen-ed61be747386029fcf009002c5bf02126aebd835.zip
made dbar functions reusable
gdbar, gcpubar make use of the api git-svn-id: http://dzen.googlecode.com/svn/trunk@190 f2baff5b-bf2c-0410-a398-912abdc3d8b2
-rw-r--r--gadgets/Makefile12
-rw-r--r--gadgets/README.gcpubar6
-rw-r--r--gadgets/README.gdbar5
-rw-r--r--gadgets/dbar-main.c86
-rw-r--r--gadgets/dbar.c276
-rw-r--r--gadgets/dbar.h39
-rw-r--r--gadgets/gcpubar.c218
-rw-r--r--gadgets/gdbar.c337
8 files changed, 430 insertions, 549 deletions
diff --git a/gadgets/Makefile b/gadgets/Makefile
index 6ea8d3e..b08b5bf 100644
--- a/gadgets/Makefile
+++ b/gadgets/Makefile
@@ -3,7 +3,7 @@
include config.mk
-SRC = dbar.c gdbar.c gcpubar.c textwidth.c
+SRC = dbar.c dbar-main.c gdbar.c gcpubar.c textwidth.c
OBJ = ${SRC:.c=.o}
all: options dbar gdbar gcpubar textwidth
@@ -19,21 +19,23 @@ options:
@echo CC $<
@${CC} -c ${CFLAGS} $<
-${OBJ}: config.mk
+${OBJ}: dbar.h config.mk
dbar: ${OBJ}
@echo LD $@
- @${LD} -o $@ dbar.o ${LDFLAGS}
+ @${LD} -o $@ dbar-main.o dbar.o ${LDFLAGS}
@strip $@
gdbar: ${OBJ}
@echo LD $@
- @${LD} -o $@ gdbar.o ${LDFLAGS} -L${X11LIB} -lX11
+# @${LD} -o $@ gdbar.o dbar.o ${LDFLAGS} -L${X11LIB} -lX11
+ @${LD} -o $@ gdbar.o dbar.o ${LDFLAGS}
@strip $@
gcpubar: ${OBJ}
@echo LD $@
- @${LD} -o $@ gcpubar.o ${LDFLAGS} -L${X11LIB}
+# @${LD} -o $@ gcpubar.o dbar.o ${LDFLAGS} -L${X11LIB}
+ @${LD} -o $@ gcpubar.o dbar.o ${LDFLAGS}
@strip $@
textwidth: ${OBJ}
diff --git a/gadgets/README.gcpubar b/gadgets/README.gcpubar
index 2ee1bb4..74caa6d 100644
--- a/gadgets/README.gcpubar
+++ b/gadgets/README.gcpubar
@@ -15,8 +15,8 @@ Command line options:
less than 1 for intervals
shorter than a second
- -c : Terminate after diplaying (default: infinite)
- argument to '-c' values
+ -c : Number of times to display (default: infinite)
+ the meter
-w : Width of the meter in pixels (default: 100)
@@ -27,7 +27,7 @@ Command line options:
-bg : Meter background color (default: darkgrey)
-s : Style, can be either o(utlined)
- gauge, g(raph) or the default
+ v(ertical), g(raph) or the default
gauge if no parameter is specified
-gw : Width of the graph elements (default: 1)
diff --git a/gadgets/README.gdbar b/gadgets/README.gdbar
index 3bfbcde..9c810e0 100644
--- a/gadgets/README.gdbar
+++ b/gadgets/README.gdbar
@@ -14,6 +14,11 @@ Options:
-fg : foreground color of the meter (default: white)
-bg : background color of the meter (default: darkgrey)
+ -s : Style, can be either o(utlined), v(ertical),
+ or the default gauge if no parameter is specified
+ -sw : Width of the segments
+ -sh : Height of the vertical segments
+ -ss : Space between the segments
-max : Value to be considered 100% (default: 100)
-min : Value to be considered 0% (default: 0 )
-w : Size in pixels to be
diff --git a/gadgets/dbar-main.c b/gadgets/dbar-main.c
new file mode 100644
index 0000000..731d676
--- /dev/null
+++ b/gadgets/dbar-main.c
@@ -0,0 +1,86 @@
+/*
+ dbar - ascii percentage meter
+
+ Copyright (c) 2007 by Robert Manea <rob dot manea at gmail dot com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+
+#include <stdio.h>
+#include "dbar.h"
+
+#define MAXLEN 512
+
+int main(int argc, char *argv[]) {
+ int i, nv;
+ char aval[MAXLEN], *endptr;
+ Dbar dbar;
+
+ dbardefaults(&dbar, textual);
+
+ for(i=1; i < argc; i++) {
+ if(!strncmp(argv[i], "-w", 3)) {
+ if(++i < argc)
+ dbar.width = atoi(argv[i]);
+ }
+ else if(!strncmp(argv[i], "-s", 3)) {
+ if(++i < argc)
+ dbar.sym = argv[i][0];
+ }
+ else if(!strncmp(argv[i], "-max", 5)) {
+ if(++i < argc) {
+ dbar.maxval = strtod(argv[i], &endptr);
+ if(*endptr) {
+ fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
+ return EXIT_FAILURE;
+ }
+ }
+ }
+ else if(!strncmp(argv[i], "-min", 5)) {
+ if(++i < argc) {
+ dbar.minval = strtod(argv[i], &endptr);
+ if(*endptr) {
+ fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
+ return EXIT_FAILURE;
+ }
+ }
+ }
+ else if(!strncmp(argv[i], "-l", 3)) {
+ if(++i < argc)
+ dbar.label = argv[i];
+ }
+ else if(!strncmp(argv[i], "-nonl", 6)) {
+ dbar.pnl = 0;
+ }
+ else {
+ fprintf(stderr, "usage: dbar [-w <characters>] [-s <symbol>] [-min <minvalue>] [-max <maxvalue>] [-l <string>] [-nonl]\n");
+ return EXIT_FAILURE;
+ }
+ }
+
+ while(fgets(aval, MAXLEN, stdin)) {
+ nv = sscanf(aval, "%lf %lf %lf", &dbar.val, &dbar.minval, &dbar.maxval);
+ if(nv == 2) {
+ dbar.maxval = dbar.minval;
+ dbar.minval = 0;
+ }
+ fdbar(&dbar, stdout);
+ }
+}
diff --git a/gadgets/dbar.c b/gadgets/dbar.c
index 93dada7..177cdb9 100644
--- a/gadgets/dbar.c
+++ b/gadgets/dbar.c
@@ -1,137 +1,161 @@
-/*
- dbar - ascii percentage meter
-
- Copyright (c) 2007 by Robert Manea <rob dot manea at gmail dot com>
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-*/
-
-
-#include<stdio.h>
-#include<stdlib.h>
-#include<unistd.h>
-#include<string.h>
-
-#define MAXLEN 512
-
-static void pbar (const char*, double, int, char, int);
-
-
-static void
-pbar(const char* label, double perc, int maxc, char sym, int pnl) {
- int i, rp;
- double l;
-
- l = perc * ((double)maxc / 100);
- if((int)(l + 0.5) >= (int)l)
- l = l + 0.5;
-
- if((int)(perc + 0.5) >= (int)perc)
- rp = (int)(perc + 0.5);
- else
- rp = (int)perc;
-
- if(label)
- printf("%s %3d%% [", label, rp);
- else
- printf("%3d%% [", rp);
-
- for(i=0; i < (int)l; i++)
- if(i == maxc) {
- putchar('>');
- break;
- } else
- putchar(sym);
-
- for(; i < maxc; i++)
- putchar(' ');
-
- printf("]%s", pnl ? "\n" : "");
- fflush(stdout);
+#include "dbar.h"
+
+void
+dbardefaults(Dbar *dbar, int mode) {
+ dbar->bg = "darkgrey";
+ dbar->fg = "white";
+ dbar->label = NULL;
+ dbar->sym = '=';
+ dbar->val = 0;
+ dbar->minval = 0;
+ dbar->maxval = 100.0;
+ dbar->mode = mode ? graphical : textual;
+ dbar->style = norm;
+ dbar->width = mode ? 80 : 25;
+ dbar->height = 10;
+ dbar->segw = 6;
+ dbar->segh = 2;
+ dbar->segb = 0;
+ dbar->gs = 0;
+ dbar->gw = 1;
+ dbar->gc = 0;
+ dbar->pnl = 1;
+ memset(dbar->gb, '\0', MAX_GRAPH_VALS);
}
- int
-main(int argc, char *argv[])
-{
- int i, nv;
- double val;
- char aval[MAXLEN], *endptr;
+void
+fdbar(Dbar *dbar, FILE *stream) {
+ int i, rp, p;
+ int segs, segsa;
+ double l, perc;
- /* defaults */
- int maxchars = 25;
- double minval = 0;
- double maxval = 100.0;
- char psym = '=';
- int print_nl = 1;
- const char *label = NULL;
+ perc = (100 * (dbar->val - dbar->minval)) / (dbar->maxval - dbar->minval);
+ switch(dbar->style) {
+ case outlined:
+ l = perc * ((double)(dbar->width-2) / 100);
+ break;
+ case vertical:
+ l = perc * ((double)dbar->height / 100);
+ break;
+ case graph:
+ l = perc * ((double)dbar->height / 100);
+ break;
+ default:
+ l = perc * ((double)dbar->width / 100);
+ break;
+ }
- for(i=1; i < argc; i++) {
- if(!strncmp(argv[i], "-w", 3)) {
- if(++i < argc)
- maxchars = atoi(argv[i]);
- }
- else if(!strncmp(argv[i], "-s", 3)) {
- if(++i < argc)
- psym = argv[i][0];
- }
- else if(!strncmp(argv[i], "-max", 5)) {
- if(++i < argc) {
- maxval = strtod(argv[i], &endptr);
- if(*endptr) {
- fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
- return EXIT_FAILURE;
+ l=(int)(l + 0.5) >= (int)l ? l+0.5 : l;
+ rp=(int)(perc + 0.5) >= (int)perc ? (int)(perc + 0.5) : (int)perc;
+
+ if(dbar->mode == textual) {
+ fprintf(stream, "%s%3d%% [", dbar->label ? dbar->label : "", rp);
+ for(i=0; i < (int)l; i++)
+ if(i == dbar->width) {
+ fputc('>', stream);
+ break;
+ } else
+ fputc(dbar->sym, stream);
+ for(; i < dbar->width; i++)
+ fputc(' ', stream);
+ fprintf(stream, "]%s", dbar->pnl ? "\n" : "");
+ } else {
+ switch(dbar->style) {
+ case outlined:
+ if(dbar->segb == 0) {
+ fprintf(stream, "%s%3d%% ^ib(1)^fg(%s)^ro(%dx%d)^p(%d)^fg(%s)^r(%dx%d)^p(%d)^ib(0)^fg()%s",
+ dbar->label ? dbar->label : "", rp,
+ dbar->bg, (int)dbar->width, dbar->height, -1*(dbar->width-2),
+ dbar->fg, (int)l, dbar->height-4>0?dbar->height-4:1,
+ dbar->width-(int)l-1, dbar->pnl ? "\n" : "");
+ } else {
+ segs = dbar->width / (dbar->segw + dbar->segb);
+ segsa = rp * segs / 100;
+
+ printf("%s%3d%% ^ib(1)^fg(%s)^ro(%dx%d)^p(%d)",
+ dbar->label ? dbar->label : "", rp,
+ dbar->bg, (int)dbar->width, dbar->height, -1*(dbar->width-2));
+ for(i=0; i < segs; i++) {
+ if(i<segsa)
+ printf("^fg(%s)^r(%dx%d+%d+%d')", dbar->fg, dbar->segw, dbar->height-4>0?dbar->height-4:1, i?dbar->segb:0, 0);
+ else
+ break;
+ }
+ printf("^fg()^p(%d)^ib(0)%s", (dbar->segw+dbar->segb)*(segs-segsa+1), dbar->pnl ? "\n" : "");
}
- }
- }
- else if(!strncmp(argv[i], "-min", 5)) {
- if(++i < argc) {
- minval = strtod(argv[i], &endptr);
- if(*endptr) {
- fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
- return EXIT_FAILURE;
+ break;
+
+ case vertical:
+ segs = dbar->height / (dbar->segh + dbar->segb);
+ segsa = rp * segs / 100;
+ fprintf(stream, "%s^ib(1)", dbar->label ? dbar->label : "");
+ if(dbar->segb == 0) {
+ fprintf(stream, "^fg(%s)^r(%dx%d+%d-%d)^fg(%s)^p(-%d)^r(%dx%d+%d-%d)",
+ dbar->bg, dbar->segw, dbar->height, 0, dbar->height+1,
+ dbar->fg, dbar->segw, dbar->segw, (int)l, 0, (int)l+1);
+ } else {
+ for(i=0; i < segs; i++) {
+ if(i<segsa)
+ fprintf(stream, "^fg(%s)^p(-%d)^r(%dx%d+%d-%d)",
+ dbar->fg, i?dbar->segw:0, dbar->segw,
+ dbar->segh, 0, (dbar->segh+dbar->segb)*(i+1));
+ else
+ fprintf(stream, "^fg(%s)^p(-%d)^r(%dx%d+%d-%d)",
+ dbar->bg, i?dbar->segw:0, dbar->segw,
+ dbar->segh, 0, (dbar->segh+dbar->segb)*(i+1));
+ }
+ }
+ fprintf(stream, "^ib(0)^fg()%s", dbar->pnl ? "\n" : "");
+ break;
+
+ case graph:
+ dbar->gc = dbar->gc < MAX_GRAPH_VALS &&
+ (dbar->gs == 0 ? dbar->gc : dbar->gc * dbar->gs + dbar->gc * dbar->gw)
+ < dbar->width ? ++dbar->gc : 0;
+ dbar->gb[dbar->gc] = l;
+
+ printf("%s", dbar->label ? dbar->label : "");
+ for(i=dbar->gc+1; i<MAX_GRAPH_VALS && (i*(dbar->gs+dbar->gw)) < dbar->width; ++i) {
+ p=100*dbar->gb[i]/dbar->height;
+ p=(int)p+0.5 >= (int)p ? (int)(p+0.5) : (int)p;
+ fprintf(stream, "^fg(%s)^p(%d)^r(%dx%d+0-%d)",
+ dbar->fg, dbar->gs, dbar->gw, (int)dbar->gb[i], (int)dbar->gb[i]+1);
}
- }
- }
- else if(!strncmp(argv[i], "-l", 3)) {
- if(++i < argc)
- label = argv[i];
- }
- else if(!strncmp(argv[i], "-nonl", 6)) {
- print_nl = 0;
- }
- else {
- fprintf(stderr, "usage: dbar [-w <characters>] [-s <symbol>] [-min <minvalue>] [-max <maxvalue>] [-l <string>] [-nonl]\n");
- return EXIT_FAILURE;
- }
- }
- while(fgets(aval, MAXLEN, stdin)) {
- nv = sscanf(aval, "%lf %lf %lf", &val, &minval, &maxval);
- if(nv == 2) {
- maxval = minval;
- minval = 0;
+ for(i=0; i < dbar->gc; ++i) {
+ p=100*dbar->gb[i]/dbar->height;
+ p=(int)p+0.5 >= (int)p ? (int)(p+0.5) : (int)p;
+ fprintf(stream, "^fg(%s)^p(%d)^r(%dx%d+0-%d)",
+ dbar->fg, dbar->gs, dbar->gw, (int)dbar->gb[i], (int)dbar->gb[i]+1);
+ }
+ fprintf(stream, "^fg()%s", dbar->pnl ? "\n" : "");
+ break;
+
+ default:
+ if(dbar->segb == 0)
+ printf("%s%3d%% ^fg(%s)^r(%dx%d)^fg(%s)^r(%dx%d)^fg()%s",
+ dbar->label ? dbar->label : "", rp,
+ dbar->fg, (int)l, dbar->height,
+ dbar->bg, dbar->width-(int)l, dbar->height,
+ dbar->pnl ? "\n" : "");
+ else {
+ segs = dbar->width / (dbar->segw + dbar->segb);
+ segsa = rp * segs / 100;
+
+ printf("%s%3d%% ", dbar->label ? dbar->label : "", rp);
+ for(i=0; i < segs; i++) {
+ if(i<segsa)
+ fprintf(stream, "^fg(%s)^r(%dx%d+%d+%d')",
+ dbar->fg, dbar->segw, dbar->height, i?dbar->segb:0, 0);
+ else
+ fprintf(stream, "^fg(%s)^r(%dx%d+%d+%d')",
+ dbar->bg, dbar->segw, dbar->height, i?dbar->segb:0, 0);
+ }
+ fprintf(stream, "^fg()%s", dbar->pnl ? "\n" : "");
+ }
+ break;
}
-
- pbar(label, (100*(val-minval))/(maxval-minval), maxchars, psym, print_nl);
}
-
- return EXIT_SUCCESS;
+ fflush(stream);
}
-
diff --git a/gadgets/dbar.h b/gadgets/dbar.h
new file mode 100644
index 0000000..90e02e1
--- /dev/null
+++ b/gadgets/dbar.h
@@ -0,0 +1,39 @@
+#ifndef __DBAR_H
+#define __DBAR_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#define MAX_GRAPH_VALS 1024
+
+typedef struct {
+ const char *bg;
+ const char *fg;
+ const char *label;
+ char sym;
+ double val;
+ double minval;
+ double maxval;
+ int mode;
+ int style;
+ int width;
+ int height;
+ int segw;
+ int segh;
+ int segb;
+ int gs;
+ int gw;
+ int gc;
+ char gb[MAX_GRAPH_VALS];
+ int pnl;
+} Dbar;
+
+enum mode { textual, graphical };
+enum style { norm, outlined, vertical, graph };
+
+void fdbar(Dbar *dbar, FILE *stream);
+void dbardefaults(Dbar *dbar, int mode);
+
+#endif /* __DBAR_H */
diff --git a/gadgets/gcpubar.c b/gadgets/gcpubar.c
index 37c4117..bd5a09e 100644
--- a/gadgets/gcpubar.c
+++ b/gadgets/gcpubar.c
@@ -1,31 +1,28 @@
/*
- gcpubar - graphical cpu usage bar, to be used with dzen
-
- Copyright (C) 2007 by Robert Manea, <rob dot manea at gmail dot com>
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
+ gcpubar - graphical cpu usage bar, to be used with dzen
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
+ Copyright (C) 2007 by Robert Manea, <rob dot manea at gmail dot com>
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-*/
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
-#include<stdio.h>
-#include<stdlib.h>
-#include<unistd.h>
-#include<string.h>
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ */
+
+#include "dbar.h"
/* critical % value, color */
#define CPUCRIT 75
@@ -34,12 +31,6 @@ THE SOFTWARE.
#define CPUMED 50
#define MEDCOL "#EBA178"
-#define MAX_GRAPH_VALUES 512
-
-static void pbar (const char *, double, int, int, int, int, int, int, int, int);
-
-enum style { norm, outlined, gauge, vertical, graph };
-
struct cpu_info {
unsigned long long user;
unsigned long long sys;
@@ -47,107 +38,19 @@ struct cpu_info {
unsigned long long iowait;
} ncpu, ocpu;
-char *bg, *fg;
-char graphbuf[MAX_GRAPH_VALUES];
-int graphcnt, mv;
-int gtw = 1;
-
-static void
-pbar(const char *label, double perc, int maxc, int height, int segh, int segb,
- int segw, int print_nl, int mode, int gspacing ) {
- int i, rp, p;
- int ov_s, nv_s, segs, segsa;
- double l;
-
- if(mode == graph || mode == vertical)
- l = perc * ((double)height / 100);
- else
- l = perc * ((mode ? (double)(maxc-2) : (double) maxc) / 100);
-
- l=(int)(l + 0.5) >= (int)l ? l+0.5 : l;
- rp=(int)(perc + 0.5) >= (int)perc ? (int)(perc + 0.5) : (int)perc;
-
- if(mode == outlined)
- printf("%s %3d%% ^ib(1)^fg(%s)^ro(%dx%d)^p(%d)^fg(%s)^r(%dx%d)^p(%d)^ib(0)^fg()%s",
- label ? label : "", rp, bg, maxc, height, -1*(maxc-1),
- (rp>=CPUCRIT) ? CRITCOL : fg, (int)l, height-2, (maxc-1)-(int)l, print_nl ? "\n" : "");
- else if(mode == gauge) {
- graphcnt = graphcnt < 128 &&
- (gspacing == 0 ? graphcnt : graphcnt*gspacing+graphcnt*gtw) < maxc ?
- ++graphcnt : 0;
- graphbuf[graphcnt] = l;
-
- printf("%s", label ? label : "");
- for(i=graphcnt+1; i<128 && (i*gspacing+i*gtw < maxc); ++i) {
- p=100*graphbuf[i]/height;
- p=(int)p+0.5 >= (int)p ? (int)(p+0.5) : (int)p;
- printf("^fg(%s)^p(%d)^r(%dx%d+0-%d)",
- p>=CPUMED ? (p >= CPUCRIT ? CRITCOL : MEDCOL) : fg, gspacing,
- gtw, (int)graphbuf[i], (int)graphbuf[i]+1);
- }
-
- for(i=0; i < graphcnt; ++i) {
- p=100*graphbuf[i]/height;
- p=(int)p+0.5 >= (int)p ? (int)(p+0.5) : (int)p;
- printf("^fg(%s)^p(%d)^r(%dx%d+0-%d)",
- p>=CPUMED ? (p >= CPUCRIT ? CRITCOL : MEDCOL) : fg, gspacing,
- gtw, (int)graphbuf[i], (int)graphbuf[i]+1);
- }
- printf("^fg()%s", print_nl ? "\n" : "");
- }
- else if(mode == vertical) {
- segs = height / (segh + segb);
- segsa = rp * segs / 100;
-
- printf("%s^ib(1)", label ? label : "");
- /* in case of no spacing, we draw just 2 rectangles */
- if(segb == 0) {
- printf("^fg(%s)^r(%dx%d+%d-%d)^fg(%s)^p(-%d)^r(%dx%d+%d-%d)",
- bg,
- segw, height, 0, height+1,
- rp >= CPUMED ? (rp>=CPUCRIT ? CRITCOL : MEDCOL) : fg, segw,
- segw, (int)l, 0, (int)l+1);
- } else {
- for(i=0; i < segs; i++) {
- if(i<segsa)
- printf("^fg(%s)^p(-%d)^r(%dx%d+%d-%d)", rp >= CPUMED ? (rp>=CPUCRIT ? CRITCOL : MEDCOL) : fg, i?segw:0, segw, segh, 0, (segh+segb)*(i+1));
- else
- printf("^fg(%s)^p(-%d)^r(%dx%d+%d-%d)", bg, i?segw:0, segw, segh, 0, (segh+segb)*(i+1));
-
- }
- }
- printf("^ib(0)^fg()%s", print_nl ? "\n" : "");
- }
- else
- printf("%s %3d%% ^fg(%s)^r(%dx%d)^fg(%s)^r(%dx%d)^fg()%s",
- label ? label : "", rp, (rp>=CPUCRIT) ? CRITCOL : fg, (int)l,
- height, bg, maxc-(int)l, height, print_nl ? "\n" : "");
-
-
- fflush(stdout);
-}
-
-int
-main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
int i, t;
double total;
struct cpu_info mcpu;
FILE *statfp;
char buf[256], *ep;
- const char *label = NULL;
+ Dbar dbar;
+
+ int counts = 0;
+ double ival = 1.0;
- /* defaults */
- int mode = 0;
- double ival = 1.0;
- int counts = 0;
- int barwidth = 100;
- int barheight = 10;
- int print_nl = 1;
- int gspacing = 1;
- int segw = 6;
- int segh = 2;
- int segb = 1;
+ dbardefaults(&dbar, graphical);
+ dbar.mode = graphical;
for(i=1; i < argc; i++) {
if(!strncmp(argv[i], "-i", 3)) {
@@ -165,16 +68,16 @@ main(int argc, char *argv[])
if(++i < argc) {
switch(argv[i][0]) {
case 'o':
- mode = outlined;
+ dbar.style = outlined;
break;
case 'g':
- mode = gauge;
+ dbar.style = graph;
+ break;
+ case 'v':
+ dbar.style = vertical;
break;
- case 'v':
- mode = vertical;
- break;
default:
- mode = norm;
+ dbar.style = norm;
break;
}
}
@@ -185,54 +88,46 @@ main(int argc, char *argv[])
}
else if(!strncmp(argv[i], "-gs", 4)) {
if(++i < argc)
- gspacing = atoi(argv[i]);
+ dbar.gs = atoi(argv[i]);
}
else if(!strncmp(argv[i], "-gw", 4)) {
if(++i < argc)
- gtw = atoi(argv[i]);
+ dbar.gw = atoi(argv[i]);
}
else if(!strncmp(argv[i], "-w", 3)) {
if(++i < argc)
- barwidth = atoi(argv[i]);
+ dbar.width = atoi(argv[i]);
}
else if(!strncmp(argv[i], "-h", 3)) {
if(++i < argc)
- barheight = atoi(argv[i]);
+ dbar.height = atoi(argv[i]);
}
else if(!strncmp(argv[i], "-sw", 4)) {
if(++i < argc)
- segw = atoi(argv[i]);
+ dbar.segw = atoi(argv[i]);
}
else if(!strncmp(argv[i], "-sh", 4)) {
if(++i < argc)
- segh = atoi(argv[i]);
+ dbar.segh = atoi(argv[i]);
}
else if(!strncmp(argv[i], "-ss", 4)) {
if(++i < argc)
- segb = atoi(argv[i]);
+ dbar.segb = atoi(argv[i]);
}
else if(!strncmp(argv[i], "-fg", 4)) {
if(++i < argc)
- fg = argv[i];
- else {
- fg = malloc(6);
- strcpy(fg, "white");
- }
+ dbar.fg = argv[i];
}
else if(!strncmp(argv[i], "-bg", 4)) {
if(++i < argc)
- bg = argv[i];
- else {
- bg = malloc(9);
- strcpy(bg, "darkgrey");
- }
+ dbar.bg = argv[i];
}
else if(!strncmp(argv[i], "-l", 3)) {
if(++i < argc)
- label = argv[i];
+ dbar.label = argv[i];
}
else if(!strncmp(argv[i], "-nonl", 6)) {
- print_nl = 0;
+ dbar.pnl = 0;
}
else {
printf("usage: %s [-l <label>] [-i <interval>] [-c <count>] [-fg <color>] [-bg <color>] [-w <pixel>] [-h <pixel>] [-s <o|g|v>] [-sw <pixel>] [-sh <pixel>] [-ss <pixel>] [-gs <pixel>] [-gw <pixel>] [-nonl]\n", argv[0]);
@@ -240,15 +135,6 @@ main(int argc, char *argv[])
}
}
- if(!fg) {
- fg = malloc(6);
- strcpy(fg, "white");
- }
- if(!bg) {
- bg = malloc(9);
- strcpy(bg, "darkgrey");
- }
-
if(!(statfp = fopen("/proc/stat", "r"))) {
printf("%s: error opening '/proc/stat'\n", argv[0]);
return EXIT_FAILURE;
@@ -263,7 +149,7 @@ main(int argc, char *argv[])
double myload;
/* linux >= 2.6 */
if((sscanf(buf, "cpu %llu %llu %llu %llu %llu",
- &ncpu.user, &unice, &ncpu.sys, &ncpu.idle, &ncpu.iowait)) < 5) {
+ &ncpu.user, &unice, &ncpu.sys, &ncpu.idle, &ncpu.iowait)) < 5) {
printf("%s: wrong field count in /proc/stat\n", argv[0]);
return EXIT_FAILURE;
}
@@ -274,11 +160,10 @@ main(int argc, char *argv[])
mcpu.idle = ncpu.idle - ocpu.idle;
mcpu.iowait = ncpu.iowait - ocpu.iowait;
- total = (mcpu.user + mcpu.sys + mcpu.idle + mcpu.iowait) / 100.0;
- myload = (mcpu.user + mcpu.sys + mcpu.iowait) / total;
+ dbar.maxval = mcpu.user + mcpu.sys + mcpu.idle + mcpu.iowait;
+ dbar.val = mcpu.user + mcpu.sys + mcpu.iowait;
- pbar(label, myload, barwidth, barheight, segh, segb, segw,
- print_nl, mode, gspacing);
+ fdbar(&dbar, stdout);
ocpu = ncpu;
}
}
@@ -291,7 +176,6 @@ main(int argc, char *argv[])
else
break;
}
-
- return EXIT_SUCCESS;
-}
+ return EXIT_SUCCESS;
+}
diff --git a/gadgets/gdbar.c b/gadgets/gdbar.c
index 242273d..6f689d3 100644
--- a/gadgets/gdbar.c
+++ b/gadgets/gdbar.c
@@ -1,256 +1,97 @@
-/*
- gdbar - graphical percentage meter, to be used with dzen
-
- Copyright (c) 2007 by Robert Manea <rob dot manea at gmail dot com>
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
-
-
-#include<stdio.h>
-#include<stdlib.h>
-#include<unistd.h>
-#include<string.h>
-#include<X11/Xlib.h>
+#include <stdio.h>
+#include "dbar.h"
#define MAXLEN 1024
-static void pbar (const char *, double, int, int, int, int, int, int, int);
-
-enum style { norm, outlined, vertical };
-char *bg, *fg;
-
-static void
-pbar(const char* label, double perc, int maxc, int height, int segw, int segh, int segb, int pnl, int mode) {
- int i, rp;
- int segs, segsa;
- double l;
-
-
-
- if(mode == vertical)
- l = perc * ((double)height / 100);
- else
- l = perc * ((mode == outlined ? (double)(maxc-2) : (double) maxc) / 100);
-
- l=(int)(l + 0.5) >= (int)l ? l+0.5 : l;
- rp=(int)(perc + 0.5) >= (int)perc ? (int)(perc + 0.5) : (int)perc;
-
- if(mode == outlined) {
- if(segb == 0) {
- printf("%s%3d%% ^ib(1)^fg(%s)^ro(%dx%d)^p(%d)^fg(%s)^r(%dx%d)^p(%d)^ib(0)^fg()%s",
- label ? label : "", rp,
- bg, (int)maxc, height, -1*(maxc-2),
- fg, (int)l, height-4>0?height-4:1,
- maxc-(int)l-1, pnl ? "\n" : "");
- } else {
- segs = (maxc-2) / (segw+segb);
- segsa = rp * segs / 100;
-
- printf("%s%3d%% ^ib(1)^fg(%s)^ro(%dx%d)^p(%d)",
- label ? label : "", rp,
- bg, (int)maxc, height, -1*(maxc-2));
- for(i=0; i < segs; i++) {
- if(i<segsa)
- printf("^fg(%s)^r(%dx%d+%d+%d')", fg, segw, height-4>0?height-4:1, i?segb:0, 0);
- else
- break;
- }
- printf("^fg()^p(%d)^ib(0)%s", (segw+segb)*(segs-segsa+1), pnl ? "\n" : "");
- }
- }
- else if(mode == vertical) {
- segs = height / (segh + segb);
- segsa = rp * segs / 100;
-
-
- printf("%s^ib(1)", label ? label : "");
- /* in case of no spacing, we draw just 2 rectangles */
- if(segb == 0) {
- printf("^fg(%s)^r(%dx%d+%d-%d)^fg(%s)^p(-%d)^r(%dx%d+%d-%d)",
- bg,
- segw, height, 0, height+1,
- fg, segw,
- segw, (int)l, 0, (int)l+1
- );
- } else {
- for(i=0; i < segs; i++) {
- if(i<segsa)
- printf("^fg(%s)^p(-%d)^r(%dx%d+%d-%d)", fg, i?segw:0, segw, segh, 0, (segh+segb)*(i+1));
- else
- printf("^fg(%s)^p(-%d)^r(%dx%d+%d-%d)", bg, i?segw:0, segw, segh, 0, (segh+segb)*(i+1));
-
- }
- }
- printf("^ib(0)^fg()%s", pnl ? "\n" : "");
+int main(int argc, char *argv[]) {
+ int i, nv;
+ char aval[MAXLEN], *endptr;
+ Dbar dbar;
+
+ dbardefaults(&dbar, graphical);
+
+ for(i=1; i < argc; i++) {
+ if(!strncmp(argv[i], "-w", 3)) {
+ if(++i < argc)
+ dbar.width = atoi(argv[i]);
+ }
+ else if(!strncmp(argv[i], "-h", 3)) {
+ if(++i < argc)
+ dbar.height = atoi(argv[i]);
+ }
+ else if(!strncmp(argv[i], "-sw", 4)) {
+ if(++i < argc)
+ dbar.segw = atoi(argv[i]);
+ }
+ else if(!strncmp(argv[i], "-sh", 4)) {
+ if(++i < argc)
+ dbar.segh = atoi(argv[i]);
+ }
+ else if(!strncmp(argv[i], "-ss", 4)) {
+ if(++i < argc)
+ dbar.segb = atoi(argv[i]);
+ }
+ else if(!strncmp(argv[i], "-s", 3)) {
+ if(++i < argc) {
+ switch(argv[i][0]) {
+ case 'o':
+ dbar.style = outlined;
+ break;
+ case 'v':
+ dbar.style = vertical;
+ break;
+ default:
+ dbar.style = norm;
+ break;
}
- else {
- if(segb == 0)
- printf("%s%3d%% ^fg(%s)^r(%dx%d)^fg(%s)^r(%dx%d)^fg()%s",
- label ? label : "", rp,
- fg, (int)l, height,
- bg, maxc-(int)l, height,
- pnl ? "\n" : "");
- else {
- segs = maxc / (segw+segb);
- segsa = rp * segs / 100;
-
- printf("%s%3d%% ", label ? label : "", rp);
- for(i=0; i < segs; i++) {
- if(i<segsa)
- printf("^fg(%s)^r(%dx%d+%d+%d')", fg, segw, height, i?segb:0, 0);
- else
- printf("^fg(%s)^r(%dx%d+%d+%d')", bg, segw, height, i?segb:0, 0);
- }
- printf("^fg()%s", pnl ? "\n" : "");
- }
+ }
+ }
+ else if(!strncmp(argv[i], "-fg", 4)) {
+ if(++i < argc)
+ dbar.fg = argv[i];
+ }
+ else if(!strncmp(argv[i], "-bg", 4)) {
+ if(++i < argc)
+ dbar.bg = argv[i];
+ }
+ else if(!strncmp(argv[i], "-max", 5)) {
+ if(++i < argc) {
+ dbar.maxval = strtod(argv[i], &endptr);
+ if(*endptr) {
+ fprintf(stderr, "gdbar: '%s' incorrect number format", argv[i]);
+ return EXIT_FAILURE;
}
-
-
- fflush(stdout);
-}
-
- int
-main(int argc, char *argv[])
-{
- int i, nv;
- double val;
- char aval[MAXLEN], *endptr;
-
- /* defaults */
- int mode = norm;
- int barwidth = 80;
- int barheight = 10;
- int segw = 6;
- int segh = 2;
- int segb = 0;
- double minval = 0;
- double maxval = 100.0;
- int print_nl = 1;
- const char *label = NULL;
-
-
- for(i=1; i < argc; i++) {
- if(!strncmp(argv[i], "-w", 3)) {
- if(++i < argc)
- barwidth = atoi(argv[i]);
- }
- else if(!strncmp(argv[i], "-h", 3)) {
- if(++i < argc)
- barheight = atoi(argv[i]);
- }
- else if(!strncmp(argv[i], "-sw", 4)) {
- if(++i < argc)
- segw = atoi(argv[i]);
- }
- else if(!strncmp(argv[i], "-sh", 4)) {
- if(++i < argc)
- segh = atoi(argv[i]);
- }
- else if(!strncmp(argv[i], "-ss", 4)) {
- if(++i < argc)
- segb = atoi(argv[i]);
- }
- else if(!strncmp(argv[i], "-s", 3)) {
- if(++i < argc) {
- switch(argv[i][0]) {
- case 'o':
- mode = outlined;
- break;
- case 'v':
- mode = vertical;
- break;
- default:
- mode = norm;
- break;
- }
- }
- }
-
- else if(!strncmp(argv[i], "-fg", 4)) {
- if(++i < argc)
- fg = argv[i];
- else {
- fg = malloc(6);
- strcpy(fg, "white");
- }
- }
- else if(!strncmp(argv[i], "-bg", 4)) {
- if(++i < argc)
- bg = argv[i];
- else {
- bg = malloc(9);
- strcpy(bg, "darkgrey");
- }
- }
- else if(!strncmp(argv[i], "-max", 5)) {
- if(++i < argc) {
- maxval = strtod(argv[i], &endptr);
- if(*endptr) {
- fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
- return EXIT_FAILURE;
- }
- }
- }
- else if(!strncmp(argv[i], "-min", 5)) {
- if(++i < argc) {
- minval = strtod(argv[i], &endptr);
- if(*endptr) {
- fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
- return EXIT_FAILURE;
- }
- }
- }
- else if(!strncmp(argv[i], "-l", 3)) {
- if(++i < argc)
- label = argv[i];
- }
- else if(!strncmp(argv[i], "-nonl", 6)) {
- print_nl = 0;
- }
- else {
- fprintf(stderr, "usage: dbar [-s <o|v>] [-w <pixel>] [-h <pixel>] [-sw <pixel>] [-ss <pixel>] [-sw <pixel>] [-fg <color>] [-bg <color>] [-min <minvalue>] [-max <maxvalue>] [-l <string>] [-nonl] \n");
- return EXIT_FAILURE;
- }
- }
-
- if(!fg) {
- fg = malloc(6);
- strcpy(fg, "white");
- }
- if(!bg) {
- bg = malloc(9);
- strcpy(bg, "darkgrey");
+ }
+ }
+ else if(!strncmp(argv[i], "-min", 5)) {
+ if(++i < argc) {
+ dbar.minval = strtod(argv[i], &endptr);
+ if(*endptr) {
+ fprintf(stderr, "gdbar: '%s' incorrect number format", argv[i]);
+ return EXIT_FAILURE;
}
+ }
+ }
+ else if(!strncmp(argv[i], "-l", 3)) {
+ if(++i < argc)
+ dbar.label = argv[i];
+ }
+ else if(!strncmp(argv[i], "-nonl", 6)) {
+ dbar.pnl = 0;
+ }
+ else {
+ fprintf(stderr, "usage: gdbar [-s <o|v>] [-w <pixel>] [-h <pixel>] [-sw <pixel>] [-ss <pixel>] [-sw <pixel>] [-fg <color>] [-bg <color>] [-min <minvalue>] [-max <maxvalue>] [-l <string>] [-nonl] \n");
+ return EXIT_FAILURE;
+ }
+ }
+
+ while(fgets(aval, MAXLEN, stdin)) {
+ nv = sscanf(aval, "%lf %lf %lf", &dbar.val, &dbar.minval, &dbar.maxval);
+ if(nv == 2) {
+ dbar.maxval = dbar.minval;
+ dbar.minval = 0;
+ }
+ fdbar(&dbar, stdout);
+ }
- while(fgets(aval, MAXLEN, stdin)) {
- nv = sscanf(aval, "%lf %lf %lf", &val, &minval, &maxval);
- if(nv == 2) {
- maxval = minval;
- minval = 0;
- }
-
- pbar(label, (100*(val-minval))/(maxval-minval),
- barwidth, barheight, segw, segh, segb,
- print_nl, mode);
- }
-
- return EXIT_SUCCESS;
}
-