1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-24 13:48:50 +08:00

Format and rename some functions

This commit is contained in:
Caleb Bassi 2019-04-25 12:52:13 -07:00
parent d3b8175035
commit 6ba81fb541
5 changed files with 66 additions and 77 deletions

View File

@ -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 {
styles := make([]string, 0)
if color, ok := textColorMap[self.Fg]; ok && self.Fg != StyleClear.Fg {
styles = append(styles, tokenFg + tokenValueSeparator + color)
if color, ok := textColorMap[self.Fg]; ok && self.Fg != StyleClear.Fg {
styles = append(styles, tokenFg+tokenValueSeparator+color)
}
if color, ok := textColorMap[self.Bg]; ok && self.Bg != StyleClear.Bg {
styles = append(styles, tokenBg + tokenValueSeparator + color)
if color, ok := textColorMap[self.Bg]; ok && self.Bg != StyleClear.Bg {
styles = append(styles, tokenBg+tokenValueSeparator+color)
}
if mod, ok := textModifierMap[self.Modifier]; ok && self.Modifier != StyleClear.Modifier {
styles = append(styles, tokenModifier + tokenValueSeparator + mod)
if mod, ok := textModifierMap[self.Modifier]; ok && self.Modifier != StyleClear.Modifier {
styles = append(styles, tokenModifier+tokenValueSeparator+mod)
}
return strings.Join(styles, tokenItemSeparator)

View File

@ -45,14 +45,14 @@ var StyleParserColorMap = map[string]Color{
}
var textColorMap = map[Color]string{
ColorRed: "red",
ColorBlue: "blue",
ColorBlack: "black",
ColorCyan: "cyan",
ColorYellow: "yellow",
ColorWhite: "white",
ColorClear: "clear",
ColorGreen: "green",
ColorRed: "red",
ColorBlue: "blue",
ColorBlack: "black",
ColorCyan: "cyan",
ColorYellow: "yellow",
ColorWhite: "white",
ColorClear: "clear",
ColorGreen: "green",
ColorMagenta: "magenta",
}
@ -63,9 +63,9 @@ var modifierMap = map[string]Modifier{
}
var textModifierMap = map[Modifier]string{
ModifierBold: "bold",
ModifierBold: "bold",
ModifierUnderline: "underline",
ModifierReverse: "reverse",
ModifierReverse: "reverse",
}
// readStyle translates an []rune like `fg:red,mod:bold,bg:white` to a style

View File

@ -188,40 +188,7 @@ func RunesToStyledCells(runes []rune, style Style) []Cell {
return cells
}
//CellsToText 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()
}
// CellsToString converts []Cell to a string without any formatting tags
func CellsToString(cells []Cell) string {
runes := make([]rune, len(cells))
for i, cell := range cells {
@ -230,7 +197,32 @@ func CellsToString(cells []Cell) string {
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 {
sb.WriteByte(tokenBeginStyledText)
sb.WriteString(string(runes))
@ -276,7 +268,7 @@ func JoinCells(cells [][]Cell, r rune) []Cell {
length := len(cells)
for i, cell := range cells {
if i < length - 1 {
if i < length-1 {
cell = append(cell, lb)
}
joinCells = append(joinCells, cell...)

View File

@ -116,18 +116,14 @@ func (self *TextBox) SetText(input string) {
self.InsertText(input)
}
//GetText gets the text in string format along all its formatting tags
func (self *TextBox) Text() string {
cells := JoinCells(self.text, '\n')
return CellsToStyledText(cells, self.TextStyle)
// GetText gets the text in string format along all its formatting tags
func (self *TextBox) GetText() string {
return CellsToStyledString(JoinCells(self.text, '\n'), self.TextStyle)
}
//GetText gets the text in string format without any formatting tags
func (self *TextBox) RawText() string {
cells := JoinCells(self.text, '\n')
return CellsToText(cells)
// GetRawText gets the text in string format without any formatting tags
func (self *TextBox) GetRawText() string {
return CellsToString(JoinCells(self.text, '\n'))
}
func (self *TextBox) MoveCursorLeft() {

View File

@ -2,58 +2,59 @@ package widgets
import (
"testing"
"github.com/stretchr/testify/assert"
)
//TestGetRawText test simple string
// TestGetRawText test simple string
func TestGetRawText(t *testing.T) {
text := "My Sample RawText"
tb := NewTextBox()
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) {
text := `My Sample RawText
with
line
with
line
breaks`
tb := NewTextBox()
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) {
text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a bit more"
tb := NewTextBox()
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) {
text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a [bit more](fg:green)"
tb := NewTextBox()
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) {
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)`
tb := NewTextBox()
tb.SetText(text)
assert.Equal(t, text, tb.Text())
}
assert.Equal(t, text, tb.GetText())
}