Skip to content

Commit

Permalink
Merge 41375b8 into 8ac4a9b
Browse files Browse the repository at this point in the history
  • Loading branch information
mum4k committed Nov 15, 2020
2 parents 8ac4a9b + 41375b8 commit 6c9aaf8
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 141 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -13,7 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `tcell` dependency was upgraded 1.4.0.
- `tcell` dependency was upgraded v2.0.0.
- aligned the definition of the first 16 colors with the definition used by
Xterm and `tcell`. Defined two non-standard colors `ColorMagenta` and
`ColorCyan` to make this change backward compatible for users that use
`termbox-go`.
- made `tcell` terminal implementation the default in examples, demos and
documentation.
- upgrading versions on all dependencies.
Expand Down
25 changes: 21 additions & 4 deletions cell/color.go
Expand Up @@ -48,17 +48,32 @@ var colorNames = map[Color]string{
const (
ColorDefault Color = iota

// 8 "system" colors.
// The 16 Xterm colors.
// See https://jonasjacek.github.io/colors/
ColorBlack
ColorRed
ColorMaroon
ColorGreen
ColorOlive
ColorNavy
ColorPurple
ColorTeal
ColorSilver
ColorGray
ColorRed
ColorLime
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorFuchsia
ColorAqua
ColorWhite
)

// Colors defined for backward compatibility with termbox-go.
const (
ColorMagenta Color = ColorPurple
ColorCyan Color = ColorTeal
)

// ColorNumber sets a color using its number.
// Make sure your terminal is set to a terminalapi.ColorMode that supports the
// target color. The provided value must be in the range 0-255.
Expand Down Expand Up @@ -86,6 +101,8 @@ func ColorRGB6(r, g, b int) Color {
return ColorDefault
}
}
// Explanation:
// https://stackoverflow.com/questions/27159322/rgb-values-of-the-colors-in-the-ansi-extended-colors-index-17-255
return Color(0x10 + 36*r + 6*g + b + 1) // Colors are off-by-one due to ColorDefault being zero.
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/mum4k/termdash
go 1.14

require (
github.com/gdamore/tcell v1.4.0
github.com/gdamore/tcell/v2 v2.0.0
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-runewidth v0.0.9
github.com/nsf/termbox-go v0.0.0-20201107200903-9b52a5faed9e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -6,6 +6,8 @@ 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/gdamore/tcell/v2 v2.0.0 h1:GRWG8aLfWAlekj9Q6W29bVvkHENc6hp79XOqG4AWDOs=
github.com/gdamore/tcell/v2 v2.0.0/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
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=
Expand Down
40 changes: 23 additions & 17 deletions terminal/tcell/cell_options.go
Expand Up @@ -15,34 +15,43 @@
package tcell

import (
"github.com/gdamore/tcell"
tcell "github.com/gdamore/tcell/v2"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/terminal/terminalapi"
)

// cellColor converts termdash cell color to the tcell format.
func cellColor(c cell.Color) tcell.Color {
return tcell.Color(c&0x1ff) - 1
if c == cell.ColorDefault {
return tcell.ColorDefault
}
// Subtract one, because cell.ColorBlack has value one instead of zero.
// Zero is used for cell.ColorDefault instead.
return tcell.Color(c-1) + tcell.ColorValid
}

// fixColor converts the target color for the current color mode
func fixColor(c tcell.Color, colorMode terminalapi.ColorMode) tcell.Color {
if c == tcell.ColorDefault {
// colorToMode adjusts the color to the color mode.
func colorToMode(c cell.Color, colorMode terminalapi.ColorMode) cell.Color {
if c == cell.ColorDefault {
return c
}
switch colorMode {
case terminalapi.ColorModeNormal:
c %= tcell.Color(16)
c %= 16 + 1 // Add one for cell.ColorDefault.
case terminalapi.ColorMode256:
c %= tcell.Color(256)
c %= 256 + 1 // Add one for cell.ColorDefault.
case terminalapi.ColorMode216:
c %= tcell.Color(216)
c += tcell.Color(16)
if c <= 216 { // Add one for cell.ColorDefault.
return c + 16
}
c = c%216 + 16
case terminalapi.ColorModeGrayscale:
c %= tcell.Color(24)
c += tcell.Color(232)
if c <= 24 { // Add one for cell.ColorDefault.
return c + 232
}
c = c%24 + 232
default:
c = tcell.ColorDefault
c = cell.ColorDefault
}
return c
}
Expand All @@ -51,11 +60,8 @@ func fixColor(c tcell.Color, colorMode terminalapi.ColorMode) tcell.Color {
func cellOptsToStyle(opts *cell.Options, colorMode terminalapi.ColorMode) tcell.Style {
st := tcell.StyleDefault

fg := cellColor(opts.FgColor)
bg := cellColor(opts.BgColor)

fg = fixColor(fg, colorMode)
bg = fixColor(bg, colorMode)
fg := cellColor(colorToMode(opts.FgColor, colorMode))
bg := cellColor(colorToMode(opts.BgColor, colorMode))

// FIXME: tcell doesn't have a strikethrough style option until #254 is resolved.
st = st.Foreground(fg).Background(bg).Bold(opts.Bold).Italic(opts.Italic).Underline(opts.Underline)
Expand Down

0 comments on commit 6c9aaf8

Please sign in to comment.