Skip to content

Commit

Permalink
Merge pull request #116 from mum4k/256-color-default
Browse files Browse the repository at this point in the history
256 color default
  • Loading branch information
mum4k committed Feb 11, 2019
2 parents e9d0471 + 69b7799 commit 3ff8826
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.6.0] - 07-Feb-2019

### Changed

- Termbox is now initialized in 256 color mode by default.

### Added

- The SegmentDisplay widget.
Expand Down
26 changes: 18 additions & 8 deletions terminal/termbox/termbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func (o option) set(t *Terminal) {
o(t)
}

// DefaultColorMode is the default value for the ColorMode option.
const DefaultColorMode = terminalapi.ColorMode256

// ColorMode sets the terminal color mode.
// Defaults to DefaultColorMode.
func ColorMode(cm terminalapi.ColorMode) Option {
return option(func(t *Terminal) {
t.colorMode = cm
Expand All @@ -60,6 +64,19 @@ type Terminal struct {
colorMode terminalapi.ColorMode
}

// newTerminal creates the terminal and applies the options.
func newTerminal(opts ...Option) *Terminal {
t := &Terminal{
events: eventqueue.New(),
done: make(chan struct{}),
colorMode: DefaultColorMode,
}
for _, opt := range opts {
opt.set(t)
}
return t
}

// New returns a new termbox based Terminal.
// Call Close() when the terminal isn't required anymore.
func New(opts ...Option) (*Terminal, error) {
Expand All @@ -68,14 +85,7 @@ func New(opts ...Option) (*Terminal, error) {
}
tbx.SetInputMode(tbx.InputEsc | tbx.InputMouse)

t := &Terminal{
events: eventqueue.New(),
done: make(chan struct{}),
}
for _, opt := range opts {
opt.set(t)
}

t := newTerminal(opts...)
om, err := colorMode(t.colorMode)
if err != nil {
return nil, err
Expand Down
60 changes: 60 additions & 0 deletions terminal/termbox/termbox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2019 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 termbox

import (
"testing"

"github.com/kylelemons/godebug/pretty"
"github.com/mum4k/termdash/terminalapi"
)

func TestNewTerminal(t *testing.T) {
tests := []struct {
desc string
opts []Option
want *Terminal
}{
{
desc: "default options",
want: &Terminal{
colorMode: terminalapi.ColorMode256,
},
},
{
desc: "sets color mode",
opts: []Option{
ColorMode(terminalapi.ColorModeNormal),
},
want: &Terminal{
colorMode: terminalapi.ColorModeNormal,
},
},
}

for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got := newTerminal(tc.opts...)

// Ignore these fields.
got.events = nil
got.done = nil

if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("newTerminal => unexpected diff (-want, +got):\n%s", diff)
}
})
}
}

0 comments on commit 3ff8826

Please sign in to comment.