add font modifier cell options: bold, italic, underline.

This commit is contained in:
“Carson 2020-11-12 15:08:58 -05:00
parent 2a7dafa3a8
commit 50310f4d29
6 changed files with 64 additions and 7 deletions

View File

@ -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
})
}

2
go.mod
View File

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

5
go.sum
View File

@ -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=

View File

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

View File

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

View File

@ -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)
}