github.com/wtfutil/wtf@v0.43.0/modules/weatherservices/weather/display.go (about)

     1  package weather
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	owm "github.com/briandowns/openweathermap"
     8  	"github.com/wtfutil/wtf/wtf"
     9  )
    10  
    11  func (widget *Widget) display() {
    12  	widget.Redraw(widget.content)
    13  }
    14  
    15  func (widget *Widget) content() (string, string, bool) {
    16  	var err string
    17  
    18  	if !widget.apiKeyValid() {
    19  		err = " Environment variable WTF_OWM_API_KEY is not set\n"
    20  	}
    21  
    22  	cityData := widget.currentData()
    23  	if err == "" && cityData == nil {
    24  		err += " Weather data is unavailable: no city data\n"
    25  	}
    26  
    27  	if err == "" && len(cityData.Weather) == 0 {
    28  		err += " Weather data is unavailable: no weather data\n"
    29  	}
    30  
    31  	title := widget.CommonSettings().Title
    32  	setWrap := false
    33  
    34  	var content string
    35  	if err != "" {
    36  		setWrap = true
    37  		content = err
    38  	} else {
    39  
    40  		title = widget.buildTitle(cityData)
    41  		_, _, width, _ := widget.View.GetRect()
    42  		content = widget.settings.PaginationMarker(len(widget.Data), widget.Idx, width) + "\n"
    43  
    44  		if widget.settings.compact {
    45  			content += widget.description(cityData) + "\n"
    46  		} else {
    47  			content += widget.description(cityData) + "\n\n"
    48  		}
    49  
    50  		content += widget.temperatures(cityData) + "\n"
    51  		content += widget.sunInfo(cityData)
    52  	}
    53  
    54  	return title, content, setWrap
    55  }
    56  
    57  func (widget *Widget) description(cityData *owm.CurrentWeatherData) string {
    58  	descs := []string{}
    59  	for _, weather := range cityData.Weather {
    60  		descs = append(descs, fmt.Sprintf(" %s", weather.Description))
    61  	}
    62  
    63  	return strings.Join(descs, ",")
    64  }
    65  
    66  func (widget *Widget) sunInfo(cityData *owm.CurrentWeatherData) string {
    67  
    68  	sunriseTime := wtf.UnixTime(int64(cityData.Sys.Sunrise))
    69  	sunsetTime := wtf.UnixTime(int64(cityData.Sys.Sunset))
    70  
    71  	renderStr := fmt.Sprintf(" Rise: %s   Set: %s", sunriseTime.Format("15:04 MST"), sunsetTime.Format("15:04 MST"))
    72  
    73  	if widget.settings.compact {
    74  		renderStr = fmt.Sprintf(" Sun: %s / %s", sunriseTime.Format("15:04"), sunsetTime.Format("15:04"))
    75  	}
    76  
    77  	return renderStr
    78  }
    79  
    80  func (widget *Widget) temperatures(cityData *owm.CurrentWeatherData) string {
    81  	str := fmt.Sprintf("%8s: %4.1f° %s\n", "High", cityData.Main.TempMax, widget.settings.tempUnit)
    82  
    83  	str += fmt.Sprintf(
    84  		"%8s: [%s]%4.1f° %s[white]\n",
    85  		"Current",
    86  		widget.settings.colors.current,
    87  		cityData.Main.Temp,
    88  		widget.settings.tempUnit,
    89  	)
    90  
    91  	if widget.settings.compact {
    92  		str += fmt.Sprintf("%8s: %4.1f° %s", "Low", cityData.Main.TempMin, widget.settings.tempUnit)
    93  	} else {
    94  		str += fmt.Sprintf("%8s: %4.1f° %s\n", "Low", cityData.Main.TempMin, widget.settings.tempUnit)
    95  	}
    96  
    97  	return str
    98  }
    99  
   100  func (widget *Widget) buildTitle(cityData *owm.CurrentWeatherData) string {
   101  	if widget.settings.useEmoji {
   102  		return fmt.Sprintf("%s %s", widget.emojiFor(cityData), cityData.Name)
   103  	}
   104  
   105  	return cityData.Name
   106  }