weather-cli/cmd/menu.go

72 lines
1.6 KiB
Go
Raw Normal View History

2022-08-02 22:15:35 -04:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
2022-08-08 04:33:25 -04:00
// Displays news and calls loop with menu options
2022-08-02 23:26:28 -04:00
func mainMenu(app *Application) {
2022-08-02 22:15:35 -04:00
fmt.Println("\n================================================")
fmt.Printf("| Welcome to the OpenWeatherMap Client! v%s |\n", app.Version)
fmt.Println("================================================")
2022-08-02 22:15:35 -04:00
fmt.Printf("New in version %s:\n", app.Version)
2022-09-03 01:07:24 -04:00
fmt.Println(" - Minor bugfixes")
fmt.Println("New in version 1.0.0:")
2022-08-08 04:33:25 -04:00
fmt.Println(" - Search by zip code")
fmt.Println(" - Search by city")
2022-08-02 22:15:35 -04:00
mainMenuLoop(app)
}
// Display main menu and get user input
func mainMenuLoop(app *Application) {
2022-08-02 22:15:35 -04:00
var option string
for option != "0" {
fmt.Print("\nMain Menu\n---------\n\n")
fmt.Println("1. Use current location (default)")
fmt.Println("2. Search by zip code")
fmt.Println("3. Search by city")
fmt.Println("4. Advanced options (Change units, precise location, etc.)")
2022-08-02 22:15:35 -04:00
fmt.Print("0. Exit\n\n")
// Read user input
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
log.Println(err)
}
option = strings.TrimSuffix(input, "\n")
switch option {
case "":
getLocation(app)
getCurrent(app)
printWeather(app)
case "1":
2022-08-02 22:15:35 -04:00
getLocation(app)
getCurrent(app)
printWeather(app)
case "2":
getZip(app)
getCurrentByLoc(app)
printWeather(app)
case "3":
getCity(app)
getCurrentByLoc(app)
printWeather(app)
case "4":
2022-08-02 22:15:35 -04:00
advancedMenu(app)
case "0":
2022-08-02 22:15:35 -04:00
return
default:
2022-08-02 22:15:35 -04:00
fmt.Print("\nOops! An error occurred, please choose a valid option.\n\n")
}
}
}