Render implements the weather.Renderer interface.
(query domain.Query, localizer localization.Localizer)
| 31 | |
| 32 | // Render implements the weather.Renderer interface. |
| 33 | func (r *V1Renderer) Render(query domain.Query, localizer localization.Localizer) (domain.RenderOutput, error) { |
| 34 | if query.Weather == nil || len(*query.Weather) == 0 { |
| 35 | return domain.RenderOutput{}, errors.New("no weather data provided") |
| 36 | } |
| 37 | |
| 38 | // Unmarshal the raw weather data |
| 39 | var data domain.Weather |
| 40 | if err := json.Unmarshal(*query.Weather, &data); err != nil { |
| 41 | return domain.RenderOutput{}, fmt.Errorf("failed to unmarshal weather data: %w", err) |
| 42 | } |
| 43 | |
| 44 | if len(data.CurrentCondition) == 0 { |
| 45 | return domain.RenderOutput{}, errors.New("no current condition data available") |
| 46 | } |
| 47 | |
| 48 | l10n := localization.New(localizer, query.Options) |
| 49 | dataResp := ConvertWeather(data) |
| 50 | |
| 51 | opts := query.Options |
| 52 | if opts == nil { |
| 53 | opts = &options.Options{} |
| 54 | } |
| 55 | |
| 56 | // Determine location name |
| 57 | locationName := "" |
| 58 | if query.Location != nil && query.Location.Name != "" { |
| 59 | locationName = query.Location.Name |
| 60 | } else if len(data.Request) > 0 && data.Request[0].Query != "" { |
| 61 | locationName = data.Request[0].Query |
| 62 | } |
| 63 | if opts.Location != "" { |
| 64 | locationName = opts.Location |
| 65 | } |
| 66 | |
| 67 | // Right-to-left support |
| 68 | r.rightToLeft = (opts.Lang == "he" || opts.Lang == "ar" || opts.Lang == "fa") |
| 69 | |
| 70 | // Build caption |
| 71 | caption := l10n.Text("CAPTION_WEATHER_REPORT_FOR") |
| 72 | |
| 73 | var header string |
| 74 | if opts.Quiet || opts.NoCaption { |
| 75 | header = locationName + "\n\n" |
| 76 | } else { |
| 77 | if r.rightToLeft { |
| 78 | caption = locationName + " " + caption |
| 79 | padding := 125 - displaywidth.String(caption) |
| 80 | if padding < 0 { |
| 81 | padding = 0 |
| 82 | } |
| 83 | space := strings.Repeat(" ", padding) |
| 84 | header = space + caption + "\n\n" |
| 85 | } else { |
| 86 | header = fmt.Sprintf("%s %s\n\n", caption, locationName) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Current condition |