mum4k / termdash

Terminal based dashboard.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Font modifier cell options (bold, italic, underline)

dyc3 opened this issue · comments

It's currently not possible to set text as bold, italic, underline, or strikethrough.

Modifier ANSI
Bold \e[1m
Italics \e[3m
Underline \e[4m
Strikethrough \e[9m

This is a good point. Looks like we are in luck and both of our terminal layer libraries support at least some of these attributes (details below). Looks like tcell supports more of them than termbox-go, so this may be a point where we start breaking away from termbox.

termbox-go: https://github.com/nsf/termbox-go/blob/9b52a5faed9e2c795f64459fd13cd179c2b0ab2f/api_common.go#L155-L159
tcell: https://github.com/gdamore/tcell/blob/7d87d8188c8d3a236c778107d4925a9b2465a5f6/style.go#L87-L127

@dyc3 please let me know if you are interested in implementing this feature in termdash. If you are, I can provide some pointers. Alternatively I could work on this in the near future.

I could give it a shot. If you could point me in the right direction that would help a lot.

Thank you @dyc3 for the offer of help. I will compile some instructions and pointers and respond here once done.

Termdash has a core concept - a terminal is essentially a cell buffer, i.e. a 2D matrix of cells. Each cell is represented as the Cell object:

// Cell represents a single cell on the terminal.
type Cell struct {
// Rune is the rune stored in the cell.
Rune rune
// Opts are the cell options.
Opts *cell.Options
}

Notice the cell.Options object which stores options for each cell instance:

termdash/cell/cell.go

Lines 25 to 28 in 2a7dafa

type Options struct {
FgColor Color
BgColor Color
}

When Termdash draws the cell buffer onto a terminal, it accesses the terminal true this API:

// Terminal abstracts an implementation of a 2-D terminal.
// A terminal consists of a number of cells.
type Terminal interface {

Which currently has two implementations, one for termbox-go and one for tcell. Notice that the SetCell method directly takes a list of cell options that it applies using methods specific to the implementation:

SetCell(p image.Point, r rune, opts ...cell.Option) error

Hopefully this introduction helps, what we need to do is:

  1. define new (additional) cell options to represent font properties we want to implement (bold, underline, etc.).
  2. update termbox-go and tcell terminalapi implementations to correctly translate these new cell options into calls required to make them work. I have included links to options supported by termbox-go and tcell above in the discussion on this issue.

Note that termbox-go supports a smaller set of options, but we can still implement all of them in termdash. The SetCell method on the terminalapi interface returns an error, so we can simply return an error if say termdash was started with termbox-go and an unsupported option was requested.

Please feel free to let me know if I can provide more details and thanks again for your help.

I've found that both termbox and tcell don't support italics or strikethrough. I'm guessing that it's because it's not supported by a decent number of terminal emulators, but most of the popular ones seem to support it. Should I just implement this for bold and underline for now until one of the backends supports it?

If having bold and underline helps you in the meantime, we can start with those. Note that it is also a possibility for us to contribute new functionality to tcell. I would expect the author to accept new options assuming they work in the popular terminals.

It turns out tcell already supports italics and strikethrough. We just need to update tcell's version. Will send a PR shortly.

Thank you for pointing out that tcell upgraded to 2.x. I have filed #254 to follow up. Reading their changelog - there are a few breaking changes that we will need to accomodate.

What we are aiming for now is to perform the tcell upgrade and add the italics and strikethrough functionality before we push the next release.

However feel free to let me know if you would prefer if we release in the current state to make the changes you contributed available at master.

@dyc3 with tcell upgraded, do you have the time to send a PR adding support for the remaining font attributes?

Yup! Will do

Great, thank you.

Thank you for your help on this feature and with upgrading tcell @dyc3. I will push a new release to master shortly.

Resolved by #266