Skip to content

Commit

Permalink
Merge pull request #7 from denzyldick/master
Browse files Browse the repository at this point in the history
Select the correct city.
  • Loading branch information
eoin-barr authored Dec 16, 2022
2 parents 516243a + a32c1cb commit 72e0ae3
Show file tree
Hide file tree
Showing 71 changed files with 8,019 additions and 591 deletions.
88 changes: 85 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"

ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)

const (
Expand Down Expand Up @@ -90,7 +93,7 @@ func getWeather(args []string, view string) {

q := u.Query()
q.Set("q", strings.ToLower(city))
q.Set("limit", "1")
q.Set("limit", "5")
q.Set("appid", secret)
u.RawQuery = q.Encode()

Expand All @@ -117,8 +120,18 @@ func getWeather(args []string, view string) {
return
}

lat := CityDetails[0].Lat
lon := CityDetails[0].Lon
var index = 0
if len(CityDetails) > 1 {
filtered := uniqueCities(CityDetails)
if len(filtered) == 1 {
CityDetails = filtered
} else {
index = renderList(filtered)
}
}

lat := CityDetails[index].Lat
lon := CityDetails[index].Lon

var u2 url.URL
u2.Scheme = "https"
Expand Down Expand Up @@ -181,6 +194,75 @@ var rootCmd = &cobra.Command{
},
}

// Remove duplicates from city list
func uniqueCities(cities types.CityDetails) types.CityDetails {
var filtered types.CityDetails
for _, city := range cities {
duplicate := false
for _, c := range filtered {
if city.Name == c.Name && city.Country == c.Country {
duplicate = true
}
}
if duplicate == false {
filtered = append(filtered, city)
}
}
return filtered
}

// Give the user a chance to select the correct city.
func renderList(cities types.CityDetails) int {
if err := ui.Init(); err != nil {
fmt.Println(err)
}
defer ui.Close()

l := widgets.NewList()
l.Title = "There are multiple cities with the same name."
l.Border = false
for i, c := range cities {
l.Rows = append(l.Rows, fmt.Sprintf("%d: %s, %s", i, c.Name, c.Country))
}

l.WrapText = false
l.SetRect(0, 0, 100, 100)
l.SelectedRowStyle.Fg = ui.ColorGreen
ui.Render(l)
uiEvents := ui.PollEvents()
var index int
selected := false
for {
e := <-uiEvents

switch e.ID {
case "j", "<Down>":
l.ScrollDown()
case "k", "<Up>":
l.ScrollUp()
case "<C-d>":
l.ScrollHalfPageDown()
case "<C-u>":
l.ScrollHalfPageUp()
case "<C-f>":
l.ScrollPageDown()
case "<C-b>":
l.ScrollPageUp()
case "<Home>":
case "<C-c>":
os.Exit(0)
case "<Enter>":
index = l.SelectedRow
selected = true
}

if(selected){
return index
}
ui.Render(l)
}
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand Down
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ module github.com/eoin-barr/weatherme

go 1.19

require (
github.com/gizak/termui/v3 v3.1.0
github.com/spf13/cobra v1.6.1
golang.org/x/text v0.4.0
)

require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/mattn/go-runewidth v0.0.2 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/text v0.4.0 // indirect
)
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/gizak/termui/v3 v3.1.0 h1:ZZmVDgwHl7gR7elfKf1xc4IudXZ5qqfDh4wExk4Iajc=
github.com/gizak/termui/v3 v3.1.0/go.mod h1:bXQEBkJpzxUAKf0+xq9MSWAvWZlE7c+aidmyFlkYTrY=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/gizak/termui/v3/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/gizak/termui/v3/alignment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions vendor/github.com/gizak/termui/v3/backend.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions vendor/github.com/gizak/termui/v3/block.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 72e0ae3

Please sign in to comment.