Print boolean with PrettyTables.jl

Christopher_Fisher
Thu, 29 Jul 2021 11:51:50 +0000
General Usage

I am printing a struct with PrettyTables.jl and would like boolean values to print out as true or false rather than Float64s. Is there a way to do this? Thank you.

Example

using PrettyTables

struct M
    x::String
    y::Float64
    z::Bool
end

function Base.show(io::IO, ::MIME"text/plain", m::M)
    values = [getfield(m, f) for f in fieldnames(M)]
    return pretty_table(
        values;
        compact_printing=false,
        header=["Value"],
        row_name_alignment=:l,
        row_names=[fieldnames(M)...],
        formatters=ft_printf("%5.2f"),
        alignment=:l,
    )
end

m = M("x", .3, true)

Output:

julia> m
┌───┬───────┐
│   │ Value │
├───┼───────┤
│ x │ x     │
│ y │  0.30 │
│ z │  1.00 │
└───┴───────┘

4 posts - 3 participants