159 lines
3.7 KiB
Lua
159 lines
3.7 KiB
Lua
local PROTOCOL = "wireless-todo"
|
|
|
|
local function pprint(...)
|
|
local pretty = require("cc.pretty")
|
|
pretty.pretty_print(...)
|
|
end
|
|
|
|
local function isWirelessModem(peripheral_name)
|
|
return peripheral.getType(peripheral_name) == "modem"
|
|
and peripheral.call(peripheral_name, "isWireless")
|
|
end
|
|
|
|
local function getWirelessModem()
|
|
for _, name in ipairs(peripheral.getNames()) do
|
|
if isWirelessModem(name) then
|
|
return name
|
|
end
|
|
end
|
|
end
|
|
|
|
local function splitTextByWidth(text, max_width)
|
|
local lines = {}
|
|
local current_line = ""
|
|
for whitespace, word in text:gmatch("(%s*)([^%s]+)") do
|
|
if #current_line + #whitespace + #word > max_width then
|
|
table.insert(lines, current_line)
|
|
current_line = word
|
|
else
|
|
current_line = current_line..whitespace..word
|
|
end
|
|
end
|
|
if #current_line > 0 then
|
|
table.insert(lines, current_line)
|
|
end
|
|
return lines
|
|
end
|
|
|
|
local function drawTodo(todo)
|
|
local w, h = term.getSize()
|
|
local x, y = term.getCursorPos()
|
|
local lines = splitTextByWidth(todo.text, w-4)
|
|
paintutils.drawFilledBox(x, y, w, y+#lines-1)
|
|
term.setCursorPos(x, y)
|
|
|
|
term.write("[ ] ")
|
|
if todo.marked then
|
|
x, y = term.getCursorPos()
|
|
term.setCursorPos(x-3, y)
|
|
term.setTextColor(colors.green)
|
|
term.write("x")
|
|
term.setTextColor(colors.white)
|
|
term.setCursorPos(x, y)
|
|
end
|
|
|
|
x, y = term.getCursorPos()
|
|
for i, line in ipairs(lines) do
|
|
term.setCursorPos(x, y+i-1)
|
|
term.write(line)
|
|
end
|
|
term.setCursorPos(1, y+#lines)
|
|
end
|
|
|
|
local function drawTodos(todos, selected_todo)
|
|
local w, h = term.getSize()
|
|
term.clear()
|
|
term.setCursorPos(1, 1)
|
|
term.setTextColor(colors.orange)
|
|
print("===== Wireless Todo ======")
|
|
|
|
term.setTextColor(colors.white)
|
|
for i, todo in ipairs(todos) do
|
|
if i == selected_todo then
|
|
term.setBackgroundColor(colors.gray)
|
|
drawTodo(todo)
|
|
term.setBackgroundColor(colors.black)
|
|
else
|
|
drawTodo(todo)
|
|
end
|
|
|
|
local _, y = term.getCursorPos()
|
|
end
|
|
end
|
|
|
|
local function getTodoUnderMouse(todos, x, y)
|
|
local w, h = term.getSize()
|
|
|
|
local todo_y = 2
|
|
for i, todo in ipairs(todos) do
|
|
local todo_height = #splitTextByWidth(todo.text, w-4)
|
|
if todo_y <= y and y < todo_y+todo_height then
|
|
return i
|
|
end
|
|
todo_y = todo_y + todo_height
|
|
end
|
|
end
|
|
|
|
local function main()
|
|
local modem_name = getWirelessModem()
|
|
if not modem_name then
|
|
print("ERROR: please attach a wireless modem")
|
|
return
|
|
end
|
|
|
|
rednet.open(modem_name)
|
|
term.clear()
|
|
term.setCursorPos(1, 1)
|
|
print("Starting...")
|
|
|
|
local servers = {rednet.lookup(PROTOCOL)}
|
|
local server = servers[1]
|
|
|
|
local selected_todo = 1
|
|
local todos = {
|
|
{
|
|
text = "Foo bar baz",
|
|
marked = false,
|
|
},
|
|
{
|
|
text = "Nobis doloremque explicabo qui amet nihil perspiciatis. ",
|
|
marked = false,
|
|
},
|
|
{ text = "Biz baz", marked = false },
|
|
{ text = "Biz baz", marked = true },
|
|
{
|
|
text = "Nobis doloremque explicabo qui amet nihil perspiciatis. foao ado ao",
|
|
marked = false,
|
|
}
|
|
}
|
|
rednet.send(server, "get", PROTOCOL)
|
|
while true do
|
|
local event = {os.pullEvent()}
|
|
if event[1] == "rednet_message" and event[4] == PROTOCOL then
|
|
-- todos = textutils.unserialise(event[3])
|
|
elseif event[1] == "mouse_up" then
|
|
local todo_index = getTodoUnderMouse(todos, event[3], event[4])
|
|
if todo_index then
|
|
selected_todo = todo_index
|
|
end
|
|
elseif event[1] == "key_up" then
|
|
local key = event[2]
|
|
if key == keys.up then
|
|
selected_todo = math.max(selected_todo-1, 1)
|
|
elseif key == keys.down then
|
|
selected_todo = math.min(selected_todo+1, #todos)
|
|
elseif key == keys.space then
|
|
todos[selected_todo].marked = not todos[selected_todo].marked
|
|
end
|
|
end
|
|
|
|
drawTodos(todos, selected_todo)
|
|
end
|
|
|
|
-- local _, response = rednet.receive(PROTOCOL)
|
|
-- pprint(textutils.unserialise(response))
|
|
end
|
|
|
|
-- pprint(splitTextByWidth("foo bar baz", 10))
|
|
main()
|