Compare commits
No commits in common. "b0004db70c0befee6b455d28ae392c463686aa4d" and "856d9df69028e321fe52f25389af0661c1e3acfb" have entirely different histories.
b0004db70c
...
856d9df690
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +0,0 @@
|
||||||
.*.swp
|
|
38
common.lua
38
common.lua
|
@ -1,4 +1,7 @@
|
||||||
|
|
||||||
|
-- TODO: I don't like to have such tiny modules. Either merge into another
|
||||||
|
-- module or provide the functionality with another approach.
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Enum
|
-- Enum
|
||||||
|
|
||||||
|
@ -24,13 +27,13 @@ local UNICODE_ZERO_WIDTH_CHARACTERS = {}
|
||||||
for i = 128, 191 do UNICODE_ZERO_WIDTH_CHARACTERS['\204'..string.char(i)] = true end
|
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
|
for i = 128, 175 do UNICODE_ZERO_WIDTH_CHARACTERS['\205'..string.char(i)] = true end
|
||||||
|
|
||||||
|
|
||||||
local function iterate_utf8_chars (str)
|
local function iterate_utf8_chars (str)
|
||||||
-- TODO: Detect invalid codepoints.
|
-- TODO: Detect invalid codepoints.
|
||||||
return str:gmatch(UNICODE_CHAR_PATTERN)
|
return str:gmatch(UNICODE_CHAR_PATTERN)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function utf8_string_length (str)
|
local function utf8_string_length (str)
|
||||||
assert(type(str) == 'string')
|
|
||||||
local len = 0
|
local len = 0
|
||||||
for char in iterate_utf8_chars(str) do
|
for char in iterate_utf8_chars(str) do
|
||||||
if not UNICODE_ZERO_WIDTH_CHARACTERS[char] then
|
if not UNICODE_ZERO_WIDTH_CHARACTERS[char] then
|
||||||
|
@ -40,43 +43,10 @@ local function utf8_string_length (str)
|
||||||
return len
|
return len
|
||||||
end
|
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 {
|
return {
|
||||||
TABLE_TYPE = enum { 'EMPTY', 'SEQUENCE', 'STRING_MAP', 'PURE_MAP', 'MIXED', 'SET' },
|
TABLE_TYPE = enum { 'EMPTY', 'SEQUENCE', 'STRING_MAP', 'PURE_MAP', 'MIXED', 'SET' },
|
||||||
DISPLAY = { HIDE = 1, SMALL = 2, INLINE = 3, EXPAND = 4 },
|
DISPLAY = { HIDE = 1, SMALL = 2, INLINE = 3, EXPAND = 4 },
|
||||||
|
|
||||||
utf8_string_length = utf8_string_length,
|
utf8_string_length = utf8_string_length,
|
||||||
width_of_strings_in_l = width_of_strings_in_l,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
function.lua
11
function.lua
|
@ -61,7 +61,6 @@ simplest, and move towards abstraction.
|
||||||
local LIBRARY = require((... and select('1', ...):match('.+%.') or '')..'library') or {}
|
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 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 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
|
-- Constants
|
||||||
|
|
||||||
|
@ -236,6 +235,15 @@ end
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Text handling
|
-- 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)
|
local function add_indent_to_string (str, indent)
|
||||||
-- Indents `str` by `indent`.
|
-- Indents `str` by `indent`.
|
||||||
|
|
||||||
|
@ -372,4 +380,3 @@ return function (value, display, l, format_value)
|
||||||
end
|
end
|
||||||
l[#l+1] = '\nend'
|
l[#l+1] = '\nend'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
121
pretty.lua
121
pretty.lua
|
@ -72,17 +72,12 @@ local ERROR_UNKNOWN_TYPE = [[
|
||||||
We are attempting to cover all Lua features, so please report this bug, so we can improve.
|
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
|
local MAX_WIDTH_FOR_SINGLE_LINE_TABLE = 38
|
||||||
|
|
||||||
if io and io.popen then
|
if io and io.popen then
|
||||||
local f = io.popen "tput cols"
|
local f = io.popen "tput cols"
|
||||||
local term_width = f:read '*n'
|
local term_width = f:read '*n'
|
||||||
f:close()
|
f:close()
|
||||||
if term_width then
|
-- if term_width then MAX_WIDTH_FOR_SINGLE_LINE_TABLE = term_width * 3 / 2 end
|
||||||
TERMINAL_WIDTH = term_width
|
|
||||||
--MAX_WIDTH_FOR_SINGLE_LINE_TABLE = term_width * (2 / 3)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local KEY_TYPE_SORT_ORDER = {
|
local KEY_TYPE_SORT_ORDER = {
|
||||||
|
@ -106,8 +101,6 @@ local VALUE_TYPE_SORT_ORDER = {
|
||||||
['function'] = 7,
|
['function'] = 7,
|
||||||
}
|
}
|
||||||
|
|
||||||
local COLUMN_SEPERATION = 1
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Key-value-pair Util
|
-- Key-value-pair Util
|
||||||
|
|
||||||
|
@ -209,7 +202,24 @@ end
|
||||||
-- Formatting Util
|
-- Formatting Util
|
||||||
|
|
||||||
local length_of_utf8_string = import 'common' . utf8_string_length
|
local length_of_utf8_string = import 'common' . utf8_string_length
|
||||||
local width_of_strings_in_l = import 'common' . width_of_strings_in_l
|
|
||||||
|
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 function ignore_alignment_info (l, start_i, stop_i)
|
local function ignore_alignment_info (l, start_i, stop_i)
|
||||||
|
|
||||||
|
@ -219,7 +229,7 @@ local function ignore_alignment_info (l, start_i, stop_i)
|
||||||
local start_i, stop_i = start_i or 1, stop_i or #l
|
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')
|
assert(type(start_i) == 'number')
|
||||||
|
|
||||||
-- Do stuff
|
-- Do stuff
|
||||||
for i = start_i, stop_i do
|
for i = start_i, stop_i do
|
||||||
|
@ -229,31 +239,6 @@ local function ignore_alignment_info (l, start_i, stop_i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
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(stop_i) == 'number')
|
|
||||||
|
|
||||||
-- Find maximums
|
|
||||||
local max = {}
|
|
||||||
for i = start_i, stop_i do
|
|
||||||
if type(l[i]) == 'table' and l[i][1] == 'align' then
|
|
||||||
max[ l[i][2] ] = math.max( l[i][3], max[ l[i][2] ] or 0 )
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Insert the proper whitespace
|
|
||||||
for i = start_i, stop_i do
|
|
||||||
if type(l[i]) == 'table' and l[i][1] == 'align' then
|
|
||||||
l[i].est_width = max[ l[i][2] ] - l[i][3]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function fix_alignment (l, start_i, stop_i)
|
local function fix_alignment (l, start_i, stop_i)
|
||||||
|
|
||||||
-- Argument fixing and Error Checking
|
-- Argument fixing and Error Checking
|
||||||
|
@ -262,15 +247,20 @@ local function fix_alignment (l, start_i, stop_i)
|
||||||
local start_i, stop_i = start_i or 1, stop_i or #l
|
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')
|
assert(type(start_i) == 'number')
|
||||||
|
|
||||||
-- Find whitespace to insert
|
-- Find maximums
|
||||||
insert_alignment_estimations(l, start_i, stop_i)
|
local max = {}
|
||||||
|
|
||||||
-- Insert whitespace
|
|
||||||
for i = start_i, stop_i do
|
for i = start_i, stop_i do
|
||||||
if type(l[i]) == 'table' and l[i][1] == 'align' then
|
if type(l[i]) == 'table' and l[i][1] == 'align' then
|
||||||
l[i] = string.rep(' ', l[i].est_width)
|
max[ l[i][2] ] = math.max( l[i][3], max[ l[i][2] ] or 0 )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 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])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -311,14 +301,12 @@ local function align_into_columns (l, start_i, stop_i)
|
||||||
assert(type(start_i) == 'number')
|
assert(type(start_i) == 'number')
|
||||||
assert(type(stop_i) == 'number')
|
assert(type(stop_i) == 'number')
|
||||||
|
|
||||||
insert_alignment_estimations(l, start_i, stop_i)
|
|
||||||
|
|
||||||
-- Find columns
|
-- Find columns
|
||||||
local columns = nil
|
local columns = nil
|
||||||
for nr_items_pr_row = 10, 1, -1 do -- TODO: Do this more intelligently.
|
for nr_items_pr_row = 10, 1, -1 do -- TODO: Do this more intelligently.
|
||||||
local column_width
|
local column_width
|
||||||
column_width, columns = attempt_to_align_into_columns(l, start_i, stop_i, nr_items_pr_row)
|
column_width, columns = attempt_to_align_into_columns(l, start_i, stop_i, nr_items_pr_row)
|
||||||
if column_width <= l.options.max_width_for_single_line_table then break end
|
if column_width <= MAX_WIDTH_FOR_SINGLE_LINE_TABLE then break end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Change alignment of columns
|
-- Change alignment of columns
|
||||||
|
@ -348,7 +336,7 @@ local function align_into_columns (l, start_i, stop_i)
|
||||||
local column_i = (item_nr-1)%#columns+1
|
local column_i = (item_nr-1)%#columns+1
|
||||||
if column_i ~= #columns then
|
if column_i ~= #columns then
|
||||||
local width_of_item = width_of_strings_in_l(l, start_of_item_i, i-1)
|
local width_of_item = width_of_strings_in_l(l, start_of_item_i, i-1)
|
||||||
l[i] = l[i][2] .. (' '):rep(COLUMN_SEPERATION+columns[column_i]-width_of_item)
|
l[i] = l[i][2] .. ' ' .. (' '):rep(columns[column_i]-width_of_item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
start_of_item_i, item_nr = i + 1, item_nr + 1
|
start_of_item_i, item_nr = i + 1, item_nr + 1
|
||||||
|
@ -357,7 +345,7 @@ local function align_into_columns (l, start_i, stop_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function align_into_tabular_style (l, start_i, stop_i)
|
local function align_into_tabular_style (l, start_i, stop_i)
|
||||||
-- Adds alignment after separators, to create nicely aligned tabular-format.
|
-- Adds alignment after seperators, to create nicely aligned tabular-format.
|
||||||
|
|
||||||
-- Argument fixing and Error Checking
|
-- Argument fixing and Error Checking
|
||||||
local start_i, stop_i = start_i or 1, stop_i or #l
|
local start_i, stop_i = start_i or 1, stop_i or #l
|
||||||
|
@ -506,14 +494,12 @@ local function format_table (t, display, l, format_value)
|
||||||
|
|
||||||
-- Decide for short or long table formatting.
|
-- Decide for short or long table formatting.
|
||||||
local table_width = width_of_strings_in_l(l, start_of_table_i)
|
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".
|
-- Is short table: Ignore "width of key".
|
||||||
l[start_of_table_i][3] = 'inline'
|
l[start_of_table_i][3] = 'inline'
|
||||||
ignore_alignment_info(l, start_of_table_i)
|
ignore_alignment_info(l, start_of_table_i)
|
||||||
elseif table_info.is_leaf_node then
|
elseif table_info.is_leaf_node then
|
||||||
-- Is leaf node: Can format into columns.
|
-- Is leaf node: Can format into columns.
|
||||||
-- Only if long or sequence.
|
|
||||||
-- NOTE: Currently we only allow leaf-nodes to format into columns, due
|
-- NOTE: Currently we only allow leaf-nodes to format into columns, due
|
||||||
-- to issues with table alignment.
|
-- to issues with table alignment.
|
||||||
align_into_columns(l, start_of_table_i)
|
align_into_columns(l, start_of_table_i)
|
||||||
|
@ -569,6 +555,7 @@ local TYPE_TO_FORMAT_FUNC = {
|
||||||
local function format_value (value, display, l)
|
local function format_value (value, display, l)
|
||||||
assert(type(display) == 'number' and type(l) == 'table')
|
assert(type(display) == 'number' and type(l) == 'table')
|
||||||
local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
|
local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
|
||||||
|
--print(value, formatting)
|
||||||
if formatting then
|
if formatting then
|
||||||
formatting(value, display, l, format_value)
|
formatting(value, display, l, format_value)
|
||||||
else
|
else
|
||||||
|
@ -594,8 +581,6 @@ local KNOWN_OPTIONS = {
|
||||||
_table_addr_comment = { type = 'boolean', default = false, debug = 'debug' }, -- TODO: Maybe automatically display table address when display = 0?
|
_table_addr_comment = { type = 'boolean', default = false, debug = 'debug' }, -- TODO: Maybe automatically display table address when display = 0?
|
||||||
|
|
||||||
indent = { type = 'string', default = ' ' },
|
indent = { type = 'string', default = ' ' },
|
||||||
|
|
||||||
max_output_width = { type = 'number', default = TERMINAL_WIDTH }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
local function ensure_that_all_options_are_known (input_options)
|
local function ensure_that_all_options_are_known (input_options)
|
||||||
|
@ -630,36 +615,10 @@ local function ensure_that_all_options_are_known (input_options)
|
||||||
output_options[option_name] = option_info.default
|
output_options[option_name] = option_info.default
|
||||||
end
|
end
|
||||||
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
|
-- Returns input_options
|
||||||
return output_options
|
return output_options
|
||||||
end
|
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)
|
local function pretty_format (value, options)
|
||||||
-- Error checking
|
-- Error checking
|
||||||
local options = ensure_that_all_options_are_known(options or {})
|
local options = ensure_that_all_options_are_known(options or {})
|
||||||
|
@ -676,13 +635,7 @@ local function pretty_format (value, options)
|
||||||
fix_seperator_info(l, l.options.indent)
|
fix_seperator_info(l, l.options.indent)
|
||||||
ignore_alignment_info(l)
|
ignore_alignment_info(l)
|
||||||
|
|
||||||
-- Concat and perform last assertions.
|
return table.concat(l, '')
|
||||||
local repr = table.concat(l, '')
|
|
||||||
assert_pretty_result(repr, options)
|
|
||||||
|
|
||||||
-- Return
|
|
||||||
return repr
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return pretty_format
|
return pretty_format
|
||||||
|
|
||||||
|
|
27
pstring.lua
27
pstring.lua
|
@ -109,27 +109,14 @@ local function safe_cut (str, si, ei)
|
||||||
-- Calculate
|
-- Calculate
|
||||||
local cut_str = str:sub(si, ei)
|
local cut_str = str:sub(si, ei)
|
||||||
|
|
||||||
-- Search for the number of backslashes and digits at the end of the string.
|
-- Search for the number of backslashes just before the send of the string.
|
||||||
-- If the number of backslashes is even, it's a sequence of backslashes, if
|
-- If that number is even, it's a sequence of backslashes, if not it's a
|
||||||
-- not it's a broken escape string.
|
-- broken escape string.
|
||||||
local start_of_backslashes, start_of_digits = cut_str:match '()\\*()%d?%d?$'
|
local start_of_backslashes, start_of_digits = cut_str:match '()\\*()%d?%d?$'
|
||||||
local nr_backslashes_before_end = start_of_digits - start_of_backslashes
|
local nr_backslashes_before_end = start_of_digits - start_of_backslashes
|
||||||
if nr_backslashes_before_end % 2 == 1 then
|
if nr_backslashes_before_end % 2 == 1 then cut_str = cut_str:sub(1, start_of_backslashes - 1) end
|
||||||
-- 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
|
|
||||||
|
|
||||||
local digits, after_digits = str:match('^\\(%d?%d?%d?)()', si - 1 + start_of_digits - 1)
|
return cut_str
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -162,8 +149,8 @@ local function format_concatted_string (str, _, l)
|
||||||
-- Cut strings
|
-- Cut strings
|
||||||
local sub_strings, str_i = {}, 1
|
local sub_strings, str_i = {}, 1
|
||||||
repeat
|
repeat
|
||||||
local sub_str, ei = safe_cut(str, str_i, str_i + width_without_overhead - 1)
|
local sub_str = safe_cut(str, str_i, str_i + width_without_overhead - 1)
|
||||||
str_i = ei + 1
|
str_i = str_i + #sub_str
|
||||||
sub_strings[#sub_strings+1] = sub_str
|
sub_strings[#sub_strings+1] = sub_str
|
||||||
until str_i >= #str
|
until str_i >= #str
|
||||||
|
|
||||||
|
|
|
@ -34,15 +34,6 @@ local function format_test (t)
|
||||||
end, { line = debug.getinfo(2).currentline })
|
end, { line = debug.getinfo(2).currentline })
|
||||||
end
|
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
|
-- Primitive types
|
||||||
|
|
||||||
|
@ -272,24 +263,13 @@ format_test {
|
||||||
expect = '{\n \'hello\', \'world\', \'how\',\n \'is\', \'it\', \'going?\',\n \'Im\', \'doing great\', \'thanks\',\n \'that\', \'was\', \'what\',\n \'I\'\n}',
|
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 {
|
format_test {
|
||||||
name = 'Tabular style with strings left aligned',
|
name = 'Tabular style with strings left aligned',
|
||||||
input = {
|
input = {
|
||||||
{ a = 'hello', b = 'hi' },
|
{ 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 { a = \'hi\', b = \'hi\' }\n}',
|
expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' }\n}',
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
|
@ -418,6 +398,53 @@ end)
|
||||||
local BIG_EXAMPLE_TABLE = [[
|
local BIG_EXAMPLE_TABLE = [[
|
||||||
return {
|
return {
|
||||||
[0] = 21082, [1] = 696,
|
[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,
|
[96] = 62, [97] = 334,
|
||||||
[98] = 119, [99] = 217,
|
[98] = 119, [99] = 217,
|
||||||
[100] = 261
|
[100] = 261
|
||||||
|
@ -431,4 +458,3 @@ end)
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
return SUITE
|
return SUITE
|
||||||
|
|
||||||
|
|
|
@ -130,13 +130,6 @@ format_test {
|
||||||
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\t\'...\n}',
|
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 {
|
format_test {
|
||||||
name = 'Cut strings are not cut in the middle of escaping \\',
|
name = 'Cut strings are not cut in the middle of escaping \\',
|
||||||
not_idempotent = true,
|
not_idempotent = true,
|
||||||
|
@ -152,19 +145,13 @@ format_test {
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
name = 'Cut strings can shorten decimal escape codes, if necessary and possible',
|
-- NOTE: Not priority functionallity.
|
||||||
|
name = 'Cut strings can shorten decimal escape codes, if nessesary and possible',
|
||||||
not_idempotent = true,
|
not_idempotent = true,
|
||||||
input = {'Lorem ipsum dolor sit amet, consec\014tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
|
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}',
|
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
|
-- Concatted Strings
|
||||||
|
|
||||||
|
@ -180,18 +167,6 @@ format_test {
|
||||||
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc ve' ..]]..'\n'..[['stibulum tempus ligula. Sed ac lobortis mi.']],
|
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
|
-- Longform Strings
|
||||||
|
|
|
@ -247,4 +247,3 @@ end)
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
return SUITE
|
return SUITE
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user