diff --git a/CHANGELOG.md b/CHANGELOG.md index 752397a0..7da09275 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/terminal/termbox/termbox.go b/terminal/termbox/termbox.go index 70d232c8..2953c055 100644 --- a/terminal/termbox/termbox.go +++ b/terminal/termbox/termbox.go @@ -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 @@ -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) { @@ -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 diff --git a/terminal/termbox/termbox_test.go b/terminal/termbox/termbox_test.go new file mode 100644 index 00000000..355ddad6 --- /dev/null +++ b/terminal/termbox/termbox_test.go @@ -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) + } + }) + } +}