disp←{⎕IO ⎕ML←0 1                           ⍝ Boxed sketch of nested array.

    ⍺←⍬ ⋄ dec ctd←2↑⍺                       ⍝ 1:decorated, 1:centred.

    box←{                                   ⍝ Recursive boxing of nested array.
        isor ⍵:⎕FMT⊂⍵                       ⍝ ⎕or: '∇name'.
        1=≡,⍵:dec open ⎕FMT dec open ⍵      ⍝ simple array: format.
        matmatr 1/dec open ⍵               ⍝ matrix of opened subarrays.
        r c←×⍴mat                           ⍝ non-null rows/cols.
        dec<0∊r c:c/r⌿∇ 1 open mat          ⍝ undecorated null: empty result.
        subsaligned ∇¨mat                  ⍝ aligned boxed subarrays.
        (≢⍴⍵)gapsplane subs              ⍝ collection into single plane.
    }

    aligned←{                               ⍝ Alignment and centring.
        rows colssepr⍴¨⍵                   ⍝ subarray dimensions.
        sizes(⌈/rows)∘.,⌈⌿cols             ⍝ aligned subarray sizes.
        ctd=0:sizes↑¨⍵                      ⍝ top-left alignment.
        v hsepr⌈0.5×↑(⍴¨⍵)-sizes           ⍝ vertical and horizontal rotation.
        v⊖¨h⌽¨sizes↑¨⍵                      ⍝ centred aligned subarrays.
    }

    gaps←{                                  ⍝ Gap-separated sub-planes.
        ⍺≤2:⍵                               ⍝ lowish rank: done.
        subs(⍺-1)∇¨⍵                       ⍝ sub-hyperplanes.
        width←⊃⌽⍴⊃subs                      ⍝ width of inter-plane gap.
        fill(width-3 0)⍴' '              ⍝ inter-plane gap.
        ↑{⍺⍪fill⍪⍵}/1 open subs             ⍝ gap-separated planes.
    }

    plane←{                                 ⍝ Boxed rank-2 plane.
        2<⍴⍴⍺:⍺ join ⍵                      ⍝ gap-separated sub-planes.
        odec(dec shape)outer ⍵           ⍝ outer type and shape decoration.
        idecinner ⍺                        ⍝ inner type and shape decorations.
        (odec,idec)collect ⍵                ⍝ collected, formatted subarrays.
    }

    join←{                                  ⍝ Join of gap-separated sub-planes.
        sep(≢⍵)÷1⌈≢⍺                       ⍝ sub plane separation.
        split(0=sep|⍳≢⍵)⊂[0]⍵              ⍝ separation along first axis.
        (⊂⍤¯1⊢⍺)plane¨split                 ⍝ sub-plane join.
    }

    outer←{                                 ⍝ Outer decoration.
        sizes←1 0{⊃↓(⍉⍣⍺)⍵}¨sepr⍴¨⍵         ⍝ row and col sizes of subarrays.
        sidessizes/¨¨'│─'                  ⍝ vert and horiz cell sides.
        bordsdec↓¨'├┬'glue¨sides           ⍝ joined up outer borders.
        ↑,¨/('┌' '')bords'└┐'             ⍝ vertical and horizontal borders.
    }

    inner←{                                 ⍝ Inner subarray decorations.
        deco←{(type),1 shape ⍵}           ⍝ type and shape decorators.
        sepr deco¨matr dec open ⍵           ⍝ decorators: tt vv hh .
    }

    collect←{                               ⍝ Collected subarrays.
        lft top tt vv hh←⍺                  ⍝ array and subarray decorations.
        cellsvv right 1 open tt hh lower ⍵ ⍝ cells boxed right and below.
        boxes(dec∨0∊⍴⍵)open cells          ⍝ opened to avoid ,/⍬ problem.
        lft,top⍪↑⍪⌿,/boxes                  ⍝ completed collection.
    }

    right←{                                 ⍝ Border right each subarray.
        types←2⊥¨(⍳⍴⍵)=⊂¯1+⍴⍵               ⍝ right border lower corner types.
        chars←'┼┤┴┘'[types]                 ⍝    ..     ..      ..      chars.
        rgt←{⍵,(-≢⍵)(≢⍵)1 1/'│',⍺}         ⍝ form right border.
        ((matr 1 open)chars)rgt¨⍵       ⍝ cells bordered right.
    }

    lower←{                                 ⍝ Border below each subarray.
        bot←{⍵⍪(-1⊃⍴⍵)↑⍺ split ⍵}           ⍝ lower border.
        split←{((¯2+1⊃⍴⍵)/'─')glue ⍺}       ⍝ decorators split with horiz line.
        (matr↑,¨/⍺)bot¨matr ⍵               ⍝ cells bordered below.
    }

    type←{                                  ⍝ Type decoration char.
        dec<|≡⍵:'─'                         ⍝ nested: '─'
        isor ⍵:'∇'                          ⍝ ⎕or:    '∇'
        sst←{                               ⍝ simple scalar type.
            0=dec×⍴⍴⍵:'─'                   ⍝ undecorated or scalar ⍕⍵: char,
            (⊃⍵∊'¯',⎕D)⊃'#~'                ⍝ otherwise, number or space ref.
        }∘⍕                                 ⍝ ⍕ distinguishes type of scalar.
        0=≡⍵:sst ⍵                          ⍝ simple scalar: type.
        {(1=⍴⍵)⊃'+'⍵}∪,sst¨dec open ⍵       ⍝ array: mixed or uniform type.
    }

    shape←{                                 ⍝ Row and column shape decorators.
        dec≤0=⍴⍴⍵:⍺/¨'│─'                   ⍝ no decoration or scalar.
        cols(ׯ1↑⍴⍵)⊃'⊖→'                  ⍝ zero or more cols.
        rsig(××/¯1↓⍴⍵)⊃'⌽↓'                ⍝ zero or more rows.
        rows(¯1+3⌊⍴⍴⍵)⊃'│'rsig'⍒'          ⍝ high rank decorator overrides.
        rows cols                           ⍝ shape decorators.
    }

    matr←{↑,↓⍵}                             ⍝ matrix from non-scalar array.
    sepr←{+/¨1⊂↑⍵}                          ⍝ vec-of-mats from mat-of-vecs.
    open←{16::(1⌈⍴⍵)⍴⊂'[ref]' ⋄ (⍺⌈⍴⍵)⍴⍵}   ⍝ stretched to expose nulls.
    isor←{1 ⍬≡(≡⍵)(⍴⍵)}                     ⍝ is ⎕or of object?
    glue←{0=⍴⍵:⍵ ⋄ ↑⍺{⍺,⍺⍺,⍵}/⍵}            ⍝ ⍵ interspersed with ⍺s.

    isor ⍵:⎕FMT⊂⍵                           ⍝ simple ⎕OR: done.
    1=≡,⍵:⎕FMT ⍵                            ⍝ simple array: done.
    box ⍵                                   ⍝ recursive boxing of array.
}
code_colours

test script

Back to: notes

Back to: Workspaces