charmbracelet / lipgloss

Style definitions for nice terminal layouts 👄
MIT License
8.19k stars 229 forks source link

Rendering a markdown table #424

Open mfridman opened 2 weeks ago

mfridman commented 2 weeks ago

Is your feature request related to a problem? Please describe.

I have a CLI that supports 3 table formats: plain (HiddenBorder), basic (RoundedBorder), and markdown (??). I used lipgloss/table to build a table and would like to output it in all those formats.

Output examples ### Plain ``` Status Elapsed Package Cover Pass Fail Skip PASS (cached) github.com/mfridman/tparse/internal/utils -- 12 0 0 PASS (cached) github.com/mfridman/tparse/parse -- 43 0 0 PASS (cached) github.com/mfridman/tparse/tests -- 137 0 0 ``` ### Basic (poorly named, "with border") ``` ╭────────┬──────────┬───────────────────────────────────────────┬───────┬──────┬──────┬──────╮ │ Status │ Elapsed │ Package │ Cover │ Pass │ Fail │ Skip │ ├────────┼──────────┼───────────────────────────────────────────┼───────┼──────┼──────┼──────┤ │ PASS │ (cached) │ github.com/mfridman/tparse/internal/utils │ -- │ 12 │ 0 │ 0 │ │ PASS │ (cached) │ github.com/mfridman/tparse/parse │ -- │ 43 │ 0 │ 0 │ │ PASS │ (cached) │ github.com/mfridman/tparse/tests │ -- │ 137 │ 0 │ 0 │ ╰────────┴──────────┴───────────────────────────────────────────┴───────┴──────┴──────┴──────╯ ``` ### Markdown ``` | Status | Elapsed | Package | Cover | Pass | Fail | Skip | |---------|----------|-------------------------------------------|-------|------|------|------| | 🟢 PASS | (cached) | github.com/mfridman/tparse/internal/utils | -- | 12 | 0 | 0 | | 🟢 PASS | (cached) | github.com/mfridman/tparse/parse | -- | 43 | 0 | 0 | | 🟢 PASS | (cached) | github.com/mfridman/tparse/tests | -- | 137 | 0 | 0 | ```

Describe the solution you'd like

I'd be nice if there was a lipglosss.MarkdownBorder(), which would allow converting a lipgloss table into a renderable markdown table.

Describe alternatives you've considered

This did the trick for me:

border := lipgloss.Border{
    Top:          "-",
    Bottom:       "-",
    Left:         "|",
    Right:        "|",
    TopLeft:      "", // empty for markdown
    TopRight:     "", // empty for markdown
    BottomLeft:   "", // empty for markdown
    BottomRight:  "", // empty for markdown
    MiddleLeft:   "|",
    MiddleRight:  "|",
    Middle:       "|",
    MiddleTop:    "|",
    MiddleBottom: "|",
}
t = t.Border(border).BorderBottom(false).BorderTop(false)

Additional context

This might be overly specific, but opening this up for discussion and/or if someone else finds this useful.

meowgorithm commented 2 weeks ago

Nice, that's actually really good. Want to open a PR?