reverting read handler weirdness, it didn't work right

Users shouldn't use tty read nor write directly, they should always use io or term. But...it is messy to try to hide these methods as private methods in the io library, so I'll just have to check the io tty-ness just in case
This commit is contained in:
payonel 2017-07-07 17:28:47 -07:00
parent fabbb630d4
commit d753d83bb4
6 changed files with 22 additions and 25 deletions

View File

@ -12,7 +12,8 @@ end
shell.prime()
local update_gpu = io.output().tty
local interactive = io.input().tty
local needs_profile = io.input().tty
local input_handler = {hint = sh.hintHandler}
if #args == 0 then
while true do
@ -20,14 +21,13 @@ if #args == 0 then
while not tty.isAvailable() do
event.pull("term_available")
end
if interactive == true then -- first time run AND interactive
interactive = 0
tty.setReadHandler({hint = sh.hintHandler})
if needs_profile then -- first time run AND interactive
needs_profile = nil
dofile("/etc/profile.lua")
end
io.write(sh.expand(os.getenv("PS1") or "$ "))
end
local command = io.read()
local command = tty:read(input_handler)
if command then
command = text.trim(command)
if command == "exit" then

View File

@ -3,8 +3,10 @@ local tty = require("tty")
local fs = require("filesystem")
if tty.isAvailable() then
tty:write("\27[40m\27[37m")
tty.clear()
if io.stdout.tty then
io.write("\27[40m\27[37m")
tty.clear()
end
tty.setCursorBlink(true)
end
dofile("/etc/motd")

View File

@ -68,7 +68,7 @@ local function findKeys(t, r, prefix, name)
end
end
tty.setReadHandler({hint = function(line, index)
local read_handler = {hint = function(line, index)
line = (line or "")
local tail = line:sub(index)
line = line:sub(1, index - 1)
@ -85,7 +85,7 @@ tty.setReadHandler({hint = function(line, index)
table.insert(hints, key .. tail)
end
return hints
end})
end}
io.write("\27[37m".._VERSION .. " Copyright (C) 1994-2017 Lua.org, PUC-Rio\n")
io.write("\27[33mEnter a statement and hit enter to evaluate it.\n")
@ -94,7 +94,7 @@ io.write("Press Ctrl+D to exit the interpreter.\n\27[37m")
while tty.isAvailable() do
io.write(env._PROMPT)
local command = io.read()
local command = tty:read(read_handler)
if not command then -- eof
return
end

View File

@ -24,7 +24,6 @@ end
local function saveConfig()
local root = filesystem.get("/")
if root and not root.isReadOnly() then
filesystem.makeDirectory("/etc")
local f = filesystem.open("/etc/filesystem.cfg", "w")
if f then
f:write("autorun="..tostring(isAutorunEnabled))

View File

@ -194,9 +194,6 @@ function term.write(value, wrap)
end
function term.read(history, dobreak, hint, pwchar, filter)
if not io.stdin.tty then
return io.read("*L")
end
history = history or {}
local handler = history
handler.hint = handler.hint or hint

View File

@ -3,7 +3,6 @@ local event = require("event")
local kb = require("keyboard")
local component = require("component")
local computer = require("computer")
local process = require("process")
local keys = kb.keys
local tty = {}
@ -19,11 +18,6 @@ tty.window =
tty.internal = {}
function tty.setReadHandler(handler)
checkArg(1, handler, "table")
process.info().data.handler = handler
end
function tty.key_down_handler(handler, cursor, char, code)
local data = cursor.data
local c = false
@ -268,15 +262,17 @@ function tty.internal.build_vertical_reader()
end
-- read n bytes, n is unused
function tty.read(_, handler, cursor)
function tty.read(self, handler, cursor)
checkArg(1, handler, "table", "number")
checkArg(2, cursor, "table", "nil")
if type(handler) == "number" then
-- standard read as a stream, asking for n bytes
handler = process.info().data.handler or {}
if not io.stdin.tty or io.stdin.stream ~= self then
return io.stdin:readLine(false)
end
if type(handler) ~= "table" then
handler = {}
end
handler.index = 0
cursor = cursor or tty.internal.build_vertical_reader()
@ -324,7 +320,10 @@ function tty.setCursor(x, y)
window.x, window.y = x, y
end
function tty.write(_, value)
function tty.write(self, value)
if not io.stdout.tty or io.stdout.stream ~= self then
return io.write(value)
end
local gpu = tty.gpu()
if not gpu then
return