2022-07-07 20:27:00 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-08-08 03:50:24 -04:00
|
|
|
// Display advanced menu and get user input
|
2022-08-02 23:26:09 -04:00
|
|
|
func advancedMenu(app *Application) {
|
2022-07-07 20:27:00 -04:00
|
|
|
var option string
|
2022-07-26 22:20:04 -04:00
|
|
|
// Menu loop
|
2022-07-07 20:27:00 -04:00
|
|
|
for option != "0" {
|
|
|
|
|
|
|
|
fmt.Print("\nAdvanced Menu\n-------------\n\n")
|
2022-07-11 19:07:24 -04:00
|
|
|
fmt.Printf("1. Change units (Default: imperial; Current: %s)\n", app.Config.Units)
|
2022-07-07 20:27:00 -04:00
|
|
|
fmt.Println("2. Enter precise location")
|
2022-08-02 23:26:09 -04:00
|
|
|
fmt.Println("3. Get historical data")
|
2022-07-07 20:27:00 -04:00
|
|
|
fmt.Print("0. Back\n\n")
|
|
|
|
|
2022-07-26 22:20:04 -04:00
|
|
|
// Read user input
|
2022-07-07 20:27:00 -04:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
input, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2022-07-08 22:12:39 -04:00
|
|
|
log.Println(err)
|
2022-07-07 20:27:00 -04:00
|
|
|
}
|
|
|
|
option = strings.TrimSuffix(input, "\n")
|
|
|
|
|
2022-08-08 03:50:24 -04:00
|
|
|
if option != "0" {
|
|
|
|
doAdvanced(app, option)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 17:04:33 -04:00
|
|
|
|
2022-08-08 03:50:24 -04:00
|
|
|
// Perform the users selected option from the advanced menu
|
|
|
|
func doAdvanced(app *Application, option string) {
|
|
|
|
var isValid bool
|
|
|
|
if option == "1" {
|
|
|
|
current := app.Config.Units
|
|
|
|
if current == "imperial" {
|
|
|
|
app.Config.Units = "metric"
|
|
|
|
} else {
|
|
|
|
app.Config.Units = "imperial"
|
|
|
|
}
|
|
|
|
fmt.Printf("\nChanged units from %s to %s...\n\n", current, app.Config.Units)
|
|
|
|
} else if option == "2" {
|
|
|
|
for !isValid {
|
|
|
|
getPreciseLocation(app)
|
|
|
|
getCurrent(app)
|
|
|
|
isValid = validateCurrent(app)
|
|
|
|
}
|
|
|
|
printWeather(app)
|
|
|
|
app.Forecast.Weather.Temp = -500.00
|
|
|
|
} else if option == "3" {
|
|
|
|
for !isValid {
|
|
|
|
getPreciseLocation(app)
|
|
|
|
getDate(app)
|
|
|
|
getHistoricalData(app.Client, app)
|
|
|
|
isValid = validateHistorical(app)
|
|
|
|
}
|
|
|
|
printHistorical(app)
|
|
|
|
app.Forecast.Weather.Temp = -500.00
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 23:26:09 -04:00
|
|
|
|
2022-08-08 03:50:24 -04:00
|
|
|
// Validates results of queries by location
|
|
|
|
func validateCurrent(app *Application) bool {
|
|
|
|
var isValid bool
|
|
|
|
if app.Forecast.Weather.Temp != -500.00 {
|
|
|
|
isValid = true
|
|
|
|
} else {
|
|
|
|
fmt.Println("I couldn't get any information for that location.")
|
|
|
|
fmt.Println("Please double check the location you entered.")
|
|
|
|
}
|
|
|
|
return isValid
|
|
|
|
}
|
2022-07-13 17:04:33 -04:00
|
|
|
|
2022-08-08 03:50:24 -04:00
|
|
|
// Validates results of queries for historical data
|
|
|
|
func validateHistorical(app *Application) bool {
|
|
|
|
var isValid bool
|
|
|
|
if app.HistoricalForecast.Temp != -500.00 {
|
|
|
|
isValid = true
|
|
|
|
} else {
|
2022-08-08 05:56:25 -04:00
|
|
|
fmt.Println("I couldn't get any information for that location and date.")
|
|
|
|
fmt.Println("Please double check the information you entered.")
|
2022-07-07 20:27:00 -04:00
|
|
|
}
|
2022-08-08 03:50:24 -04:00
|
|
|
return isValid
|
2022-07-07 20:27:00 -04:00
|
|
|
}
|