box←{                           ⍝ Box the simple text array ⍵.

    (⎕IO ⎕ML)←1 3 ⋄ ⍺←⍬ ⍬ 0 ⋄ ar←{⍵,(⍴⍵)↓⍬ ⍬ 0}{2>≡⍵:,⊂,⍵ ⋄ ⍵}⍺  ⍝ controls

    ch←{⍵:'++++++++-|+' ⋄ '┌┐└┘┬┤├┴─│┼'}1=3⊃ar             ⍝ char set
    z←,[⍳⍴⍴⍵],[0.1]⍵ ⋄ rh←⍴z                               ⍝ matricise
                                                           ⍝ simple boxing? ↓
    0∊⍴∊2↑ar:{qch[9]⍪(ch[10],⍵,10⊃ch)⍪9⊃chq[1,↑⍴q;1,2⊃⍴q]←2 2⍴chq}z

    (r c)←rh{∪⍺{(⍵∊0,⍳⍺)/⍵}⍵,(~¯1∊⍵)/0,⍺}¨2↑ar             ⍝ rows and columns
    (rw cl)←rh{{⍵[⍋⍵]}⍵∪0,⍺}¨r c

    (~(0,2⊃rh)c){                                         ⍝ draw left/right?
        (↑⍺)↓[2](-2⊃⍺)↓[2]⍵[;⍋(⍳2⊃rh),cl]                  ⍝ rearrange columns
    }(~(0,1⊃rh)r){                                        ⍝ draw top/bottom?
        (↑⍺)↓[1](-2⊃⍺)↓[1]⍵[⍋(⍳1⊃rh),rw;]                  ⍝ rearrange rows
    }{
        (h w)←(rw),⍴clqh w⍴11⊃ch                      ⍝ size; special,
        hz(h,2⊃rh)⍴9⊃ch                                   ⍝  horizontal and
        vr(rh[1],w)⍴10⊃ch                                 ⍝  vertical lines
        ∨/0∊¨⍴¨rw cl:(⍵⍪hz),vrq                           ⍝ one direction only?
        q[1;]←5⊃chq[;w]←6⊃chq[;1]←7⊃chq[h;]←8⊃ch  ⍝ end marks
        q[1,h;1,w]←2 2⍴ch(⍵⍪hz),vrq                    ⍝ corners, add parts
    }z
}

code_colours

test script

Back to: notes

Back to: Workspaces