[wmii] sswriter

From: <stanio_AT_cs.tu-berlin.de>
Date: Tue, 16 Sep 2008 16:58:34 +0200

Hi,

I used to have a plenty of perl scripts to fill the status line, but find
the native way of doing this considerably faster => better.

When I found sswriter on this thread in May I was missing the ability to
get the content of a file at particular place in the output. After this
feature have been implemented a month later the main thing I was missing
was the ability to limit the number of chars in particular areas of the
output.

I need this, because I like to have the more important part on the right
(battery, network ifaces, load, etc.) and the less important on the left,
near the wmii/dwm tags. The less important part tends to have unpredictable
size, e.g. incoming instant messages, and sometimes eats the space for the
important part.

I have implemented the areas concept some weeks after that. Please find
below the patch for 0.3.1. If it gets corrupted by the mail transfer, I can
provided it through http, too. Just drop a line, if needed. The code is far
from good quality -- suggestions are welcome.

You can start an area in the format string with ``['' and close it with
(surprise :o) ) ``]''. With ``--area max1 max2 max3'' on the command line,
the maximal width of the first, second, and third opened area can be
specified.

The perl script I used before implements an extended version of the areas
concept: If the content of the area is larger than the given maximum, it
scrolls the content with configurable speed (chars/sec). (No status has to
be kept by the algorithm, it is based on the current time). I would like to
have this implemented in sswriter too. This is on the top of my todo for
sswriter when I have more time to spend (and this is not going to be soon,
I am afraid). Next thing is wide chars support. Often, the files I print
have non-ascii (Cyrillic) chars -- would be nice to handle them correctly.
If somebody has already implemented this feature, please let me know.

But for now I am comfortable with this version, too.

Hope this helps to somebody.

:o)

-- 
 cheers
 stanio_
-=-=-=-=-= patch -=-=-=
Index: src/formating.c
===================================================================
RCS file: /home/stanio/CVS/sswriter/src/formating.c,v
retrieving revision 1.1
retrieving revision 1.3
diff -u -r1.1 -r1.3
--- src/formating.c	25 Jun 2008 17:43:50 -0000	1.1
+++ src/formating.c	25 Jun 2008 23:22:27 -0000	1.3
@@ -48,6 +48,7 @@
    free(data->files);
    free(data->net_devices);
    free(data->sensors);
+   free(data->areas);
    free(data->lm_sensors);
 #ifdef __WITH_ALSA__
    free(data->mixers_data);
@@ -70,6 +71,12 @@
    /* Groups */
    stack_t *groups = NULL;
    int cgroup = 0;
+   /* Areas */
+   stack_t *areas = NULL; // area types (later)
+   stack_t *area_widths = NULL; // well, ... widths
+   stack_t *area_starts = NULL; // start (relative to  line, in order to avoid long to int cast)
+   char * area_start;
+   int nb_area = 0;
 
    /* Cpufreq cache */
    int fcached = -1;
@@ -355,7 +362,6 @@
             break;
 
 
-
             /* Groups are handled in the second pass */
             case '(':
                it++;
@@ -365,6 +371,16 @@
                len = snprintf(line_it, line_left, "%%)");
             break;
 
+
+            /* Scroll areas handled later */
+            case ']':
+               it++;
+               len = snprintf(line_it, line_left, "%%]%c", *it);
+            break;
+            case '[':
+               len = snprintf(line_it, line_left, "%%[");
+            break;
+
             default:
                slog("Invalid format sequence: %%%c.", next);
                return EXIT_FAILURE;
@@ -454,13 +470,67 @@
       }
    }
    *line_it = '\0';
-   
-   printf("%s", line);
 
    if (!stack_is_empty(groups))
       slog("Warning: there is one or more groups not closed.");
 
    stack_delete(groups);
 
+   /* Do a third pass on the line to handle the scroll areas */
+   areas = stack_create(4);
+   area_widths = stack_create(4);
+   area_starts = stack_create(4);
+   nb_area = 0;
+
+   line_it = line;
+   for (it = line; *it != '\0'; it++) {
+      char c = *it;
+
+      /* Close the current area */
+      if (c == '%' && it[1] == ']') {
+         if (stack_is_empty(areas)) {
+            slog("Too many close braces `]'!");
+            stack_delete(areas);
+            stack_delete(area_starts);
+            stack_delete(area_widths);
+            return EXIT_FAILURE;
+         }
+         stack_pop(areas);
+         stack_pop(area_widths);
+         stack_pop(area_starts);
+         it++;
+         continue;
+      }
+
+      if(
+		      stack_peek(areas) &&
+		      stack_peek(area_widths) &&
+		      ((line_it - (line + stack_peek(area_starts)) ) >= stack_peek(area_widths))){
+	 continue; 
+      }
+
+      /* Match area opening */
+      if (c == '%' && it[1] == '[') {
+         it += 2;
+	 stack_push(areas, AREA);
+	 stack_push(area_starts, line_it - line);
+	 stack_push(area_widths, data->areas[nb_area]);
+	 ++nb_area;
+      } else {
+         *line_it = c;
+         line_it++;
+      }
+   }
+   *line_it = '\0';
+   
+   printf("%s", line);
+
+   if (!stack_is_empty(areas))
+      slog("Warning: there is one or more areas not closed.");
+
+   stack_delete(areas);
+   stack_delete(area_widths);
+   stack_delete(area_widths);
+
    return EXIT_SUCCESS;
 }
Index: src/formating.h
===================================================================
RCS file: /home/stanio/CVS/sswriter/src/formating.h,v
retrieving revision 1.1
retrieving revision 1.3
diff -u -r1.1 -r1.3
--- src/formating.h	25 Jun 2008 17:43:50 -0000	1.1
+++ src/formating.h	25 Jun 2008 23:22:27 -0000	1.3
@@ -26,6 +26,9 @@
 #define GRP_BAT_WARN (1 << 1)
 #define GRP_BAT_NORM (1 << 2)
 
+/* Area types */
+#define AREA (1 << 3)
+
 #include "acpi_parsing.h"
 
 /* Data type */
@@ -55,6 +58,10 @@
    const char **files;
    unsigned int nb_files;
 
+   /* List of area specs */
+   unsigned int *areas;
+   unsigned int nb_areas;
+
 #ifdef __WITH_ALSA__
    /* List of mixers names */
    const char **mixers;
Index: src/main.c
===================================================================
RCS file: /home/stanio/CVS/sswriter/src/main.c,v
retrieving revision 1.1
retrieving revision 1.3
diff -u -r1.1 -r1.3
--- src/main.c	25 Jun 2008 17:43:50 -0000	1.1
+++ src/main.c	25 Jun 2008 23:22:27 -0000	1.3
@@ -73,6 +73,30 @@
          i = store_args_strings(argc, argv, i, &data->net_devices,
                &data->nb_net_devices);
 
+         /* Scroll areas */
+      } else if (data->areas == NULL && (!strcmp(argv[i], "-a")
+               || !strcmp(argv[i], "--areas"))) {
+         int j = i + 1;
+         while (j < (argc - 1) && argv[j][0] != '-') {
+            data->nb_areas++;
+            j++;
+         }
+
+         if (data->nb_areas > 0) {
+            data->areas = calloc(2 * data->nb_areas,
+                  sizeof(unsigned int));
+            for (j = 0; j < data->nb_areas; j++) {
+               unsigned int v1;
+               if (sscanf(argv[i + j + 1], "%u", &v1 ) == 1) {
+                  data->areas[j] = v1;
+               } else {
+                  print_usage(argv[0]);
+                  return EXIT_FAILURE;
+               }
+            }
+            i += j;
+         }
+
          /* Temperature sensors */
       } else if (data->sensors == NULL && (!strcmp(argv[i], "-t")
                || !strcmp(argv[i], "--tsensors"))) {
@@ -181,6 +205,8 @@
    printf("\tspecify differents couples of hwmon-num/temp-num (see README)\n\n");
    printf("-w --warnlimit <number>\n");
    printf("\tset the limit of the battery warning (by default 10%%)\n\n");
+   printf("-a --areas <maxwidth ... >\n");
+   printf("\tset the limit of the battery warning (by default 10%%)\n\n");
    printf("For more informations and examples, see the README\n");
 }
 
-=-=-=-=-= patch end -=
Received on Tue Sep 16 2008 - 14:58:34 UTC

This archive was generated by hypermail 2.2.0 : Tue Sep 16 2008 - 15:12:06 UTC