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

     1  package lunarphase
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/rivo/tview"
    10  	"github.com/wtfutil/wtf/utils"
    11  	"github.com/wtfutil/wtf/view"
    12  	"github.com/wtfutil/wtf/wtf"
    13  )
    14  
    15  type Widget struct {
    16  	view.ScrollableWidget
    17  
    18  	current   bool
    19  	day       string
    20  	date      time.Time
    21  	last      string
    22  	result    string
    23  	settings  *Settings
    24  	timeout   time.Duration
    25  	titleBase string
    26  }
    27  
    28  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
    29  	widget := &Widget{
    30  		ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
    31  		settings:         settings,
    32  	}
    33  
    34  	widget.current = true
    35  	widget.date = time.Now()
    36  	widget.day = widget.date.Format(dateFormat)
    37  	widget.last = ""
    38  	widget.timeout = time.Duration(widget.settings.requestTimeout) * time.Second
    39  	widget.titleBase = widget.settings.Title
    40  
    41  	widget.SetRenderFunction(widget.Refresh)
    42  	widget.initializeKeyboardControls()
    43  
    44  	return widget
    45  }
    46  
    47  func (widget *Widget) Refresh() {
    48  	if widget.current {
    49  		widget.date = time.Now()
    50  		widget.day = widget.date.Format(dateFormat)
    51  	}
    52  	if widget.day != widget.last {
    53  		widget.lunarPhase()
    54  	}
    55  
    56  	if !widget.settings.Enabled {
    57  		widget.settings.Common.Title = widget.titleBase + " " + widget.day + " [ Disabled ]"
    58  		widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, "", false })
    59  		widget.View.Clear()
    60  		return
    61  	}
    62  	widget.settings.Common.Title = widget.titleBase + " " + widget.day
    63  
    64  	widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, widget.result, false })
    65  }
    66  
    67  func (widget *Widget) RefreshTitle() {
    68  	if !widget.settings.Enabled {
    69  		widget.settings.Common.Title = widget.titleBase + " " + widget.day + " [ Disabled ]"
    70  		widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, "", false })
    71  		widget.View.Clear()
    72  		return
    73  	}
    74  	widget.settings.Common.Title = widget.titleBase + " [" + widget.day + "]"
    75  
    76  	widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, widget.result, false })
    77  }
    78  
    79  // this method reads the config and calls wttr.in for lunar phase
    80  func (widget *Widget) lunarPhase() {
    81  	client := &http.Client{
    82  		Timeout: widget.timeout,
    83  	}
    84  
    85  	language := widget.settings.language
    86  
    87  	req, err := http.NewRequest("GET", "https://wttr.in/Moon@"+widget.day+"?AF&lang="+language, http.NoBody)
    88  	if err != nil {
    89  		widget.result = err.Error()
    90  		return
    91  	}
    92  
    93  	req.Header.Set("Accept-Language", widget.settings.language)
    94  	req.Header.Set("User-Agent", "curl")
    95  	response, err := client.Do(req)
    96  	if err != nil {
    97  		widget.result = err.Error()
    98  		return
    99  	}
   100  	defer func() { _ = response.Body.Close() }()
   101  
   102  	contents, err := io.ReadAll(response.Body)
   103  	if err != nil {
   104  		widget.result = err.Error()
   105  		return
   106  	}
   107  
   108  	widget.last = widget.day
   109  	widget.result = strings.TrimSpace(wtf.ASCIItoTviewColors(string(contents)))
   110  }
   111  
   112  // NextDay shows the next day's lunar phase (KeyRight / 'n')
   113  func (widget *Widget) NextDay() {
   114  	widget.current = false
   115  	tomorrow := widget.date.AddDate(0, 0, 1)
   116  	widget.setDay(tomorrow)
   117  }
   118  
   119  // NextWeek shows the next week's lunar phase (KeyUp / 'N')
   120  func (widget *Widget) NextWeek() {
   121  	widget.current = false
   122  	nextweek := widget.date.AddDate(0, 0, 7)
   123  	widget.setDay(nextweek)
   124  }
   125  
   126  // PrevDay shows the previous day's lunar phase (KeyLeft / 'p')
   127  func (widget *Widget) PrevDay() {
   128  	widget.current = false
   129  	yesterday := widget.date.AddDate(0, 0, -1)
   130  	widget.setDay(yesterday)
   131  }
   132  
   133  // Today shows the current day's lunar phase ('t')
   134  func (widget *Widget) Today() {
   135  	widget.current = true
   136  	widget.Refresh()
   137  }
   138  
   139  // PrevWeek shows the previous week's lunar phase (KeyDown / 'P')
   140  func (widget *Widget) PrevWeek() {
   141  	widget.current = false
   142  	lastweek := widget.date.AddDate(0, 0, -7)
   143  	widget.setDay(lastweek)
   144  }
   145  
   146  func (widget *Widget) setDay(ts time.Time) {
   147  	widget.date = ts
   148  	widget.day = widget.date.Format(dateFormat)
   149  	widget.RefreshTitle()
   150  }
   151  
   152  // Open nineplanets.org in a browser (Enter / 'o')
   153  func (widget *Widget) OpenMoonPhase() {
   154  	phasedate := widget.date.Format(phaseFormat)
   155  	utils.OpenFile("https://nineplanets.org/moon/phase/" + phasedate + "/")
   156  }
   157  
   158  // Disable/Enable the widget (Ctrl-D)
   159  func (widget *Widget) DisableWidget() {
   160  	if widget.settings.Enabled {
   161  		widget.settings.Enabled = false
   162  		widget.RefreshTitle()
   163  	} else {
   164  		widget.settings.Enabled = true
   165  		widget.Refresh()
   166  	}
   167  }