Color watch

Continuing with http://archlinux.me/procyon/?p=44

I thought adding colors would mess up pr, but if you add the colors after pr, and preserve whatever stood there it will work, because the same amount of characters remain. That means using characters like {} () and color in what is in between.

First to edit watch.c

Before the for-loop add

start_color();
init_pair(49,1,0);
init_pair(50,2,0);
init_pair(51,3,0);
init_pair(52,4,0);
init_pair(53,5,0);
init_pair(54,6,0);
init_pair(55,7,0);
init_pair(56,0,0);
curs_set(0);

Note that 49 is decimal ascii 1

Next the for loop and initializations


for (y = show_title; y < height; y++) {
size_t len = 0;
char *line = NULL;
char *backup = NULL;
char *color= NULL;
getline(&backup,&len,p);
line=backup;
move(y,0);
color_set(0,NULL);

The added part is tokenizing the line and setting attributes


do {
color=strstr(line,"\x1b\x5b\x00");
if (color != NULL) {
color[0]=0;
addstr(line);
if (color[2] == 'm') {
attr_on(COLOR_PAIR(55),NULL);
color+=3;
}
else if (color[2] == '3') {
attr_on(COLOR_PAIR(color[3]), NULL);
color+=5;
}
} else
addstr(line);
line=color;
}
while ( color != NULL);
free(backup);
}

notes: I am pretty sure this is causing a memleak but I don’t know how to fix it (I tried a few things but no success) if you know how, post in the comments please.

That takes care of watch. The script can then use something like


colorize='s/[`|>)}]/&/g;s/{/&/g;s/</&/g;s/(/&/g'

Where  is ^[ : CTRL+V ESC

And for the output just add it at the end

TOPRINTL=$(( $(echo "$PARTB" | wc -l) +1 ))
(echo "$PARTA" | head -n $TOPRINTL ; ( echo "$PARTB" ; echo '`---------------------------------------------------------' ) | sed 's/^$/|/;s/^\([^,|`]\)/| \1/' ) | pr -2 -T -W $realcols | sed "$colorize"
echo "$PARTA" | tail -n +$((TOPRINTL+1)) | sed "$colorize"

Then any ({< will give different colors. For [] it hits recursion or at least something weird, but it should be possible too.

This entry was posted in Programming. Bookmark the permalink.

3 Responses to Color watch

  1. procyon says:

    About the memleak, I got one when I tried to free a pointer that pointed to the original data, but the method above seems to work. (it’s been at 1MB since it started yesterday)

  2. procyon says:

    The memleak got bigger when I added more colors. Obviously not enough data is freed. At least now it is visible (still pretty slow though) so I can try some different methods.

  3. procyon says:

    I think the memleak is solved by storing the pointer in a backup variable and freeing that instead.

Comments are closed.