2024-03-04 09:01:54 +08:00
|
|
|
// Copyright 2024 The TCell Authors
|
2015-09-27 14:37:54 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use 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
|
|
|
|
|
2020-10-17 06:33:31 +08:00
|
|
|
// Style represents a complete text style, including both foreground color,
|
|
|
|
// background color, and additional attributes such as "bold" or "underline".
|
2015-10-06 06:46:51 +08:00
|
|
|
//
|
|
|
|
// Note that not all terminals can display all colors or attributes, and
|
|
|
|
// many might have specific incompatibilities between specific attributes
|
|
|
|
// and color combinations.
|
2015-10-08 11:15:33 +08:00
|
|
|
//
|
|
|
|
// To use Style, just declare a variable of its type.
|
2020-08-26 10:29:11 +08:00
|
|
|
type Style struct {
|
2024-03-05 15:56:23 +08:00
|
|
|
fg Color
|
|
|
|
bg Color
|
|
|
|
ulStyle UnderlineStyle
|
|
|
|
ulColor Color
|
|
|
|
attrs AttrMask
|
|
|
|
url string
|
|
|
|
urlId string
|
2020-08-26 10:29:11 +08:00
|
|
|
}
|
2015-09-27 14:37:54 +08:00
|
|
|
|
2015-10-08 11:15:33 +08:00
|
|
|
// StyleDefault represents a default style, based upon the context.
|
|
|
|
// It is the zero value.
|
2020-08-26 10:29:11 +08:00
|
|
|
var StyleDefault Style
|
2015-09-27 14:37:54 +08:00
|
|
|
|
2020-08-26 10:29:11 +08:00
|
|
|
// styleInvalid is just an arbitrary invalid style used internally.
|
|
|
|
var styleInvalid = Style{attrs: AttrInvalid}
|
2015-10-28 05:29:32 +08:00
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Foreground returns a new style based on s, with the foreground color set
|
|
|
|
// as requested. ColorDefault can be used to select the global default.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Foreground(c Color) Style {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2 := s
|
|
|
|
s2.fg = c
|
|
|
|
return s2
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Background returns a new style based on s, with the background color set
|
|
|
|
// as requested. ColorDefault can be used to select the global default.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Background(c Color) Style {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2 := s
|
|
|
|
s2.bg = c
|
|
|
|
return s2
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Decompose breaks a style up, returning the foreground, background,
|
2022-04-17 00:49:31 +08:00
|
|
|
// and other attributes. The URL if set is not included.
|
2024-03-05 12:22:09 +08:00
|
|
|
// Deprecated: Applications should not attempt to decompose style,
|
|
|
|
// as this content is not sufficient to describe the actual style.
|
2015-10-06 06:46:51 +08:00
|
|
|
func (s Style) Decompose() (fg Color, bg Color, attr AttrMask) {
|
2020-08-26 10:29:11 +08:00
|
|
|
return s.fg, s.bg, s.attrs
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2020-08-26 10:29:11 +08:00
|
|
|
func (s Style) setAttrs(attrs AttrMask, on bool) Style {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2 := s
|
2015-09-27 14:37:54 +08:00
|
|
|
if on {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2.attrs |= attrs
|
|
|
|
} else {
|
|
|
|
s2.attrs &^= attrs
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
2024-03-05 12:22:09 +08:00
|
|
|
return s2
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal returns the style with all attributes disabled.
|
|
|
|
func (s Style) Normal() Style {
|
2020-08-26 10:29:11 +08:00
|
|
|
return Style{
|
|
|
|
fg: s.fg,
|
|
|
|
bg: s.bg,
|
|
|
|
}
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Bold returns a new style based on s, with the bold attribute set
|
|
|
|
// as requested.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Bold(on bool) Style {
|
2020-08-26 10:29:11 +08:00
|
|
|
return s.setAttrs(AttrBold, on)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Blink returns a new style based on s, with the blink attribute set
|
|
|
|
// as requested.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Blink(on bool) Style {
|
2020-08-26 10:29:11 +08:00
|
|
|
return s.setAttrs(AttrBlink, on)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Dim returns a new style based on s, with the dim attribute set
|
|
|
|
// as requested.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Dim(on bool) Style {
|
2020-08-26 10:29:11 +08:00
|
|
|
return s.setAttrs(AttrDim, on)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2020-03-27 01:09:10 +08:00
|
|
|
// Italic returns a new style based on s, with the italic attribute set
|
|
|
|
// as requested.
|
|
|
|
func (s Style) Italic(on bool) Style {
|
2020-08-26 10:29:11 +08:00
|
|
|
return s.setAttrs(AttrItalic, on)
|
2020-03-27 01:09:10 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Reverse returns a new style based on s, with the reverse attribute set
|
|
|
|
// as requested. (Reverse usually changes the foreground and background
|
|
|
|
// colors.)
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Reverse(on bool) Style {
|
2020-08-26 10:29:11 +08:00
|
|
|
return s.setAttrs(AttrReverse, on)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2020-08-31 23:28:45 +08:00
|
|
|
// StrikeThrough sets strikethrough mode.
|
|
|
|
func (s Style) StrikeThrough(on bool) Style {
|
|
|
|
return s.setAttrs(AttrStrikeThrough, on)
|
2020-08-31 11:25:30 +08:00
|
|
|
}
|
2020-10-17 06:33:31 +08:00
|
|
|
|
2024-03-05 15:56:23 +08:00
|
|
|
// Underline style. Modern terminals have the option of rendering the
|
|
|
|
// underline using different styles, and even different colors.
|
|
|
|
type UnderlineStyle int
|
2024-03-04 09:01:54 +08:00
|
|
|
|
2024-03-05 15:56:23 +08:00
|
|
|
const (
|
|
|
|
UnderlineStyleNone = UnderlineStyle(iota)
|
|
|
|
UnderlineStyleSolid
|
|
|
|
UnderlineStyleDouble
|
|
|
|
UnderlineStyleCurly
|
|
|
|
UnderlineStyleDotted
|
|
|
|
UnderlineStyleDashed
|
|
|
|
)
|
2024-03-04 09:01:54 +08:00
|
|
|
|
2024-03-05 15:56:23 +08:00
|
|
|
// Underline returns a new style based on s, with the underline attribute set
|
|
|
|
// as requested. The parameters can be:
|
|
|
|
//
|
|
|
|
// bool: on / off - enables just a simple underline
|
|
|
|
// UnderlineStyle: sets a specific style (should not coexist with the bool)
|
|
|
|
// Color: the color to use
|
|
|
|
func (s Style) Underline(params ...interface{}) Style {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2 := s
|
2024-03-05 15:56:23 +08:00
|
|
|
for _, param := range params {
|
|
|
|
switch v := param.(type) {
|
|
|
|
case bool:
|
|
|
|
if v {
|
|
|
|
s2.ulStyle = UnderlineStyleSolid
|
|
|
|
s2.attrs |= AttrUnderline
|
|
|
|
} else {
|
|
|
|
s2.ulStyle = UnderlineStyleNone
|
|
|
|
s2.attrs &^= AttrUnderline
|
|
|
|
}
|
|
|
|
case UnderlineStyle:
|
|
|
|
if v == UnderlineStyleNone {
|
|
|
|
s2.attrs &^= AttrUnderline
|
|
|
|
} else {
|
|
|
|
s2.attrs |= AttrUnderline
|
|
|
|
}
|
|
|
|
s2.ulStyle = v
|
|
|
|
case Color:
|
|
|
|
s2.ulColor = v
|
|
|
|
default:
|
|
|
|
panic("Bad type for underline")
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 12:22:09 +08:00
|
|
|
return s2
|
|
|
|
}
|
|
|
|
|
2020-10-17 06:33:31 +08:00
|
|
|
// Attributes returns a new style based on s, with its attributes set as
|
|
|
|
// specified.
|
|
|
|
func (s Style) Attributes(attrs AttrMask) Style {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2 := s
|
|
|
|
s2.attrs = attrs
|
|
|
|
return s2
|
2022-04-17 00:49:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Url returns a style with the Url set. If the provided Url is not empty,
|
|
|
|
// and the terminal supports it, text will typically be marked up as a clickable
|
|
|
|
// link to that Url. If the Url is empty, then this mode is turned off.
|
|
|
|
func (s Style) Url(url string) Style {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2 := s
|
|
|
|
s2.url = url
|
|
|
|
return s2
|
2022-10-15 20:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UrlId returns a style with the UrlId set. If the provided UrlId is not empty,
|
|
|
|
// any marked up Url with this style will be given the UrlId also. If the
|
|
|
|
// terminal supports it, any text with the same UrlId will be grouped as if it
|
|
|
|
// were one Url, even if it spans multiple lines.
|
|
|
|
func (s Style) UrlId(id string) Style {
|
2024-03-05 12:22:09 +08:00
|
|
|
s2 := s
|
|
|
|
s2.urlId = "id=" + id
|
|
|
|
return s2
|
2020-10-17 06:33:31 +08:00
|
|
|
}
|