From 088142bf91deae874aaa52071af1ef41936db729 Mon Sep 17 00:00:00 2001 From: devppratik Date: Mon, 13 Feb 2023 13:49:43 +0530 Subject: [PATCH] Replaced Select List with prompt-ui --- cmd/tmz/search.go | 30 +++++------------------------- cmd/tmz/select.go | 22 ++++------------------ cmd/tmz/utc.go | 1 + go.mod | 7 +++++-- go.sum | 23 +++++++++++++++++++---- pkg/ui/select.go | 21 +++++++++++++++++++++ pkg/ui/table.go | 13 +++++++++++++ 7 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 pkg/ui/select.go diff --git a/cmd/tmz/search.go b/cmd/tmz/search.go index 19d2092..8ca9dc5 100644 --- a/cmd/tmz/search.go +++ b/cmd/tmz/search.go @@ -7,7 +7,6 @@ import ( "time" tmzUI "tmz/pkg/ui" - "github.com/rivo/tview" "github.com/spf13/cobra" "github.com/thedevsaddam/gojsonq/v2" ) @@ -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() @@ -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...) }, } diff --git a/cmd/tmz/select.go b/cmd/tmz/select.go index 368aba6..a9dba29 100644 --- a/cmd/tmz/select.go +++ b/cmd/tmz/select.go @@ -6,7 +6,6 @@ import ( tmzUI "tmz/pkg/ui" tmzUtils "tmz/pkg/utils" - "github.com/rivo/tview" "github.com/spf13/cobra" ) @@ -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...) }, } diff --git a/cmd/tmz/utc.go b/cmd/tmz/utc.go index bf3495b..e5dbaa7 100644 --- a/cmd/tmz/utc.go +++ b/cmd/tmz/utc.go @@ -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) }, } diff --git a/go.mod b/go.mod index cbee92a..69053a1 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ) diff --git a/go.sum b/go.sum index ae1a0dd..366eaa4 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/pkg/ui/select.go b/pkg/ui/select.go new file mode 100644 index 0000000..d962e02 --- /dev/null +++ b/pkg/ui/select.go @@ -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 +} diff --git a/pkg/ui/table.go b/pkg/ui/table.go index d772e41..97ba32c 100644 --- a/pkg/ui/table.go +++ b/pkg/ui/table.go @@ -1,6 +1,9 @@ package tmz import ( + "os" + + "github.com/aquasecurity/table" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) @@ -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() +}