This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kingdaro's profile picture

[basically every version] implement - Copy/paste code from one file to another.

Started by Kingdaro, 18 November 2012 - 09:32 AM
Kingdaro #1
Posted 18 November 2012 - 10:32 AM
implement
Making your life easier one implementation at a time.


Description
Allows you to append the code of another file at the top of the file(s) of your choosing. It also removes existing implementations. Useful if, for example, you have a checkFuel() function stored in another file, and you'd like to copy it over to other turtle programs.

Works with normal and advanced computers.

Usage
implement <source> into <file> (<file2> <file3> ...)

Download
http://pastebin.com/rv2gGQDX

Screenshots


billysback #2
Posted 18 November 2012 - 10:54 AM
I'll let this pass my personal rule of not posting anything unless it's above or around 100 lines of code :)/>/>
It looks pretty good; but isn't it essentially fs.combine but with a comment either side of the inputted lines?

I kinda made something similar as my first program and am working on something vaguely similar to this now :D/>/>
Kingdaro #3
Posted 18 November 2012 - 11:07 AM
I'll let this pass my personal rule of not posting anything unless it's above or around 100 lines of code :)/>/>
It's pretty simple and tiny, but useful nonetheless. My analog clock was smaller, I'm pretty sure. :D/>/>

It looks pretty good; but isn't it essentially fs.combine but with a comment either side of the inputted lines?
Those comments are there so that the script knows there's an existing implementation, and to remove it before readding the implementation.

And I don't think this really does the job of fs.combine. Even if it did, there are a couple more features with this, and it's more convenient as a command line application.

I kinda made something similar as my first program and am working on something vaguely similar to this now :D/>/>
That's pretty cool. Good luck on that.
billysback #4
Posted 18 November 2012 - 11:16 AM
not saying you shouldn't have made it, maybe try making it unimplement it by writing the file again but missing any lines which equal
"–IMPLEMENT base"
or
"–END"
and anything in between

I created this function for that other project and as it's of utter relevance to the suggestion I just made I though I would post it:
Spoiler

local function getChunk(lns, open, close)
    local lines = {}
    local on = false
    for i=1,#lns do
        local line = lns[i]
        if line == close then on = false end
        if on then
            lines[#lines + 1] = line
        end
        if line == open then on = true end
    end
    return lines
end

This specific code gets all of the lines in a table (lns) between the opening and closing statement, but it could be easily modified to include the opening and closing statement.
Kingdaro #5
Posted 18 November 2012 - 11:28 AM
I would rather use :gsub than loop through the file's lines. I'll keep the suggestion in mind, though.