Sorry, for late comment, but this just occurred to me: don't you need `buf.flush()` before applying new attributes?  Otherwise if you have a buffering writer, you'll apply new attributes to text that's already been output.