From 1025631dce86558f8b7209185087c8a9831bba22 Mon Sep 17 00:00:00 2001 From: Jeffrey Serio <23226432+hyperreal64@users.noreply.github.com> Date: Wed, 5 Apr 2023 15:52:26 -0500 Subject: [PATCH] Simplify table rendering --- cgi-bin/main.go | 59 ++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/cgi-bin/main.go b/cgi-bin/main.go index 20beda3..383877a 100644 --- a/cgi-bin/main.go +++ b/cgi-bin/main.go @@ -67,11 +67,18 @@ type TorrentInfo struct { PeersGettingFromUs int64 } +func renderTable(data [][]string) { + table := tablewriter.NewWriter(os.Stdout) + table.SetAlignment(tablewriter.ALIGN_LEFT) + table.AppendBulk(data) + table.Render() +} + func main() { fmt.Println("20 text/gemini\r") fmt.Println("```") - transmissionbt, err := transmissionrpc.New(os.Getenv("RPC_HOST"), os.Getenv("RPC_USER"), os.Getenv("RPC_PASSWD"), nil) + transmissionbt, err := transmissionrpc.New("", "", "", nil) if err != nil { log.Fatalln(err) } @@ -91,15 +98,7 @@ func main() { fmt.Println("SESSION STATS") fmt.Println() - sessionStatsTable := tablewriter.NewWriter(os.Stdout) - sessionStatsTable.SetAlignment(tablewriter.ALIGN_LEFT) - sessionStatsTable.SetBorder(false) - sessionStatsTable.SetCenterSeparator("│") - sessionStatsTable.SetColumnSeparator("│") - sessionStatsTable.SetRowLine(true) - sessionStatsTable.SetRowSeparator("─") - sessionStatsTable.AppendBulk(sessionStats) - sessionStatsTable.Render() + renderTable(sessionStats) fmt.Println() currentStats := [][]string{ @@ -112,15 +111,7 @@ func main() { fmt.Println("CURRENT STATS") fmt.Println() - currentStatsTable := tablewriter.NewWriter(os.Stdout) - currentStatsTable.SetAlignment(tablewriter.ALIGN_LEFT) - currentStatsTable.SetBorder(false) - currentStatsTable.SetCenterSeparator("│") - currentStatsTable.SetColumnSeparator("│") - currentStatsTable.SetRowLine(true) - currentStatsTable.SetRowSeparator("─") - currentStatsTable.AppendBulk(currentStats) - currentStatsTable.Render() + renderTable(currentStats) fmt.Println() cumulativeStats := [][]string{ @@ -133,15 +124,7 @@ func main() { fmt.Println("CUMULATIVE STATS") fmt.Println() - cumulativeStatsTable := tablewriter.NewWriter(os.Stdout) - cumulativeStatsTable.SetAlignment(tablewriter.ALIGN_LEFT) - cumulativeStatsTable.SetBorder(false) - cumulativeStatsTable.SetCenterSeparator("│") - cumulativeStatsTable.SetColumnSeparator("│") - cumulativeStatsTable.SetRowLine(true) - cumulativeStatsTable.SetRowSeparator("─") - cumulativeStatsTable.AppendBulk(cumulativeStats) - cumulativeStatsTable.Render() + renderTable(cumulativeStats) fmt.Println() var torrentInfo = []TorrentInfo{} @@ -160,18 +143,14 @@ func main() { fmt.Println("TORRENT INFO") fmt.Println() - torrentStatsTable := tablewriter.NewWriter(os.Stdout) - torrentStatsTable.SetHeader([]string{"Name", "Date Added", "Total Size", "Peers Connected", "Peers Getting From Us"}) - torrentStatsTable.SetAlignment(tablewriter.ALIGN_LEFT) - torrentStatsTable.SetBorder(false) - torrentStatsTable.SetCenterSeparator("│") - torrentStatsTable.SetColumnSeparator("│") - torrentStatsTable.SetRowLine(true) - torrentStatsTable.SetRowSeparator("─") - for _, v := range torrentInfo { - torrentStatsTable.Append([]string{v.Name, fmt.Sprintf("%s", v.DateAdded), byteCountIEC(int64(v.TotalSize)), fmt.Sprintf("%d", v.PeersConnected), fmt.Sprintf("%d", v.PeersGettingFromUs)}) + torrentInfoDatum := [][]string{ + {"Name", v.Name}, + {"Date Added", v.DateAdded.String()}, + {"Total Size", fmt.Sprintf("%d", int64(v.TotalSize))}, + {"Peers Connected", fmt.Sprintf("%d", v.PeersConnected)}, + {"Peers Getting From Us", fmt.Sprintf("%d", v.PeersGettingFromUs)}, + } + renderTable(torrentInfoDatum) } - - torrentStatsTable.Render() }