Merge branch 'master-MC1.8.9' into master-MC1.9.4

This commit is contained in:
payonel 2017-09-17 11:48:14 +02:00
commit 55f8381e67
5 changed files with 70 additions and 72 deletions

View File

@ -7,7 +7,7 @@ if #args == 0 then
args = {"-"} args = {"-"}
end end
local input_method, input_param = "read", 2048 local input_method, input_param = "read", require("tty").window.width
for i = 1, #args do for i = 1, #args do
local arg = shell.resolve(args[i]) local arg = shell.resolve(args[i])

View File

@ -1,11 +1,7 @@
local buffer = require("buffer")
local keyboard = require("keyboard") local keyboard = require("keyboard")
local shell = require("shell") local shell = require("shell")
local term = require("term") -- TODO use tty and cursor position instead of global area and gpu local tty = require("tty")
local text = require("text")
if not io.output().tty then
return loadfile(shell.resolve("cat", "lua"), "bt", _G)(...)
end
local args = shell.parse(...) local args = shell.parse(...)
if #args > 1 then if #args > 1 then
@ -13,55 +9,49 @@ if #args > 1 then
io.write("- or no args reads stdin\n") io.write("- or no args reads stdin\n")
return 1 return 1
end end
local arg = args[1] or "-"
local file, reason local function clear_line()
if arg == "-" then tty.window.x = 1 -- move cursor to start of line
file, reason = io.stdin, "this process has no stdin" io.write("\27[2K") -- clear line
else
file, reason = io.open(shell.resolve(arg))
end
if not file then
io.stderr:write(reason,'\n')
return 1
end end
local line = nil if io.output().tty then
local function readlines(num) io.write("\27[2J\27[H")
local _, _, w, h = term.getGlobalArea()
num = num or (h - 1) local intercept_active = true
for _ = 1, num do local original_stream = io.stdout.stream
if not line then local custom_stream = setmetatable({
line = file:read("*l") scroll = function(...)
if not line then -- eof local _, height, _, _, _, y = tty.getViewport()
return nil local lines_below = height - y
if intercept_active and lines_below < 1 then
intercept_active = false
original_stream.scroll(-lines_below) -- if zero no scroll action is made [good]
tty.setCursor(1, height) -- move to end
clear_line()
io.write(":") -- status
local _, _, _, code = original_stream:pull(nil, "key_down") -- nil timeout is math.huge
if code == keyboard.keys.q then
clear_line()
os.exit(1) -- abort
elseif code == keyboard.keys["end"] then
io.stdout.stream.scroll = nil -- remove handler
elseif code == keyboard.keys.space or code == keyboard.keys.pageDown then
io.write("\27[2J\27[H") -- clear whole screen, get new page drawn; move cursor to 1,1
elseif code == keyboard.keys.enter or code == keyboard.keys.down then
clear_line() -- remove status bar
original_stream.scroll(1) -- move everything up one
tty.setCursor(1, height - 1)
end
intercept_active = true
end end
return original_stream.scroll(...)
end end
local wrapped }, {__index=original_stream})
wrapped, line = text.wrap(text.detab(line), w, w)
io.write(wrapped,"\n") local custom_output_buffer = buffer.new("w", custom_stream)
end custom_output_buffer:setvbuf("no")
term.setCursor(1, h) io.output(custom_output_buffer)
term.write(":")
return true
end end
while true do return loadfile(shell.resolve("cat", "lua"))(...)
term.clear()
if not readlines() then
return
end
while true do
local _, _, _, code = term.pull("key_down")
if code == keyboard.keys.q then
term.clearLine()
return
elseif code == keyboard.keys.space or code == keyboard.keys.pageDown then
break
elseif code == keyboard.keys.enter or code == keyboard.keys.down then
term.clearLine()
if not readlines(1) then
return
end
end
end
end

View File

@ -248,5 +248,12 @@ function term.bind(gpu, window)
return as_window(window, tty.bind, gpu) return as_window(window, tty.bind, gpu)
end end
return term function term.scroll(...)
if io.stdout.tty then
return io.stdout.stream.scroll(...)
end
end
term.internal.run_in_window = as_window
return term

View File

@ -60,17 +60,18 @@ function thread.waitForAll(threads, timeout)
end end
local box_thread = {} local box_thread = {}
local box_thread_handle = {close = thread.waitForAll} local box_thread_list = {close = thread.waitForAll}
local function get_box_thread_handle(handles, bCreate) local function get_process_threads(proc, bCreate)
local handles = proc.data.handles
for _,next_handle in ipairs(handles) do for _,next_handle in ipairs(handles) do
local btm_mt = getmetatable(next_handle) local handle_mt = getmetatable(next_handle)
if btm_mt and btm_mt.__index == box_thread_handle then if handle_mt and handle_mt.__index == box_thread_list then
return next_handle return next_handle
end end
end end
if bCreate then if bCreate then
local btm = setmetatable({}, {__index = box_thread_handle}) local btm = setmetatable({}, {__index = box_thread_list})
table.insert(handles, btm) table.insert(handles, btm)
return btm return btm
end end
@ -126,10 +127,10 @@ function box_thread:attach(parent)
if mt.attached == proc then return self end -- already attached if mt.attached == proc then return self end -- already attached
if mt.attached then if mt.attached then
local prev_btHandle = assert(get_box_thread_handle(mt.attached.data.handles), "thread panic: no thread handle") local prev_threads = assert(get_process_threads(mt.attached), "thread panic: no thread handle")
for i,h in ipairs(prev_btHandle) do for index,t_in_list in ipairs(prev_threads) do
if h == self then if t_in_list == self then
table.remove(prev_btHandle, i) table.remove(prev_threads, index)
break break
end end
end end
@ -141,9 +142,9 @@ function box_thread:attach(parent)
-- attach to parent or the current process -- attach to parent or the current process
mt.attached = proc mt.attached = proc
-- this process may not have a box_thread manager handle -- this process may not have a box_thread list
local btHandle = get_box_thread_handle(proc.data.handles, true) local threads = get_process_threads(proc, true)
table.insert(btHandle, self) table.insert(threads, self)
-- register on the new parent -- register on the new parent
if waiting_handler then -- event-waiting if waiting_handler then -- event-waiting
@ -158,7 +159,7 @@ function thread.current()
local thread_root local thread_root
while proc do while proc do
if thread_root then if thread_root then
for _,bt in ipairs(get_box_thread_handle(proc.data.handles) or {}) do for _,bt in ipairs(get_process_threads(proc) or {}) do
if bt.pco.root == thread_root then if bt.pco.root == thread_root then
return bt return bt
end end
@ -287,10 +288,10 @@ function thread.create(fp, ...)
if t:status() == "dead" then if t:status() == "dead" then
return return
end end
local htm = get_box_thread_handle(mt.attached.data.handles) local threads = get_process_threads(mt.attached)
for _,ht in ipairs(htm) do for index,t_in_list in ipairs(threads) do
if ht == t then if t_in_list == t then
table.remove(htm, _) table.remove(threads, index)
break break
end end
end end

View File

@ -577,7 +577,7 @@ do
checkArg(3, repl, "number", "string", "function", "table") checkArg(3, repl, "number", "string", "function", "table")
checkArg(4, n, "number", "nil") checkArg(4, n, "number", "nil")
if #s < SHORT_STRING or type(repl) == "function" then if #s < SHORT_STRING then
return string_gsub(s, pattern, repl, n) return string_gsub(s, pattern, repl, n)
end end