Comparison

util/human/io.lua @ 10894:d15a4284fdf8

util.human.io: table: Return title row when no row data passed
author Matthew Wild <mwild1@gmail.com>
date Wed, 03 Jun 2020 22:58:29 +0100
parent 10893:a256044c1d12
child 10896:c7a0eab27165
comparison
equal deleted inserted replaced
10893:a256044c1d12 10894:d15a4284fdf8
1 local array = require "util.array";
2
1 local function getchar(n) 3 local function getchar(n)
2 local stty_ret = os.execute("stty raw -echo 2>/dev/null"); 4 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
3 local ok, char; 5 local ok, char;
4 if stty_ret == true or stty_ret == 0 then 6 if stty_ret == true or stty_ret == 0 then
5 ok, char = pcall(io.read, n or 1); 7 ok, char = pcall(io.read, n or 1);
117 widths[i] = math.floor(free_width*(pc_width/100)); 119 widths[i] = math.floor(free_width*(pc_width/100));
118 end 120 end
119 end 121 end
120 122
121 return function (row) 123 return function (row)
124 if not row then
125 row = array.pluck(col_specs, "title");
126 end
122 local output = {}; 127 local output = {};
123 for i, column in ipairs(col_specs) do 128 for i, column in ipairs(col_specs) do
124 local width = widths[i]; 129 local width = widths[i];
125 local v = tostring(row[column.key or i] or ""):sub(1, width); 130 local v = tostring(row[column.key or i] or ""):sub(1, width);
126 if #v < width then 131 if #v < width then