Created
October 4, 2019 03:07
-
-
Save trentgill/3e2117eeae0dcf4e40334ecfa50c6b0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- druid.lua | |
-- a tiny shell for crow | |
local keycodes = include("druid/lib/keycodes") | |
local word = "" | |
local prompt = "> " | |
local console = {} | |
local CONSOLE_LEN = 6 | |
local hist = {} | |
local hist_point = 0 | |
local HIST_LEN = 10 | |
local keyb = hid.connect() | |
function init() | |
for i=1,CONSOLE_LEN do console[i] = "" end | |
for i=1,HIST_LEN do hist[i] = "" end | |
screen.aa(1) | |
redraw() | |
end | |
local shift = false | |
function get_key(code, val) | |
if code == hid.codes.KEY_LEFTSHIFT then | |
shift = (val > 0) and true or false | |
end | |
local k | |
if keycodes.keys[code] and val == 1 then | |
if shift then | |
if keycodes.shifts[code] then | |
k = keycodes.shifts[code] | |
else | |
k = keycodes.keys[code] | |
end | |
else | |
k = string.lower(keycodes.keys[code]) | |
end | |
elseif keycodes.cmds[code] and val == 1 then | |
if (code == hid.codes.KEY_ENTER) then | |
k = "_enter" | |
elseif (code == hid.codes.KEY_BACKSPACE or code == hid.codes.KEY_DELETE) then | |
k = "_delete" | |
elseif code == hid.codes.KEY_UP then | |
k = "_up" | |
elseif code == hid.codes.KEY_DOWN then | |
k = "_down" | |
end | |
end | |
return k | |
end | |
function pushlog(h, line) | |
table.remove(h) | |
table.insert(h, 1, line) | |
return h | |
end | |
norns.crow.event = function(id, line) | |
line = string.sub(line,1,-3) -- strip newline | |
if util.string_starts(line,"^^") == true then | |
line = line:gsub("%^^","_norns.crow_") | |
assert(load(line))() | |
else | |
console = pushlog(console, " " .. line) | |
end | |
end | |
function keyb.event(typ, code, val) | |
local k = get_key(code, val) | |
if k == "_enter" then | |
crow.send(word) | |
console = pushlog(console, word) | |
hist = pushlog(hist, word) | |
hist_point = 0 | |
word = "" | |
elseif k == "_delete" then | |
word = string.sub(word,1,-2) | |
elseif k == "_up" then | |
hist_point = hist_point + 1 | |
if hist_point > HIST_LEN then hist_point = HIST_LEN end | |
word = hist[hist_point] | |
elseif k == "_down" then | |
hist_point = hist_point - 1 | |
if hist_point <= 0 then hist_point = 0 end | |
word = (hist_point >= 1) and hist[hist_point] or "" | |
elseif k then -- not nil | |
word = word .. k | |
end | |
redraw() | |
end | |
function redraw() | |
-- setup screen | |
screen.clear() | |
screen.level(15) | |
screen.line_width(1) | |
screen.font_face(0) | |
screen.font_size(8) | |
-- print history | |
screen.level(7) | |
for n=1,CONSOLE_LEN do | |
screen.move(0,62 - n*9) | |
screen.text(console[n]) | |
end | |
-- print console line | |
screen.level(15) | |
screen.move(0,62) | |
screen.text(prompt .. word) | |
screen.update() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment