Software /
code /
prosody
Comparison
util/human/io.lua @ 13030:1f89a2a9f532
util.human.io: Support for dynamic "proportional" columns
Instead of a percentage, this allows you to specify e.g. `width="[N]p"`, where
a width="2p" will be twice the width of a width="1p" column.
Compatibility with the old %-based widths is preserved, and percentages adding
up to more than 100 are handled more gracefully.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 06 Apr 2023 14:51:52 +0100 |
parent | 12975:d10957394a3c |
child | 13031:1023c3faffac |
comparison
equal
deleted
inserted
replaced
13029:8ad432953300 | 13030:1f89a2a9f532 |
---|---|
122 local total_width = max_width - #separator * (#col_specs-1); | 122 local total_width = max_width - #separator * (#col_specs-1); |
123 local free_width = total_width; | 123 local free_width = total_width; |
124 -- Calculate width of fixed-size columns | 124 -- Calculate width of fixed-size columns |
125 for i = 1, #col_specs do | 125 for i = 1, #col_specs do |
126 local width = col_specs[i].width or "0"; | 126 local width = col_specs[i].width or "0"; |
127 if not(type(width) == "string" and width:sub(-1) == "%") then | 127 if not (type(width) == "string" and width:match("[p%%]$")) then |
128 local title = col_specs[i].title; | 128 local title = col_specs[i].title; |
129 width = math.max(tonumber(width), title and (#title+1) or 0); | 129 width = math.max(tonumber(width), title and (#title+1) or 0); |
130 widths[i] = width; | 130 widths[i] = width; |
131 free_width = free_width - width; | 131 free_width = free_width - width; |
132 if i > 1 then | 132 if i > 1 then |
133 free_width = free_width - #separator; | 133 free_width = free_width - #separator; |
134 end | 134 end |
135 end | 135 end |
136 end | 136 end |
137 -- Calculate width of %-based columns | 137 |
138 -- Calculate width of proportional columns | |
139 local total_proportional_width = 0; | |
138 for i = 1, #col_specs do | 140 for i = 1, #col_specs do |
139 if not widths[i] then | 141 if not widths[i] then |
140 local pc_width = tonumber((col_specs[i].width:gsub("%%$", ""))); | 142 local width_spec = col_specs[i].width:match("(%d+)[p%%]"); |
141 widths[i] = math.floor(free_width*(pc_width/100)); | 143 total_proportional_width = total_proportional_width + tonumber(width_spec); |
144 end | |
145 end | |
146 | |
147 for i = 1, #col_specs do | |
148 if not widths[i] then | |
149 local width_spec = col_specs[i].width:match("(%d+)[p%%]"); | |
150 local rel_width = tonumber(width_spec); | |
151 widths[i] = math.floor(free_width*(rel_width/total_proportional_width)); | |
142 end | 152 end |
143 end | 153 end |
144 | 154 |
145 return function (row) | 155 return function (row) |
146 local titles; | 156 local titles; |