1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-27 13:48:51 +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

@ -188,8 +188,8 @@ func RunesToStyledCells(runes []rune, style Style) []Cell {
return cells
}
//CellsToText converts []Cell to a string without any formatting tags
func CellsToText(cells []Cell) 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 {
runes[i] = cell.Rune
@ -197,8 +197,8 @@ func CellsToText(cells []Cell) string {
return string(runes)
}
//CellsToStyledText converts []Cell to a string preserving the formatting tags
func CellsToStyledText(cells []Cell, defaultStyle Style) string {
// 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
@ -206,7 +206,7 @@ func CellsToStyledText(cells []Cell, defaultStyle Style) string {
for _, cell := range cells {
if currentStyle != cell.Style {
writeText(&sb, runes[:j], currentStyle, defaultStyle)
writeStyledText(&sb, runes[:j], currentStyle, defaultStyle)
currentStyle = cell.Style
j = 0
@ -217,20 +217,12 @@ func CellsToStyledText(cells []Cell, defaultStyle Style) string {
}
// Write the last characters left in runes slice
writeText(&sb, runes[:j], currentStyle, defaultStyle)
writeStyledText(&sb, runes[:j], currentStyle, defaultStyle)
return sb.String()
}
func CellsToString(cells []Cell) string {
runes := make([]rune, len(cells))
for i, cell := range cells {
runes[i] = cell.Rune
}
return string(runes)
}
func writeText(sb *strings.Builder,runes []rune, currentStyle Style, defaultStyle Style) {
func writeStyledText(sb *strings.Builder, runes []rune, currentStyle Style, defaultStyle Style) {
if currentStyle != defaultStyle && currentStyle != StyleClear {
sb.WriteByte(tokenBeginStyledText)
sb.WriteString(string(runes))

View File

@ -117,17 +117,13 @@ func (self *TextBox) SetText(input string) {
}
// 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)
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,6 +2,7 @@ package widgets
import (
"testing"
"github.com/stretchr/testify/assert"
)
@ -11,7 +12,7 @@ func TestGetRawText(t *testing.T) {
tb := NewTextBox()
tb.SetText(text)
assert.Equal(t, text, tb.RawText())
assert.Equal(t, text, tb.GetRawText())
}
// TestGetRawTextWithLBs test line breaks in the text
@ -24,7 +25,7 @@ func TestGetRawTextWithLBs(t *testing.T) {
tb := NewTextBox()
tb.SetText(text)
assert.Equal(t, text, tb.RawText())
assert.Equal(t, text, tb.GetRawText())
}
// TestGetStyledText test styled text
@ -34,7 +35,7 @@ func TestGetStyledText(t *testing.T) {
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
@ -44,7 +45,7 @@ func TestGetStyledText2(t *testing.T) {
tb := NewTextBox()
tb.SetText(text)
assert.Equal(t, text, tb.Text())
assert.Equal(t, text, tb.GetText())
}
// TestGetStyledTextWithLBs test styled text with line breaks
@ -55,5 +56,5 @@ func TestGetStyledTextWithLBs(t *testing.T) {
tb := NewTextBox()
tb.SetText(text)
assert.Equal(t, text, tb.Text())
assert.Equal(t, text, tb.GetText())
}