mirror of
https://github.com/mum4k/termdash.git
synced 2025-04-25 13:48:50 +08:00

Our tcell library incorrectly referenced tcell color values rather than just names. The values aren't part of the public API and did change with the update to v2. This commit switches our tcell library to using the public constants exported by the `tcell` project. Also aligning our color definition of the first 16 colors with `tcell` and Xterm. Adding two additional colors to make this change backward compatible with `termbox-go`.
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
// 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 (
|
|
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 {
|
|
/*
|
|
switch c {
|
|
case cell.ColorDefault:
|
|
return tcell.ColorDefault
|
|
case cell.ColorBlack:
|
|
return tcell.ColorBlack
|
|
case cell.ColorRed:
|
|
return tcell.ColorRed
|
|
case cell.ColorGreen:
|
|
return tcell.ColorGreen
|
|
case cell.ColorYellow:
|
|
return tcell.ColorYellow
|
|
case cell.ColorBlue:
|
|
return tcell.ColorBlue
|
|
case cell.ColorMagenta:
|
|
return tcell.ColorFuchsia
|
|
case cell.ColorCyan:
|
|
return tcell.ColorAqua
|
|
case cell.ColorWhite:
|
|
return tcell.ColorWhite
|
|
}
|
|
*/
|
|
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
|
|
}
|
|
|
|
// 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 %= 16 + 1 // Add one for cell.ColorDefault.
|
|
case terminalapi.ColorMode256:
|
|
c %= 256 + 1 // Add one for cell.ColorDefault.
|
|
case terminalapi.ColorMode216:
|
|
if c <= 216 { // Add one for cell.ColorDefault.
|
|
return c + 16
|
|
}
|
|
c = c%216 + 16
|
|
case terminalapi.ColorModeGrayscale:
|
|
if c <= 24 { // Add one for cell.ColorDefault.
|
|
return c + 232
|
|
}
|
|
c = c%24 + 232
|
|
default:
|
|
c = cell.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(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)
|
|
return st
|
|
}
|