Mercurial > prosody-hg
comparison util/human/io.lua @ 13937:cfe3e85e715f
util.human.io: Support disabling column widths
This makes columns be of variable with, padding and ellipsis is
disabled. With the separator set to "\t", this produces TSV output
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 30 Aug 2025 21:55:03 +0200 |
| parents | 4f1b6f6e68c0 |
| children | 7008869fcce2 |
comparison
equal
deleted
inserted
replaced
| 13936:4f1b6f6e68c0 | 13937:cfe3e85e715f |
|---|---|
| 165 | 165 |
| 166 return widths; | 166 return widths; |
| 167 end | 167 end |
| 168 | 168 |
| 169 local function new_table(col_specs, max_width, separator) | 169 local function new_table(col_specs, max_width, separator) |
| 170 max_width = max_width or term_width(80); | |
| 171 separator = separator or " | "; | 170 separator = separator or " | "; |
| 172 | 171 |
| 173 local widths = calculate_column_widths(col_specs, separator, max_width); | 172 local widths; |
| 173 if max_width ~= -1 then | |
| 174 max_width = max_width or term_width(80); | |
| 175 widths = calculate_column_widths(col_specs, separator, max_width) | |
| 176 else | |
| 177 widths = {}; | |
| 178 end | |
| 174 | 179 |
| 175 return function (row) | 180 return function (row) |
| 176 local titles; | 181 local titles; |
| 177 if not row then | 182 if not row then |
| 178 titles, row = true, array.pluck(col_specs, "title", ""); | 183 titles, row = true, array.pluck(col_specs, "title", ""); |
| 187 if v == nil then | 192 if v == nil then |
| 188 v = column.default or ""; | 193 v = column.default or ""; |
| 189 else | 194 else |
| 190 v = tostring(v); | 195 v = tostring(v); |
| 191 end | 196 end |
| 192 if len(v) < width then | 197 if width then |
| 193 if column.align == "right" then | 198 if len(v) < width then |
| 194 v = padleft(v, width); | 199 if column.align == "right" then |
| 195 else | 200 v = padleft(v, width); |
| 196 v = padright(v, width); | 201 else |
| 202 v = padright(v, width); | |
| 203 end | |
| 204 elseif len(v) > width then | |
| 205 v = (column.ellipsis or ellipsis)(v, width); | |
| 197 end | 206 end |
| 198 elseif len(v) > width then | |
| 199 v = (column.ellipsis or ellipsis)(v, width); | |
| 200 end | 207 end |
| 201 table.insert(output, v); | 208 table.insert(output, v); |
| 202 end | 209 end |
| 203 return table.concat(output, separator); | 210 return table.concat(output, separator); |
| 204 end, max_width; | 211 end, max_width; |
