From 3feec6ebfb5ed9eb4626ba0dcb27c5c1ab10852e Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Fri, 15 May 2020 14:56:19 -0700 Subject: [PATCH] Document adding TextView.SetMaxLines --- CHANGELOG | 1 + textview.go | 35 +++++++++++++++++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 910d8a8..ac877ab 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ v1.4.6 (WIP) - Add Box.ShowFocus - Add List.GetOffset, List.SetOffset and List.SetSelectedTextAttributes +- Add TextView.SetMaxLines - Add Vim-style keybindings to List - Fix List not updating selected item before calling selected handlers - Do not handle right click on List when there is no context menu diff --git a/textview.go b/textview.go index 6cc5c91..02106d7 100644 --- a/textview.go +++ b/textview.go @@ -136,7 +136,7 @@ type TextView struct { // The index of the first line shown in the text view. lineOffset int - // The maximum number of liens the text view will hold (set to 0 for no limit) + // The maximum number of newlines the text view will hold (0 = unlimited). maxLines int // If set to true, the text view will always remain at the end of the content. @@ -197,16 +197,13 @@ type TextView struct { // NewTextView returns a new text view. func NewTextView() *TextView { return &TextView{ - Box: NewBox(), - highlights: make(map[string]struct{}), - lineOffset: -1, - scrollable: true, - align: AlignLeft, - wrap: true, - textColor: Styles.PrimaryTextColor, - regions: false, - dynamicColors: false, - maxLines: 0, + Box: NewBox(), + highlights: make(map[string]struct{}), + lineOffset: -1, + scrollable: true, + align: AlignLeft, + wrap: true, + textColor: Styles.PrimaryTextColor, } } @@ -394,16 +391,18 @@ func (t *TextView) SetHighlightedFunc(handler func(added, removed, remaining []s } func (t *TextView) clipBuffer() { - if t.maxLines > 0 { - lenbuf := len(t.buffer) - if lenbuf > t.maxLines { - t.buffer = t.buffer[lenbuf-t.maxLines:] - } + if t.maxLines <= 0 { + return + } + + lenbuf := len(t.buffer) + if lenbuf > t.maxLines { + t.buffer = t.buffer[lenbuf-t.maxLines:] } } -// SetMaxLines establishes the maximum amount of lines the text view will hold -// before discarding older data from the buffer +// SetMaxLines sets the maximum number of newlines the text view will hold +// before discarding older data from the buffer. func (t *TextView) SetMaxLines(maxLines int) *TextView { t.maxLines = maxLines t.clipBuffer()