Minor documentation and restructure.
This commit is contained in:
parent
aa4ca9d67d
commit
fe1c166044
67
pretty.lua
67
pretty.lua
|
@ -136,31 +136,39 @@ local function get_key_value_pairs_in_proper_order (t)
|
||||||
-- Error checking
|
-- Error checking
|
||||||
assert(type(t) == 'table')
|
assert(type(t) == 'table')
|
||||||
|
|
||||||
-- Do stuff
|
-- Find Key-Value Pairs
|
||||||
local key_value_pairs = {}
|
local kv_pairs = {}
|
||||||
|
for key, value in pairs(t) do kv_pairs[#kv_pairs+1] = { key, value } end
|
||||||
|
|
||||||
for key, value in pairs(t) do
|
-- Sort them into the correct order
|
||||||
key_value_pairs[#key_value_pairs+1] = { key, value }
|
table.sort(kv_pairs, compare_key_value_pair)
|
||||||
end
|
|
||||||
|
|
||||||
table.sort(key_value_pairs, compare_key_value_pair)
|
-- Return them
|
||||||
|
return kv_pairs
|
||||||
return key_value_pairs
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function fill_holes_in_key_value_pairs (key_value_pairs)
|
local function fill_holes_in_key_value_pairs (kv_pairs)
|
||||||
-- NOTE: Assumes that all keys are numbers
|
-- Fills holes in key-value pairs for a sequences with `nil` values. All
|
||||||
|
-- keys must be numbers, and key-value pairs must be sorted already.
|
||||||
|
|
||||||
|
-- Holes can sometimes appear in otherwise nicely structured sequences. We
|
||||||
|
-- want to avoid displaying a sequence as `{[1] = 1, [3] = 3}` when
|
||||||
|
-- `{1, nil, 3}` would work nicely.
|
||||||
|
|
||||||
-- Error checking
|
-- Error checking
|
||||||
assert(type(key_value_pairs) == 'table')
|
assert(type(kv_pairs) == 'table')
|
||||||
|
|
||||||
-- Do stuff
|
-- Add hole filling value
|
||||||
for i = 2, #key_value_pairs do
|
assert(type(kv_pairs[1][1]) == 'number')
|
||||||
for j = key_value_pairs[i-1][1] + 1, key_value_pairs[i][1] - 1 do
|
for i = 2, #kv_pairs do
|
||||||
key_value_pairs[#key_value_pairs+1] = { j, nil }
|
assert(type(kv_pairs[i][1]) == 'number')
|
||||||
|
for j = kv_pairs[i-1][1] + 1, kv_pairs[i][1] - 1 do
|
||||||
|
kv_pairs[#kv_pairs+1] = { j, nil }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.sort(key_value_pairs, compare_key_value_pair)
|
|
||||||
|
-- Sort key-value pairs, to place above pairs into their correct locations.
|
||||||
|
table.sort(kv_pairs, compare_key_value_pair)
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -378,20 +386,16 @@ local analyze_structure = import 'analyze_structure'
|
||||||
local TABLE_TYPE = import 'table_type'
|
local TABLE_TYPE = import 'table_type'
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Formatting stuff
|
-- Key-value pair formatting.
|
||||||
|
|
||||||
local format_value
|
local function format_key_and_value_string_map (key, value, depth, l, format_value)
|
||||||
|
|
||||||
-- Ways to format keys
|
|
||||||
|
|
||||||
local function format_key_and_value_string_map (l, key, value, depth)
|
|
||||||
l[#l+1] = key
|
l[#l+1] = key
|
||||||
l[#l+1] = { 'align', 'key', #key }
|
l[#l+1] = { 'align', 'key', #key }
|
||||||
l[#l+1] = ' = '
|
l[#l+1] = ' = '
|
||||||
return format_value(value, depth, l)
|
return format_value(value, depth, l)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function format_key_and_value_arbitr_map (l, key, value, depth)
|
local function format_key_and_value_arbitr_map (key, value, depth, l, format_value)
|
||||||
local index_before_key = #l+1
|
local index_before_key = #l+1
|
||||||
l[#l+1] = '['
|
l[#l+1] = '['
|
||||||
format_value(key, math.huge, l)
|
format_value(key, math.huge, l)
|
||||||
|
@ -401,7 +405,7 @@ local function format_key_and_value_arbitr_map (l, key, value, depth)
|
||||||
return format_value(value, depth, l)
|
return format_value(value, depth, l)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function format_key_and_value_sequence (l, key, value, depth)
|
local function format_key_and_value_sequence (key, value, depth, l, format_value)
|
||||||
return format_value(value, depth, l)
|
return format_value(value, depth, l)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -414,9 +418,10 @@ local TABLE_TYPE_TO_PAIR_FORMAT = {
|
||||||
[TABLE_TYPE.PURE_MAP] = format_key_and_value_arbitr_map,
|
[TABLE_TYPE.PURE_MAP] = format_key_and_value_arbitr_map,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Formatting tables
|
-------------------------------------------------------------------------------
|
||||||
|
-- Table formatting
|
||||||
|
|
||||||
local function format_table (t, depth, l)
|
local function format_table (t, depth, l, format_value)
|
||||||
-- Error Checking
|
-- Error Checking
|
||||||
assert(type(t) == 'table')
|
assert(type(t) == 'table')
|
||||||
assert(type(depth) == 'number' and type(l) == 'table')
|
assert(type(depth) == 'number' and type(l) == 'table')
|
||||||
|
@ -455,7 +460,7 @@ local function format_table (t, depth, l)
|
||||||
l[#l+1] = {'indent', '{'}
|
l[#l+1] = {'indent', '{'}
|
||||||
if l.options._table_addr_comment then l[#l+1], l[#l+2] = '--[['..table_info.address..']]', {'seperator'} end
|
if l.options._table_addr_comment then l[#l+1], l[#l+2] = '--[['..table_info.address..']]', {'seperator'} end
|
||||||
for _, pair in ipairs(key_value_pairs) do
|
for _, pair in ipairs(key_value_pairs) do
|
||||||
pair_format_func(l, pair[1], pair[2], depth + 1)
|
pair_format_func(pair[1], pair[2], depth + 1, l, format_value)
|
||||||
l[#l+1] = {'seperator', ','}
|
l[#l+1] = {'seperator', ','}
|
||||||
end
|
end
|
||||||
if l[#l][1] == 'seperator' then l[#l] = nil end
|
if l[#l][1] == 'seperator' then l[#l] = nil end
|
||||||
|
@ -480,7 +485,8 @@ local function format_table (t, depth, l)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Formatting coroutine
|
-------------------------------------------------------------------------------
|
||||||
|
-- Coroutine formatting
|
||||||
|
|
||||||
local function format_coroutine (value, depth, l)
|
local function format_coroutine (value, depth, l)
|
||||||
-- Formats a coroutine. Unfortunantly we cannot gather a lot of information
|
-- Formats a coroutine. Unfortunantly we cannot gather a lot of information
|
||||||
|
@ -496,7 +502,8 @@ local function format_coroutine (value, depth, l)
|
||||||
l[#l+1] = tostring(value):sub(9)
|
l[#l+1] = tostring(value):sub(9)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Formatting primitive
|
-------------------------------------------------------------------------------
|
||||||
|
-- Primitive formatting
|
||||||
|
|
||||||
local function format_primitive (value, depth, l)
|
local function format_primitive (value, depth, l)
|
||||||
-- Error check
|
-- Error check
|
||||||
|
@ -518,7 +525,7 @@ local TYPE_TO_FORMAT_FUNC = {
|
||||||
['cdata'] = format_primitive, -- TODO & Luajit only
|
['cdata'] = format_primitive, -- TODO & Luajit only
|
||||||
}
|
}
|
||||||
|
|
||||||
function format_value (value, depth, l)
|
local function format_value (value, depth, l)
|
||||||
assert(type(depth) == 'number' and type(l) == 'table')
|
assert(type(depth) == 'number' and type(l) == 'table')
|
||||||
local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
|
local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
|
||||||
--print(value, formatting)
|
--print(value, formatting)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user