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

     1  package twitter
     2  
     3  import (
     4  	"fmt"
     5  	"html"
     6  	"regexp"
     7  
     8  	"github.com/dustin/go-humanize"
     9  	"github.com/rivo/tview"
    10  	"github.com/wtfutil/wtf/utils"
    11  	"github.com/wtfutil/wtf/view"
    12  )
    13  
    14  type Widget struct {
    15  	view.MultiSourceWidget
    16  	view.TextWidget
    17  
    18  	client   *Client
    19  	idx      int
    20  	settings *Settings
    21  }
    22  
    23  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
    24  	widget := Widget{
    25  		MultiSourceWidget: view.NewMultiSourceWidget(settings.Common, "screenName", "screenNames"),
    26  		TextWidget:        view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common),
    27  
    28  		idx:      0,
    29  		settings: settings,
    30  	}
    31  
    32  	widget.initializeKeyboardControls()
    33  
    34  	widget.SetDisplayFunction(widget.Refresh)
    35  
    36  	widget.client = NewClient(settings)
    37  
    38  	widget.View.SetBorderPadding(1, 1, 1, 1)
    39  	widget.View.SetWrap(true)
    40  	widget.View.SetWordWrap(true)
    41  
    42  	return &widget
    43  }
    44  
    45  /* -------------------- Exported Functions -------------------- */
    46  
    47  // Refresh is called on the interval and refreshes the data
    48  func (widget *Widget) Refresh() {
    49  	widget.Redraw(widget.content)
    50  }
    51  
    52  /* -------------------- Unexported Functions -------------------- */
    53  
    54  func (widget *Widget) content() (string, string, bool) {
    55  	widget.client.screenName = widget.CurrentSource()
    56  	tweets := widget.client.Tweets()
    57  
    58  	title := fmt.Sprintf("Twitter - [green]@%s[white]", widget.CurrentSource())
    59  
    60  	if len(tweets) == 0 {
    61  		str := fmt.Sprintf("\n\n\n%s", utils.CenterText("[lightblue]No Tweets[white]", 50))
    62  		return title, str, true
    63  	}
    64  
    65  	_, _, width, _ := widget.View.GetRect()
    66  	str := widget.settings.PaginationMarker(len(widget.Sources), widget.Idx, width-2) + "\n"
    67  	for _, tweet := range tweets {
    68  		str += widget.format(tweet)
    69  	}
    70  
    71  	return title, str, true
    72  }
    73  
    74  // If the tweet's Username is the same as the account we're watching, no
    75  // need to display the username
    76  func (widget *Widget) displayName(tweet Tweet) string {
    77  	if widget.CurrentSource() == tweet.User.ScreenName {
    78  		return ""
    79  	}
    80  	return tweet.User.ScreenName
    81  }
    82  
    83  func (widget *Widget) formatText(text string) string {
    84  	result := text
    85  
    86  	// Convert HTML entities
    87  	result = html.UnescapeString(result)
    88  
    89  	// RT indicator
    90  	rtRegExp := regexp.MustCompile(`^RT`)
    91  	result = rtRegExp.ReplaceAllString(result, "[olive]${0}[white::-]")
    92  
    93  	// @name mentions
    94  	atRegExp := regexp.MustCompile(`@[0-9A-Za-z_]*`)
    95  	result = atRegExp.ReplaceAllString(result, "[lightblue]${0}[white]")
    96  
    97  	// HTTP(S) links
    98  	linkRegExp := regexp.MustCompile(`http[s:\/.0-9A-Za-z]*`)
    99  	result = linkRegExp.ReplaceAllString(result, "[lightblue::u]${0}[white::-]")
   100  
   101  	// Hash tags
   102  	hashRegExp := regexp.MustCompile(`#[0-9A-Za-z_]*`)
   103  	result = hashRegExp.ReplaceAllString(result, "[yellow]${0}[white]")
   104  
   105  	return result
   106  }
   107  
   108  func (widget *Widget) format(tweet Tweet) string {
   109  	body := widget.formatText(tweet.Text)
   110  	name := widget.displayName(tweet)
   111  
   112  	var attribution string
   113  	if name == "" {
   114  		attribution = humanize.Time(tweet.Created())
   115  	} else {
   116  		attribution = fmt.Sprintf(
   117  			"%s, %s",
   118  			name,
   119  			humanize.Time(tweet.Created()),
   120  		)
   121  	}
   122  
   123  	return fmt.Sprintf("%s\n[grey]%s[white]\n\n", body, attribution)
   124  }
   125  func (widget *Widget) currentSourceURI() string {
   126  
   127  	src := "https://twitter.com/" + widget.CurrentSource()
   128  	return src
   129  }