2020-02-29 15:15:23 +08:00
|
|
|
// 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.
|
|
|
|
|
2020-02-15 07:00:45 +08:00
|
|
|
package tcell
|
|
|
|
|
|
|
|
import (
|
2020-11-15 12:21:07 +08:00
|
|
|
tcell "github.com/gdamore/tcell/v2"
|
2020-02-15 07:00:45 +08:00
|
|
|
"github.com/mum4k/termdash/cell"
|
2020-02-29 15:01:13 +08:00
|
|
|
"github.com/mum4k/termdash/terminal/terminalapi"
|
2020-02-15 07:00:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// cellColor converts termdash cell color to the tcell format.
|
|
|
|
func cellColor(c cell.Color) tcell.Color {
|
2020-11-15 12:21:07 +08:00
|
|
|
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
|
2020-02-29 15:01:13 +08:00
|
|
|
}
|
|
|
|
|
2020-11-15 12:21:07 +08:00
|
|
|
// colorToMode adjusts the color to the color mode.
|
|
|
|
func colorToMode(c cell.Color, colorMode terminalapi.ColorMode) cell.Color {
|
|
|
|
if c == cell.ColorDefault {
|
2020-02-29 15:01:13 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
switch colorMode {
|
|
|
|
case terminalapi.ColorModeNormal:
|
2020-11-15 12:21:07 +08:00
|
|
|
c %= 16 + 1 // Add one for cell.ColorDefault.
|
2020-02-29 15:01:13 +08:00
|
|
|
case terminalapi.ColorMode256:
|
2020-11-15 12:21:07 +08:00
|
|
|
c %= 256 + 1 // Add one for cell.ColorDefault.
|
2020-02-29 15:01:13 +08:00
|
|
|
case terminalapi.ColorMode216:
|
2020-11-15 12:21:07 +08:00
|
|
|
if c <= 216 { // Add one for cell.ColorDefault.
|
|
|
|
return c + 16
|
|
|
|
}
|
|
|
|
c = c%216 + 16
|
2020-02-29 15:01:13 +08:00
|
|
|
case terminalapi.ColorModeGrayscale:
|
2020-11-15 12:21:07 +08:00
|
|
|
if c <= 24 { // Add one for cell.ColorDefault.
|
|
|
|
return c + 232
|
|
|
|
}
|
|
|
|
c = c%24 + 232
|
2020-02-29 15:01:13 +08:00
|
|
|
default:
|
2020-11-15 12:21:07 +08:00
|
|
|
c = cell.ColorDefault
|
2020-02-29 15:01:13 +08:00
|
|
|
}
|
|
|
|
return c
|
2020-02-15 07:00:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cellOptsToStyle converts termdash cell color to the tcell format.
|
2020-02-29 15:01:13 +08:00
|
|
|
func cellOptsToStyle(opts *cell.Options, colorMode terminalapi.ColorMode) tcell.Style {
|
|
|
|
st := tcell.StyleDefault
|
|
|
|
|
2020-11-15 12:21:07 +08:00
|
|
|
fg := cellColor(colorToMode(opts.FgColor, colorMode))
|
|
|
|
bg := cellColor(colorToMode(opts.BgColor, colorMode))
|
2020-02-29 15:01:13 +08:00
|
|
|
|
2020-11-17 21:33:36 +08:00
|
|
|
st = st.Foreground(fg).
|
|
|
|
Background(bg).
|
|
|
|
Bold(opts.Bold).
|
|
|
|
Italic(opts.Italic).
|
|
|
|
Underline(opts.Underline).
|
|
|
|
StrikeThrough(opts.Strikethrough).
|
|
|
|
Reverse(opts.Inverse).
|
|
|
|
Blink(opts.Blink)
|
2020-02-15 07:00:45 +08:00
|
|
|
return st
|
|
|
|
}
|