Skip to content

Commit

Permalink
Replaced Select List with prompt-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
devppratik committed Feb 13, 2023
1 parent 1aa35a0 commit 088142b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 49 deletions.
30 changes: 5 additions & 25 deletions cmd/tmz/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"
tmzUI "tmz/pkg/ui"

"github.com/rivo/tview"
"github.com/spf13/cobra"
"github.com/thedevsaddam/gojsonq/v2"
)
Expand All @@ -24,8 +23,6 @@ var searchCmd = &cobra.Command{
}
// Read the Country List JSON
countryList := gojsonq.New().File("pkg/data/country.json")
app := tview.NewApplication()
list := tview.NewList()

if len(countryCode) > 2 {
res, _ := countryList.From("ALL").GetR()
Expand All @@ -36,38 +33,21 @@ var searchCmd = &cobra.Command{
listOfTimeZones = append(listOfTimeZones, tmZone)
}
}
for _, tmZone := range listOfTimeZones {
list.AddItem(tmZone, "", 'a', func() {
selectedTimeZone = listOfTimeZones[list.GetCurrentItem()]
app.Stop()
})
}
selectedTimeZone = tmzUI.SelectTimeZone(listOfTimeZones)
} else {
res, _ := countryList.From(strings.ToUpper(countryCode)).GetR()
conv, _ := res.StringSlice()
for _, tmZone := range conv {
list.AddItem(tmZone, "", 'a', func() {
selectedTimeZone = conv[list.GetCurrentItem()]
app.Stop()
})
}
selectedTimeZone = tmzUI.SelectTimeZone(conv)
}

location, err := time.LoadLocation(selectedTimeZone)
if err != nil {
fmt.Print("error")
}
// pages := tview.NewPages()
// table := tmzUI.GetTableWidget(tableItems, len(tableItems)/2, 2)
// fmt.Println("ZONE : ", foundSearchItem, "Current Time :", currentTime)
// pages.AddPage("List", list, true, true)
// pages.AddPage("Display", table, true, false)
currentTime := time.Now().In(location).Format(time.Stamp)
tableItems := []string{"Time Zone", "Current Time", selectedTimeZone, currentTime}
if err := app.SetRoot(list, true).EnableMouse(false).Run(); err != nil {
panic(err)
}
tmzUI.DisplayTable(tableItems, len(tableItems)/2, 2)
tableHeaders := []string{"Time Zone", "Current Time"}
tableItems := []string{selectedTimeZone, currentTime}
tmzUI.DisplayNewTable(tableItems, tableHeaders...)
},
}

Expand Down
22 changes: 4 additions & 18 deletions cmd/tmz/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
tmzUI "tmz/pkg/ui"
tmzUtils "tmz/pkg/utils"

"github.com/rivo/tview"
"github.com/spf13/cobra"
)

Expand All @@ -18,28 +17,15 @@ var selectCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
var selectedLocation string
var listOfTimeZones []string = tmzUtils.ReadConfigFile()
var itemOption rune = 'a'
app := tview.NewApplication()
list := tview.NewList()

for _, tmZone := range listOfTimeZones {
list.AddItem(tmZone, "", itemOption, func() {
selectedLocation = listOfTimeZones[list.GetCurrentItem()]
app.Stop()
})
itemOption += 1
}
if err := app.SetRoot(list, true).EnableMouse(false).Run(); err != nil {
log.Fatalln(err)
}

selectedLocation = tmzUI.SelectTimeZone(listOfTimeZones)
location, err := time.LoadLocation(selectedLocation)
if err != nil {
log.Fatalln(err)
}
currentTime := time.Now().In(location).Format(time.Stamp)
tableItems := []string{"Time Zone", "Current Time", selectedLocation, currentTime}
tmzUI.DisplayTable(tableItems, len(tableItems)/2, 2)
tableHeaders := []string{"Time Zone", "Current Time"}
tableItems := []string{selectedLocation, currentTime}
tmzUI.DisplayNewTable(tableItems, tableHeaders...)
},
}

Expand Down
1 change: 1 addition & 0 deletions cmd/tmz/utc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var utcCmd = &cobra.Command{
}
tableItems := []string{"Local Time Zone", "Local Time", " UTC Time ", localTZName, currentLocalTime, currentUTCTime}
tmzUI.DisplayTable(tableItems, 2, 3)
// tmzUI.DisplayNewTable(localTZName, currentLocalTime, currentUTCTime)
},
}

Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module tmz
go 1.19

require (
github.com/aquasecurity/table v1.8.0
github.com/gdamore/tcell/v2 v2.5.3
github.com/manifoldco/promptui v0.9.0
github.com/rivo/tview v0.0.0-20230206063056-c1eadf1b0bf6
github.com/spf13/cobra v1.6.1
github.com/thedevsaddam/gojsonq/v2 v2.5.2
Expand All @@ -12,13 +14,14 @@ require (
)

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
golang.org/x/text v0.3.7 // indirect
)
23 changes: 19 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
github.com/aquasecurity/table v1.8.0 h1:9ntpSwrUfjrM6/YviArlx/ZBGd6ix8W+MtojQcM7tv0=
github.com/aquasecurity/table v1.8.0/go.mod h1:eqOmvjjB7AhXFgFqpJUEE/ietg7RrMSJZXyTN8E/wZw=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.5.3 h1:b9XQrT6QGbgI7JvZOJXFNczOQeIYbo8BfeSMzt2sAV0=
Expand All @@ -7,8 +16,11 @@ github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7P
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/tview v0.0.0-20230206063056-c1eadf1b0bf6 h1:1V5cn83/adQYICFGoPbFoh+eEy0O1VlbhYoroWIQ8VE=
github.com/rivo/tview v0.0.0-20230206063056-c1eadf1b0bf6/go.mod h1:lBUy/T5kyMudFzWUH/C2moN+NlU5qF505vzOyINXuUQ=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -19,23 +31,26 @@ github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/thedevsaddam/gojsonq/v2 v2.5.2 h1:CoMVaYyKFsVj6TjU6APqAhAvC07hTI6IQen8PHzHYY0=
github.com/thedevsaddam/gojsonq/v2 v2.5.2/go.mod h1:bv6Xa7kWy82uT0LnXPE2SzGqTj33TAEeR560MdJkiXs=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI=
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 h1:xP7rWLUr1e1n2xkK5YB4LI0hPEy3LJC6Wk+D4pGlOJg=
golang.org/x/exp v0.0.0-20230206171751-46f607a40771/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21 changes: 21 additions & 0 deletions pkg/ui/select.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tmz

import (
"log"

"github.com/manifoldco/promptui"
)

func SelectTimeZone(listOfTimeZones []string) string {
prompt := promptui.Select{
Label: "Select the TimeZone",
Items: listOfTimeZones,
}

_, result, err := prompt.Run()

if err != nil {
log.Fatalln("Prompt failed ", err)
}
return result
}
13 changes: 13 additions & 0 deletions pkg/ui/table.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tmz

import (
"os"

"github.com/aquasecurity/table"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -32,3 +35,13 @@ func GetTableWidget(tableItems []string, rows int, cols int) *tview.Table {
}
return table
}

func DisplayNewTable(rowItems []string, rowHeaders ...string) {
t := table.New(os.Stdout)
t.SetLineStyle(table.StyleWhite)
t.SetHeaders(rowHeaders...)
t.SetHeaderStyle(table.StyleBold)
t.SetHeaderStyle(table.StyleBrightYellow)
t.AddRows(rowItems)
t.Render()
}

0 comments on commit 088142b

Please sign in to comment.