Simulated search function

@Simeon
Here is an example of a simulated search function using some ghost characters that I mentioned in another thread. Since I can’t read an actual file, I put some sample code in a string that I search. I set up some left and right ghost characters, but there are more characters that need to be added. I wanted to change the color of the found characters, but instead I just show them below the simulated code line. Just follow the instructions on the screen, keying each character slowly and watching what gets matched below the simulated code line. After each set, press the backspace key to clear everything for the next search.

Dave


-- Simulate a search function using ghost characters

function setup()
    displayMode(FULLSCREEN)
    
    st=""
    str = ""
    str2=""

    showKeyboard()
    font("courier")

    -- since I can't read a file, I put simulated code in a string
    str1=" counter=1234 count[xx] =1.23 coun-12 cou[x]=12 co[12]= 1. c=.3 u xxx  "
    
    g_left="[%[( .=-]"        -- some left ghost characters within []
    g_right="[ .=()%]%[-]"    -- some right ghost characters within []
    
end

function draw()
    background(30,30,0,255)
    stroke(255)
    strokeWidth(6)
    fill(255)
    
    text(st,160,900)
    text(str1,10,800)
    text(str2,10,780)

    text("Simulated a search function using right and left ghost characters",30,980)
    text("Simulated code",30,820)
    text("Search string with left & right ghost characters",30,920)
     text("Key in the word counter and watch below the simulated code line.",30,580)  
    text("Press the backspace key before keying each of the next strings",30,540) 
    text("Key in xxxx",30,500)  
    text("Key in uu",30,480)    
    text("Key in the number 12345",30,460)
    text("Key in 1.234",30,440)
    text("Key in .30",30,420)

    fill(255, 0, 0, 255)

    rectMode(CENTER) 
    textMode(CENTER) 
    rect(200,700,200,50)
    fill(255)
    text(str,200,700)
    rectMode(STANDARD)
    textMode(STANDARD)
end

-- accept a key from the keyboard
function keyboard(key)
    if key == BACKSPACE then
        st=""        -- clear variables
        str = ""
        str2=""
        return
    end
    -- some of the magic characters, proceed them with a %
    if key == "." or key == "%" or key == "[" or key == "]" then
        str = str.."%"
    end
    str = str..key
    search(str) 
end

-- search the string str1 for the keyed string
function search(ss)
    sx=1
    -- setup search string with left and right ghost characters
    st = g_left..ss..g_right
    str2=""
    while sx < string.len(str1) do
        s,e = string.find(str1,st,sx)
        if s ~= nil then
            fillstr(s+1,e-1)
            sx = s+1
        elseif s == nil then
            return
        end
    end
    fillstr()    -- show found strings
end


-- I didn't know how to change the color of the text so I just
-- show the characters that are found below the str1 line.
function fillstr(s,e)
    for x=string.len(str2)+1,e do
        if x>= s and x<=e then
            xx=string.sub(str1,x,x)
        else
            xx=" "
        end        
        str2 = str2..xx
    end
end

Thanks @dave1707. That’s a really clever way to prototype a feature. I’ll have a look at it now and post my thoughts.

I like your idea and the implementation works well. I can see this working for 1 and 2 character searches — though as you get longer than that your method loses the ability to find substrings (e.g., " count" doesn’t highlight in “counter”).

@Simeon
Not finding count in counter is the whole point. For example, if I was searching for all occurances of an if statement that had ‘or’ in it, I don’t want it to show me all of the ‘for’ statements or the ‘string.format’ statements. Same way if I had a variable called ‘mat’. I don’t want to see all of the math functions. The purpose of this type of search is to find only the name you’re looking for, nothing more, nothing less. If the ghost characters are set up correctly, this should find any name in the code from 1 to whatever length.

Dave.

@dave1707 I have to say that I use substring search a lot when coding. Perhaps this is a personal preference, but the search function in most coding apps (e.g. Xcode) defaults to substring search — not entire-symbol search. It’s more convenient to type the start of the thing you’d like to find, then refine it to pare down the results.

That said, for 1 and 2 character searches substring search doesn’t make much sense. In IDEs like Xcode this isn’t a problem because you have to press an explicit search button (or hit return) to start the search. But for instant searching I think we could use your method for 1 and 2 character searches, then move to substring search past that.

Thanks @Simeon for considering to add a 1 or 2 character search. When I code, I tend to use 1 or 2 character variable names because I modify the code a lot and don’t want to keep keying long names. Once I have something working the way I want it, I look for the variables and give them more meaningful names. On small programs, looking through not many lines of code isn’t bad, but on really long programs, it’s easy to miss some names. Looking through some of the example programs, I see other programmers also use a lot of 1 or 2 character variables.

Dave

That’s a good point. I’ll add it for 1 and 2 character searches.