ronisbr / TerminalPager.jl

Pure Julia implementation of the command less
MIT License
116 stars 8 forks source link

abbreviated output for DimensionalData #34

Closed bjarthur closed 7 months ago

bjarthur commented 7 months ago

is there a show method or the like that can be defined to customize the output for a specific type to make this work better?

julia> using DimensionalData

julia> A = rand(X(50), Y(10.0:40.0))
50×31 DimArray{Float64,2} with dimensions: 
  X,
  Y Sampled{Float64} 10.0:1.0:40.0 ForwardOrdered Regular Points
 10.0         11.0        12.0         13.0         …  38.0        39.0        40.0
  0.414429     0.967962    0.720678     0.31398         0.664142    0.178525    0.198205
  0.451637     0.736537    0.936694     0.370182        0.23804     0.269602    0.80307
  0.84869      0.173599    0.646813     0.769767        0.78232     0.234421    0.492158
  0.782584     0.73037     0.0140345    0.716155        0.0134053   0.762526    0.937726
  0.00782866   0.319108    0.0899623    0.925041    …   0.100216    0.955839    0.0219956
  0.966206     0.0576132   0.889478     0.811982        0.910926    0.732233    0.543933
  0.434441     0.413684    0.825703     0.92534         0.245011    0.532117    0.916558
  0.837677     0.748269    0.00288684   0.788718        0.240337    0.0597967   0.053107
  0.0928123    0.338985    0.770077     0.875858        0.808507    0.449472    0.698103
  0.443271     0.699966    0.390675     0.114863    …   0.509491    0.980215    0.015727
  0.707604     0.761463    0.12522      0.987492        0.180278    0.556036    0.879508
  0.315138     0.146992    0.631747     0.364538        0.701736    0.490354    0.212169
  0.960195     0.319206    0.556295     0.478452        0.853045    0.786151    0.704802
  0.722219     0.891343    0.742402     0.386098        0.710249    0.130277    0.281468
  ⋮                                                 ⋱                           ⋮
  0.453267     0.264981    0.101285     0.100797        0.916495    0.28729     0.0962622
  0.99934      0.385766    0.501177     0.278182        0.931742    0.364607    0.953542
  0.753522     0.228334    0.816532     0.49223         0.84888     0.278374    0.936007
  0.813674     0.846556    0.209538     0.053302    …   0.795106    0.440521    0.674403
  0.907359     0.742182    0.36885      0.67759         0.806291    0.713175    0.487266
  0.381995     0.566324    0.67409      0.0376533       0.232274    0.378529    0.644125
  0.175485     0.598527    0.98843      0.152351        0.688907    0.135789    0.798922
  0.347392     0.743278    0.919895     0.739641        0.670573    0.697128    0.827335
  0.189002     0.142781    0.812652     0.728756    …   0.175437    0.219649    0.0820538
  0.654293     0.322825    0.850235     0.212976        0.660898    0.863237    0.117781
  0.685628     0.535986    0.183944     0.190564        0.180055    0.93551     0.560684
  0.86256      0.474736    0.721193     0.703336        0.10152     0.184241    0.713716
  0.112952     0.509283    0.0895051    0.00164909      0.28468     0.47871     0.818351
  0.0483211    0.663536    0.280515     0.90239     …   0.997375    0.336445    0.64751

julia> using TerminalPager

julia> A |> pager

50×31 DimArray{Float64,2} with dimensions: 
  X,
  Y Sampled{Float64} 10.0:1.0:40.0 ForwardOrdered Regular Points
 10.0         11.0       12.0        …  38.0        39.0       40.0
  0.414429     0.967962   0.720678       0.664142    0.178525   0.198205
  0.451637     0.736537   0.936694       0.23804     0.269602   0.80307
  0.84869      0.173599   0.646813       0.78232     0.234421   0.492158
  0.782584     0.73037    0.0140345      0.0134053   0.762526   0.937726
  0.00782866   0.319108   0.0899623  …   0.100216    0.955839   0.0219956
  ⋮                                  ⋱                          ⋮
  0.189002     0.142781   0.812652       0.175437    0.219649   0.0820538
  0.654293     0.322825   0.850235       0.660898    0.863237   0.117781
  0.685628     0.535986   0.183944       0.180055    0.93551    0.560684
  0.86256      0.474736   0.721193   …   0.10152     0.184241   0.713716
  0.112952     0.509283   0.0895051      0.28468     0.47871    0.818351
  0.0483211    0.663536   0.280515       0.997375    0.336445   0.64751

:                                                            (↑ ↓ ← →:move, ?:help, q:quit) 100%

this is on julia v1.10 with DimensionalData v0.25.8 and TerminalPager v0.5.1

bjarthur commented 7 months ago

Array(A) |> pager works of course but then the lookup values are not shown which defeats the point of DimensionalData

ronisbr commented 7 months ago

Hi @bjarthur !

TerminalPager.jl just take the default show method for each type. In this case, I am not sure if I understood the problem.

bjarthur commented 7 months ago

in this case, A has 50 rows, but only 11 are displayed by pager, the rest of the screen is just blank instead of being used to display more rows.

ronisbr commented 7 months ago

Ah! That should be a problem in the display function of DimensionalData. When we render the output to be shown in the pager, we use this context: IOContext(buf, :color => hascolor, :limit => false). Meaning that the output must not be limited. Probably DimensionalData is getting the display size (that defaults to 24 x 80), and using this as the screen size.

bjarthur commented 7 months ago

yet if i just type A on the REPL, 28 rows are displayed, not 11 like in pager

ronisbr commented 7 months ago

Because in REPL, we use stdout, and its dimension is whatever you have in your terminal. However, when we create the IOContext, we do not have dimensions (it is a pager). By default, Julia returns 24 x 80. That's why we have :limit => false. I cannot manually set the size to (-1, -1) because it can lead to many problems. The only right thing to do here is modifying DimensionalData to honor the :limit flag.

julia> displaysize(stdout)
(39, 128)

julia> displaysize(IOContext(IOBuffer(), :limit => false))
(24, 80)
ronisbr commented 7 months ago

That's why it works with Array. In this case, if :limit => false, Julia prints the array without cropping.

bjarthur commented 7 months ago

thanks! see https://github.com/rafaqz/DimensionalData.jl/pull/588

rafaqz commented 7 months ago

Thanks @ronisbr for solving this!

ronisbr commented 7 months ago

You’re welcome @rafaqz! Awesome package by the way!