Skip to content

Commit

Permalink
Merge 6c6d276 into 68bf056
Browse files Browse the repository at this point in the history
  • Loading branch information
mum4k committed Mar 7, 2020
2 parents 68bf056 + 6c6d276 commit 2f34655
Show file tree
Hide file tree
Showing 13 changed files with 1,211 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,8 +1,8 @@
language: go
go:
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- stable
script:
- go get -t ./...
Expand Down
23 changes: 22 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.11.0] - 7-Mar-2020

#### Breaking API changes

- Termdash now requires at least Go version 1.11.

### Added

- New [`tcell`](https://github.com/gdamore/tcell) based terminal implementation
which implements the `terminalapi.Terminal` interface.
- tcell implementation supports two initialization `Option`s:
- `ColorMode` the terminal color output mode (defaults to 256 color mode)
- `ClearStyle` the foreground and background color style to use when clearing
the screen (defaults to the global ColorDefault for both foreground and
background)

### Fixed

- Improved test coverage of the `Gauge` widget.

## [0.10.0] - 5-Jun-2019

### Added
Expand Down Expand Up @@ -295,7 +315,8 @@ identifiers shouldn't be used externally.
- The Gauge widget.
- The Text widget.

[unreleased]: https://github.com/mum4k/termdash/compare/v0.10.0...devel
[unreleased]: https://github.com/mum4k/termdash/compare/v0.11.0...devel
[0.11.0]: https://github.com/mum4k/termdash/compare/v0.10.0...v0.11.0
[0.10.0]: https://github.com/mum4k/termdash/compare/v0.9.1...v0.10.0
[0.9.1]: https://github.com/mum4k/termdash/compare/v0.9.0...v0.9.1
[0.9.0]: https://github.com/mum4k/termdash/compare/v0.8.0...v0.9.0
Expand Down
16 changes: 2 additions & 14 deletions internal/numbers/numbers_test.go
Expand Up @@ -83,8 +83,7 @@ func TestZeroBeforeDecimal(t *testing.T) {
}
}

// Copied from the math package of Go 1.10 for backwards compatibility with Go
// 1.8 where the math.Round function doesn't exist yet.
// Copied from the Go's math package, file all_test.go.
func tolerance(a, b, e float64) bool {
// Multiplying by e here can underflow denormal values to zero.
// Check a==b so that at least if a and b are small and identical
Expand All @@ -107,18 +106,7 @@ func tolerance(a, b, e float64) bool {
}
return d < e
}
func close(a, b float64) bool { return tolerance(a, b, 1e-14) }
func veryclose(a, b float64) bool { return tolerance(a, b, 4e-16) }
func soclose(a, b, e float64) bool { return tolerance(a, b, e) }
func alike(a, b float64) bool {
switch {
case math.IsNaN(a) && math.IsNaN(b):
return true
case a == b:
return math.Signbit(a) == math.Signbit(b)
}
return false
}
func veryclose(a, b float64) bool { return tolerance(a, b, 4e-16) }

func TestMinMax(t *testing.T) {
tests := []struct {
Expand Down
27 changes: 26 additions & 1 deletion termdashdemo/termdashdemo.go
Expand Up @@ -18,7 +18,9 @@ package main

import (
"context"
"flag"
"fmt"
"log"
"math"
"math/rand"
"sync"
Expand All @@ -31,6 +33,7 @@ import (
"github.com/mum4k/termdash/container/grid"
"github.com/mum4k/termdash/keyboard"
"github.com/mum4k/termdash/linestyle"
"github.com/mum4k/termdash/terminal/tcell"
"github.com/mum4k/termdash/terminal/termbox"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/mum4k/termdash/widgets/barchart"
Expand Down Expand Up @@ -466,8 +469,30 @@ func contLayout(w *widgets) ([]container.Option, error) {
// rootID is the ID assigned to the root container.
const rootID = "root"

// Terminal implementations
const (
termboxTerminal = "termbox"
tcellTerminal = "tcell"
)

func main() {
t, err := termbox.New(termbox.ColorMode(terminalapi.ColorMode256))
terminalPtr := flag.String("terminal",
"termbox",
"The terminal implementation to use. Available implementations are 'termbox' and 'tcell' (default = termbox).")
flag.Parse()

var t terminalapi.Terminal
var err error
switch terminal := *terminalPtr; terminal {
case termboxTerminal:
t, err = termbox.New(termbox.ColorMode(terminalapi.ColorMode256))
case tcellTerminal:
t, err = tcell.New(tcell.ColorMode(terminalapi.ColorMode256))
default:
log.Fatalf("Unknown terminal implementation '%s' specified. Please choose between 'termbox' and 'tcell'.", terminal)
return
}

if err != nil {
panic(err)
}
Expand Down
62 changes: 62 additions & 0 deletions terminal/tcell/cell_options.go
@@ -0,0 +1,62 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tcell

import (
"github.com/gdamore/tcell"
"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
}

// fixColor converts the target color for the current color mode
func fixColor(c tcell.Color, colorMode terminalapi.ColorMode) tcell.Color {
if c == tcell.ColorDefault {
return c
}
switch colorMode {
case terminalapi.ColorModeNormal:
c %= tcell.Color(16)
case terminalapi.ColorMode256:
c %= tcell.Color(256)
case terminalapi.ColorMode216:
c %= tcell.Color(216)
c += tcell.Color(16)
case terminalapi.ColorModeGrayscale:
c %= tcell.Color(24)
c += tcell.Color(232)
default:
c = tcell.ColorDefault
}
return c
}

// cellOptsToStyle converts termdash cell color to the tcell format.
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)

st = st.Foreground(fg).Background(bg)
return st
}
157 changes: 157 additions & 0 deletions terminal/tcell/cell_options_test.go
@@ -0,0 +1,157 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tcell

import (
"testing"

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

func TestCellColor(t *testing.T) {
tests := []struct {
color cell.Color
want tcell.Color
}{
{cell.ColorDefault, tcell.ColorDefault},
{cell.ColorBlack, tcell.ColorBlack},
{cell.ColorRed, tcell.ColorMaroon},
{cell.ColorGreen, tcell.ColorGreen},
{cell.ColorYellow, tcell.ColorOlive},
{cell.ColorBlue, tcell.ColorNavy},
{cell.ColorMagenta, tcell.ColorPurple},
{cell.ColorCyan, tcell.ColorTeal},
{cell.ColorWhite, tcell.ColorSilver},
{cell.ColorNumber(42), tcell.Color(42)},
}

for _, tc := range tests {
t.Run(tc.color.String(), func(t *testing.T) {
got := cellColor(tc.color)
if got != tc.want {
t.Errorf("cellColor(%v) => got %v, want %v", tc.color, got, tc.want)
}
})
}
}

func TestFixColor(t *testing.T) {
tests := []struct {
colorMode terminalapi.ColorMode
color cell.Color
want tcell.Color
}{
// See https://jonasjacek.github.io/colors/ for a good reference of all 256 xterm colors
// All 256 colors
{terminalapi.ColorMode256, cell.ColorDefault, tcell.ColorDefault},
{terminalapi.ColorMode256, cell.ColorBlack, tcell.ColorBlack},
{terminalapi.ColorMode256, cell.ColorRed, tcell.ColorMaroon},
{terminalapi.ColorMode256, cell.ColorGreen, tcell.ColorGreen},
{terminalapi.ColorMode256, cell.ColorYellow, tcell.ColorOlive},
{terminalapi.ColorMode256, cell.ColorBlue, tcell.ColorNavy},
{terminalapi.ColorMode256, cell.ColorMagenta, tcell.ColorPurple},
{terminalapi.ColorMode256, cell.ColorCyan, tcell.ColorTeal},
{terminalapi.ColorMode256, cell.ColorWhite, tcell.ColorSilver},
{terminalapi.ColorMode256, cell.ColorNumber(42), tcell.Color(42)},
// 8 system colors
{terminalapi.ColorModeNormal, cell.ColorDefault, tcell.ColorDefault},
{terminalapi.ColorModeNormal, cell.ColorBlack, tcell.ColorBlack},
{terminalapi.ColorModeNormal, cell.ColorRed, tcell.ColorMaroon},
{terminalapi.ColorModeNormal, cell.ColorGreen, tcell.ColorGreen},
{terminalapi.ColorModeNormal, cell.ColorYellow, tcell.ColorOlive},
{terminalapi.ColorModeNormal, cell.ColorBlue, tcell.ColorNavy},
{terminalapi.ColorModeNormal, cell.ColorMagenta, tcell.ColorPurple},
{terminalapi.ColorModeNormal, cell.ColorCyan, tcell.ColorTeal},
{terminalapi.ColorModeNormal, cell.ColorWhite, tcell.ColorSilver},
{terminalapi.ColorModeNormal, cell.ColorNumber(42), tcell.Color(10)},
// Grayscale colors (all the grey colours from 231 to 255)
{terminalapi.ColorModeGrayscale, cell.ColorDefault, tcell.ColorDefault},
{terminalapi.ColorModeGrayscale, cell.ColorBlack, tcell.Color232},
{terminalapi.ColorModeGrayscale, cell.ColorRed, tcell.Color233},
{terminalapi.ColorModeGrayscale, cell.ColorGreen, tcell.Color234},
{terminalapi.ColorModeGrayscale, cell.ColorYellow, tcell.Color235},
{terminalapi.ColorModeGrayscale, cell.ColorBlue, tcell.Color236},
{terminalapi.ColorModeGrayscale, cell.ColorMagenta, tcell.Color237},
{terminalapi.ColorModeGrayscale, cell.ColorCyan, tcell.Color238},
{terminalapi.ColorModeGrayscale, cell.ColorWhite, tcell.Color239},
{terminalapi.ColorModeGrayscale, cell.ColorNumber(42), tcell.Color(250)},
// 216 colors (16 to 231)
{terminalapi.ColorMode216, cell.ColorDefault, tcell.ColorDefault},
{terminalapi.ColorMode216, cell.ColorBlack, tcell.Color16},
{terminalapi.ColorMode216, cell.ColorRed, tcell.Color17},
{terminalapi.ColorMode216, cell.ColorGreen, tcell.Color18},
{terminalapi.ColorMode216, cell.ColorYellow, tcell.Color19},
{terminalapi.ColorMode216, cell.ColorBlue, tcell.Color20},
{terminalapi.ColorMode216, cell.ColorMagenta, tcell.Color21},
{terminalapi.ColorMode216, cell.ColorCyan, tcell.Color22},
{terminalapi.ColorMode216, cell.ColorWhite, tcell.Color23},
{terminalapi.ColorMode216, cell.ColorNumber(42), tcell.Color(58)},
// Unknown color mode
{-1, cell.ColorRed, tcell.ColorDefault},
}

for _, tc := range tests {
t.Run(tc.colorMode.String()+"_"+tc.color.String(), func(t *testing.T) {
color := cellColor(tc.color)
got := fixColor(color, tc.colorMode)
if got != tc.want {
t.Errorf("fixColor(%v_%v), => got %v, want %v", tc.colorMode, tc.color, got, tc.want)
}
})
}
}

func TestCellOptsToStyle(t *testing.T) {
tests := []struct {
colorMode terminalapi.ColorMode
opts cell.Options
want tcell.Style
}{
{
colorMode: terminalapi.ColorMode256,
opts: cell.Options{FgColor: cell.ColorWhite, BgColor: cell.ColorBlack},
want: tcell.StyleDefault.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack),
},
{
colorMode: terminalapi.ColorModeNormal,
opts: cell.Options{FgColor: cell.ColorWhite, BgColor: cell.ColorBlack},
want: tcell.StyleDefault.Foreground(tcell.ColorSilver).Background(tcell.ColorBlack),
},
{
colorMode: terminalapi.ColorModeGrayscale,
opts: cell.Options{FgColor: cell.ColorWhite, BgColor: cell.ColorBlack},
want: tcell.StyleDefault.Foreground(tcell.Color239).Background(tcell.Color232),
},
{
colorMode: terminalapi.ColorMode216,
opts: cell.Options{FgColor: cell.ColorWhite, BgColor: cell.ColorBlack},
want: tcell.StyleDefault.Foreground(tcell.Color23).Background(tcell.Color16),
},
}

for _, tc := range tests {
t.Run(tc.opts.FgColor.String()+"+"+tc.opts.BgColor.String(), func(t *testing.T) {
got := cellOptsToStyle(&tc.opts, tc.colorMode)
if got != tc.want {
fg, bg, _ := got.Decompose()
wantFg, wantBg, _ := tc.want.Decompose()
t.Errorf("cellOptsToStyle(%v, fg=%v, bg=%v) => got (fg=%X, bg=%X), want (fg=%X, bg=%X)",
tc.colorMode, tc.opts.FgColor, tc.opts.BgColor, fg, bg, wantFg, wantBg)
}
})
}
}

0 comments on commit 2f34655

Please sign in to comment.