Skip to content

Writer based on Tables.jl #3

Description

@non-Jedi

I've developed the following writer based on Tables.jl. Obviously needs some work to integrate into this package, so I'm attaching it to an issue (for now) instead of making a PR. In my own smoke-testing, it seems to work relatively well. Not optimized by any means.

It doesn't yet support directly writing AbstractVector/AbstractMatrix, but it should be pretty easy to add dispatch to support such cases.

Looks like we should also insert zero-width spaces a escape characters for any special characters. But I haven't done so yet. Or maybe enclose any special characters in ~verbatim~? Not sure what the best way of handling newlines though.

using Tables

struct OrgTable{T}
    data::T
end

function Base.show(io::IO, ::MIME"text/org", x::OrgTable)
    header = [' ' * string(n) * ' ' for n in Tables.columnnames(x.data)]
    rows = Vector{String}[]
    lengths = length.(header)

    for tablerow in Tables.rows(x.data)
        row = [' ' * string(v) * ' ' for v in tablerow]
        lengths .= max.(length.(row), lengths)
        push!(rows, row)
    end

    # Write header
    for (s, l) in zip(header, lengths)
        print(io, '|', rpad(s, l))
    end
    println(io, '|')

    # Write |---+---| divider
    print(io, '|')
    let (a, rest) = Iterators.peel(lengths)
        print(io, '-'^a)
        for n in rest
            print(io, '+', '-'^n)
        end
    end
    println(io, '|')

    # Write rows
    for row in rows
        for (s, l) in zip(row, lengths)
            print(io, '|', rpad(s, l))
        end
        println(io, '|')
    end
end

Posting this here based on a comment you made to me over 2 years ago. Took me a while to circle back around to this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions