From 88ce07859df4bf833fc35d38f22b809db55ba8e4 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Mon, 18 Jul 2022 18:25:24 +0200 Subject: [PATCH] feat: add programs directory in path --- install.lua | 19 +++++++++--- cube.lua => programs/cube.lua | 51 ++++++++++++++++++++++++++++--- ping.lua => programs/ping.lua | 0 router.lua => programs/router.lua | 0 startup/servers.lua | 6 ++++ 5 files changed, 67 insertions(+), 9 deletions(-) rename cube.lua => programs/cube.lua (77%) rename ping.lua => programs/ping.lua (100%) rename router.lua => programs/router.lua (100%) diff --git a/install.lua b/install.lua index 2b4ad69..6daf42e 100644 --- a/install.lua +++ b/install.lua @@ -1,16 +1,26 @@ -local _VERSION = '1.0.0' +local _VERSION = '1.1.0' local LIST_FILES = { + -- startup 'startup/servers.lua', + -- servers 'servers/ping-server.lua', - 'ping.lua', - 'router.lua', + 'servers/cube-server.lua', + 'servers/cube-startup.lua', + -- programs + 'programs/router.lua', -- router is not in servers folder because he's not ran on every machines + 'programs/ping.lua', + 'programs/cube.lua', + -- apis 'apis/net.lua', 'apis/eventloop.lua', }; -- remove old files -fs.delete('ping-server.lua') +fs.delete('ping-server.lua'); +fs.delete('ping.lua') +fs.delete('cube.lua') +fs.delete('router.lua') local REPO_PREFIX = 'https://raw.githubusercontent.com/guillaumearm/cc-libs/master/' @@ -18,6 +28,7 @@ local previousDir = shell.dir() shell.setDir('/') +fs.makeDir('/programs'); fs.makeDir('/apis'); fs.makeDir('/startup'); diff --git a/cube.lua b/programs/cube.lua similarity index 77% rename from cube.lua rename to programs/cube.lua index 5403b2d..305e975 100644 --- a/cube.lua +++ b/programs/cube.lua @@ -5,6 +5,38 @@ local net = require('/apis/net')(); local cubeCommand, firstArg, secondArg = ...; +--- Pads str to length len with char from right +local leftPad = function(str, len, char) + if char == nil then char = ' ' end + local nbRepetition = len - #str; + + if nbRepetition > 0 then + return str .. string.rep(char, len - #str) + end + + return str; +end + +--- Pads str to length len with char from left +local rightPad = function(str, len, char) + if char == nil then char = ' ' end + local nbRepetition = len - #str; + + if nbRepetition > 0 then + return string.rep(char, len - #str) .. str + end + + return str; +end + +local function getRow(str1, str2, str3) + local row1 = leftPad(tostring(str1 or ''), 6, ' ') + local row2 = leftPad(tostring(str2 or ''), 16, ' ') + local row3 = leftPad(tostring(str3 or ''), 6, ' ') + + return row1 .. row2 .. row3; +end + local function isFlag(name) return function(arg) return arg == '-' .. name or arg == '--' .. name; @@ -138,15 +170,24 @@ local COMMANDS = { error(results); end + -- print('ID LABEL\t\t\t\tSTARTUP'); + print(getRow('ID', 'LABEL', 'STARTUP')) + for k in ipairs(results) do local result = results[k]; local packet = packets[k]; - print("=> " .. tostring(packet.sourceId) - .. - ( - packet.sourceLabel and " (label=" .. tostring(packet.sourceLabel) .. ")" or - "") .. ": startup='" .. result.startup .. "'"); + + -- local row1 = leftPad(tostring(packet.sourceId or ''), 8, ' ') + -- local row2 = leftPad(tostring(packet.sourceLabel or ''), 12, ' ') + -- local row3 = leftPad(tostring(result.startup or ''), 12, ' ') + -- print(packet.sourceId, packet.sourceLabel or '', result.startup) + print(getRow(packet.sourceId, packet.sourceLabel, result.startup)) + -- print("=> " .. tostring(packet.sourceId) + -- .. + -- ( + -- packet.sourceLabel and " (label=" .. tostring(packet.sourceLabel) .. ")" or + -- "") .. ": startup='" .. result.startup .. "'"); end end, configure = function() diff --git a/ping.lua b/programs/ping.lua similarity index 100% rename from ping.lua rename to programs/ping.lua diff --git a/router.lua b/programs/router.lua similarity index 100% rename from router.lua rename to programs/router.lua diff --git a/startup/servers.lua b/startup/servers.lua index defa5c7..055d5b5 100644 --- a/startup/servers.lua +++ b/startup/servers.lua @@ -6,6 +6,12 @@ local SERVERS = { "servers/cube-startup.lua", }; +local function init() + shell.setPath(shell.path() .. ':/programs'); +end + +init(); + local periphEmulation = function() -- attach modem periphemu.create('top', 'modem');