From 89fa78d9850742609da66f1970e21c20b5a83a13 Mon Sep 17 00:00:00 2001 From: Rob Hoelz Date: Fri, 14 Sep 2012 18:52:09 +0200 Subject: Add cursor and clear functions --- term/cursor.lua | 31 +++++++++++++++++++++++++++++++ term/init.lua | 28 +++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 term/cursor.lua diff --git a/term/cursor.lua b/term/cursor.lua new file mode 100644 index 0000000..ebd81ed --- /dev/null +++ b/term/cursor.lua @@ -0,0 +1,31 @@ +-- Copyright (c) 2009 Rob Hoelz +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in +-- all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +-- THE SOFTWARE. + +local term = require 'term.core' + +return { + goto = term.maketermfunc '%d;%dH', + goup = term.maketermfunc '%d;A', + godown = term.maketermfunc '%d;B', + goright = term.maketermfunc '%d;C', + goleft = term.maketermfunc '%d;D', + save = term.maketermfunc 's', + restore = term.maketermfunc 'u', +} diff --git a/term/init.lua b/term/init.lua index 07be081..8e59ce6 100644 --- a/term/init.lua +++ b/term/init.lua @@ -18,7 +18,33 @@ -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -- THE SOFTWARE. -local term = require 'term.core' +local term = require 'term.core' +local sformat = string.format +local iotype = io.type +local stdout = io.stdout + +function term.maketermfunc(sequence_fmt) + sequence_fmt = '\027[' .. sequence_fmt + + local func + + func = function(handle, ...) + if iotype(handle) ~= 'file' then + return func(stdout, handle, ...) + end + + return handle:write(sformat(sequence_fmt, ...)) + end + + return func +end + term.colors = require 'term.colors' +term.cursor = require 'term.cursor' + +term.clear = term.maketermfunc '2J' +term.cleareol = term.maketermfunc 'K' + +term.maketermfunc = nil return term -- cgit v1.2.3-55-g6feb