lua shell

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

lua shell

Mildred Ki'Lya
Hi,

I just created a module for lua 5.1.

It is an interactive shell that can be used with debug.debug() or when
lua is in interactive mode. Its purpose is to easily know the structure
of tables.

In fact, it is like a unix shell where tables are folders and others
variables the files. To show you how ot works, there is a copy of a
session :

mildred@louve:~$ lua
Lua 5.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
> require("shell").maxcol = 74 -- for the email. default=80
> require("shell").new():shell()
[/root table]$ ll
string/ xpcall* package/        tostring*       print*  os/     unpack*
require*        getfenv*        setmetatable*   next*   assert*
tonumber* io/     rawequal*               collectgarbage*
getmetatable*   module* rawset* math/   debug/          pcall*
table/  newproxy* type*   coroutine/              _G/
select* gcinfo* pairs* rawget* loadstring*             ipairs*
_VERSION        dofile* setfenv*        load*           error*
loadfile* [/root table]$ cd os
[/root/os table]$ ls
exit    setlocale       date    getenv  difftime        remove  time
clock   tmpname         rename  execute
[/root/os table]$ cat date
function: 0x806c9f8
[/root/os table]$ cdup
[/root table]$ cd _VERSION
[/root/_VERSION string]$ cat
Lua 5.1
[/root/_VERSION string]$ cdmt
[/root/_VERSION/*mt table]$ ll
__index/
[/root/_VERSION/*mt table]$ cd __index
[/root/_VERSION/*mt/__index table]$ ll
sub*    upper*  len*    gfind*  rep*    find*   match*  char*   dump*
gmatch* reverse*        byte*   format* gsub*   lower*
[/root/_VERSION/*mt/__index table]$ pwd
/root/_VERSION/*mt/__index
[/root/_VERSION/*mt/__index table]$ help
Usage: help [COMMAND]
Usage: man [COMMAND]
commands : ls ll pwd ty cd cdup cdmt goto cat echo eval exit end
[/root/_VERSION/*mt/__index table]$ cdup
[/root/_VERSION/*mt table]$ cdup
[/root/_VERSION string]$ cdup
[/root table]$ cdup
error : You are in root directory, you can't cdup
[/root table]$ goto 12
[/root/*12 number]$ cat
12
[/root/*12 number]$ goto *_G
[/root/*12/*table: 0x8069450 table]$ ls
string  xpcall  package tostring        print   os      unpack  require
getfenv setmetatable    next            assert  tonumber        io
rawequal        collectgarbage          getmetatable    module  rawset
math    debug   pcall   table           newproxy        type
coroutine _G      select  gcinfo  pairs           rawget
loadstring      ipairs _VERSION        dofile  setfenv         load
error   loadfile [/root/*12/*table: 0x8069450 table]$ cdup
[/root/*12 number]$ ty
number
[/root/*12 number]$ exit
>
mildred@louve:~$


The attached file is the module :)

Mildred
--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]


--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]

shell.lua (11K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: lua shell

Mildred Ki'Lya

I already made some changes :) in the prompt (which you can change now)
and in the path display.

Aviable there:

http://louve.dyndns.org/projets/lua/viewsource/my-lua-modules/5.1/shell.lua?action=view
http://mildred632.free.fr/projets/lua/viewsource/my-lua-modules/5.1/shell.lua?action=view

Mildred
--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: lua shell

Bret Victor
In reply to this post by Mildred Ki'Lya
Mildred <ml.mildred593 <at> online.fr> writes:

> I just created a module for lua 5.1.
> It is an interactive shell that can be used with debug.debug() or when
> lua is in interactive mode. Its purpose is to easily know the structure
> of tables.

Oh man, that is hilarious.

I figured that we needed to complete the circle, so I made a little module
that represents the actual filesystem as Lua tables.  Now we can use your
shell to cd into actual directories and cat actual files!

--------------------------------------------------------------------------
--
-- "Mounts" the filesystem root in the "files" global.  Directories
-- are represented as tables, and files are strings.
--
-- for filename in pairs(files) do print(filename) end
-- local secrets = files.Users.bret["secrets.txt"]
--
--------------------------------------------------------------------------

local lfs = require "lfs"
local yield = coroutine.yield

local dirpaths = {}  -- Maps dir proxy table to pathname, and vice-versa.
local dir_mt = {}    -- Metatable for dir proxy tables.

local function dirProxy (pathname) -- Make new proxy if one doesn't exist.
    local dir = dirpaths[pathname] or setmetatable({}, dir_mt)
    dirpaths[dir] = pathname
    dirpaths[pathname] = dir
    return dir
end

dir_mt.__index = function (t,k)  -- Read file or directory.
    local pathname = dirpaths[t] .. "/" .. k
    local mode = lfs.attributes(pathname,"mode")
    if mode == "directory" then return dirProxy(pathname)
    elseif mode == "file" then
        local file = io.open(pathname)
        if not file then return end
        local contents = file:read("*a")
        file:close()
        return contents
    end
end

local function dirNext (t) -- Iterate over directory (but don't read files)
    local dirpath = dirpaths[t] .. "/"
    for filename in lfs.dir(dirpath) do
        local pathname = dirpath .. filename
        local mode = lfs.attributes(pathname,"mode")
        if mode == "file" then yield(filename, "Your data here.")
        elseif mode == "directory" then yield(filename, dirProxy(pathname))
        end
    end
end

-- Let us say a prayer for the __pairs metamethod.
local oldpairs = pairs
local function newpairs (t)  -- Override pairs so we can iterate over dirs.
    if dirpaths[t] then return coroutine.wrap(dirNext), t, nil end
    return oldpairs(t)
end

-- Exports.
files = dirProxy("")  -- filesystem root
pairs = newpairs