Script for searching all projects and tabs!

Since I couldn’t find a way to search for something in all my Codea projects at the same time, I made one myself!

I even tried to replicate Codea’s style so you feel right at home when using it!

Here’s the code in case GitHub isn’t working for some reason:

local function searchProjects()
    hideKeyboard()
    output.clear()
    matches = {}
    for _, p in ipairs( listProjects( "documents" ) ) do
        for _, t in ipairs( listProjectTabs( p ) ) do
            local code = readProjectTab( p .. ":" .. t )
            local i = 1
            for l in string.gmatch( code, "([^\\r\

]*)\r?
?" ) do
if string.find( l, Search, 1, true ) then
table.insert( matches, { project = p, tab = t, line = i, code = l } )
print( p, t, i, l )
end
i = i + 1
end
end
end
searched = true
end

local function drawSearch( y )
    fill( 65, 150, 190 )
    fontSize( 27 )
    local w, h = textSize( "Search(" )
    text( "Search(", 36, y )
    local w2, h2 = textSize( Search )
    fill( 255 )
    text( Search, 36 + w, y )
    if isKeyboardShowing() then
        fill( 0, 155, 225, 155 + math.sin( ElapsedTime * 6 ) * 100 )
        rect( 36 + w + w2, y, 3, h2 )
    end
    fill( 65, 150, 190 )
    text( ")", 36 + w + w2, y )
    return y - h2
end

local function drawLabel( col, str, y )
    fill( col )
    fontSize( 17 )
    local _, h = textSize( str )
    text( str, 36, y )
    return y - h
end

local function drawLine( str, y )
    fill( 110 )
    fontSize( 11 )
    local w = textSize( str )
    text( str, 24 - w, y )
end

function setup()
    supportedOrientations( CurrentOrientation )
    displayMode( FULLSCREEN )
    parameter.text( "Search", "" )
    parameter.action( "Go", searchProjects )
    font( "Inconsolata" )
    textMode( CORNER )
    showKeyboard()
    matches = {}
    searched = false
    scroll = HEIGHT - 33
end

function draw()
    background( 30, 32, 40 )
    fill( 20, 22, 30 )
    rect( 0, 0, 27, HEIGHT )
    local y = drawSearch( scroll )
    if searched then
        y = drawLabel( color( 220, 130, 120 ), #matches .. "_MATCHES", y )
        local prevTitle = ""
        for _, match in ipairs( matches ) do
            local title = "-- " .. match.project .. "/" .. match.tab .. ":"
            if title ~= prevTitle then
                y = drawLabel( color( 30, 160, 140 ), title, y )
            end
            prevTitle = title
            drawLine( match.line, y )
            y = drawLabel( color( 225, 230, 245 ), match.code, y )
        end
    end
end

function touched( touch )
    if touch.state == BEGAN and touch.y > scroll - 16 then
        showKeyboard()
    else
        scroll = scroll + touch.deltaY
    end
end

function keyboard( key )
    if key == "\

" then
searchProjects()
elseif key == BACKSPACE then
Search = string.sub( Search, 1, -2 )
else
Search = Search … key
end
end

@MPan1 Here’s a link to a program I wrote back in March 2013 that searches all projects. You had to create a table of projects manually because listProject wasn’t implemented yet. I eventually used a plist file to get the list of projects. You can see that I asked for that function to be added. I updated my program since then to use listProjects and made other improvements to it. One thing that can be done is to select a project from the matched list to view all the code of that project and then return back to the matched list to select another project to view. I have over 500 projects, so that’s why I created it back then.

Good job on yours.

https://codea.io/talk/discussion/2494/search-display#latest

Damn
EDIT: Yours doesn’t appear to use listProjects since that wasn’t around when you made it. Mine does though.