From c3391c3c3b56ee7a00a6485ea722b6483ff52d34 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 13 Oct 2020 10:17:33 -0700 Subject: [PATCH] Make printWithStyle public and rename as PrintStyle --- CHANGELOG | 1 + table.go | 4 ++-- treeview.go | 2 +- util.go | 16 ++++++++-------- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 1bed48a..f9ddf10 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -8,6 +8,7 @@ v1.5.1 (WIP) - Fix List dividers allowing selection - Allow modification of scroll bar render text - Allow scrolling List horizontally +- Make printWithStyle public and rename as PrintStyle - Optimize TextView (writing is 90% faster, drawing is 50% faster) - Remove return values from methods which return their primitive (breaks chaining) - Remove Application.ForceDraw (Application.Draw may be called anywhere) diff --git a/table.go b/table.go index 736b904..8147f50 100644 --- a/table.go +++ b/table.go @@ -1070,10 +1070,10 @@ ColumnLoop: finalWidth = width - columnX - 1 } cell.x, cell.y, cell.width = x+columnX+1, y+rowY, finalWidth - _, printed := printWithStyle(screen, cell.Text, x+columnX+1, y+rowY, finalWidth, cell.Align, SetAttributes(tcell.StyleDefault.Foreground(cell.Color), cell.Attributes)) + _, printed := PrintStyle(screen, cell.Text, x+columnX+1, y+rowY, finalWidth, cell.Align, SetAttributes(tcell.StyleDefault.Foreground(cell.Color), cell.Attributes)) if TaggedTextWidth(cell.Text)-printed > 0 && printed > 0 { _, _, style, _ := screen.GetContent(x+columnX+finalWidth, y+rowY) - printWithStyle(screen, []byte(string(SemigraphicsHorizontalEllipsis)), x+columnX+finalWidth, y+rowY, 1, AlignLeft, style) + PrintStyle(screen, []byte(string(SemigraphicsHorizontalEllipsis)), x+columnX+finalWidth, y+rowY, 1, AlignLeft, style) } } diff --git a/treeview.go b/treeview.go index a474d6c..5c6e3f3 100644 --- a/treeview.go +++ b/treeview.go @@ -852,7 +852,7 @@ func (t *TreeView) Draw(screen tcell.Screen) { } style = tcell.StyleDefault.Background(backgroundColor).Foreground(foregroundColor) } - printWithStyle(screen, []byte(node.text), x+node.textX+prefixWidth, posY, width-node.textX-prefixWidth, AlignLeft, style) + PrintStyle(screen, []byte(node.text), x+node.textX+prefixWidth, posY, width-node.textX-prefixWidth, AlignLeft, style) } } diff --git a/util.go b/util.go index 5e222db..7c8a6c6 100644 --- a/util.go +++ b/util.go @@ -277,12 +277,12 @@ func decomposeText(text []byte, findColors, findRegions bool) (colorIndices [][] // Returns the number of actual bytes of the text printed (including color tags) // and the actual width used for the printed runes. func Print(screen tcell.Screen, text []byte, x, y, maxWidth, align int, color tcell.Color) (int, int) { - return printWithStyle(screen, text, x, y, maxWidth, align, tcell.StyleDefault.Foreground(color)) + return PrintStyle(screen, text, x, y, maxWidth, align, tcell.StyleDefault.Foreground(color)) } -// printWithStyle works like Print() but it takes a style instead of just a +// PrintStyle works like Print() but it takes a style instead of just a // foreground color. -func printWithStyle(screen tcell.Screen, text []byte, x, y, maxWidth, align int, style tcell.Style) (int, int) { +func PrintStyle(screen tcell.Screen, text []byte, x, y, maxWidth, align int, style tcell.Style) (int, int) { if maxWidth <= 0 || len(text) == 0 { return 0, 0 } @@ -294,7 +294,7 @@ func printWithStyle(screen tcell.Screen, text []byte, x, y, maxWidth, align int, if align == AlignRight { if strippedWidth <= maxWidth { // There's enough space for the entire text. - return printWithStyle(screen, text, x+maxWidth-strippedWidth, y, maxWidth, AlignLeft, style) + return PrintStyle(screen, text, x+maxWidth-strippedWidth, y, maxWidth, AlignLeft, style) } // Trim characters off the beginning. var ( @@ -322,7 +322,7 @@ func printWithStyle(screen tcell.Screen, text []byte, x, y, maxWidth, align int, text = append(text[:escapeCharPos], text[escapeCharPos+1:]...) } // Print and return. - bytes, width = printWithStyle(screen, text[textPos+tagOffset:], x, y, maxWidth, AlignLeft, style) + bytes, width = PrintStyle(screen, text[textPos+tagOffset:], x, y, maxWidth, AlignLeft, style) return true } return false @@ -331,11 +331,11 @@ func printWithStyle(screen tcell.Screen, text []byte, x, y, maxWidth, align int, } else if align == AlignCenter { if strippedWidth == maxWidth { // Use the exact space. - return printWithStyle(screen, text, x, y, maxWidth, AlignLeft, style) + return PrintStyle(screen, text, x, y, maxWidth, AlignLeft, style) } else if strippedWidth < maxWidth { // We have more space than we need. half := (maxWidth - strippedWidth) / 2 - return printWithStyle(screen, text, x+half, y, maxWidth-half, AlignLeft, style) + return PrintStyle(screen, text, x+half, y, maxWidth-half, AlignLeft, style) } else { // Chop off runes until we have a perfect fit. var choppedLeft, choppedRight, leftIndex, rightIndex int @@ -390,7 +390,7 @@ func printWithStyle(screen tcell.Screen, text []byte, x, y, maxWidth, align int, escapePos++ } } - return printWithStyle(screen, text[leftIndex+tagOffset:], x, y, maxWidth, AlignLeft, style) + return PrintStyle(screen, text[leftIndex+tagOffset:], x, y, maxWidth, AlignLeft, style) } }