Comparison

util/human/io.lua @ 10907:6af28c756752

util.human.io: Draw a separator between columns
author Kim Alvefur <zash@zash.se>
date Thu, 04 Jun 2020 18:31:50 +0200
parent 10904:d009a79f723a
child 10908:18dc4639442e
comparison
equal deleted inserted replaced
10906:a1fed82c44b9 10907:6af28c756752
95 return string.rep(" ", width-#s)..s; 95 return string.rep(" ", width-#s)..s;
96 end 96 end
97 97
98 local function new_table(col_specs, max_width) 98 local function new_table(col_specs, max_width)
99 max_width = max_width or tonumber(os.getenv("COLUMNS")) or 80; 99 max_width = max_width or tonumber(os.getenv("COLUMNS")) or 80;
100 local separator = " | ";
100 101
101 local widths = {}; 102 local widths = {};
102 local total_width = max_width; 103 local total_width = max_width - #separator * (#col_specs-1);
103 local free_width = total_width; 104 local free_width = total_width;
104 -- Calculate width of fixed-size columns 105 -- Calculate width of fixed-size columns
105 for i = 1, #col_specs do 106 for i = 1, #col_specs do
106 local width = col_specs[i].width or "0"; 107 local width = col_specs[i].width or "0";
107 if not(type(width) == "string" and width:sub(-1) == "%") then 108 if not(type(width) == "string" and width:sub(-1) == "%") then
135 v = padright(v, width); 136 v = padright(v, width);
136 end 137 end
137 end 138 end
138 table.insert(output, v); 139 table.insert(output, v);
139 end 140 end
140 return table.concat(output); 141 return table.concat(output, separator);
141 end; 142 end;
142 end 143 end
143 144
144 return { 145 return {
145 getchar = getchar; 146 getchar = getchar;