1
0

Fixed alignment issue for certain tables.

This commit is contained in:
Jon Michael Aanes 2017-11-18 13:23:43 +01:00
parent c5389dfa42
commit 88fa144c9e
6 changed files with 98 additions and 110 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 -- Enum
@ -42,10 +39,43 @@ 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,
} }

View File

@ -61,6 +61,7 @@ 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
@ -235,15 +236,6 @@ 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`.
@ -380,3 +372,4 @@ return function (value, display, l, format_value)
end end
l[#l+1] = '\nend' l[#l+1] = '\nend'
end end

View File

@ -101,6 +101,8 @@ local VALUE_TYPE_SORT_ORDER = {
['function'] = 7, ['function'] = 7,
} }
local COLUMN_SEPERATION = 1
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Key-value-pair Util -- Key-value-pair Util
@ -202,24 +204,7 @@ 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)
@ -229,7 +214,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(start_i) == 'number') assert(type(stop_i) == 'number')
-- Do stuff -- Do stuff
for i = start_i, stop_i do for i = start_i, stop_i do
@ -239,15 +224,14 @@ local function ignore_alignment_info (l, start_i, stop_i)
end end
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 -- Argument fixing and Error Checking
assert(type(l) == 'table') assert(type(l) == 'table')
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(start_i) == 'number') assert(type(stop_i) == 'number')
-- Find maximums -- Find maximums
local max = {} local max = {}
@ -260,7 +244,28 @@ local function fix_alignment (l, start_i, stop_i)
-- Insert the proper whitespace -- Insert the proper 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(' ', 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 end
end end
@ -301,6 +306,8 @@ 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.
@ -336,7 +343,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(columns[column_i]-width_of_item) l[i] = l[i][2] .. (' '):rep(COLUMN_SEPERATION+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
@ -345,7 +352,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 seperators, to create nicely aligned tabular-format. -- Adds alignment after separators, 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
@ -639,3 +646,4 @@ local function pretty_format (value, options)
end end
return pretty_format return pretty_format

View File

@ -267,9 +267,10 @@ 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}', expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' },\n { a = \'hi\', b = \'hi\' }\n}',
} }
format_test { format_test {
@ -398,53 +399,6 @@ 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
@ -458,3 +412,4 @@ end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return SUITE return SUITE

View File

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