github.com/wtfutil/wtf@v0.43.0/modules/gitter/widget.go (about)

     1  package gitter
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rivo/tview"
     7  	"github.com/wtfutil/wtf/utils"
     8  	"github.com/wtfutil/wtf/view"
     9  )
    10  
    11  // A Widget represents a Gitter widget
    12  type Widget struct {
    13  	view.ScrollableWidget
    14  
    15  	messages []Message
    16  	settings *Settings
    17  }
    18  
    19  // NewWidget creates a new instance of a widget
    20  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
    21  	widget := Widget{
    22  		ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
    23  
    24  		settings: settings,
    25  	}
    26  
    27  	widget.SetRenderFunction(widget.Refresh)
    28  	widget.initializeKeyboardControls()
    29  
    30  	return &widget
    31  }
    32  
    33  /* -------------------- Exported Functions -------------------- */
    34  
    35  func (widget *Widget) Refresh() {
    36  	if widget.Disabled() {
    37  		return
    38  	}
    39  
    40  	room, err := GetRoom(widget.settings.roomURI, widget.settings.apiToken)
    41  	if err != nil {
    42  		widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, err.Error(), true })
    43  		return
    44  	}
    45  
    46  	if room == nil {
    47  		widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, "No room", true })
    48  		return
    49  	}
    50  
    51  	messages, err := GetMessages(room.ID, widget.settings.numberOfMessages, widget.settings.apiToken)
    52  
    53  	if err != nil {
    54  		widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, err.Error(), true })
    55  		return
    56  	}
    57  	widget.messages = messages
    58  	widget.SetItemCount(len(messages))
    59  
    60  	widget.display()
    61  }
    62  
    63  /* -------------------- Unexported Functions -------------------- */
    64  
    65  func (widget *Widget) display() {
    66  	widget.Redraw(widget.content)
    67  }
    68  
    69  func (widget *Widget) content() (string, string, bool) {
    70  	title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.settings.roomURI)
    71  	if widget.messages == nil || len(widget.messages) == 0 {
    72  		return title, "No Messages To Display", false
    73  	}
    74  	var str string
    75  	for idx, message := range widget.messages {
    76  		row := fmt.Sprintf(
    77  			`[%s] [blue]%s [lightslategray]%s: [%s]%s [aqua]%s`,
    78  			widget.RowColor(idx),
    79  			message.From.DisplayName,
    80  			message.From.Username,
    81  			widget.RowColor(idx),
    82  			message.Text,
    83  			message.Sent.Format("Jan 02, 15:04 MST"),
    84  		)
    85  
    86  		str += utils.HighlightableHelper(widget.View, row, idx, len(message.Text))
    87  	}
    88  
    89  	return title, str, true
    90  }