new library: luafilesystem (lfs)

https://github.com/keplerproject/luafilesystem

The new commander.tlv app demonstrates it working.
This commit is contained in:
Kartik K. Agaram 2022-01-29 12:11:21 -08:00
parent 90fc24ed04
commit 24f0781d2b
7 changed files with 1425 additions and 1 deletions

View File

@ -158,6 +158,8 @@ behave unexpectedly under Teliva.
## What's included?
* [Lua 5.1](https://www.lua.org/manual/5.1)
* The [Lua File System](https://keplerproject.github.io/luafilesystem) library
for portably accessing directories (module `lfs`).
* The [ncurses](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO) library for
building text-mode user interfaces. ([Alternative documentation](https://tldp.org/LDP/lpg-0.4.pdf))
* The [Kilo](https://github.com/antirez/kilo) text editor, modified to use

199
commander.tlv Normal file
View File

@ -0,0 +1,199 @@
# .tlv file generated by https://github.com/akkartik/teliva
# You may edit it if you are careful; however, you may see cryptic errors if you
# violate Teliva's assumptions.
#
# .tlv files are representations of Teliva programs. Teliva programs consist of
# sequences of definitions. Each definition is a table of key/value pairs. Keys
# and values are both strings.
#
# Lines in .tlv files always follow exactly one of the following forms:
# - comment lines at the top of the file starting with '#' at column 0
# - beginnings of definitions starting with '- ' at column 0, followed by a
# key/value pair
# - key/value pairs consisting of ' ' at column 0, containing either a
# spaceless value on the same line, or a multi-line value
# - multiline values indented by more than 2 spaces, starting with a '>'
#
# If these constraints are violated, Teliva may unceremoniously crash. Please
# report bugs at http://akkartik.name/contact
- __teliva_timestamp: original
str_helpers:
>-- some string helpers from http://lua-users.org/wiki/StringIndexing
>
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return string.sub(str,i,i)
> else
> return string[i]
> end
>end
>
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return string.sub(str,i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=string.sub(str,v,v)
> end
> return table.concat(t)
> end
>end
>
>-- iterate over an ordered sequence
>function q(x)
> if type(x) == 'string' then
> return x:gmatch('.')
> else
> return ipairs(x)
> end
>end
>
>-- insert within string
>function string.insert(str1, str2, pos)
> return str1:sub(1,pos)..str2..str1:sub(pos+1)
>end
>
>function string.remove(s, pos)
> return s:sub(1,pos-1)..s:sub(pos+1)
>end
>
>-- TODO: backport utf-8 support from Lua 5.3
- __teliva_timestamp: original
debugy:
>debugy = 5
- __teliva_timestamp: original
dbg:
>-- helper for debug by print; overlay debug information towards the right
>-- reset debugy every time you refresh screen
>function dbg(window, s)
> local oldy = 0
> local oldx = 0
> oldy, oldx = window:getyx()
> window:mvaddstr(debugy, 60, s)
> debugy = debugy+1
> window:mvaddstr(oldy, oldx, '')
>end
- __teliva_timestamp: original
check_eq:
>function check_eq(x, expected, msg)
> if x == expected then
> curses.addch('.')
> else
> print('F - '..msg)
> print(' expected '..tostring(expected)..' but got '..x)
> teliva_num_test_failures = teliva_num_test_failures + 1
> -- overlay first test failure on editors
> if teliva_first_failure == nil then
> teliva_first_failure = msg
> end
> end
>end
- __teliva_timestamp: original
map:
>-- only for arrays
>function map(l, f)
> result = {}
> for _, x in ipairs(l) do
> table.insert(result, f(x))
> end
> return result
>end
- __teliva_timestamp: original
reduce:
>-- only for arrays
>function reduce(l, f, init)
> result = init
> for _, x in ipairs(l) do
> result = f(result, x)
> end
> return result
>end
- __teliva_timestamp: original
filter:
>-- only for arrays
>function filter(l, f)
> result = {}
> for _, x in ipairs(l) do
> if f(x) then
> table.insert(result, x)
> end
> end
> return result
>end
- __teliva_timestamp: original
find_index:
>function find_index(arr, x)
> for n, y in ipairs(arr) do
> if x == y then
> return n
> end
> end
>end
- __teliva_timestamp: original
trim:
>function trim(s)
> return s:gsub('^%s*', ''):gsub('%s*$', '')
>end
- __teliva_timestamp: original
split:
>function split(s, d)
> result = {}
> for match in (s..d):gmatch("(.-)"..d) do
> table.insert(result, match);
> end
> return result
>end
- __teliva_timestamp: original
window:
>window = curses.stdscr()
- __teliva_timestamp: original
render:
>function render(window)
> window:clear()
> for f in lfs.dir('.') do
> if f ~= '.' and f ~= '..' then
> local attr = lfs.attributes(f)
> print(f, attr.permissions, attr.size, attr.access, attr.modification)
> end
> end
> curses.refresh()
>end
- __teliva_timestamp: original
menu:
>-- To show app-specific hotkeys in the menu bar, add hotkey/command
>-- arrays of strings to the menu array.
>menu = {}
- __teliva_timestamp: original
update:
>function update(window)
> local key = curses.getch()
> -- process key here
>end
- __teliva_timestamp: original
init_colors:
>function init_colors()
> for i=0,7 do
> curses.init_pair(i, i, -1)
> end
> curses.init_pair(8, 7, 0)
> curses.init_pair(9, 7, 1)
> curses.init_pair(10, 7, 2)
> curses.init_pair(11, 7, 3)
> curses.init_pair(12, 7, 4)
> curses.init_pair(13, 7, 5)
> curses.init_pair(14, 7, 6)
> curses.init_pair(15, -1, 15)
>end
- main:
>function main()
> init_colors()
>
> while true do
> render(window)
> update(window)
> end
>end
__teliva_timestamp: original

View File

@ -26,7 +26,7 @@ LUA_A= liblua.a
CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
lundump.o lvm.o lzio.o \
kilo.o tlv.o teliva.o
lfs.o kilo.o tlv.o teliva.o
LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o \
ltablib.o lstrlib.o loadlib.o linit.o
@ -189,6 +189,7 @@ lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \
lzio.h
print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
ltm.h lzio.h lmem.h lopcodes.h lundump.h
lfs.o: lfs.c lfs.h lua.h lauxlib.h lualib.h
kilo.o: kilo.c lua.h teliva.h
teliva.o: teliva.c lua.h lauxlib.h lualib.h teliva.h tlv.h

1183
src/lfs.c Normal file

File diff suppressed because it is too large Load Diff

35
src/lfs.h Normal file
View File

@ -0,0 +1,35 @@
/*
** LuaFileSystem
** Copyright Kepler Project 2003 - 2020
** (http://keplerproject.github.io/luafilesystem)
*/
/* Define 'chdir' for systems that do not implement it */
#ifdef NO_CHDIR
#define chdir(p) (-1)
#define chdir_error "Function 'chdir' not provided by system"
#else
#define chdir_error strerror(errno)
#endif
#ifdef _WIN32
#define chdir(p) (_chdir(p))
#define getcwd(d, s) (_getcwd(d, s))
#define rmdir(p) (_rmdir(p))
#define LFS_EXPORT __declspec (dllexport)
#ifndef fileno
#define fileno(f) (_fileno(f))
#endif
#else
#define LFS_EXPORT
#endif
#ifdef __cplusplus
extern "C" {
#endif
LFS_EXPORT int luaopen_lfs(lua_State * L);
#ifdef __cplusplus
}
#endif

View File

@ -22,6 +22,7 @@ static const luaL_Reg lualibs[] = {
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_LFSLIBNAME, luaopen_lfs},
{LUA_CURSESLIBNAME, luaopen_curses},
{LUA_SOCKETCORELIBNAME, luaopen_socket_core},
{LUA_MIMECORELIBNAME, luaopen_mime_core},

View File

@ -33,6 +33,9 @@ LUALIB_API int (luaopen_string) (lua_State *L);
#define LUA_MATHLIBNAME "math"
LUALIB_API int (luaopen_math) (lua_State *L);
#define LUA_LFSLIBNAME "lfs"
LUALIB_API int (luaopen_lfs) (lua_State *L);
#define LUA_CURSESLIBNAME "curses"
LUALIB_API int (luaopen_curses) (lua_State *L);