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

Command Stacking

Started by kreezxil, 29 August 2013 - 06:13 PM
kreezxil #1
Posted 29 August 2013 - 08:13 PM
Hey guys!

I've created a program that I have named "do" over at http://turtlescripts.com/file/gjdhq8.

What the program does is allow you to stack commands at the prompt in both your computers and turtles. This is similar to how you can compound commands Linux and Unix.

For instance my favorite turtle is chock full of custom programs such as my "dig" program also at turtlescripts.com that allows you to make your turtle dig in a direction for any length of blocks. And a few other programs that I've copied from M3gabuild and Miz3craft.

So using the program I can tear down a quarry plus its associated portal rather effortlessly as seen in my video at [media]http://www.youtube.com/watch?v=l8uKdPkLRhs[/media].

I now present you with my code which is amazingly simplistic and I am betting after you guys dive into it; it'll get even more awesome and optimized. Not to mention feature enriched.
Spoiler

--[[
Created by Kreezxil
On 8/28/2013
Changelog:
5/31/2014 - cleaned up code, optimized explode and implode, fixed parameter dropping

pastebin: wSWv68HL

Description: Allows turtles and computers to have
command line scripting.

Usage:
 do <<command>[args];[<command>[args];...]>

--]]

local debug = false

--[[
  explode function by Kreezxil
  cause: learned by example. yay. \o/
--]]
function explode(div,str)
  if(div=='') then 
      return false 
  end

  if string.sub(str,-1,-1) ~= ";" then
      str = str .. ";"
  end

  local tmp = ""
  local arr = {}

  for i=1,#str do
      local char = string.sub(str,i,i)
      if char == div then
          if tmp ~= "" then
            print("Adding [" .. tmp .."] to result array") 
            table.insert(arr,tmp)
          end
          tmp = ""
      else
          if char ~= nil then
            tmp = tmp .. char
          end
      end
  end

  return arr
end

--[[
  implode function by Kreezxil
  cause: learned by example. yay. \o/
--]]
function implode(div,arr)
  if(div=='') then 
      return false 
  end

  result = ""
  for i=1,#arr do
      result = result .. div .. arr[i]
  end
  return result
end

local args = { ... }
if #args <1 then
  print ("Usage:")
  print("")
  print("do <<commands>[args];[<commands>[args];...]>")
  return
end

if debug then 
    for i=1,#args do
      print("Arg[" .. i .. "]: " .. args[i])
    end
end
-- turn args into a string called compacted
local compacted = implode(" ",args)

if debug then
    print("Args after implosion:")
    print(compacted)
end

--[[
  turn compacted string of args into our
  array for use with shell.run
--]]
stack = explode(";" , compacted)

if debug then
    print("Command Stack is: ")
    for i=1,#stack do
      print("CMD[" .. i .. "] " .. stack[i])
    end
end

print ("Processing command stack.")

for i=1,#stack do
  shell.run(stack[i])
end

Pastebin: wSWv68HL
Url: http://pastebin.com/wSWv68HL

Changes to this page: 5/31/2014 updated code block to reflect v2.0
Edited on 31 May 2014 - 03:37 PM
Zudo #2
Posted 01 September 2013 - 05:47 AM
I like this, it is quite useful
kreezxil #3
Posted 03 September 2013 - 06:37 AM
Thanks, I hope to add more features to it soon.
Mitchfizz05 #4
Posted 03 September 2013 - 07:13 AM
Ooooo… explode function.
Yoink.
kreezxil #5
Posted 07 September 2013 - 10:07 PM
Ooooo… explode function.
Yoink.

Yeah, borrow it if you want, I don't own the rights to it. :)/>
kreezxil #6
Posted 31 May 2014 - 05:39 PM
So yeah, I've been using my code heavily for the past few months and have finally decided to fix the command arguments dropping. Also I optimized the implode and explode functions so that now they are my own code. You can still use all my code in your projects though. Just give me some cred until such time as you think the code is your own doing. :)/>

I've also turned off the debugging output, if you like it you can turn it back on in the top of the program by setting debug to true.

Btw, if anyone has suggestions on how to optimize the code yet still allow it to be newbie friendly, I'm all ears.

Thanks!