Closed ondrovic closed 1 month ago
Auto-sizing tables has been something I wanted to implement, thanks for the reminder.
Hey @ondrovic try the code in branch table-auto-width and see if that works for you?
Sample usage: https://github.com/jedib0t/go-pretty/blob/table-auto-width/table/render_test.go#L1356-L1533
Hey @ondrovic try the code in branch table-auto-width and see if that works for you?
Sample usage: https://github.com/jedib0t/go-pretty/blob/table-auto-width/table/render_test.go#L1356-L1533
I will give it a try later today and let you know
Hey @ondrovic try the code in branch table-auto-width and see if that works for you?
Sample usage: https://github.com/jedib0t/go-pretty/blob/table-auto-width/table/render_test.go#L1356-L1533
As a quick test I tried it like this
func renderResultsToTable(results interface{}, totalCount int, totalFileSize int64, ff types.FileFinder) {
t := table.Table{}
// Determine header and footer based on the type of results
var header table.Row
var footer table.Row
switch results.(type) {
case []types.DirectoryResult:
header = table.Row{"Directory", "Count"}
footer = table.Row{"Total", pterm.Sprintf("%v", totalCount)}
case []types.EntryResult:
header = table.Row{"Directory", "FileName", "FileSize"}
footer = table.Row{"Total", pterm.Sprintf("%v", totalCount), pterm.Sprintf("%v", commonFormatters.FormatSize(totalFileSize))}
default:
return // Exit if results type is not supported
}
t.AppendHeader(header)
// Append rows based on the display mode
switch results := results.(type) {
case []types.DirectoryResult:
for _, result := range results {
t.AppendRow(table.Row{
formatResultHyperLink(result.Directory, result.Directory),
pterm.Sprintf("%v", result.Count),
})
}
case []types.EntryResult:
if ff.DisplayDetailedResults {
for _, result := range results {
newLink := pterm.Sprintf("%s/%s", result.Directory, result.FileName)
t.AppendRow(table.Row{
formatResultHyperLink(result.Directory, result.Directory),
formatResultHyperLink(newLink, result.FileName),
result.FileSize,
})
}
}
}
t.AppendFooter(footer)
t.SetStyle(table.StyleColoredDark)
t.SetStyle(table.Style{Size: table.SizeOptions{
WidthMax: 50,
WidthMin: 0,
}})
t.SetOutputMirror(os.Stdout)
t.Render()
}
It didn't quite work as I expected
without size styles
With new size styles
It seems like it broke the headers / footers, it could have been that I wasn't using it like your example using the table writer, I will try that when I get back from errands this morning
But also I was hoping more for an automatic / dynamic min / max width where it would auto calculate the min / and the maximum based on the content so you don't have to hard code in the values. While I understand it may increase rendering times based on the the size of the data set.
Your second SetStyle just overrode the first call's effect. Try it like this:
t.SetStyle(table.StyleColoredDark)
t.Style().Size = table.SizeOptions{
WidthMax: 50,
WidthMin: 0,
}})
To auto-size for your terminal, you'd do something like this:
package main
import (
"fmt"
"os"
"github.com/jedib0t/go-pretty/v6/table"
"golang.org/x/term"
)
func main() {
w, h, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
panic(err.Error())
}
fmt.Println(w, h)
tw := table.NewWriter()
tw.SetTitle("Title")
tw.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
tw.AppendRows([]table.Row{
{1, "Arya", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
tw.AppendFooter(table.Row{"", "", "Total", 10000})
tw.SetStyle(table.StyleLight)
tw.Style().Size = table.SizeOptions{
WidthMin: w,
}
fmt.Println(tw.Render())
}
Output:
Title sizing logic is broken now and I will fix it before merging this code.
I've pushed a couple of commit just now. So try to use the latest to get the fix for the title rendering being broken.
I've pushed a couple of commit just now. So try to use the latest to get the fix for the title rendering being broken.
so based on this still using non table.NewWriter()
method (going to do that next)
func renderResultsToTable(results interface{}, totalCount int, totalFileSize int64, ff types.FileFinder) {
t := table.Table{}
w, h, err := getTerminalSize()
if err != nil {
panic(err.Error())
}
fmt.Printf("Terminal size: Width = %d, Height = %d\n", w, h)
// Determine header and footer based on the type of results
var header table.Row
var footer table.Row
switch results.(type) {
case []types.DirectoryResult:
header = table.Row{"Directory", "Count"}
footer = table.Row{"Total", pterm.Sprintf("%v", totalCount)}
case []types.EntryResult:
header = table.Row{"Directory", "FileName", "FileSize"}
footer = table.Row{"Total", pterm.Sprintf("%v", totalCount), pterm.Sprintf("%v", commonFormatters.FormatSize(totalFileSize))}
default:
return // Exit if results type is not supported
}
t.AppendHeader(header)
// Append rows based on the display mode
switch results := results.(type) {
case []types.DirectoryResult:
for _, result := range results {
t.AppendRow(table.Row{
formatResultHyperLink(result.Directory, result.Directory),
pterm.Sprintf("%v", result.Count),
})
}
case []types.EntryResult:
if ff.DisplayDetailedResults {
for _, result := range results {
newLink := pterm.Sprintf("%s/%s", result.Directory, result.FileName)
t.AppendRow(table.Row{
formatResultHyperLink(result.Directory, result.Directory),
formatResultHyperLink(newLink, result.FileName),
result.FileSize,
})
}
}
}
t.AppendFooter(footer)
t.SetStyle(table.StyleColoredDark)
t.SetStyle(table.Style{Size: table.SizeOptions{
WidthMin: w,
}})
t.SetOutputMirror(os.Stdout)
t.Render()
}
it does seem to have fixed the formatting
The only thing is with really long filenames Main monitor 5120x1440 Second monitor 1920x860
Two things:
Two things:
- You are still calling SetStyle() twice with the second call overriding everything except for Size; please use Style().Size for the second call.
- This logic does auto-expand, and not auto-contract; maybe I'll find a way to do it next.
That worked
// just posting for anyone else who stumbles across this ;-)
t.Style().Size = table.SizeOptions{
WidthMin: w,
}
Let me know if you end up doing it and need me to test it out
Is your feature request related to a problem? Please describe. Tables with and column spacing seem to be dependent on row content
Header / Row content alignment, I found in using the
text.Hyperlink(text, link)
it table formattingDescribe the solution you'd like A config option allow the table to calculate no only the full table width, but also split the header / column locations based on the terminal size
Header / Row content alignment, I found in using the
text.Hyperlink(text, link)
seems to break the content alignmentDescribe alternatives you've considered Tried adjusting the
.SetColumnConfigs( WidthMin, WidthMax)
Additional context
Examples:
using regular non hyperlink - works as expected, wish I could force it to be the full size of the terminal with everything aligned perfectly
using hyperlink - throws alignment off just a bit
I was able to find fix the alignment issue by doing the following but it's very hacky and I haven't tested it enough, would be better if this was automagically calculated
another example using colors with hyperlink illustration how alignment is broken - illustration how my temp fix is garbage