1
0

Compare commits

...

3 Commits

8 changed files with 209 additions and 125 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.*.swp

View File

@ -1,7 +1,4 @@
-- TODO: I don't like to have such tiny modules. Either merge into another
-- module or provide the functionality with another approach.
--------------------------------------------------------------------------------
-- Enum
@ -27,13 +24,13 @@ local UNICODE_ZERO_WIDTH_CHARACTERS = {}
for i = 128, 191 do UNICODE_ZERO_WIDTH_CHARACTERS['\204'..string.char(i)] = true end
for i = 128, 175 do UNICODE_ZERO_WIDTH_CHARACTERS['\205'..string.char(i)] = true end
local function iterate_utf8_chars (str)
-- TODO: Detect invalid codepoints.
return str:gmatch(UNICODE_CHAR_PATTERN)
end
local function utf8_string_length (str)
assert(type(str) == 'string')
local len = 0
for char in iterate_utf8_chars(str) do
if not UNICODE_ZERO_WIDTH_CHARACTERS[char] then
@ -43,10 +40,43 @@ local function utf8_string_length (str)
return len
end
--------------------------------------------------------------------------------
-- L utility
local function width_of_strings_in_l (l, start_i, stop_i)
-- Argument fixing and Error Checking
assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(stop_i) == 'number')
-- Do stuff
local width = 0
for i = start_i, stop_i do
local item_width = 0
if type(l[i]) == 'string' then
item_width = utf8_string_length(l[i])
elseif l[i].est_width then
item_width = l[i].est_width
end
width = width + item_width
end
-- Return
return width
end
--------------------------------------------------------------------------------
return {
TABLE_TYPE = enum { 'EMPTY', 'SEQUENCE', 'STRING_MAP', 'PURE_MAP', 'MIXED', 'SET' },
DISPLAY = { HIDE = 1, SMALL = 2, INLINE = 3, EXPAND = 4 },
utf8_string_length = utf8_string_length,
width_of_strings_in_l = width_of_strings_in_l,
}

View File

@ -61,6 +61,7 @@ simplest, and move towards abstraction.
local LIBRARY = require((... and select('1', ...):match('.+%.') or '')..'library') or {}
local DISPLAY = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . DISPLAY
local utf8_string_length = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . utf8_string_length
local width_of_strings_in_l = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . width_of_strings_in_l
-- Constants
@ -235,15 +236,6 @@ end
--------------------------------------------------------------------------------
-- Text handling
local function width_of_strings_in_l (l, start_i, end_i)
-- FIXME: Copy of the one in pretty.lua
local width = 0
for i = start_i or 1, (end_i or #l) do
width = width + utf8_string_length(l[i])
end
return width
end
local function add_indent_to_string (str, indent)
-- Indents `str` by `indent`.
@ -380,3 +372,4 @@ return function (value, display, l, format_value)
end
l[#l+1] = '\nend'
end

View File

@ -72,12 +72,17 @@ local ERROR_UNKNOWN_TYPE = [[
We are attempting to cover all Lua features, so please report this bug, so we can improve.
]]
local TERMINAL_WIDTH = 80
local MAX_WIDTH_FOR_SINGLE_LINE_TABLE = 38
if io and io.popen then
local f = io.popen "tput cols"
local term_width = f:read '*n'
f:close()
-- if term_width then MAX_WIDTH_FOR_SINGLE_LINE_TABLE = term_width * 3 / 2 end
if term_width then
TERMINAL_WIDTH = term_width
--MAX_WIDTH_FOR_SINGLE_LINE_TABLE = term_width * (2 / 3)
end
end
local KEY_TYPE_SORT_ORDER = {
@ -101,6 +106,8 @@ local VALUE_TYPE_SORT_ORDER = {
['function'] = 7,
}
local COLUMN_SEPERATION = 1
--------------------------------------------------------------------------------
-- Key-value-pair Util
@ -202,24 +209,7 @@ end
-- Formatting Util
local length_of_utf8_string = import 'common' . utf8_string_length
local function width_of_strings_in_l (l, start_i, stop_i)
-- Argument fixing and Error Checking
assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(start_i) == 'number')
-- Do stuff
local width = 0
for i = start_i, stop_i do
width = width + ((type(l[i]) ~= 'string') and 1 or length_of_utf8_string(l[i]))
end
return width
end
local width_of_strings_in_l = import 'common' . width_of_strings_in_l
local function ignore_alignment_info (l, start_i, stop_i)
@ -229,7 +219,7 @@ local function ignore_alignment_info (l, start_i, stop_i)
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(start_i) == 'number')
assert(type(stop_i) == 'number')
-- Do stuff
for i = start_i, stop_i do
@ -239,15 +229,14 @@ local function ignore_alignment_info (l, start_i, stop_i)
end
end
local function fix_alignment (l, start_i, stop_i)
local function insert_alignment_estimations (l, start_i, stop_i)
-- Argument fixing and Error Checking
assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(start_i) == 'number')
assert(type(stop_i) == 'number')
-- Find maximums
local max = {}
@ -260,7 +249,28 @@ local function fix_alignment (l, start_i, stop_i)
-- Insert the proper whitespace
for i = start_i, stop_i do
if type(l[i]) == 'table' and l[i][1] == 'align' then
l[i] = string.rep(' ', max[ l[i][2] ] - l[i][3])
l[i].est_width = max[ l[i][2] ] - l[i][3]
end
end
end
local function fix_alignment (l, start_i, stop_i)
-- Argument fixing and Error Checking
assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(stop_i) == 'number')
-- Find whitespace to insert
insert_alignment_estimations(l, start_i, stop_i)
-- Insert whitespace
for i = start_i, stop_i do
if type(l[i]) == 'table' and l[i][1] == 'align' then
l[i] = string.rep(' ', l[i].est_width)
end
end
end
@ -301,12 +311,14 @@ local function align_into_columns (l, start_i, stop_i)
assert(type(start_i) == 'number')
assert(type(stop_i) == 'number')
insert_alignment_estimations(l, start_i, stop_i)
-- Find columns
local columns = nil
for nr_items_pr_row = 10, 1, -1 do -- TODO: Do this more intelligently.
local column_width
column_width, columns = attempt_to_align_into_columns(l, start_i, stop_i, nr_items_pr_row)
if column_width <= MAX_WIDTH_FOR_SINGLE_LINE_TABLE then break end
if column_width <= l.options.max_width_for_single_line_table then break end
end
-- Change alignment of columns
@ -336,7 +348,7 @@ local function align_into_columns (l, start_i, stop_i)
local column_i = (item_nr-1)%#columns+1
if column_i ~= #columns then
local width_of_item = width_of_strings_in_l(l, start_of_item_i, i-1)
l[i] = l[i][2] .. ' ' .. (' '):rep(columns[column_i]-width_of_item)
l[i] = l[i][2] .. (' '):rep(COLUMN_SEPERATION+columns[column_i]-width_of_item)
end
end
start_of_item_i, item_nr = i + 1, item_nr + 1
@ -345,7 +357,7 @@ local function align_into_columns (l, start_i, stop_i)
end
local function align_into_tabular_style (l, start_i, stop_i)
-- Adds alignment after seperators, to create nicely aligned tabular-format.
-- Adds alignment after separators, to create nicely aligned tabular-format.
-- Argument fixing and Error Checking
local start_i, stop_i = start_i or 1, stop_i or #l
@ -494,12 +506,14 @@ local function format_table (t, display, l, format_value)
-- Decide for short or long table formatting.
local table_width = width_of_strings_in_l(l, start_of_table_i)
if table_width <= MAX_WIDTH_FOR_SINGLE_LINE_TABLE then
if table_width <= l.options.max_width_for_single_line_table then
-- Is short table: Ignore "width of key".
l[start_of_table_i][3] = 'inline'
ignore_alignment_info(l, start_of_table_i)
elseif table_info.is_leaf_node then
-- Is leaf node: Can format into columns.
-- Only if long or sequence.
-- NOTE: Currently we only allow leaf-nodes to format into columns, due
-- to issues with table alignment.
align_into_columns(l, start_of_table_i)
@ -555,7 +569,6 @@ local TYPE_TO_FORMAT_FUNC = {
local function format_value (value, display, l)
assert(type(display) == 'number' and type(l) == 'table')
local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
--print(value, formatting)
if formatting then
formatting(value, display, l, format_value)
else
@ -581,6 +594,8 @@ local KNOWN_OPTIONS = {
_table_addr_comment = { type = 'boolean', default = false, debug = 'debug' }, -- TODO: Maybe automatically display table address when display = 0?
indent = { type = 'string', default = ' ' },
max_output_width = { type = 'number', default = TERMINAL_WIDTH }
}
local function ensure_that_all_options_are_known (input_options)
@ -615,10 +630,36 @@ local function ensure_that_all_options_are_known (input_options)
output_options[option_name] = option_info.default
end
end
-- Calculate derived settings
output_options.max_width_for_single_line_table = MAX_WIDTH_FOR_SINGLE_LINE_TABLE -- TODO: Make dynamic
-- Returns input_options
return output_options
end
local function length_of_longest_line_in_text (text)
assert(type(text) == 'string')
local longest_line_len = text:match '([^\n]*)$' : len()
for line in text:gmatch '(.-)\n' do
longest_line_len = math.max(longest_line_len, line:len())
end
return longest_line_len
end
local function internal_warning (fmt, ...)
io.stderr:write('[pretty/internal]: '..string.format(fmt, ...))
end
local function assert_pretty_result (repr, options)
assert(type(repr) == 'string')
assert(type(options) == 'table')
-- Determine length of longest line in output
local max_width = length_of_longest_line_in_text(repr)
if max_width > options.max_output_width then
internal_warning('Internal assertion failed. Width of output was %i, but should be less than %i.', max_width, options.max_output_width)
end
end
local function pretty_format (value, options)
-- Error checking
local options = ensure_that_all_options_are_known(options or {})
@ -635,7 +676,13 @@ local function pretty_format (value, options)
fix_seperator_info(l, l.options.indent)
ignore_alignment_info(l)
return table.concat(l, '')
-- Concat and perform last assertions.
local repr = table.concat(l, '')
assert_pretty_result(repr, options)
-- Return
return repr
end
return pretty_format

View File

@ -109,14 +109,27 @@ local function safe_cut (str, si, ei)
-- Calculate
local cut_str = str:sub(si, ei)
-- Search for the number of backslashes just before the send of the string.
-- If that number is even, it's a sequence of backslashes, if not it's a
-- broken escape string.
-- Search for the number of backslashes and digits at the end of the string.
-- If the number of backslashes is even, it's a sequence of backslashes, if
-- not it's a broken escape string.
local start_of_backslashes, start_of_digits = cut_str:match '()\\*()%d?%d?$'
local nr_backslashes_before_end = start_of_digits - start_of_backslashes
if nr_backslashes_before_end % 2 == 1 then cut_str = cut_str:sub(1, start_of_backslashes - 1) end
if nr_backslashes_before_end % 2 == 1 then
-- Lets see if we can't shorten the escape code, to fit within the
-- cut limit.
local space_left = #cut_str - (start_of_digits - 2)
cut_str = cut_str:sub(1, start_of_digits - 2)
ei = ei - space_left
return cut_str
local digits, after_digits = str:match('^\\(%d?%d?%d?)()', si - 1 + start_of_digits - 1)
if space_left >= 1 + 3 - #digits:match '0*' then
ei = after_digits - 1
cut_str = cut_str .. ('\\%0'..(space_left-1)..'i'):format(digits)
end
end
return cut_str, ei
end
@ -149,8 +162,8 @@ local function format_concatted_string (str, _, l)
-- Cut strings
local sub_strings, str_i = {}, 1
repeat
local sub_str = safe_cut(str, str_i, str_i + width_without_overhead - 1)
str_i = str_i + #sub_str
local sub_str, ei = safe_cut(str, str_i, str_i + width_without_overhead - 1)
str_i = ei + 1
sub_strings[#sub_strings+1] = sub_str
until str_i >= #str

View File

@ -34,6 +34,15 @@ local function format_test (t)
end, { line = debug.getinfo(2).currentline })
end
local function format_parsable_test (t)
local stripped = t.text:match '^%s*(.-)%s*$'
return format_test {
name = t.name,
input = loadstring('return '..stripped)(),
expect = stripped
}
end
--------------------------------------------------------------------------------
-- Primitive types
@ -263,13 +272,24 @@ format_test {
expect = '{\n \'hello\', \'world\', \'how\',\n \'is\', \'it\', \'going?\',\n \'Im\', \'doing great\', \'thanks\',\n \'that\', \'was\', \'what\',\n \'I\'\n}',
}
format_parsable_test {
name = 'Column style with aligned numbers',
text = [[
{
200, -522, 423, 516, 523, 126, 2912,
523, -620, 0, 0, 0, -5, 2,
72, 6
}
]] }
format_test {
name = 'Tabular style with strings left aligned',
input = {
{ a = 'hello', b = 'hi' },
{ a = 'hi', b = 'hello' }
{ a = 'hi', b = 'hello' },
{ a = 'hi', b = 'hi' },
},
expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' }\n}',
expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' },\n { a = \'hi\', b = \'hi\' }\n}',
}
format_test {
@ -398,53 +418,6 @@ end)
local BIG_EXAMPLE_TABLE = [[
return {
[0] = 21082, [1] = 696,
[2] = 463, [3] = 235,
[4] = 315, [5] = 312,
[6] = 204, [7] = 124,
[8] = 692, [9] = 84,
[10] = 248, [11] = 148,
[12] = 108, [13] = 109,
[14] = 1019, [15] = 1211,
[16] = 470, [17] = 73,
[18] = 121, [19] = 36,
[20] = 149, [21] = 514,
[22] = 38, [23] = 45,
[24] = 353, [25] = 27,
[26] = 27, [27] = 51,
[28] = 84, [29] = 61,
[30] = 29, [31] = 448,
[32] = 2064, [33] = 65,
[34] = 34, [35] = 20,
[36] = 859, [37] = 239,
[38] = 24, [39] = 41,
[40] = 297, [41] = 95,
[42] = 43, [43] = 30,
[44] = 202, [45] = 123,
[46] = 243, [47] = 98,
[48] = 207, [49] = 484,
[50] = 31, [51] = 59,
[52] = 51, [53] = 118,
[54] = 27, [55] = 22,
[56] = 227, [57] = 168,
[58] = 55, [59] = 38,
[60] = 74, [61] = 106,
[62] = 62, [63] = 40,
[64] = 170, [65] = 857,
[66] = 412, [67] = 136,
[68] = 737, [69] = 238,
[70] = 64, [71] = 119,
[72] = 2567, [73] = 481,
[74] = 50, [75] = 55,
[76] = 714, [77] = 189,
[78] = 61, [79] = 55,
[80] = 114, [81] = 26,
[82] = 69, [83] = 150,
[84] = 238, [85] = 172,
[86] = 65, [87] = 81,
[88] = 102, [89] = 39,
[90] = 30, [91] = 154,
[92] = 155, [93] = 191,
[94] = 75, [95] = 185,
[96] = 62, [97] = 334,
[98] = 119, [99] = 217,
[100] = 261
@ -458,3 +431,4 @@ end)
--------------------------------------------------------------------------------
return SUITE

View File

@ -130,6 +130,13 @@ format_test {
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\t\'...\n}',
}
format_test {
name = 'Cut strings are not cut in the middle of backslash sequence',
not_idempotent = true,
input = {'Lorem ipsum dolor sit amet, consec\\\\\\\\\\\\\\tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\\\\'...\n}',
}
format_test {
name = 'Cut strings are not cut in the middle of escaping \\',
not_idempotent = true,
@ -145,13 +152,19 @@ format_test {
}
format_test {
-- NOTE: Not priority functionallity.
name = 'Cut strings can shorten decimal escape codes, if nessesary and possible',
name = 'Cut strings can shorten decimal escape codes, if necessary and possible',
not_idempotent = true,
input = {'Lorem ipsum dolor sit amet, consec\014tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\14\'...\n}',
}
format_test {
name = 'Cut strings can shorten decimal escape codes, if necessary and possible, but will keep them as long as possible',
not_idempotent = true,
input = {'Lorem ipsum dolor sit amet, consec\004tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\04\'...\n}',
}
--------------------------------------------------------------------------------
-- Concatted Strings
@ -167,6 +180,18 @@ format_test {
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc ve' ..]]..'\n'..[['stibulum tempus ligula. Sed ac lobortis mi.']],
}
format_test {
name = 'Concatted string with decimal escape at border',
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004\255\000lum tempus ligula. Sed ac lobortis mi.',
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004' ..]]..'\n'..[['\255\000lum tempus ligula. Sed ac lobortis mi.']],
}
format_test {
name = 'Concatted string with decimal escape at border 2',
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004\002\000lum tempus ligula. Sed ac lobortis mi.',
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004\02' ..]]..'\n'..[['\000lum tempus ligula. Sed ac lobortis mi.']],
}
--------------------------------------------------------------------------------
-- Longform Strings

View File

@ -247,3 +247,4 @@ end)
--------------------------------------------------------------------------------
return SUITE