Skip to content

Commit 2ed954e

Browse files
committed
WIP: config: add simple pretty-printer for tabular data
The next commit needs to output a lot of environment variables, their types in the config schema, defaults and so on. It looks much more readable when formatted in the tabular form. This commits adds a simple tabular formatter for this purpose. TBD: Add a test. TBD: Part of #xxxx NO_DOC=the new module is internal one NO_CHANGELOG=see NO_DOC
1 parent f199991 commit 2ed954e

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

src/box/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ lua_source(lua_sources lua/config/source/env.lua config_source_env_lua)
5252
lua_source(lua_sources lua/config/source/file.lua config_source_file_lua)
5353
lua_source(lua_sources lua/config/utils/log.lua config_utils_log_lua)
5454
lua_source(lua_sources lua/config/utils/schema.lua config_utils_schema_lua)
55+
lua_source(lua_sources lua/config/utils/tabulate.lua config_utils_tabulate_lua)
5556

5657
if (ENABLE_CONFIG_EXTRAS)
5758
lua_source(lua_sources ${CONFIG_EXTRAS_DIR}/source/etcd.lua config_source_etcd_lua)

src/box/lua/config/utils/tabulate.lua

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- Very simple pretty-printer of tabular data.
2+
--
3+
-- Example:
4+
--
5+
-- tabulate.encode({
6+
-- {'a', 'b', 'c'},
7+
-- tabulate.SPACER,
8+
-- {'d', 'e', 'f'},
9+
-- {'g', 'h', 'i'},
10+
-- })
11+
--
12+
-- ->
13+
--
14+
-- | a | b | c |
15+
-- | - | - | - |
16+
-- | d | e | f |
17+
-- | g | h | i |
18+
19+
local SPACER = {}
20+
21+
-- Format data as a table.
22+
--
23+
-- Accepts an array of rows. Each row in an array of values. Each
24+
-- value is a string.
25+
local function encode(rows)
26+
-- Calculate column widths and columns amount.
27+
local column_widths = {}
28+
for _i, row in ipairs(rows) do
29+
for j, v in ipairs(row) do
30+
assert(type(v) == 'string')
31+
column_widths[j] = math.max(column_widths[j] or 0, #v)
32+
end
33+
end
34+
local column_count = #column_widths
35+
36+
-- Use a table as a string buffer.
37+
local acc = {}
38+
39+
-- Add all the values into the accumulator with proper spacing
40+
-- around and appropriate separators.
41+
for _i, row in ipairs(rows) do
42+
if row == SPACER then
43+
for j = 1, column_count do
44+
local width = column_widths[j]
45+
table.insert(acc, '| ')
46+
table.insert(acc, ('-'):rep(width))
47+
table.insert(acc, ' ')
48+
end
49+
table.insert(acc, '|\n')
50+
else
51+
for j = 1, column_count do
52+
assert(row[j] ~= nil)
53+
local width = column_widths[j]
54+
table.insert(acc, '| ')
55+
table.insert(acc, row[j]:ljust(width))
56+
table.insert(acc, ' ')
57+
end
58+
table.insert(acc, '|\n')
59+
end
60+
end
61+
62+
return table.concat(acc)
63+
end
64+
65+
return {
66+
SPACER = SPACER,
67+
encode = encode,
68+
}

src/box/lua/init.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ extern char session_lua[],
150150
config_source_env_lua[],
151151
config_source_file_lua[],
152152
config_utils_log_lua[],
153-
config_utils_schema_lua[]
153+
config_utils_schema_lua[],
154+
config_utils_tabulate_lua[]
154155
#if ENABLE_CONFIG_EXTRAS
155156
,
156157
config_source_etcd_lua[],
@@ -322,6 +323,10 @@ static const char *lua_sources[] = {
322323
"internal.config.utils.schema",
323324
config_utils_schema_lua,
324325

326+
"config/utils/tabulate",
327+
"internal.config.utils.tabulate",
328+
config_utils_tabulate_lua,
329+
325330
"config/instance_config",
326331
"internal.config.instance_config",
327332
config_instance_config_lua,

0 commit comments

Comments
 (0)