@@ -450,38 +450,44 @@ M.curl_post = async.wrap(function(url, opts, callback)
450450 curl .post (url , args )
451451end , 3 )
452452
453- function M .serializeTable (val , name , skipnewlines , depth )
454- skipnewlines = skipnewlines or false
455- depth = depth or 0
456-
457- local tmp = string.rep (' ' , depth )
458-
459- if name then
460- tmp = tmp .. name .. ' = '
461- end
462-
463- if type (val ) == ' table' then
464- tmp = tmp .. ' {' .. (not skipnewlines and ' \n ' or ' ' )
465-
466- for k , v in pairs (val ) do
467- tmp = tmp .. M .serializeTable (v , k , skipnewlines , depth + 1 ) .. ' ,' .. (not skipnewlines and ' \n ' or ' ' )
453+ function M .to_string (tbl )
454+ -- credit: http://lua-users.org/wiki/TableSerialization (universal tostring)
455+ local function table_print (tt , indent , done )
456+ done = done or {}
457+ indent = indent or 0
458+ if type (tt ) == ' table' then
459+ local sb = {}
460+ for key , value in pairs (tt ) do
461+ table.insert (sb , string.rep (' ' , indent )) -- indent it
462+ if type (value ) == ' table' and not done [value ] then
463+ done [value ] = true
464+ table.insert (sb , key .. ' = {\n ' )
465+ table.insert (sb , table_print (value , indent + 2 , done ))
466+ table.insert (sb , string.rep (' ' , indent )) -- indent it
467+ table.insert (sb , ' }\n ' )
468+ elseif ' number' == type (key ) then
469+ table.insert (sb , string.format (' "%s"\n ' , tostring (value )))
470+ else
471+ table.insert (sb , string.format (' %s = "%s"\n ' , tostring (key ), tostring (value )))
472+ end
473+ end
474+ return table.concat (sb )
475+ else
476+ return tt .. ' \n '
468477 end
478+ end
469479
470- tmp = tmp .. string.rep (' ' , depth ) .. ' }'
471- elseif type (val ) == ' number' then
472- tmp = tmp .. tostring (val )
473- elseif type (val ) == ' string' then
474- tmp = tmp .. string.format (' %q' , val )
475- elseif type (val ) == ' boolean' then
476- tmp = tmp .. (val and ' true' or ' false' )
480+ if ' nil' == type (tbl ) then
481+ return tostring (nil )
482+ elseif ' table' == type (tbl ) then
483+ return table_print (tbl )
484+ elseif ' string' == type (tbl ) then
485+ return tbl
477486 else
478- tmp = tmp .. ' "[inserializeable datatype: ' .. type ( val ) .. ' ]" '
487+ return tostring ( tbl )
479488 end
480-
481- return tmp
482489end
483490
484-
485491local function filter_files (files , max_count )
486492 local filetype = require (' plenary.filetype' )
487493
0 commit comments