Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Code information

dave1707dave1707 Mod
edited July 16 in Code Sharing Posts: 7,683

Here's some programs I have that might help with coding or just look at code. The first one lists the names of classes and the tabs they're in. It also shows the instance name used with a class. The second one lists variable names and other things used in a program and gives a count of how many times they were found. For starters, both programs read the project Cargo-Bot which contains a lot of tabs and classes and variables. Just change the project name for what you want to look at.

Show class names and instances.

Code removed. See modified code farther down. 

Show names and the count.

function setup()
    font("Courier")
    textMode(CORNER)
    project="Examples:Cargo-Bot"
    tab={}
    tab1={}
    dy=0
    process()
end

function process()
    output.clear()
    tab={}
    tab1={}
    dy=0
    local str=""
    local lp=listProjectTabs(project)
    if #lp==0 then
        print("Project not found.")
        return
    end
    print("Reading the project list.")
    for a,b in pairs(lp) do
        print(b)
        local rp=readProjectTab(project..":"..b)
        str=str..rp
    end
    local str1=""
    for z=1,string.len(str) do
        local v=string.sub(str,z,z)
        if v>="a" and v<="z" or v>="A" and v<="Z" then
            per=0
            str1=str1..v
        elseif str1~="" and v=="_" then
            per=0
            str1=str1..v
        elseif str1~="" and v=="." then
            per=per+1
            if per==2 then
                per=0
                str1=string.sub(str1,1,string.len(str1)-1)
                if tab[str1]==nil then
                    tab[str1]=1
                else
                    tab[str1]=tab[str1]+1
                end
                str1=""
            else
                str1=str1..v
            end
        elseif v>="0" and v<="9" then
            per=0
            str1=str1..v
        elseif str1~="" and v=="'" then
            per=0
            str1=str1..v
        elseif str1~="" then
            per=0
            local len=string.len(str1)
            if string.sub(str1,len,len)=="." then
                str1=string.sub(str1,1,len-1)
            end
            if string.sub(str1,len,len)=="'" then
                str1=string.sub(str1,1,len-1) 
            end  
            if tab[str1]==nil then
                tab[str1]=1
            else
                tab[str1]=tab[str1]+1
            end
            str1=""
        else
            str1=""
        end
    end
    for a,b in pairs(tab) do
        table.insert(tab1,a)
    end
    table.sort(tab1)
    showKeyboard()
    hideKeyboard()
end

function draw()
    background(40,40,50)
    fill(255)
    text("Count      Names       "..#tab1,50,HEIGHT-50)
    if #tab1>0 then
        local st=math.floor(dy/20)+1
        if st+45>=#tab1 then
            st=#tab1-45
        end
        local en=st+45
        for z=st,en do
            local c=tab1[z]
            text(string.format("%5d      %-s",tab[c],c),50,dy+HEIGHT-70-z*20)
        end
    end
end

function touched(t)
    if t.state==MOVING then
        dy=dy+t.deltaY
        if dy<0 then
            dy=0
        end
        if dy/20>#tab1 then
            dy=#tab1*20
        end
    end
end

Comments

  • dave1707dave1707 Mod
    Posts: 7,683

    Modified the original class/instance program. It now shows 2 columns. The left column are the classes defined within a project tab. The right column are the instances within a project tab. Scroll the screen up or down to view the information. The example project is Cargo-Bot. Change the project name for other projects.

    -- find classes and instances of those classes
    
    function setup()
        project="Examples:Cargo-Bot"  -- change project name to search 
        textMode(CORNER)
        dy=-50
        iTab={}
        cls={}
        tab={}
        ins={}
        print("Find classes")
        findClasses()
        for a,b in pairs(cls) do
            print("Find instances in "..b)
            findInstances("="..b.."(")
        end
        displayMode(FULLSCREEN)
    end
    
    function draw()
        background(40, 40, 50)
        fill(255)
        text("Classes within < tabs >",50,HEIGHT+30+dy)
        text("Instances within < tabs >",WIDTH/2,HEIGHT+30+dy)
    
        for a,b in pairs(tab) do
            text(b,50,HEIGHT-a*20+dy)
        end
    
        cnt=0
        for a,b in pairs(iTab) do
            if #iTab[a]>0 then
                cnt=cnt+1
                text("< "..a.." >",WIDTH/2,HEIGHT-cnt*20+dy)
                for c,d in pairs(b) do
                    cnt=cnt+1
                    text(d,WIDTH/2+20,HEIGHT-cnt*20+dy)
                end
                cnt=cnt+2
            end
        end
    end
    
    function touched(t)
        if t.state==BEGAN or t.state==MOVING then
            dy=dy+t.deltaY
            if dy<-50 then
                dy=-50
            end
        end
    end
    
    function findClasses()
        local r=listProjectTabs(project)
        for a,b in pairs(r) do
            local str=readProjectTab(project..":"..b)
            table.insert(tab,"\n")
            table.insert(tab,"< "..b.." >\n")
            local b1=1
            local s,e=1,0
            while true do
                s,e=string.find(str,"\n",s)
                if s==nil then
                    break
                end
                local str1=leaveSpaces(0,string.sub(str,b1,e+1))
                local s1,e1=string.find(str1,"class(",1,true)
                if s1~=nil then
                    table.insert(tab,"       "..string.sub(str,b1,e))
                    local s2,e2=string.find(str1,"=")
                    if s2~=nil then
                        table.insert(cls,string.sub(str1,1,e2-1))
                    end                
                end
                b1=e+1
                s=b1
            end
            table.insert(tab,"\n")
        end
    end
    
    function findInstances(class)
        local r=listProjectTabs(project)
        for a,b in pairs(r) do
            local str=readProjectTab(project..":"..b)
            if iTab[b]==nil then
                iTab[b]={}
            end
            local b1=1
            local s,e=1,0
            while true do
                s,e=string.find(str,"\n",s)
                if s==nil then
                    break
                end
                local str1=string.sub(str,b1,e+1)
                local str2=leaveSpaces(0,str1)
                local s1,e1=string.find(str2,class,1,true)
                if s1~=nil then 
                    local s2,e2=string.find(str1,"(",1,true)
                    table.insert(iTab[b],leaveSpaces(1,string.sub(str1,1,e2-1)))
                end
                b1=e+1
                s=b1
            end
        end
    end
    
    function leaveSpaces(nbr,str)   -- nbr spaces between words in str (0 or 1)
        local str1=""
        local sp=0
        for z=1,#str do
            local c=string.sub(str,z,z)
            if c~=" " then
                sp=0
                str1=str1..c
            elseif sp<nbr then
                sp=sp+1
                str1=str1..c
            end
        end
        return str1
    end
    
Sign In or Register to comment.