Skip to content

Commit

Permalink
Added a gizak/termui for selecting the countries
Browse files Browse the repository at this point in the history
  • Loading branch information
denzyldick committed Dec 10, 2022
1 parent 627c17b commit 14d7cf8
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -19,6 +18,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 @@ -213,29 +215,55 @@ func uniqueCities(cities types.CityDetails) types.CityDetails {
}

// Give the user a chance to select the correct city.
func selectCity(cities types.CityDetails) int {
fmt.Println("There are multiple cities with the same name.")
fmt.Println("Please select the 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 {
fmt.Println(fmt.Sprintf("%d: %s, %s", i, c.Name, c.Country))
l.Rows = append(l.Rows, fmt.Sprintf("%d: %s, %s", i, c.Name, c.Country))
}
var number int
var err error

r := bufio.NewReader(os.Stdin)
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 {
fmt.Fprint(os.Stderr, fmt.Sprintf("Choose a city between 0 - %d: ", len(cities)-1))
input, _ := r.ReadString('\n')
number, err = strconv.Atoi(input[:len(input)-1])
if err != nil {
fmt.Println(fmt.Sprintf("%s is not a valid number", input))
}

if number >= 0 && number < len(cities) {
break
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)
}
return number
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down

0 comments on commit 14d7cf8

Please sign in to comment.