diff options
| author | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-06-27 21:17:27 +0000 |
|---|---|---|
| committer | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-06-27 21:17:27 +0000 |
| commit | e98dc61d71dced7929e6012e31905925d0eaaa91 (patch) | |
| tree | 7fe695a24e39adc76920f539fb38b4b2656d7de4 /src | |
| parent | 4cd417b8abaccea3bd08bd50b8db2af9b63371f4 (diff) | |
| download | luarocks-e98dc61d71dced7929e6012e31905925d0eaaa91.tar.gz luarocks-e98dc61d71dced7929e6012e31905925d0eaaa91.tar.bz2 luarocks-e98dc61d71dced7929e6012e31905925d0eaaa91.zip | |
prettier output
git-svn-id: http://luarocks.org/svn/luarocks/trunk@29 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/persist.lua | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index 4f69184c..2bfd5765 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua | |||
| @@ -33,22 +33,30 @@ end | |||
| 33 | -- are keys (tables are handled recursively). | 33 | -- are keys (tables are handled recursively). |
| 34 | -- @param out userdata: a file object, open for writing. | 34 | -- @param out userdata: a file object, open for writing. |
| 35 | -- @param tbl table: the table to be written. | 35 | -- @param tbl table: the table to be written. |
| 36 | local function write_table(out, tbl) | 36 | local function write_table(out, tbl, level) |
| 37 | out:write("{") | 37 | out:write("{") |
| 38 | local size = table.getn(tbl) | 38 | local size = table.getn(tbl) |
| 39 | local sep = "" | 39 | local sep = "\n" |
| 40 | local indent = true | ||
| 40 | local i = 1 | 41 | local i = 1 |
| 41 | for k, v in pairs(tbl) do | 42 | for k, v in pairs(tbl) do |
| 42 | out:write(sep) | 43 | out:write(sep) |
| 44 | if indent then | ||
| 45 | for n = 1,level do out:write(" ") end | ||
| 46 | end | ||
| 47 | sep = ",\n" | ||
| 48 | indent = true | ||
| 43 | if type(k) == "number" then | 49 | if type(k) == "number" then |
| 44 | if k ~= i then | 50 | if k ~= i then |
| 45 | out:write(tostring(k).."=") | 51 | out:write(tostring(k).."=") |
| 46 | else | 52 | else |
| 47 | i = i + 1 | 53 | i = i + 1 |
| 48 | end | 54 | end |
| 55 | indent = false | ||
| 56 | sep = ", " | ||
| 49 | elseif type(k) == "table" then | 57 | elseif type(k) == "table" then |
| 50 | out:write("[") | 58 | out:write("[") |
| 51 | write_table(out, k) | 59 | write_table(out, k, level + 1) |
| 52 | out:write("]=") | 60 | out:write("]=") |
| 53 | else | 61 | else |
| 54 | if k:match("^[a-z_]+$") then | 62 | if k:match("^[a-z_]+$") then |
| @@ -57,17 +65,19 @@ local function write_table(out, tbl) | |||
| 57 | out:write("['"..k:gsub("'", "\\'").."']=") | 65 | out:write("['"..k:gsub("'", "\\'").."']=") |
| 58 | end | 66 | end |
| 59 | end | 67 | end |
| 60 | local typ = type(v) | 68 | if type(v) == "table" then |
| 61 | if typ == "table" then | 69 | write_table(out, v, level + 1) |
| 62 | write_table(out, v) | 70 | elseif type(v) == "string" then |
| 63 | elseif typ == "string" then | ||
| 64 | out:write("'"..v:gsub("'", "\\'").."'") | 71 | out:write("'"..v:gsub("'", "\\'").."'") |
| 65 | else | 72 | else |
| 66 | out:write(tostring(v)) | 73 | out:write(tostring(v)) |
| 67 | end | 74 | end |
| 68 | sep = ", " | ||
| 69 | end | 75 | end |
| 70 | out:write("}\n") | 76 | if sep ~= "\n" then |
| 77 | out:write("\n") | ||
| 78 | for n = 1,level-1 do out:write(" ") end | ||
| 79 | end | ||
| 80 | out:write("}") | ||
| 71 | end | 81 | end |
| 72 | 82 | ||
| 73 | --- Save the contents of a table in a file. | 83 | --- Save the contents of a table in a file. |
| @@ -83,7 +93,7 @@ function save_from_table(filename, tbl) | |||
| 83 | end | 93 | end |
| 84 | for k, v in pairs(tbl) do | 94 | for k, v in pairs(tbl) do |
| 85 | out:write(k.." = ") | 95 | out:write(k.." = ") |
| 86 | write_table(out, v) | 96 | write_table(out, v, 1) |
| 87 | out:write("\n") | 97 | out:write("\n") |
| 88 | end | 98 | end |
| 89 | out:close() | 99 | out:close() |
