Skip to content

Commit

Permalink
chore: update table view
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefcipa committed May 13, 2024
1 parent 5c6d05d commit 183badf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/arsham/figurine v1.3.0
github.com/go-playground/validator/v10 v10.18.0
github.com/ivanpirog/coloredcobra v1.0.1
github.com/jedib0t/go-pretty/v6 v6.5.8
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/ivanpirog/coloredcobra v1.0.1 h1:aURSdEmlR90/tSiWS0dMjdwOvCVUeYLfltLfbgNxrN4=
github.com/ivanpirog/coloredcobra v1.0.1/go.mod h1:iho4nEKcnwZFiniGSdcgdvRgZNjxm+h20acv8vqmN6Q=
github.com/jedib0t/go-pretty/v6 v6.5.8 h1:8BCzJdSvUbaDuRba4YVh+SKMGcAAKdkcF3SVFbrHAtQ=
github.com/jedib0t/go-pretty/v6 v6.5.8/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand All @@ -45,8 +43,11 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
59 changes: 35 additions & 24 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package tui

import (
"bufio"
"cmp"
"fmt"
"os"
"slices"
"strings"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/jozefcipa/novus/internal/logger"
"github.com/jozefcipa/novus/internal/novus"
"github.com/jozefcipa/novus/internal/shared"
"github.com/olekukonko/tablewriter"
)

func AskUser(prompt string) string {
Expand Down Expand Up @@ -39,37 +42,45 @@ func PrintRoutingTable(novusState novus.NovusState) {
return
}

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Application", "Upstream ", "Domain", "Status", "Directory"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: true})
table.SetAutoMergeCellsByColumnIndex([]int{0, 3, 4})
table.SetCenterSeparator("|")
table.SetRowLine(true)

t.AppendHeader(table.Row{"Application", "Upstream ", "Domain", "Status", "Directory"})
// Sort apps
sortedAppNames := shared.MapKeys(allApps)
slices.SortFunc(sortedAppNames, func(a, b string) int { return cmp.Compare(a, b) })

for appName, appState := range allApps {
statusIcon := "🚀"
for _, appName := range sortedAppNames {
appState := allApps[appName]
color := tablewriter.FgGreenColor
if appState.Status == novus.APP_PAUSED {
statusIcon = "🛑" // TODO: try to use ⏸️ with the new library
color = tablewriter.FgYellowColor
}

for _, route := range appState.Routes {
t.AppendRow(table.Row{appName, route.Upstream, fmt.Sprintf("https://%s", route.Domain), statusIcon, appState.Directory}, table.RowConfig{AutoMerge: true})
table.Rich(
[]string{
appName,
route.Upstream,
fmt.Sprintf("https://%s", route.Domain),
strings.ToUpper(string(appState.Status)),
appState.Directory,
},
[]tablewriter.Colors{
{tablewriter.FgCyanColor},
{tablewriter.UnderlineSingle},
{tablewriter.Bold, tablewriter.UnderlineSingle, color},
{color},
{},
})
}
}

// TODO: refactor table to use https://github.com/olekukonko/tablewriter instead
// TODO: domain should not be green when the app is paused
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, AutoMerge: true, Colors: text.Colors{text.FgCyan}, VAlign: text.VAlignMiddle},
{Number: 2, AutoMerge: false},
{Number: 3, AutoMerge: false, Colors: text.Colors{text.FgHiGreen}},
{Number: 4, AutoMerge: true, Align: text.AlignCenter, VAlign: text.VAlignMiddle}, // TODO: set to true when figure out how to vertical align to middle
{Number: 5, AutoMerge: true},
})

t.SortBy([]table.SortBy{{Name: "Application", Mode: table.Asc}})
t.SetStyle(table.StyleLight)
t.Style().Options.SeparateRows = true

t.Render()
fmt.Println() // print empty line
table.Render()
}

func ParseAppFromArgs(args []string) (string, *novus.AppState) {
Expand Down

0 comments on commit 183badf

Please sign in to comment.