github.com/wtfutil/wtf@v0.43.0/view/text_widget.go (about)

     1  package view
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/rivo/tview"
     7  	"github.com/wtfutil/wtf/cfg"
     8  	"github.com/wtfutil/wtf/wtf"
     9  )
    10  
    11  // TextWidget defines the data necessary to make a text widget
    12  type TextWidget struct {
    13  	*Base
    14  	*KeyboardWidget
    15  
    16  	View *tview.TextView
    17  }
    18  
    19  // NewTextWidget creates and returns an instance of TextWidget
    20  func NewTextWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, commonSettings *cfg.Common) TextWidget {
    21  	widget := TextWidget{
    22  		Base:           NewBase(tviewApp, redrawChan, pages, commonSettings),
    23  		KeyboardWidget: NewKeyboardWidget(commonSettings),
    24  	}
    25  
    26  	widget.View = widget.createView(widget.bordered)
    27  	widget.View.SetInputCapture(widget.KeyboardWidget.InputCapture)
    28  
    29  	widget.Base.SetView(widget.View)
    30  	widget.Base.helpTextFunc = widget.KeyboardWidget.HelpText
    31  
    32  	return widget
    33  }
    34  
    35  /* -------------------- Exported Functions -------------------- */
    36  
    37  // TextView returns the tview.TextView instance
    38  func (widget *TextWidget) TextView() *tview.TextView {
    39  	return widget.View
    40  }
    41  
    42  // Redraw forces a refresh of the onscreen text content of this widget
    43  func (widget *TextWidget) Redraw(data func() (string, string, bool)) {
    44  	title, content, wrap := data()
    45  
    46  	widget.View.Clear()
    47  	widget.View.SetWrap(wrap)
    48  	widget.View.SetTitle(widget.ContextualTitle(title))
    49  	widget.View.SetText(strings.TrimRight(content, "\n"))
    50  
    51  	widget.RedrawChan <- true
    52  }
    53  
    54  /* -------------------- Unexported Functions -------------------- */
    55  
    56  func (widget *TextWidget) createView(bordered bool) *tview.TextView {
    57  	view := tview.NewTextView()
    58  
    59  	view.SetBackgroundColor(wtf.ColorFor(widget.commonSettings.Colors.WidgetTheme.Background))
    60  	view.SetBorder(bordered)
    61  	view.SetBorderColor(wtf.ColorFor(widget.BorderColor()))
    62  	view.SetDynamicColors(true)
    63  	view.SetTextColor(wtf.ColorFor(widget.commonSettings.Colors.TextTheme.Text))
    64  	view.SetTitleColor(wtf.ColorFor(widget.commonSettings.Colors.TextTheme.Title))
    65  	view.SetWrap(false)
    66  
    67  	return view
    68  }