Code viewer

Here’s a program that will show you variable and function names with a count of how many times they are used. Just enter a project name and press “show list”. A list of the tab names in that project is shown in the print window. There will be 2 columns shown, Count and Names that can be scrolled up or down. Also shown at the top is a count of the number of names found. I not sure how well this works because I would have to read in a lot of different projects and look thru the names to see if there’s any errors. I’m not about to do that, so if you find any errors, you can try to update the code yourself or let me know. Try looking at Cargo-Bot or any of the other examples just for kicks.

EDIT: I’m not sure if this has any use, but it was just something to do.


--# Main

function setup()
    font("Courier")
    textMode(CORNER)
    parameter.text("project")
    parameter.action("show list",process)
    tab={}
    tab1={}
    dy=0
end

function process()
    output.clear()
    tab={}
    tab1={}
    dy=0
    if project=="" then
        print("No project name entered.")
        return
    end
    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,dy+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