shellex: fix aliasing and colorize grep

This commit is contained in:
kepler155c@gmail.com
2019-05-13 09:30:27 -04:00
parent 9f3070bbdc
commit 180f0f9b93
5 changed files with 132 additions and 26 deletions

96
ignore/plc.lua Normal file
View File

@@ -0,0 +1,96 @@
local os = _G.os
local peripheral = _G.peripheral
local running, modemSide
local tasks = { }
local sides = { 'front', 'back', 'left', 'right', 'top', 'bottom' }
for _,v in pairs(sides) do
if peripheral.getType(v) == 'modem' and not peripheral.call(v, 'isWireless') then
modemSide = v
break
end
end
if not modemSide then
error('Must be conncted to a wired modem')
end
print('Using modem at side: ' .. modemSide)
peripheral.call(modemSide, 'open', 42)
local function addTask(fn)
local task
task = {
co = coroutine.create(fn)
}
table.insert(tasks, task)
return task
end
local function run()
if #tasks > 0 then
local event = { }
while true do
for n = #tasks, 1, -1 do
local task = tasks[n]
if coroutine.status(task.co) == 'dead' then
table.remove(tasks, n)
elseif task.filter == nil or task.filter == event[1] or event[1] == "terminate" then
local ok, param = coroutine.resume(task.co, table.unpack(event))
if not ok then
_G.printError(param)
else
task.filter = param
end
if coroutine.status(task.co) == 'dead' then
table.remove(tasks, n)
end
end
end
if #tasks == 0 then
break
end
event = { os.pullEventRaw() }
end
end
end
local function startProgram(program)
print('Starting: ' .. program.name)
local sandbox = { }
for k,v in pairs(_ENV) do
sandbox[k] = v
end
local env = setmetatable(sandbox, { __index = _G })
local fn, m = load(program.data, program.name, nil, env)
if fn then
running = addTask(function()
pcall(fn)
print('Ended: ' .. program.name)
end)
else
_G.printError(m)
end
end
addTask(function()
print('Waiting for program...')
while true do
local _, side, dport, dhost, msg = os.pullEvent('modem_message')
if side == modemSide and dport == 42 and dhost == os.getComputerID() then
if running and coroutine.status(running.co) ~= 'dead' then
coroutine.resume(running.co, 'terminate')
end
if msg.type == 'start' then
startProgram(msg)
end
os.queueEvent('dummy')
end
end
end)
run()

View File

@@ -3,9 +3,9 @@
Must be run on a mob with the same height. Must be run on a mob with the same height.
]] ]]
local neural = require('neural.interface') local neural = require('neural.interface')
local Sound = require('sound') local Sound = require('sound')
local Util = require('util') local Map = require('map')
local os = _G.os local os = _G.os
@@ -30,7 +30,7 @@ local function resupply()
end end
print('resupplying') print('resupplying')
for _ = 1, 2 do for _ = 1, 2 do
local dispenser = Util.find(neural.scan(), 'name', 'minecraft:dispenser') local dispenser = Map.find(neural.scan(), 'name', 'minecraft:dispenser')
if not dispenser then if not dispenser then
print('dispenser not found') print('dispenser not found')
break break
@@ -76,7 +76,7 @@ local function kill(entity)
end end
local function getEntities() local function getEntities()
return Util.filter(neural.sense(), function(entity) return Map.filter(neural.sense(), function(entity)
if entity.name == BREEDING and entity.y > -.5 then if entity.name == BREEDING and entity.y > -.5 then
return true return true
end end
@@ -92,7 +92,7 @@ local function getHungry(entities)
end end
local function randomEntity(entities) local function randomEntity(entities)
local r = math.random(1, Util.size(entities)) local r = math.random(1, Map.size(entities))
local i = 1 local i = 1
for _, v in pairs(entities) do for _, v in pairs(entities) do
i = i + 1 i = i + 1
@@ -107,7 +107,7 @@ while true do
local entities = getEntities() local entities = getEntities()
if Util.size(entities) > MAX_GROWN then if Map.size(entities) > MAX_GROWN then
kill(randomEntity(entities)) kill(randomEntity(entities))
else else
local entity = getHungry(entities) local entity = getHungry(entities)

View File

@@ -8,12 +8,23 @@ local cmap = {
[ 0x44CC00 ] = colors.lime, [ 0x44CC00 ] = colors.lime,
[ 0xB0B00F ] = colors.yellow, [ 0xB0B00F ] = colors.yellow,
[ 0xFFFFFF ] = colors.white, [ 0xFFFFFF ] = colors.white,
[ 0xb000b0 ] = colors.purple,
[ 0x00FF00 ] = colors.green,
[ 0xFF0000 ] = colors.red,
[ 0x00FFFF ] = colors.cyan,
[ 0x000000 ] = colors.black,
} }
return { return {
gpu = function() gpu = function()
local current = 0xFFFFFF
return { return {
setForeground = function(c) term.setTextColor(cmap[c]) end, setForeground = function(c)
current = c
term.setTextColor(cmap[c])
end,
getForeground = function() return current end,
} }
end, end,
getViewport = term.getSize, getViewport = term.getSize,

View File

@@ -1,6 +1,4 @@
local Config = require('config') local fs = _G.fs
local kernel = _G.kernel
local os = _G.os local os = _G.os
local settings = _G.settings local settings = _G.settings
@@ -13,12 +11,7 @@ function os.getenv(k)
return settings.get(k) return settings.get(k)
end end
local config = Config.load('shell', { aliases = { } }) fs.mount('rom/programs/list.lua', 'linkfs', 'packages/shellex/ls.lua')
if not config.openOsInit then fs.mount('rom/programs/delete.lua', 'linkfs', 'packages/shellex/rm.lua')
config.openOsInit = true fs.mount('rom/programs/copy.lua', 'linkfs', 'packages/shellex/cp.lua')
for _, alias in pairs({ 'ls', 'rm', 'cp', 'mv' }) do fs.mount('rom/programs/move.lua', 'linkfs', 'packages/shellex/mv.lua')
config.aliases[alias] = nil
kernel.getShell().clearAlias(alias)
end
Config.update('shell', config)
end

View File

@@ -40,7 +40,7 @@ end
local LABEL_COLOR = 0xb000b0 local LABEL_COLOR = 0xb000b0
local LINE_NUM_COLOR = 0x00FF00 local LINE_NUM_COLOR = 0x00FF00
local MATCH_COLOR = 0xFF0000 local MATCH_COLOR = 0xB0B00F
local COLON_COLOR = 0x00FFFF local COLON_COLOR = 0x00FFFF
local function pop(...) local function pop(...)
@@ -115,7 +115,7 @@ local quiet = pop('q','quiet','silent')
local print_count = pop('c','count') local print_count = pop('c','count')
local colorize = pop('color','colour') and io.output().tty and tty.isAvailable() local colorize = pop('color','colour') and io.output().tty and tty.isAvailable()
colorize = true
local noop = function(...)return ...;end local noop = function(...)return ...;end
local setc = colorize and gpu.setForeground or noop local setc = colorize and gpu.setForeground or noop
local getc = colorize and gpu.getForeground or noop local getc = colorize and gpu.getForeground or noop
@@ -203,13 +203,19 @@ local function readLines()
meta.label = file meta.label = file
local file, reason = resolve(file) local file, reason = resolve(file)
if fs.exists(file) then if fs.exists(file) then
curHand, reason = io.open(file, 'r') if fs.isDirectory(file) then
if not curHand then local msg = string.format("%s: Is a directory", meta.label)
local msg = string.format("failed to read from %s: %s", meta.label, reason)
stderr:write("grep: ",msg,"\n") stderr:write("grep: ",msg,"\n")
return false, 2 return false, 2
else else
curFile = meta.label curHand, reason = io.open(file, 'r')
if not curHand then
local msg = string.format("failed to read from %s: %s", meta.label, reason)
stderr:write("grep: ",msg,"\n")
return false, 2
else
curFile = meta.label
end
end end
else else
stderr:write("grep: ",file,": file not found\n") stderr:write("grep: ",file,": file not found\n")