util.human.io
Utilities for human interactions.
local human_io = require "prosody.util.human.io";
High Level Functions
show_yesno
Shows a yes/no question and returns whether the user selected yes.
It takes a prompt as argument.
Return Values
true
-
The answer was positive (
y
) false
-
The answer was negative (
n
) nil
-
No answer, the user pressed
Ctrl-D
Example
local response = human_io.show_yesno("Do the thing? [yn]")
if response == true then
();
do_the_thingelseif response == false then
print("okay, won't do the thing")
elseif response == nil then
-- the user did not answer
print("...")
end
show_prompt
Prompt for some information.
Example
local response = human_io.show_prompt("What's a good question?")
read_password
Request a (new) password from the user. Asks twice to ensure it was not mistyped.
Example
local password = human_io.read_password()
table
Print data in columns.
local row = human_io.table({ { title = "A", width = 10 }, { title = "B", width = 10 } })
print(row()) -- prints header row
print(row({ "Hello", "World" }))
column properties
width
-
Width of the column as a number (of characters), percentage
(
"25%"
) or part ("1p"
). title
- Heading for the column
key
- The table key to use. By default the column number is used as an integer index.
mapper
- A function for transforming the value.
default
- Default value to use when missing from the row.
align
-
Column text alignment,
"left"
or"right"
. ellipsis
- Handles values that do not fit in the (calculated) width, defaults to ellipsis.
Low Level Functions
getchar
Retrieves one character from the console, e.g. the answer to a yes/no
question ([yn]
).
getline
Retrieves one line from the console.
getpass
Retrieves one line from the console with echo disabled, i.e. what the user types is not shown.
printf
A combination of print()
and string.format()
.
("Hello %s", "World") --> Hello World printf
padleft
Pad a spring with space characters on the left until it is the requested length.
.padleft("foo", 10) --> " foo" humanio
padright
Pad a spring with space characters on the right until it is the requested length.
.padright("foo", 10) --> "foo " humanio
term_width
Returns the width of the terminal, or a default number.
local terminal_width = humanio.term_width(80);
ellipsis
Ensures a string is shorter than a given value, or clips away the overflow.
print(human_io.ellipsis("Hello, World", 6)) --> Hello…
parse_duration
Parse a string describing a length of time, e.g. "3w"
into seconds.
parse_duration_lax
Relaxed version of parse_duration()
that accepts some
ambiguous strings.