How to combine prettytables and pager?

When using prettytables I have a lot of keyword options that I can use to display a pretty table. With the function pager from TerminalPager I can browse through a dataset that does not fit on the screen. How can I combine both?

Example:

using InMemoryDatasets, TerminalPager

function demo_data()
    n = 100
    m = 8
    time = 0.1:0.1:n*0.1
    addr = rand(Int16(0x101):Int16(0x12e), n)
    data = (Symbol("d", i) => rand(UInt8, n) for i in 1:m)
    ds = Dataset(;time, addr, data...)
    ds.addr[5] = missing
    ds
end
ds = demo_data()

Working:

pager(ds, frozen_rows=2)

also

show(ds, show_row_number=false, eltypes=false)

Not working:

pager(ds,  frozen_rows=2, show_row_number=false, eltypes=false)

Any idea?

1 Like

Iā€™m not familiar with InMemoryDatasets but with DataFrames (I assume similar) you can do:

using DataFrames, TerminalPager

n = 100
m = 8
data = (Symbol("d", i) => rand(UInt8, n) for i in 1:m)
ds = DataFrame(collect(data))

io = IOBuffer();
show(io, ds, show_row_number=false, eltypes=false)
pager(String(take!(io)), frozen_rows=2)
1 Like

Thanks! Works nicely!

But there is one little thing missing: When I type

ds

in your example the header is highlighted, but the highlighting is not shown when using the pager like

pager(String(take!(io)), frozen_rows=2)

If I do pager(ds) the header also highlighted.

How can I achieve that highlighting is preserved when combining show and pager?

Replace the last three lines with:

io = IOContext(IOBuffer(), :color => true);
show(io, ds, show_row_number=false, eltypes=false)
pager(String(take!(io.io)), frozen_rows=2)
2 Likes

Using this function now:

function browse(ds)
    io = IOContext(IOBuffer(), :color => true);
    show(io, ds, show_row_number=false, eltypes=false)
    pager(String(take!(io.io)), frozen_rows=3)
end

Works perfectly! Thank you!

2 Likes