1+ -- Execute lua commands interactively or from files.
2+
13local args = {... }
2- if args [1 ]== " --file" or args [1 ]== " -f" then
4+ local cmd = args [1 ]
5+
6+ if cmd == " --file" or cmd == " -f" then
37 local f ,err = loadfile (args [2 ])
48 if f == nil then
59 qerror (err )
610 end
711 dfhack .pcall (f ,table.unpack (args ,3 ))
8- elseif args [ 1 ] == " --save" or args [ 1 ] == " -s" then
12+ elseif cmd == " --save" or cmd == " -s" then
913 if df .global .world .cur_savegame .save_dir == " " then
1014 qerror (" Savefile not loaded" )
1115 end
@@ -16,12 +20,27 @@ elseif args[1]=="--save" or args[1]=="-s" then
1620 qerror (err )
1721 end
1822 dfhack .pcall (f ,table.unpack (args ,3 ))
19- elseif args [1 ]~= nil then
20- local f ,err = load (args [1 ],' =(lua command)' , ' t' )
23+ elseif cmd ~= nil then
24+ -- Support some of the prefixes allowed by dfhack.interpreter
25+ local prefix
26+ if string.match (cmd , " ^[~!]" ) then
27+ prefix = string.sub (cmd , 1 , 1 )
28+ cmd = ' return ' .. string.sub (cmd , 2 )
29+ end
30+
31+ local f ,err = load (cmd ,' =(lua command)' , ' t' )
2132 if f == nil then
2233 qerror (err )
2334 end
24- dfhack .pcall (f ,table.unpack (args ,2 ))
35+
36+ local rv = table.pack (dfhack .safecall (f ,table.unpack (args ,2 )))
37+
38+ if rv [1 ] and prefix then
39+ print (table.unpack (rv ,2 ,rv .n ))
40+ if prefix == ' ~' then
41+ printall (rv [2 ])
42+ end
43+ end
2544else
2645 dfhack .interpreter (" lua" ," lua.history" )
27- end
46+ end
0 commit comments