Annotate

util/human/io.lua @ 10872:a3f3f42736f2

util.human.io: Fix variable name [luacheck]
author Matthew Wild <mwild1@gmail.com>
date Tue, 02 Jun 2020 08:02:03 +0100
parent 10870:3f1889608f3e
child 10891:8d47858805c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10870
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local function getchar(n)
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 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
3 local ok, char;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 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
5 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
6 os.execute("stty sane");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 else
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 ok, char = pcall(io.read, "*l");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if ok then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 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
11 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 if ok then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 return char;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local function getline()
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 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
20 if ok then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 return line;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local function getpass()
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 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
27 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
28 stty_ret = status_code;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if stty_ret ~= 0 then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 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
32 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 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
34 if stty_ret == 0 then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 os.execute("stty sane");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 else
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 io.write("\027[00m");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 io.write("\n");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if ok then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return pass;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local function show_yesno(prompt)
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 io.write(prompt, " ");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local choice = getchar():lower();
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 io.write("\n");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if not choice:match("%a") then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 choice = prompt:match("%[.-(%U).-%]$");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 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
52 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return (choice == "y");
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
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local function read_password()
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local password;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 while true do
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 io.write("Enter new password: ");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 password = getpass();
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if not password then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 print("No password - cancelled");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 io.write("Retype new password: ");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if getpass() ~= password then
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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
68 return;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 else
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 break;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return password;
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
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local function show_prompt(prompt)
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 io.write(prompt, " ");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local line = getline();
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 line = line and line:gsub("\n$","");
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 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
82 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local function printf(fmt, ...)
10872
a3f3f42736f2 util.human.io: Fix variable name [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 10870
diff changeset
85 print(fmt:format(...));
10870
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return {
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 getchar = getchar;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 getline = getline;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 getpass = getpass;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 show_yesno = show_yesno;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 read_password = read_password;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 show_prompt = show_prompt;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 printf = printf;
3f1889608f3e util.human.io: New central place for UI helpers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 };