Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("lib_log");
- dofile("lib_turtle_0.2.4");
- local manText = [[
- NAME
- tr - Turtle Runner
- SYNOPSIS
- tr [options] [FILE]
- DESCRIPTION
- tr is a program which operates a turtle using the script which can be described by a few keystrokes.
- The script for tr consists of COMMANDS in which a name is extremely short. For example, forward is defined as "f", turnLeft is "l" and placeDown is "p2".
- Moreover, control syntax is also prepared for tr. (Although it is only a repetition.)
- The following example is the script which perform tofu construction.
- s1 (up2 ((fp2)4 r (fp2)3 r)2)3 fr (fp2)2 lfp2l fp2 rf p2rf dd p0 blbbd s2p1 rflfr e1fe0f ll s3p1 lbl
- *** Set items as follows. ***
- slot1 : block x 48
- slot2 : bed x 1
- slot3 : door x 1
- When tr is executed, without specifying a FILE, the prompt for script is displayed.
- After the input of a script, the prompt for file name is displayed.
- If you don't want to save, press the Enter key, with no input.
- options
- --help
- display this help and exit.
- --demo
- interactive input mode with "tofu construction" example.
- FILE
- optional. The path of a script file.
- COMMANDS
- f - <f>orward
- b - <b>ack
- l - turn <l>eft
- r - turn <r>ight
- u - <u>p
- d - <d>own
- s[slot]
- - <s>elect
- slot - 1 ~ 16
- p[side]
- - <p>lace
- side - 0:up, 1:front, 2:down
- t[side],[count]
- - <t>hrow (= drop)
- side - 0:up, 1:front, 2:down
- count
- e[side]
- - <e>xcavate (= dig)
- side - 0:up, 1:front, 2:down
- z[millisecond]
- - <z>zz = sleep
- o[side],[value]
- - set redstone <o>utput
- side - 0:up, 1:front, 2:down,
- 3:left, 4:back, 5:right
- value - 0:off, 1:on
- k[side]
- - suc<k>
- side - 0:up, 1:front, 2:down
- a[side]
- - <a>ttack
- side - 0:up, 1:front, 2:down
- v[id]
- - user e<v>ent
- id -
- Control syntax
- (xx)[loopCount]
- - loop
- loopCount - loop count.
- AUTHOR
- kssr (http://cct.blog.shinobi.jp)
- REPORTING BUGS
- Report bugs to <http://cct.blog.shinobi.jp>.
- COPYRIGHT
- Copyright (C) 2014 kssr (http://cct.blog.shinobi.jp)
- SEE ALSO
- http://cct.blog.shinobi.jp
- ]];
- local function split(src, sepalator)
- local ret = { };
- local bgnIdx = 1;
- repeat
- local endIdx = src:find(sepalator, bgnIdx, true);
- if nil == endIdx then
- endIdx = #src + 1;
- end
- table.insert(ret, src:sub(bgnIdx, endIdx - 1));
- bgnIdx = endIdx + 1;
- until #src < bgnIdx;
- return ret;
- end
- local KNtC = { };
- for i = 0, 256 do
- local name = keys.getName(i);
- if nil ~= name then
- KNtC[name] = i;
- end
- end
- local pages = { };
- local function makePages()
- local w, h = term.getSize();
- w = w - 1;
- h = h - 1;
- local current = { };
- local beginIdx = 1;
- local lines = split(manText, "\n");
- for _, val in ipairs(lines) do
- local indent = 0;
- for j = 1, #val do
- if " " ~= val:sub(j, j) then
- indent = j - 1;
- val = val:sub(j);
- break;
- end
- end
- local x = 1;
- local BLANK = " ";
- while true do
- if #val < x then
- break;
- end
- table.insert(current, BLANK:rep(indent) ..
- val:sub(x, math.min(x + w - indent - 1, #val)));
- x = x + w - indent;
- if h == #current then
- table.insert(pages, current);
- current = { };
- end
- end
- end
- if 0 < #current then
- table.insert(pages, current);
- end
- end
- local function isOneOf(val, possibleValues)
- for _, v in ipairs(possibleValues) do
- if val == v then
- return true;
- end
- end
- return false;
- end
- local pageIdx = 1;
- local function man()
- while true do
- term.clear();
- term.setCursorPos(1, 1);
- term.setBackgroundColor(colors.black);
- term.setTextColor(colors.white);
- local w, h = term.getSize();
- local BLANK = " ";
- for i, val in ipairs(pages[pageIdx]) do
- term.setCursorPos(1, i);
- term.write(val .. string.rep(BLANK, w - #val));
- end
- for i = #pages[pageIdx] + 1, h do
- term.setCursorPos(1, i);
- term.write(string.rep(BLANK, w));
- end
- term.setCursorPos(1, h);
- term.setTextColor(colors.black);
- term.setBackgroundColor(colors.white);
- term.write(" [b]ack, [n]ext, [q]uit / page : " .. pageIdx .. " of " .. #pages .. ".");
- while true do
- local event, scanCode = os.pullEvent("key");
- -- key name
- -- http://computercraft.info/wiki/Keys_(API)
- if isOneOf(scanCode, { KNtC.up, KNtC.left, KNtC.p, KNtC.b, KNtC.w, KNtC.a }) then
- pageIdx = math.max(pageIdx - 1, 1);
- break;
- elseif isOneOf(scanCode, { KNtC.down, KNtC.right, KNtC.n, KNtC.f, KNtC.s, KNtC.d, KNtC.space }) then
- pageIdx = math.min(pageIdx + 1, #pages);
- break;
- elseif isOneOf(scanCode, { KNtC.q, KNtC.e }) then
- term.setBackgroundColor(colors.black);
- term.setTextColor(colors.white);
- term.clear();
- term.setCursorPos(1, 1);
- return;
- end
- sleep(0.5);
- end
- end
- end
- local function interactiveScriptInputMode(history)
- local input = history;
- while true do
- print("input command or type ? >");
- if nil ~= input and "" ~= input then
- print("** In order to re-display your script,");
- print("** press the 'up' cursor key.");
- end
- input = read(nil, { input });
- if string.find(input, "?", 1, true) then
- man();
- else
- if nil ~= input then
- script = input;
- else
- script = "";
- end
- break;
- end
- end
- return script;
- end
- local function interactiveFileNameInputMode()
- local pass = false;
- local input;
- local exitFlg = false;
- while true do
- print("Input script name if you want to save. >");
- input = read(nil, { input });
- if nil == input then
- fileName = "";
- pass = true;
- elseif "" ~= input and fs.exists(input) then
- while true do
- print("file exists. [o]verwrite, [n]ame other, [d]on't save, [q]uit");
- local ip2 = read();
- if "o" == ip2 then
- fileName = input;
- pass = true;
- break;
- elseif "n" == ip2 then
- break;
- elseif "d" == ip2 then
- print("Don't save.");
- fileName = "";
- pass = true;
- break;
- elseif "q" == ip2 then
- print("Quit.");
- exitFlg = true;
- pass = true;
- break;
- else
- print("Wrong input. retry.");
- end
- end
- else
- fileName = input;
- break;
- end
- if pass then
- break;
- end
- end
- return fileName, exitFlg;
- end
- local tArgs = { ... };
- makePages();
- local script;
- local fileName = "";
- local history = "";
- local exitFlg;
- if isOneOf("--help", tArgs) then
- man();
- return;
- elseif isOneOf("--demo", tArgs) then
- history = "s1 (up2 ((fp2)4 r (fp2)3 r)2)3 " ..
- "fr (fp2)2 lfp2l fp2 rf p2rf dd p0 " ..
- "blbbd s2p1 rflfr e1fe0f ll s3p1 lbl";
- elseif 1 == #tArgs then
- fileName = tArgs[1];
- end
- if "" ~= fileName then
- local hFile = fs.open(fileName, "r");
- local txt = hFile.readAll();
- hFile.close();
- script = txt;
- else
- script = interactiveScriptInputMode(history);
- fileName, exitFlg = interactiveFileNameInputMode();
- if exitFlg then
- return;
- end
- if "" ~= fileName then
- local hFile = fs.open("/" .. fileName, "w");
- hFile.writeLine(script);
- hFile.close();
- end
- end
- if "" ~= script then
- doCommand(script);
- print("tr was completed.");
- else
- print("script is empty.");
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement