You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbar.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* dbar - ascii percentage meter
  2. *
  3. * Copyright (C) 2007 by Robert Manea, <rob dot manea at gmail dot com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. *
  20. * Compile with:
  21. * gcc dbar.c -o dbar
  22. *
  23. */
  24. /*
  25. dbar lets you define static 0% and 100% marks or you can define these
  26. marks dynamically at runtime. Static and dynamic marks can be mixed,
  27. in this case the value specified at runtime will have a higher priority.
  28. You can specify ranges of numbers, negative, positive or ranges with a
  29. negative min value and positive max value.
  30. All numbers are treated as double precision floating point, i.e. the
  31. input is NOT limited to integers.
  32. Options:
  33. -max : Value to be considered 100% (default: 100)
  34. -min : Value to be considered 0% (default: 0 )
  35. -w : Number of charcaters to be
  36. considered 100% in the meter (default: 25 )
  37. -s : Symbol represeting the
  38. percentage value in the meter (default: = )
  39. -l : label to be prepended to
  40. the bar (default: '' )
  41. -nonl: no new line, don't put
  42. '\n' at the end of the bar (default: do print '\n')
  43. Usage examples:
  44. 1) Static 100% mark or single value input:
  45. echo 25 | dbar -m 100 -l Sometext
  46. Output: Sometext 25% [====== ]
  47. 2) If your 100% mark changes dynamically or 2-values input:
  48. echo "50 150" | dbar
  49. ^ ^
  50. | |__ max. value
  51. |
  52. |__ value to display
  53. Output: 33% [======== ]
  54. 3) If your value range is not between [0, maxval] or 3-values input:
  55. echo "50 -25 150" | dbar
  56. ^ ^ ^
  57. | | |__ max. value 100% mark
  58. | |
  59. | |_____ min. value 0% mark
  60. |
  61. |________ value to display
  62. Output: 43% [=========== ]
  63. 4) Multiple runs:
  64. for i in 2 20 50 75 80; do echo $i; sleep 1; done | dbar | dzen2
  65. Output: Find out yourself.
  66. */
  67. #include<stdio.h>
  68. #include<stdlib.h>
  69. #include<unistd.h>
  70. #include<string.h>
  71. #define MAXLEN 512
  72. static void pbar (const char*, double, int, char, int);
  73. static void
  74. pbar(const char* label, double perc, int maxc, char sym, int pnl) {
  75. int i, rp;
  76. double l;
  77. l = perc * ((double)maxc / 100);
  78. if((int)(l + 0.5) >= (int)l)
  79. l = l + 0.5;
  80. if((int)(perc + 0.5) >= (int)perc)
  81. rp = (int)(perc + 0.5);
  82. else
  83. rp = (int)perc;
  84. if(label)
  85. printf("%s %3d%% [", label, rp);
  86. else
  87. printf("%3d%% [", rp);
  88. for(i=0; i < (int)l; i++)
  89. if(i == maxc) {
  90. putchar('>');
  91. break;
  92. } else
  93. putchar(sym);
  94. for(; i < maxc; i++)
  95. putchar(' ');
  96. printf("]%s", pnl ? "\n" : "");
  97. fflush(stdout);
  98. }
  99. int
  100. main(int argc, char *argv[])
  101. {
  102. int i, nv;
  103. double val;
  104. char aval[MAXLEN], *endptr;
  105. /* defaults */
  106. int maxchars = 25;
  107. double minval = 0;
  108. double maxval = 100.0;
  109. char psym = '=';
  110. int print_nl = 1;
  111. const char *label = NULL;
  112. for(i=1; i < argc; i++) {
  113. if(!strncmp(argv[i], "-w", 3)) {
  114. if(++i < argc)
  115. maxchars = atoi(argv[i]);
  116. }
  117. else if(!strncmp(argv[i], "-s", 3)) {
  118. if(++i < argc)
  119. psym = argv[i][0];
  120. }
  121. else if(!strncmp(argv[i], "-max", 5)) {
  122. if(++i < argc) {
  123. maxval = strtod(argv[i], &endptr);
  124. if(*endptr) {
  125. fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
  126. return EXIT_FAILURE;
  127. }
  128. }
  129. }
  130. else if(!strncmp(argv[i], "-min", 5)) {
  131. if(++i < argc) {
  132. minval = strtod(argv[i], &endptr);
  133. if(*endptr) {
  134. fprintf(stderr, "dbar: '%s' incorrect number format", argv[i]);
  135. return EXIT_FAILURE;
  136. }
  137. }
  138. }
  139. else if(!strncmp(argv[i], "-l", 3)) {
  140. if(++i < argc)
  141. label = argv[i];
  142. }
  143. else if(!strncmp(argv[i], "-nonl", 6)) {
  144. print_nl = 0;
  145. }
  146. else {
  147. fprintf(stderr, "usage: dbar [-w <characters>] [-s <symbol>] [-min <minvalue>] [-max <maxvalue>] [-l <string>] [-nonl]\n");
  148. return EXIT_FAILURE;
  149. }
  150. }
  151. while(fgets(aval, MAXLEN, stdin)) {
  152. nv = sscanf(aval, "%lf %lf %lf", &val, &minval, &maxval);
  153. if(nv == 2) {
  154. maxval = minval;
  155. minval = 0;
  156. }
  157. pbar(label, (100*(val-minval))/(maxval-minval), maxchars, psym, print_nl);
  158. }
  159. return EXIT_SUCCESS;
  160. }