Simplify table rendering

This commit is contained in:
Jeffrey Serio 2023-04-05 15:52:26 -05:00
parent 7462627584
commit 1025631dce

View File

@ -67,11 +67,18 @@ type TorrentInfo struct {
PeersGettingFromUs int64 PeersGettingFromUs int64
} }
func renderTable(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(data)
table.Render()
}
func main() { func main() {
fmt.Println("20 text/gemini\r") fmt.Println("20 text/gemini\r")
fmt.Println("```") 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 { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -91,15 +98,7 @@ func main() {
fmt.Println("SESSION STATS") fmt.Println("SESSION STATS")
fmt.Println() fmt.Println()
sessionStatsTable := tablewriter.NewWriter(os.Stdout) renderTable(sessionStats)
sessionStatsTable.SetAlignment(tablewriter.ALIGN_LEFT)
sessionStatsTable.SetBorder(false)
sessionStatsTable.SetCenterSeparator("│")
sessionStatsTable.SetColumnSeparator("│")
sessionStatsTable.SetRowLine(true)
sessionStatsTable.SetRowSeparator("─")
sessionStatsTable.AppendBulk(sessionStats)
sessionStatsTable.Render()
fmt.Println() fmt.Println()
currentStats := [][]string{ currentStats := [][]string{
@ -112,15 +111,7 @@ func main() {
fmt.Println("CURRENT STATS") fmt.Println("CURRENT STATS")
fmt.Println() fmt.Println()
currentStatsTable := tablewriter.NewWriter(os.Stdout) renderTable(currentStats)
currentStatsTable.SetAlignment(tablewriter.ALIGN_LEFT)
currentStatsTable.SetBorder(false)
currentStatsTable.SetCenterSeparator("│")
currentStatsTable.SetColumnSeparator("│")
currentStatsTable.SetRowLine(true)
currentStatsTable.SetRowSeparator("─")
currentStatsTable.AppendBulk(currentStats)
currentStatsTable.Render()
fmt.Println() fmt.Println()
cumulativeStats := [][]string{ cumulativeStats := [][]string{
@ -133,15 +124,7 @@ func main() {
fmt.Println("CUMULATIVE STATS") fmt.Println("CUMULATIVE STATS")
fmt.Println() fmt.Println()
cumulativeStatsTable := tablewriter.NewWriter(os.Stdout) renderTable(cumulativeStats)
cumulativeStatsTable.SetAlignment(tablewriter.ALIGN_LEFT)
cumulativeStatsTable.SetBorder(false)
cumulativeStatsTable.SetCenterSeparator("│")
cumulativeStatsTable.SetColumnSeparator("│")
cumulativeStatsTable.SetRowLine(true)
cumulativeStatsTable.SetRowSeparator("─")
cumulativeStatsTable.AppendBulk(cumulativeStats)
cumulativeStatsTable.Render()
fmt.Println() fmt.Println()
var torrentInfo = []TorrentInfo{} var torrentInfo = []TorrentInfo{}
@ -160,18 +143,14 @@ func main() {
fmt.Println("TORRENT INFO") fmt.Println("TORRENT INFO")
fmt.Println() 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 { 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()
} }