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

View File

@ -2,6 +2,7 @@ package widgets
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -11,7 +12,7 @@ func TestGetRawText(t *testing.T) {
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
@ -24,7 +25,7 @@ func TestGetRawTextWithLBs(t *testing.T) {
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
@ -34,7 +35,7 @@ func TestGetStyledText(t *testing.T) {
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
@ -44,7 +45,7 @@ func TestGetStyledText2(t *testing.T) {
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
@ -55,5 +56,5 @@ func TestGetStyledTextWithLBs(t *testing.T) {
tb := NewTextBox() tb := NewTextBox()
tb.SetText(text) tb.SetText(text)
assert.Equal(t, text, tb.Text()) assert.Equal(t, text, tb.GetText())
} }