Working on outphasing the options
value.
This commit is contained in:
parent
192575ded8
commit
7005a080c0
38
pretty.lua
38
pretty.lua
|
@ -301,25 +301,25 @@ local format_value
|
||||||
|
|
||||||
-- Ways to format keys
|
-- Ways to format keys
|
||||||
|
|
||||||
local function format_key_and_value_string_map (l, key, value, options, depth)
|
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, options, depth, l)
|
return format_value(value, nil, depth, l)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function format_key_and_value_arbitr_map (l, key, value, options, depth)
|
local function format_key_and_value_arbitr_map (l, key, value, _, depth)
|
||||||
local index_before_key = #l+1
|
local index_before_key = #l+1
|
||||||
l[#l+1] = '['
|
l[#l+1] = '['
|
||||||
format_value(key, options, math.huge, l)
|
format_value(key, nil, math.huge, l)
|
||||||
l[#l+1] = ']'
|
l[#l+1] = ']'
|
||||||
l[#l+1] = { 'align', 'key', width_of_strings_in_l(l, index_before_key) }
|
l[#l+1] = { 'align', 'key', width_of_strings_in_l(l, index_before_key) }
|
||||||
l[#l+1] = ' = '
|
l[#l+1] = ' = '
|
||||||
return format_value(value, options, depth, l)
|
return format_value(value, nil, depth, l)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function format_key_and_value_sequence (l, key, value, options, depth)
|
local function format_key_and_value_sequence (l, key, value, _, depth)
|
||||||
return format_value(value, options, depth, l)
|
return format_value(value, nil, depth, l)
|
||||||
end
|
end
|
||||||
|
|
||||||
local TABLE_TYPE_TO_PAIR_FORMAT = {
|
local TABLE_TYPE_TO_PAIR_FORMAT = {
|
||||||
|
@ -333,18 +333,18 @@ local TABLE_TYPE_TO_PAIR_FORMAT = {
|
||||||
|
|
||||||
-- Formatting tables
|
-- Formatting tables
|
||||||
|
|
||||||
function format_table (t, options, depth, l)
|
function format_table (t, _, depth, l)
|
||||||
-- Error Checking
|
-- Error Checking
|
||||||
-- TODO: Add more nuanced formatting.
|
-- TODO: Add more nuanced formatting.
|
||||||
|
|
||||||
assert(type(t) == 'table')
|
assert(type(t) == 'table')
|
||||||
assert(type(options) == 'table' and type(depth) == 'number' and type(l) == 'table')
|
assert(type(depth) == 'number' and type(l) == 'table')
|
||||||
|
|
||||||
-- Do stuff
|
-- Do stuff
|
||||||
|
|
||||||
local table_info = l.info[t] or get_table_info(t)
|
local table_info = l.info[t] or get_table_info(t)
|
||||||
|
|
||||||
if options.recursion == 'marked' and table_info.marker then
|
if l.options.recursion == 'marked' and table_info.marker then
|
||||||
l[#l+1], l[#l+2], l[#l+3] = '<', table_info.marker, '>'
|
l[#l+1], l[#l+2], l[#l+3] = '<', table_info.marker, '>'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -354,7 +354,7 @@ function format_table (t, options, depth, l)
|
||||||
if table_info.type == TABLE_TYPE.EMPTY then
|
if table_info.type == TABLE_TYPE.EMPTY then
|
||||||
-- Empty Map
|
-- Empty Map
|
||||||
return l '{}'
|
return l '{}'
|
||||||
elseif depth >= options.max_depth or already_visited then
|
elseif depth >= l.options.max_depth or already_visited then
|
||||||
-- Already visited or above max depth
|
-- Already visited or above max depth
|
||||||
return l '{...}'
|
return l '{...}'
|
||||||
end
|
end
|
||||||
|
@ -373,7 +373,7 @@ function format_table (t, options, depth, l)
|
||||||
-- Begin formatting table.
|
-- Begin formatting table.
|
||||||
l[#l+1] = {'indent', '{'}
|
l[#l+1] = {'indent', '{'}
|
||||||
for _, pair in ipairs(key_value_pairs) do
|
for _, pair in ipairs(key_value_pairs) do
|
||||||
pair_format_func(l, pair[1], pair[2], options, depth + 1)
|
pair_format_func(l, pair[1], pair[2], nil, depth + 1)
|
||||||
l[#l+1] = ','
|
l[#l+1] = ','
|
||||||
l[#l+1] = {'seperator'}
|
l[#l+1] = {'seperator'}
|
||||||
end
|
end
|
||||||
|
@ -394,12 +394,12 @@ end
|
||||||
|
|
||||||
-- Formatting Strings
|
-- Formatting Strings
|
||||||
|
|
||||||
local function format_string (str, options, depth, l)
|
local function format_string (str, _, depth, l)
|
||||||
-- TODO: Add option for escaping unicode characters.
|
-- TODO: Add option for escaping unicode characters.
|
||||||
|
|
||||||
-- Error checking
|
-- Error checking
|
||||||
assert( type(str) == 'string' )
|
assert( type(str) == 'string' )
|
||||||
assert(type(options) == 'table' and type(depth) == 'number' and type(l) == 'table')
|
assert(type(depth) == 'number' and type(l) == 'table')
|
||||||
|
|
||||||
-- Do work
|
-- Do work
|
||||||
|
|
||||||
|
@ -410,7 +410,7 @@ local function format_string (str, options, depth, l)
|
||||||
|
|
||||||
-- ...
|
-- ...
|
||||||
local chance_of_longform = is_long_string and ((newline_or_tab_index or math.huge) <= NR_CHARS_IN_LONG_STRING) or double_quote_index and single_quote_index
|
local chance_of_longform = is_long_string and ((newline_or_tab_index or math.huge) <= NR_CHARS_IN_LONG_STRING) or double_quote_index and single_quote_index
|
||||||
local cut_string_index = options.cut_strings and (is_long_string or chance_of_longform)
|
local cut_string_index = l.options.cut_strings and (is_long_string or chance_of_longform)
|
||||||
and math.min(NR_CHARS_IN_LONG_STRING - 3, newline_or_tab_index or 1/0, double_quote_index or 1/0, single_quote_index or 1/0)
|
and math.min(NR_CHARS_IN_LONG_STRING - 3, newline_or_tab_index or 1/0, double_quote_index or 1/0, single_quote_index or 1/0)
|
||||||
|
|
||||||
local longform = chance_of_longform and ((not cut_string_index) or cut_string_index < math.min(newline_or_tab_index or 1/0, double_quote_index or 1/0, single_quote_index or 1/0))
|
local longform = chance_of_longform and ((not cut_string_index) or cut_string_index < math.min(newline_or_tab_index or 1/0, double_quote_index or 1/0, single_quote_index or 1/0))
|
||||||
|
@ -440,13 +440,13 @@ local function format_string (str, options, depth, l)
|
||||||
l[#l+1] = right
|
l[#l+1] = right
|
||||||
end
|
end
|
||||||
|
|
||||||
local function format_coroutine (value, options, depth, l)
|
local function format_coroutine (value, _, depth, l)
|
||||||
l[#l+1] = coroutine.status(value)
|
l[#l+1] = coroutine.status(value)
|
||||||
l[#l+1] = ' coroutine: '
|
l[#l+1] = ' coroutine: '
|
||||||
l[#l+1] = tostring(value):sub(9)
|
l[#l+1] = tostring(value):sub(9)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function format_primitive (value, options, depth, l)
|
local function format_primitive (value, _, depth, l)
|
||||||
l[#l+1] = tostring(value)
|
l[#l+1] = tostring(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -464,9 +464,11 @@ local TYPE_TO_FORMAT_FUNC = {
|
||||||
}
|
}
|
||||||
|
|
||||||
function format_value (value, _, depth, l)
|
function format_value (value, _, depth, l)
|
||||||
|
assert(type(depth) == 'number' and type(l) == 'table')
|
||||||
|
if _ ~= nil then print('Ugh: ' .. tostring(_)) end
|
||||||
local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
|
local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
|
||||||
if formatting then
|
if formatting then
|
||||||
formatting(value, l.options, depth, l, format_value)
|
formatting(value, nil, depth, l, format_value)
|
||||||
else
|
else
|
||||||
error(ERROR_UNKNOWN_TYPE:format(type(value), tostring(value)), 2)
|
error(ERROR_UNKNOWN_TYPE:format(type(value), tostring(value)), 2)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user