mirror of
https://github.com/gizak/termui.git
synced 2025-04-29 13:48:51 +08:00
Format and rename some functions
This commit is contained in:
parent
d3b8175035
commit
6ba81fb541
14
style.go
14
style.go
@ -66,20 +66,20 @@ func NewStyle(fg Color, args ...interface{}) Style {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//String returns a string representation of a Style
|
// String returns a string representation of a Style
|
||||||
func (self Style) String() string {
|
func (self Style) String() string {
|
||||||
styles := make([]string, 0)
|
styles := make([]string, 0)
|
||||||
|
|
||||||
if color, ok := textColorMap[self.Fg]; ok && self.Fg != StyleClear.Fg {
|
if color, ok := textColorMap[self.Fg]; ok && self.Fg != StyleClear.Fg {
|
||||||
styles = append(styles, tokenFg + tokenValueSeparator + color)
|
styles = append(styles, tokenFg+tokenValueSeparator+color)
|
||||||
}
|
}
|
||||||
|
|
||||||
if color, ok := textColorMap[self.Bg]; ok && self.Bg != StyleClear.Bg {
|
if color, ok := textColorMap[self.Bg]; ok && self.Bg != StyleClear.Bg {
|
||||||
styles = append(styles, tokenBg + tokenValueSeparator + color)
|
styles = append(styles, tokenBg+tokenValueSeparator+color)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mod, ok := textModifierMap[self.Modifier]; ok && self.Modifier != StyleClear.Modifier {
|
if mod, ok := textModifierMap[self.Modifier]; ok && self.Modifier != StyleClear.Modifier {
|
||||||
styles = append(styles, tokenModifier + tokenValueSeparator + mod)
|
styles = append(styles, tokenModifier+tokenValueSeparator+mod)
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(styles, tokenItemSeparator)
|
return strings.Join(styles, tokenItemSeparator)
|
||||||
|
@ -45,14 +45,14 @@ var StyleParserColorMap = map[string]Color{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var textColorMap = map[Color]string{
|
var textColorMap = map[Color]string{
|
||||||
ColorRed: "red",
|
ColorRed: "red",
|
||||||
ColorBlue: "blue",
|
ColorBlue: "blue",
|
||||||
ColorBlack: "black",
|
ColorBlack: "black",
|
||||||
ColorCyan: "cyan",
|
ColorCyan: "cyan",
|
||||||
ColorYellow: "yellow",
|
ColorYellow: "yellow",
|
||||||
ColorWhite: "white",
|
ColorWhite: "white",
|
||||||
ColorClear: "clear",
|
ColorClear: "clear",
|
||||||
ColorGreen: "green",
|
ColorGreen: "green",
|
||||||
ColorMagenta: "magenta",
|
ColorMagenta: "magenta",
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,9 +63,9 @@ var modifierMap = map[string]Modifier{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var textModifierMap = map[Modifier]string{
|
var textModifierMap = map[Modifier]string{
|
||||||
ModifierBold: "bold",
|
ModifierBold: "bold",
|
||||||
ModifierUnderline: "underline",
|
ModifierUnderline: "underline",
|
||||||
ModifierReverse: "reverse",
|
ModifierReverse: "reverse",
|
||||||
}
|
}
|
||||||
|
|
||||||
// readStyle translates an []rune like `fg:red,mod:bold,bg:white` to a style
|
// readStyle translates an []rune like `fg:red,mod:bold,bg:white` to a style
|
||||||
|
64
utils.go
64
utils.go
@ -188,40 +188,7 @@ func RunesToStyledCells(runes []rune, style Style) []Cell {
|
|||||||
return cells
|
return cells
|
||||||
}
|
}
|
||||||
|
|
||||||
//CellsToText converts []Cell to a string without any formatting tags
|
// CellsToString converts []Cell to a string without any formatting tags
|
||||||
func CellsToText(cells []Cell) string {
|
|
||||||
runes := make([]rune, len(cells))
|
|
||||||
for i, cell := range cells {
|
|
||||||
runes[i] = cell.Rune
|
|
||||||
}
|
|
||||||
return string(runes)
|
|
||||||
}
|
|
||||||
|
|
||||||
//CellsToStyledText converts []Cell to a string preserving the formatting tags
|
|
||||||
func CellsToStyledText(cells []Cell, defaultStyle Style) string {
|
|
||||||
sb := strings.Builder{}
|
|
||||||
runes := make([]rune, len(cells))
|
|
||||||
currentStyle := cells[0].Style
|
|
||||||
var j int
|
|
||||||
|
|
||||||
for _, cell := range cells {
|
|
||||||
if currentStyle != cell.Style {
|
|
||||||
writeText(&sb, runes[:j], currentStyle, defaultStyle)
|
|
||||||
|
|
||||||
currentStyle = cell.Style
|
|
||||||
j=0
|
|
||||||
}
|
|
||||||
|
|
||||||
runes[j] = cell.Rune
|
|
||||||
j++
|
|
||||||
}
|
|
||||||
|
|
||||||
//Write the last characters left in runes slice
|
|
||||||
writeText(&sb, runes[:j], currentStyle, defaultStyle)
|
|
||||||
|
|
||||||
return sb.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func CellsToString(cells []Cell) string {
|
func CellsToString(cells []Cell) string {
|
||||||
runes := make([]rune, len(cells))
|
runes := make([]rune, len(cells))
|
||||||
for i, cell := range cells {
|
for i, cell := range cells {
|
||||||
@ -230,7 +197,32 @@ func CellsToString(cells []Cell) string {
|
|||||||
return string(runes)
|
return string(runes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeText(sb *strings.Builder,runes []rune, currentStyle Style, defaultStyle Style) {
|
// CellsToStyledString converts []Cell to a string preserving the formatting tags
|
||||||
|
func CellsToStyledString(cells []Cell, defaultStyle Style) string {
|
||||||
|
sb := strings.Builder{}
|
||||||
|
runes := make([]rune, len(cells))
|
||||||
|
currentStyle := cells[0].Style
|
||||||
|
var j int
|
||||||
|
|
||||||
|
for _, cell := range cells {
|
||||||
|
if currentStyle != cell.Style {
|
||||||
|
writeStyledText(&sb, runes[:j], currentStyle, defaultStyle)
|
||||||
|
|
||||||
|
currentStyle = cell.Style
|
||||||
|
j = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
runes[j] = cell.Rune
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the last characters left in runes slice
|
||||||
|
writeStyledText(&sb, runes[:j], currentStyle, defaultStyle)
|
||||||
|
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeStyledText(sb *strings.Builder, runes []rune, currentStyle Style, defaultStyle Style) {
|
||||||
if currentStyle != defaultStyle && currentStyle != StyleClear {
|
if currentStyle != defaultStyle && currentStyle != StyleClear {
|
||||||
sb.WriteByte(tokenBeginStyledText)
|
sb.WriteByte(tokenBeginStyledText)
|
||||||
sb.WriteString(string(runes))
|
sb.WriteString(string(runes))
|
||||||
@ -276,7 +268,7 @@ func JoinCells(cells [][]Cell, r rune) []Cell {
|
|||||||
length := len(cells)
|
length := len(cells)
|
||||||
|
|
||||||
for i, cell := range cells {
|
for i, cell := range cells {
|
||||||
if i < length - 1 {
|
if i < length-1 {
|
||||||
cell = append(cell, lb)
|
cell = append(cell, lb)
|
||||||
}
|
}
|
||||||
joinCells = append(joinCells, cell...)
|
joinCells = append(joinCells, cell...)
|
||||||
|
@ -116,18 +116,14 @@ func (self *TextBox) SetText(input string) {
|
|||||||
self.InsertText(input)
|
self.InsertText(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetText gets the text in string format along all its formatting tags
|
// GetText gets the text in string format along all its formatting tags
|
||||||
func (self *TextBox) Text() string {
|
func (self *TextBox) GetText() string {
|
||||||
cells := JoinCells(self.text, '\n')
|
return CellsToStyledString(JoinCells(self.text, '\n'), self.TextStyle)
|
||||||
|
|
||||||
return CellsToStyledText(cells, self.TextStyle)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetText gets the text in string format without any formatting tags
|
// GetRawText gets the text in string format without any formatting tags
|
||||||
func (self *TextBox) RawText() string {
|
func (self *TextBox) GetRawText() string {
|
||||||
cells := JoinCells(self.text, '\n')
|
return CellsToString(JoinCells(self.text, '\n'))
|
||||||
|
|
||||||
return CellsToText(cells)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TextBox) MoveCursorLeft() {
|
func (self *TextBox) MoveCursorLeft() {
|
||||||
|
@ -2,58 +2,59 @@ package widgets
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
//TestGetRawText test simple string
|
// TestGetRawText test simple string
|
||||||
func TestGetRawText(t *testing.T) {
|
func TestGetRawText(t *testing.T) {
|
||||||
text := "My Sample RawText"
|
text := "My Sample RawText"
|
||||||
tb := NewTextBox()
|
tb := NewTextBox()
|
||||||
tb.SetText(text)
|
tb.SetText(text)
|
||||||
|
|
||||||
assert.Equal(t, text, tb.RawText())
|
assert.Equal(t, text, tb.GetRawText())
|
||||||
}
|
}
|
||||||
|
|
||||||
//TestGetRawTextWithLBs test line breaks in the text
|
// TestGetRawTextWithLBs test line breaks in the text
|
||||||
func TestGetRawTextWithLBs(t *testing.T) {
|
func TestGetRawTextWithLBs(t *testing.T) {
|
||||||
text := `My Sample RawText
|
text := `My Sample RawText
|
||||||
with
|
with
|
||||||
line
|
line
|
||||||
breaks`
|
breaks`
|
||||||
|
|
||||||
tb := NewTextBox()
|
tb := NewTextBox()
|
||||||
tb.SetText(text)
|
tb.SetText(text)
|
||||||
|
|
||||||
assert.Equal(t, text, tb.RawText())
|
assert.Equal(t, text, tb.GetRawText())
|
||||||
}
|
}
|
||||||
|
|
||||||
//TestGetStyledText test styled text
|
// TestGetStyledText test styled text
|
||||||
func TestGetStyledText(t *testing.T) {
|
func TestGetStyledText(t *testing.T) {
|
||||||
text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a bit more"
|
text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a bit more"
|
||||||
|
|
||||||
tb := NewTextBox()
|
tb := NewTextBox()
|
||||||
tb.SetText(text)
|
tb.SetText(text)
|
||||||
|
|
||||||
assert.Equal(t, text, tb.Text())
|
assert.Equal(t, text, tb.GetText())
|
||||||
}
|
}
|
||||||
|
|
||||||
//TestGetStyledText2 test styled text ending in a styled string
|
// TestGetStyledText2 test styled text ending in a styled string
|
||||||
func TestGetStyledText2(t *testing.T) {
|
func TestGetStyledText2(t *testing.T) {
|
||||||
text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a [bit more](fg:green)"
|
text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a [bit more](fg:green)"
|
||||||
|
|
||||||
tb := NewTextBox()
|
tb := NewTextBox()
|
||||||
tb.SetText(text)
|
tb.SetText(text)
|
||||||
|
|
||||||
assert.Equal(t, text, tb.Text())
|
assert.Equal(t, text, tb.GetText())
|
||||||
}
|
}
|
||||||
|
|
||||||
//TestGetStyledTextWithLBs test styled text with line breaks
|
// TestGetStyledTextWithLBs test styled text with line breaks
|
||||||
func TestGetStyledTextWithLBs(t *testing.T) {
|
func TestGetStyledTextWithLBs(t *testing.T) {
|
||||||
text := `[red text](fg:red,mod:bold) more
|
text := `[red text](fg:red,mod:bold) more
|
||||||
text [blue text](fg:blue,mod:bold) a [bit more](fg:green)`
|
text [blue text](fg:blue,mod:bold) a [bit more](fg:green)`
|
||||||
|
|
||||||
tb := NewTextBox()
|
tb := NewTextBox()
|
||||||
tb.SetText(text)
|
tb.SetText(text)
|
||||||
|
|
||||||
assert.Equal(t, text, tb.Text())
|
assert.Equal(t, text, tb.GetText())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user