From 50310f4d29e7e7164a97b70a10c3d8e7ab837cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CCarson?= Date: Thu, 12 Nov 2020 15:08:58 -0500 Subject: [PATCH] add font modifier cell options: bold, italic, underline. --- cell/cell.go | 36 +++++++++++++++++++++++++++++-- go.mod | 2 +- go.sum | 5 +++++ terminal/tcell/cell_options.go | 3 ++- terminal/termbox/cell_options.go | 11 +++++++++- widgets/text/textdemo/textdemo.go | 14 ++++++++++-- 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/cell/cell.go b/cell/cell.go index c3eb6df..8088b94 100644 --- a/cell/cell.go +++ b/cell/cell.go @@ -23,8 +23,12 @@ type Option interface { // Options stores the provided options. type Options struct { - FgColor Color - BgColor Color + FgColor Color + BgColor Color + Bold bool + Italic bool + Underline bool + Strikethrough bool } // Set allows existing options to be passed as an option. @@ -62,3 +66,31 @@ func BgColor(color Color) Option { co.BgColor = color }) } + +// Bold makes cell's text bold. +func Bold() Option { + return option(func(co *Options) { + co.Bold = true + }) +} + +// Italic makes cell's text italic. Only works when using the tcell backend. +func Italic() Option { + return option(func(co *Options) { + co.Italic = true + }) +} + +// Underline makes cell's text underlined. +func Underline() Option { + return option(func(co *Options) { + co.Underline = true + }) +} + +// Strikethrough strikes through the cell's text. Currently a no-op until tcell is updated to >= v2.0.0 +func Strikethrough() Option { + return option(func(co *Options) { + co.Strikethrough = true + }) +} diff --git a/go.mod b/go.mod index 3a81b32..e007d5e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/mum4k/termdash go 1.14 require ( - github.com/gdamore/tcell v1.3.0 + github.com/gdamore/tcell v1.4.0 github.com/kylelemons/godebug v1.1.0 github.com/mattn/go-runewidth v0.0.9 github.com/nsf/termbox-go v0.0.0-20200204031403-4d2b513ad8be diff --git a/go.sum b/go.sum index 5e34e27..1a5c516 100644 --- a/go.sum +++ b/go.sum @@ -4,11 +4,16 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM= github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM= +github.com/gdamore/tcell v1.4.0 h1:vUnHwJRvcPQa3tzi+0QI4U9JINXYJlOz9yiaiPQ2wMU= +github.com/gdamore/tcell v1.4.0/go.mod h1:vxEiSDZdW3L+Uhjii9c3375IlDmR05bzxY404ZVSMo0= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4= github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s= +github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/nsf/termbox-go v0.0.0-20200204031403-4d2b513ad8be h1:yzmWtPyxEUIKdZg4RcPq64MfS8NA6A5fNOJgYhpR9EQ= diff --git a/terminal/tcell/cell_options.go b/terminal/tcell/cell_options.go index e0357a9..7195181 100644 --- a/terminal/tcell/cell_options.go +++ b/terminal/tcell/cell_options.go @@ -57,6 +57,7 @@ func cellOptsToStyle(opts *cell.Options, colorMode terminalapi.ColorMode) tcell. fg = fixColor(fg, colorMode) bg = fixColor(bg, colorMode) - st = st.Foreground(fg).Background(bg) + // FIXME: tcell doesn't have a strikethrough style option + st = st.Foreground(fg).Background(bg).Bold(opts.Bold).Italic(opts.Italic).Underline(opts.Underline) return st } diff --git a/terminal/termbox/cell_options.go b/terminal/termbox/cell_options.go index 41ee760..3c12fc8 100644 --- a/terminal/termbox/cell_options.go +++ b/terminal/termbox/cell_options.go @@ -28,7 +28,16 @@ func cellColor(c cell.Color) tbx.Attribute { // cellOptsToFg converts the cell options to the termbox foreground attribute. func cellOptsToFg(opts *cell.Options) tbx.Attribute { - return cellColor(opts.FgColor) + a := cellColor(opts.FgColor) + if opts.Bold { + a |= tbx.AttrBold + } + // FIXME: Termbox doesn't have an italics attribute + if opts.Underline { + a |= tbx.AttrUnderline + } + // FIXME: Termbox doesn't have a strikethrough attribute + return a } // cellOptsToBg converts the cell options to the termbox background attribute. diff --git a/widgets/text/textdemo/textdemo.go b/widgets/text/textdemo/textdemo.go index ede5368..cf30ea9 100644 --- a/widgets/text/textdemo/textdemo.go +++ b/widgets/text/textdemo/textdemo.go @@ -22,11 +22,12 @@ import ( "math/rand" "time" + "github.com/mum4k/termdash/terminal/tcell" + "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" - "github.com/mum4k/termdash/terminal/termbox" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/text" ) @@ -67,7 +68,7 @@ func writeLines(ctx context.Context, t *text.Text, delay time.Duration) { } func main() { - t, err := termbox.New() + t, err := tcell.New() if err != nil { panic(err) } @@ -108,6 +109,15 @@ func main() { if err := wrapped.Write(" colors", text.WriteCellOpts(cell.FgColor(cell.ColorBlue))); err != nil { panic(err) } + if err := wrapped.Write(" and"); err != nil { + panic(err) + } + if err := wrapped.Write(" font ", text.WriteCellOpts(cell.Bold(), cell.Italic())); err != nil { + panic(err) + } + if err := wrapped.Write("modifiers", text.WriteCellOpts(cell.Underline(), cell.Italic())); err != nil { + panic(err) + } if err := wrapped.Write(". Wraps long lines at rune boundaries if the WrapAtRunes() option is provided.\nSupports newline character to\ncreate\nnewlines\nmanually.\nTrims the content if it is too long.\n\n\n\nToo long."); err != nil { panic(err) }