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

View File

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

View File

@ -68,7 +68,7 @@ local function findKeys(t, r, prefix, name)
end end
end end
tty.setReadHandler({hint = function(line, index) local read_handler = {hint = function(line, index)
line = (line or "") line = (line or "")
local tail = line:sub(index) local tail = line:sub(index)
line = line:sub(1, index - 1) line = line:sub(1, index - 1)
@ -85,7 +85,7 @@ tty.setReadHandler({hint = function(line, index)
table.insert(hints, key .. tail) table.insert(hints, key .. tail)
end end
return hints return hints
end}) end}
io.write("\27[37m".._VERSION .. " Copyright (C) 1994-2017 Lua.org, PUC-Rio\n") 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") 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 while tty.isAvailable() do
io.write(env._PROMPT) io.write(env._PROMPT)
local command = io.read() local command = tty:read(read_handler)
if not command then -- eof if not command then -- eof
return return
end end

View File

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

View File

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

View File

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