List Displayer

Date:    Thu, 19 Jun 1997 15:07:42 +0100
From:    Marcus Bointon <marcus@sound-impressions.co.uk>
Subject: List displayer handler
Something I always find gets me in trouble is handling complicated list structures. Counting square brackets is no fun. So I wrote this handler which recursively (as deep as you like!) breaks out a list into an indented hierarchical view. It works with property and linear lists, and lists that combine the two, such as linear lists that contain property lists and vice versa. If a list is a property list, it also displays the name of each property. There are a few extraneous blank lines, but it basically works.
--Recurse any list structure and return a hierarchical list of it
--You just need to pass it one list - the other params are for recursion.
--example: put
breakOutList([#a:[1,2,3,4],#b:[1,[2,"2a","2b","2c"],3,5,6],#c:[#x:1,#y:2,#z:
5],#d:5,#e:8,#f:"hello"])
on breakoutlist list, listView, depth
  if the paramCount=1 then --not a recursive call, so initialise
    set listView=""&return --makes more readable in message window
    set depth=0
  end if

  if ilk(list)=#list then --linear list
    repeat with l in list
      if ilk(l,#list) then
        set listView=breakoutList(l,listView, depth+1) & return --list
element, so recurse
      else
        put padspaces(depth) & l & return after listView --normal element,
just display
      end if
    end repeat

  else
    if ilk(list)=#propList then --property list
      repeat with n=1 to count(list)
        put padspaces(depth) after listView
        put "#" & getPropAt(list,n) &":" after listView --property name
        if ilk(getAt(list,n),#list) then --it's another list of some kind,
so recurse
          put return after listView
          set listView=breakoutlist(getAt(list,n),listView,depth+1) & return
        else
          put getAt(list,n) &return after listView --display element
        end if
      end repeat
    else --not a list
      return ilk(list) & " passed: breakOutList only works on lists"
    end if
  end if

  return listView
end

on padspaces num
  set spaces=""
  repeat with n=1 to num
    put "  " after spaces
  end repeat
  return spaces
end