local expectlib = require("cc.expect") local expect, field = expectlib.expect, expectlib.field local term_stack = {} local cursor_stack = { } local setCursorPos = term.setCursorPos local getCursorPos = term.getCursorPos local cursor_ox = 0 local cursor_oy = 0 function term_stack.push_cursor(x, y) expect(1, x, "number", "nil") expect(2, y, "number", "nil") local dx = ((x or 1) - 1) local dy = ((y or 1) - 1) table.insert(cursor_stack, { dx, dy }) cursor_ox = cursor_ox + dx cursor_oy = cursor_oy + dy setCursorPos(cursor_ox+1, cursor_oy+1) end function term_stack.pop_cursor() local frame = table.remove(cursor_stack) cursor_ox = cursor_ox - frame[1] cursor_oy = cursor_oy - frame[2] setCursorPos(cursor_ox+1, cursor_oy+1) end function term.setCursorPos(x, y) expect(1, x, "number") expect(2, y, "number") setCursorPos(x + cursor_ox, y + cursor_oy) end function term.getCursorPos() local x, y = getCursorPos() return x - cursor_ox, y - cursor_oy end local size_stack = {} local getSize = term.getSize local current_w, current_h = getSize() function term_stack.push_size(w, h) expect(1, w, "number") expect(2, h, "number") table.insert(size_stack, { current_w, current_h }) current_w = w current_h = h return w, h end function term_stack.pop_size() local frame = table.remove(cursor_stack) current_w = frame[1] current_h = frame[2] end function term.getSize() return current_w, current_h end function term_stack.push_rect(x, y, w, h) if type(x) == "table" then local rect = x field(rect, "x", "number") field(rect, "y", "number") field(rect, "w", "number") field(rect, "h", "number") term_stack.push_cursor(rect.x, rect.y) term_stack.push_size(rect.w, rect.h) return rect.w, rect.h else expect(1, x, "number") expect(2, y, "number") expect(3, w, "number") expect(4, h, "number") term_stack.push_cursor(x, y) term_stack.push_size(w, h) return w, h end end function term_stack.pop_rect() term_stack.pop_cursor() term_stack.pop_size() end return term_stack