Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table: Possible to enable all borders around headings but only vertical borders around body? #765

Open
lionel-rowe opened this issue Nov 8, 2024 · 1 comment

Comments

@lionel-rowe
Copy link
Contributor

lionel-rowe commented Nov 8, 2024

The effect I want is this:

┌─────────────────────────┬───────────────┬─────────────────────────────┐
│ Browser                 │ Version Added │ Bug Tracker                 │
├─────────────────────────┼───────────────┼─────────────────────────────┤
│ chrome                  │ 122           │                             │
│ chrome_android          │ 122           │                             │
│ deno                    │ 1.42          │                             │
│ edge                    │ 122           │                             │
│ firefox                 │ 131           │                             │
│ firefox_android         │ 131           │                             │
│ nodejs                  │ 22.0.0        │                             │
│ opera                   │ 108           │                             │
│ opera_android           │ 81            │                             │
│ safari                  │ [none]        │ https://webkit.org/b/248650 │
│ safari_ios              │ [none]        │ https://webkit.org/b/248650 │
│ samsunginternet_android │ 26.0          │                             │
│ webview_android         │ 122           │                             │
│ webview_ios             │ [none]        │ https://webkit.org/b/248650 │
└─────────────────────────┴───────────────┴─────────────────────────────┘

I can't see a way of changing the border chars for individual Rows/Cells (only enabling/disabling the border entirely), and in any case that still wouldn't have the desired effect, as I want the body rows to be compact, i.e. no additional newline is inserted between each row.

I'm currently using this hack:

import { Table, border as defaultBorder, type Border } from 'jsr:@cliffy/[email protected]'
import { regExpEscape } from 'jsr:@li/[email protected]'

const r: typeof String.raw = (s, ...vals) => String.raw(s, ...vals.map(regExpEscape))

function table(header: string[], body: string[][], border: Border = defaultBorder) {
    const { leftMid, mid, midMid, rightMid } = border
    const replacer = new RegExp(r`\n${leftMid}(?:${mid}|${midMid})+${rightMid}`, 'g')
    let rowIdx = 0

    return new Table()
        .chars(border)
        .border()
        .header(header)
        .body(body)
        .toString()
        .replace(replacer, (m) => rowIdx++ ? '' : m)
}

Is there a way of doing this that doesn't involve constructing a custom regex like this though?

@c4spar
Copy link
Owner

c4spar commented Nov 25, 2024

Not yet, but I have a local wip branch where I have started to add some more helper functions, such as:

  • table.outerBorder()
  • table.innerBorder()
  • table.rowBorder()
  • table.columnBorder()
  • row.outerBorder()
  • row.rowBorder()
  • row.columnBorder()
  • cell.borderLeft()
  • cell.borderRight()
  • cell.borderTop()
  • cell.borderBottom()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants