Follow

📣 Just published: reflow, a package that lets you word-wrap strings or entire blocks of text.

It conveniently follows the io.Writer / io.WriteCloser interface and supports ANSI escape sequences. This means you can style your terminal output without it affecting the word-wrapping algorithm!

github.com/muesli/reflow

· · Web · 4 · 8 · 24

@gudenau

No. I should have been more explicit here. Moving the cursor will break it, but it does support all the styling sequences.

@fribbledom
>supports ANSI escape sequences.

That's an interesting design choice. If memory serves, for a good while text/tabwriter only supported HTML escaping, i.e. tags were considered 0-length, and entities were considered 1. Now, it supports escape codes as well. I'd be interested to know if there's a more generic solution to those than fixed options for filtering, perhaps something to the tune of
type Filterer interface{
Length(in string, offset int) int
Reset()
}

The return value of Filter being used to identify how many items the character at in[k] should count as. The arguments are not obvious because a string can be interpreted as a []byte or a []rune, both of which are potentially valid use cases, and []byte(in)[k] and []rune(in)[k] could have very different consequences.

One could argue that it's more generic to use it as []byte and have the filter function panic if it needs utf8 but the input is not valid, but that would lead to boilerplate for what seems to be a more common use case.

Reset() would be called before reusing an existing filter on a different string in case it's somehow stateful.

A second variation on this could be to return (adv int, length int) from Filter, one being used to signify how many characters k should advance, and how many the item from in[k:k+adv] should be counted as.
Sign in to participate in the conversation
Mastodon

Server run by the main developers of the project 🐘 It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!