Software /
code /
prosody
Annotate
util/human/io.lua @ 11523:5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
The metric subsystem of Prosody has had some shortcomings from
the perspective of the current state-of-the-art in metric
observability.
The OpenMetrics standard [0] is a formalization of the data
model (and serialization format) of the well-known and
widely-used Prometheus [1] software stack.
The previous stats subsystem of Prosody did not map well to that
format (see e.g. [2] and [3]); the key reason is that it was
trying to do too much math on its own ([2]) while lacking
first-class support for "families" of metrics ([3]) and
structured metric metadata (despite the `extra` argument to
metrics, there was no standard way of representing common things
like "tags" or "labels").
Even though OpenMetrics has grown from the Prometheus world of
monitoring, it maps well to other popular monitoring stacks
such as:
- InfluxDB (labels can be mapped to tags and fields as necessary)
- Carbon/Graphite (labels can be attached to the metric name with
dot-separation)
- StatsD (see graphite when assuming that graphite is used as
backend, which is the default)
The util.statsd module has been ported to use the OpenMetrics
model as a proof of concept. An implementation which exposes
the util.statistics backend data as Prometheus metrics is
ready for publishing in prosody-modules (most likely as
mod_openmetrics_prometheus to avoid breaking existing 0.11
deployments).
At the same time, the previous measure()-based API had one major
advantage: It is really simple and easy to use without requiring
lots of knowledge about OpenMetrics or similar concepts. For that
reason as well as compatibility with existing code, it is preserved
and may even be extended in the future.
However, code relying on the `stats-updated` event as well as
`get_stats` from `statsmanager` will break because the data
model has changed completely; in case of `stats-updated`, the
code will simply not run (as the event was renamed in order
to avoid conflicts); the `get_stats` function has been removed
completely (so it will cause a traceback when it is attempted
to be used).
Note that the measure_*_event methods have been removed from
the module API. I was unable to find any uses or documentation
and thus deemed they should not be ported. Re-implementation is
possible when necessary.
[0]: https://openmetrics.io/
[1]: https://prometheus.io/
[2]: #959
[3]: #960
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Sun, 18 Apr 2021 11:47:41 +0200 |
parent | 10917:1eb83bc6f706 |
child | 11892:e712133b4de1 |
rev | line source |
---|---|
10894
d15a4284fdf8
util.human.io: table: Return title row when no row data passed
Matthew Wild <mwild1@gmail.com>
parents:
10893
diff
changeset
|
1 local array = require "util.array"; |
d15a4284fdf8
util.human.io: table: Return title row when no row data passed
Matthew Wild <mwild1@gmail.com>
parents:
10893
diff
changeset
|
2 |
10870
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local function getchar(n) |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local stty_ret = os.execute("stty raw -echo 2>/dev/null"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local ok, char; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 if stty_ret == true or stty_ret == 0 then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 ok, char = pcall(io.read, n or 1); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 os.execute("stty sane"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 else |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 ok, char = pcall(io.read, "*l"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 if ok then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 char = char:sub(1, n or 1); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 if ok then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 return char; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local function getline() |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local ok, line = pcall(io.read, "*l"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 if ok then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 return line; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local function getpass() |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local stty_ret, _, status_code = os.execute("stty -echo 2>/dev/null"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 if status_code then -- COMPAT w/ Lua 5.1 |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 stty_ret = status_code; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if stty_ret ~= 0 then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 io.write("\027[08m"); -- ANSI 'hidden' text attribute |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local ok, pass = pcall(io.read, "*l"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 if stty_ret == 0 then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 os.execute("stty sane"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 else |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 io.write("\027[00m"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 io.write("\n"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 if ok then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 return pass; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local function show_yesno(prompt) |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 io.write(prompt, " "); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local choice = getchar():lower(); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 io.write("\n"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 if not choice:match("%a") then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 choice = prompt:match("%[.-(%U).-%]$"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 if not choice then return nil; end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 return (choice == "y"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 local function read_password() |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 local password; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 while true do |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 io.write("Enter new password: "); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 password = getpass(); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 if not password then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 print("No password - cancelled"); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 io.write("Retype new password: "); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 if getpass() ~= password then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 return; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 else |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 break; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 return password; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local function show_prompt(prompt) |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 io.write(prompt, " "); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 local line = getline(); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 line = line and line:gsub("\n$",""); |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 return (line and #line > 0) and line or nil; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 local function printf(fmt, ...) |
10872
a3f3f42736f2
util.human.io: Fix variable name [luacheck]
Matthew Wild <mwild1@gmail.com>
parents:
10870
diff
changeset
|
87 print(fmt:format(...)); |
10870
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 end |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
90 local function padright(s, width) |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
91 return s..string.rep(" ", width-#s); |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
92 end |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
93 |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
94 local function padleft(s, width) |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
95 return string.rep(" ", width-#s)..s; |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
96 end |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
97 |
10904
d009a79f723a
util.human.io: Remove padding option and use $COLUMNS as default width
Matthew Wild <mwild1@gmail.com>
parents:
10896
diff
changeset
|
98 local function new_table(col_specs, max_width) |
d009a79f723a
util.human.io: Remove padding option and use $COLUMNS as default width
Matthew Wild <mwild1@gmail.com>
parents:
10896
diff
changeset
|
99 max_width = max_width or tonumber(os.getenv("COLUMNS")) or 80; |
10907
6af28c756752
util.human.io: Draw a separator between columns
Kim Alvefur <zash@zash.se>
parents:
10904
diff
changeset
|
100 local separator = " | "; |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
101 |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
102 local widths = {}; |
10907
6af28c756752
util.human.io: Draw a separator between columns
Kim Alvefur <zash@zash.se>
parents:
10904
diff
changeset
|
103 local total_width = max_width - #separator * (#col_specs-1); |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
104 local free_width = total_width; |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
105 -- Calculate width of fixed-size columns |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
106 for i = 1, #col_specs do |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
107 local width = col_specs[i].width or "0"; |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
108 if not(type(width) == "string" and width:sub(-1) == "%") then |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
109 local title = col_specs[i].title; |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
110 width = math.max(tonumber(width), title and (#title+1) or 0); |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
111 widths[i] = width; |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
112 free_width = free_width - width; |
10910
e890b83f08cf
util.human.io: Consider separator when calculating remaining width
Kim Alvefur <zash@zash.se>
parents:
10909
diff
changeset
|
113 if i > 1 then |
e890b83f08cf
util.human.io: Consider separator when calculating remaining width
Kim Alvefur <zash@zash.se>
parents:
10909
diff
changeset
|
114 free_width = free_width - #separator; |
e890b83f08cf
util.human.io: Consider separator when calculating remaining width
Kim Alvefur <zash@zash.se>
parents:
10909
diff
changeset
|
115 end |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
116 end |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
117 end |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
118 -- Calculate width of %-based columns |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
119 for i = 1, #col_specs do |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
120 if not widths[i] then |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
121 local pc_width = tonumber((col_specs[i].width:gsub("%%$", ""))); |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
122 widths[i] = math.floor(free_width*(pc_width/100)); |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
123 end |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
124 end |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
125 |
10893
a256044c1d12
util.human.io: table: switch row function to simply returning prepared row string
Matthew Wild <mwild1@gmail.com>
parents:
10891
diff
changeset
|
126 return function (row) |
10896
c7a0eab27165
util.human.io: table: Fix title printing when columns use named keys
Matthew Wild <mwild1@gmail.com>
parents:
10894
diff
changeset
|
127 local titles; |
10894
d15a4284fdf8
util.human.io: table: Return title row when no row data passed
Matthew Wild <mwild1@gmail.com>
parents:
10893
diff
changeset
|
128 if not row then |
10896
c7a0eab27165
util.human.io: table: Fix title printing when columns use named keys
Matthew Wild <mwild1@gmail.com>
parents:
10894
diff
changeset
|
129 titles, row = true, array.pluck(col_specs, "title", ""); |
10894
d15a4284fdf8
util.human.io: table: Return title row when no row data passed
Matthew Wild <mwild1@gmail.com>
parents:
10893
diff
changeset
|
130 end |
10893
a256044c1d12
util.human.io: table: switch row function to simply returning prepared row string
Matthew Wild <mwild1@gmail.com>
parents:
10891
diff
changeset
|
131 local output = {}; |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
132 for i, column in ipairs(col_specs) do |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
133 local width = widths[i]; |
10911
9dc34e1556d9
util.human.io.table: Allow a map callaback per column
Kim Alvefur <zash@zash.se>
parents:
10910
diff
changeset
|
134 local v = (not titles and column.mapper or tostring)(row[not titles and column.key or i] or "", row); |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
135 if #v < width then |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
136 if column.align == "right" then |
10917
1eb83bc6f706
util.human.io: Fix right-alignment
Kim Alvefur <zash@zash.se>
parents:
10911
diff
changeset
|
137 v = padleft(v, width); |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
138 else |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
139 v = padright(v, width); |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
140 end |
10908
18dc4639442e
util.human.io: Replace overflow with ellipsis
Kim Alvefur <zash@zash.se>
parents:
10907
diff
changeset
|
141 elseif #v > width then |
10909
3af3354366eb
util.human.io: Use literal ellipsis instead of \u escape
Kim Alvefur <zash@zash.se>
parents:
10908
diff
changeset
|
142 v = v:sub(1, width-1) .. "…"; |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
143 end |
10893
a256044c1d12
util.human.io: table: switch row function to simply returning prepared row string
Matthew Wild <mwild1@gmail.com>
parents:
10891
diff
changeset
|
144 table.insert(output, v); |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
145 end |
10907
6af28c756752
util.human.io: Draw a separator between columns
Kim Alvefur <zash@zash.se>
parents:
10904
diff
changeset
|
146 return table.concat(output, separator); |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
147 end; |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
148 end |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
149 |
10870
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 return { |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 getchar = getchar; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 getline = getline; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 getpass = getpass; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 show_yesno = show_yesno; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 read_password = read_password; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 show_prompt = show_prompt; |
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 printf = printf; |
10891
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
158 padleft = padleft; |
8d47858805c9
util.human.io: Add padleft, padright and a table printing function
Matthew Wild <mwild1@gmail.com>
parents:
10872
diff
changeset
|
159 padright = padright; |
10893
a256044c1d12
util.human.io: table: switch row function to simply returning prepared row string
Matthew Wild <mwild1@gmail.com>
parents:
10891
diff
changeset
|
160 table = new_table; |
10870
3f1889608f3e
util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 }; |