Styled output in Poke programs

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; }


(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/ 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
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"
        push   "Name: "
        push   "string"
        push   "Joe"
        push   "string"
        push   " Age: "
        push   "integer"
        push   39
        printi 32, 10
        push   "integer"
        push   ""
        popf   1
        push   null

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...