Styled output in Poke programs
[21-10-2019]

by Jose E. Marchesi

I just committed support for styling in printf. Basically, it uses the libtextstyle approach of having styling classes that the user can customize in a .css file.

The support is implemented as follows. There are two new %- tags recognized in the format strings passed to printf:

Where NAME is some name identifying the class. Note that NAME cannot be empty. Styling classes can be nested. All classes should be ended before finishing the format string.

If you use a NAME class, you can define how to style it in the .css file (poke installs and uses poke-default.css but you can set the POKE_STYLE environment variable to point to another css) like this:

.NAME { text-decoration: blink; color : pink; }

Examples:

(poke) printf "This is a NAME: %<NAME:xxx%>"
This is a NAME: xxx 
(poke) printf "Name: %<string:%s%> Age: %<integer:%i32d%>", "Joe", 39
Name: Joe Age: 39

I already stylized the dump command (you can take a look at src/pk-dump.pk) which is itself written in Poke. For this purpose I added three new classes to poke-default.css:

.dump-ruler { text-decoration: underline; }
.dump-address { color : green; }
.dump-ascii { color : brown; }

It looks like this:

(poke) dump
76543210  0011 2233 4455 6677 8899 aabb ccdd eeff  0123456789ABCDEF
00000000: 3600 0000 026e 616d 6500 1100 0000 4a6f  6....name.....Jo
00000010: 7365 2045 2e20 4d61 7263 6865 7369 0010  se E. Marchesi..
00000020: 6167 6500 2700 0000 1262 6967 00b9 3e9e  age.'....big..>.
00000030: 3f1c d303 0000 

Yes I know you will find my styling ugly :P

For the curious pokist, this is how this is implemented at the PVM level (note the begsc and endsc instructions):

(poke) fun foo = void: { printf "Name: %<string:%s%> Age: %<integer:%i32d%>", "Joe", 39; }
(poke) .vm dis f foo
        note   "foo"
$L1:
        prolog 
        pushf  
        push   "Name: "
        prints 
        push   "string"
        begsc  
        push   "Joe"
        prints 
        push   "string"
        endsc  
        push   " Age: "
        prints 
        push   "integer"
        begsc  
        push   39
        printi 32, 10
        push   "integer"
        endsc  
        push   ""
        prints 
        popf   1
        push   null
        return 
        exitvm 

For testing it I pass --color=html to poke to get strings I can match. Example of a test from the poke.cmd testsuite:

/* { dg-do run } */
/* { dg-options "--color=html" } */

/* { dg-command {printf "xxx%<foo:%i32d%>yyy", 10} } */
/* { dg-output "xxx<span class=\"foo\">10</span>yyy" } */

Happy styling! :)

Back to Applied Pokology Follow up in the mailing list...