authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-29 04:40:31 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-29 04:40:31 -07:00
log4d1e7c0043dce7faabde3ff33b2becd977f8a555
tree540c66acb9d75e17da37139aab136f52dcd9d84a
parenta9b167300ba0c1d87f7cfa3aa9fcb13eea2132e2

change api to accept reader/writer instead of only to stdin/err


3 files changed, 49 insertions(+), 49 deletions(-)

README.md+4-4
...@@ -18,13 +18,13 @@ zig build run...@@ -18,13 +18,13 @@ zig build run
18![image](https://user-images.githubusercontent.com/5464072/127479686-fda8f860-a705-4fd6-9768-a3e1f53a6bc7.png)18![image](https://user-images.githubusercontent.com/5464072/127479686-fda8f860-a705-4fd6-9768-a3e1f53a6bc7.png)
1919
20## Usage20## Usage
21- `pub fn answer(comptime prompt: []const u8, value: []const u8) []const u8`21- `pub fn answer(writer, reader, comptime prompt: []const u8, value: []const u8) []const u8`
22 - Prints just the done string.22 - Prints just the done string.
23- `pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: enum, default: ?options) !options`23- `pub fn forEnum(writer, reader, comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: enum, default: ?options) !options`
24 - Accepts an enum and prompts the user to pick on of the fields.24 - Accepts an enum and prompts the user to pick on of the fields.
25- `pub fn forString(comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8`25- `pub fn forString(writer, reader, comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8`
26 - Base function, asks prompt and returns non-empty answer.26 - Base function, asks prompt and returns non-empty answer.
27- `pub fn forConfirm(comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool`27- `pub fn forConfirm(writer, reader, comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool`
28 - Calls `forEnum` with `y/n`28 - Calls `forEnum` with `y/n`
2929
30## TODO30## TODO
src/lib.zig+35-38
...@@ -2,10 +2,10 @@ const std = @import("std");...@@ -2,10 +2,10 @@ const std = @import("std");
2const ansi = @import("ansi");2const ansi = @import("ansi");
3const range = @import("range").range;3const range = @import("range").range;
44
5pub fn answer(comptime prompt: []const u8, comptime T: type, comptime valfmt: []const u8, value: T) T {5pub fn answer(out: anytype, comptime prompt: []const u8, comptime T: type, comptime valfmt: []const u8, value: T) !T {
6 std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{});6 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
7 std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});7 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
8 std.debug.print(comptime ansi.color.Fg(.Cyan, valfmt ++ "\n"), .{value});8 try out.print(comptime ansi.color.Fg(.Cyan, valfmt ++ "\n"), .{value});
9 return value;9 return value;
10}10}
1111
...@@ -14,11 +14,11 @@ const PromptRet = struct {...@@ -14,11 +14,11 @@ const PromptRet = struct {
14 value: []const u8,14 value: []const u8,
15};15};
1616
17fn doprompt(in: std.fs.File.Reader, alloc: *std.mem.Allocator, default: ?[]const u8) !PromptRet {17fn doprompt(out: anytype, in: anytype, alloc: *std.mem.Allocator, default: ?[]const u8) !PromptRet {
18 var n: usize = 1;18 var n: usize = 1;
19 var value: []const u8 = undefined;19 var value: []const u8 = undefined;
20 while (true) : (n += 1) {20 while (true) : (n += 1) {
21 std.debug.print(comptime ansi.color.Faint("> "), .{});21 try out.print(comptime ansi.color.Faint("> "), .{});
22 const input = try in.readUntilDelimiterAlloc(alloc, '\n', 100);22 const input = try in.readUntilDelimiterAlloc(alloc, '\n', 100);
2323
24 if (input.len == 0) if (default) |d| {24 if (input.len == 0) if (default) |d| {
...@@ -33,32 +33,31 @@ fn doprompt(in: std.fs.File.Reader, alloc: *std.mem.Allocator, default: ?[]const...@@ -33,32 +33,31 @@ fn doprompt(in: std.fs.File.Reader, alloc: *std.mem.Allocator, default: ?[]const
33 return PromptRet{ .n = n, .value = value };33 return PromptRet{ .n = n, .value = value };
34}34}
3535
36fn clean(n: usize) void {36fn clean(out: anytype, n: usize) !void {
37 for (range(n)) |_| {37 for (range(n)) |_| {
38 std.debug.print(comptime ansi.csi.CursorUp(1), .{});38 try out.print(comptime ansi.csi.CursorUp(1), .{});
39 std.debug.print(comptime ansi.csi.EraseInLine(0), .{});39 try out.print(comptime ansi.csi.EraseInLine(0), .{});
40 }40 }
41}41}
4242
43pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options {43pub fn forEnum(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options {
44 comptime std.debug.assert(@typeInfo(options) == .Enum);44 comptime std.debug.assert(@typeInfo(options) == .Enum);
4545
46 std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{});46 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
47 std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});47 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
4848
49 std.debug.print(ansi.style.Faint ++ "(", .{});49 try out.print(ansi.style.Faint ++ "(", .{});
50 inline for (std.meta.fields(options)) |f, i| {50 inline for (std.meta.fields(options)) |f, i| {
51 if (i != 0) std.debug.print("/", .{});51 if (i != 0) try out.print("/", .{});
52 std.debug.print(f.name, .{});52 try out.print(f.name, .{});
53 }53 }
54 std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});54 try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
5555
56 const stdin = std.io.getStdIn().reader();
57 var value: options = undefined;56 var value: options = undefined;
58 var i: usize = 0;57 var i: usize = 0;
59 while (true) {58 while (true) {
60 const def: ?[]const u8 = if (default) |d| @tagName(d) else null;59 const def: ?[]const u8 = if (default) |d| @tagName(d) else null;
61 const p = try doprompt(stdin, alloc, def);60 const p = try doprompt(out, in, alloc, def);
62 defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value);61 defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value);
6362
64 i += p.n;63 i += p.n;
...@@ -67,43 +66,41 @@ pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime...@@ -67,43 +66,41 @@ pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime
67 break;66 break;
68 }67 }
69 }68 }
70 clean(i);69 try clean(out, i);
71 _ = answer(prompt, []const u8, "{s}", @tagName(value));70 _ = try answer(out, prompt, []const u8, "{s}", @tagName(value));
7271
73 return value;72 return value;
74}73}
7574
76pub fn forString(comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8 {75pub fn forString(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8 {
77 std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{});76 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
78 std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});77 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
7978
80 if (default != null) {79 if (default != null) {
81 std.debug.print(ansi.style.Faint ++ "(", .{});80 try out.print(ansi.style.Faint ++ "(", .{});
82 std.debug.print("{s}", .{default.?});81 try out.print("{s}", .{default.?});
83 std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});82 try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
84 }83 }
8584
86 const stdin = std.io.getStdIn().reader();85 const p = try doprompt(out, in, alloc, default);
87 const p = try doprompt(stdin, alloc, default);86 try clean(out, p.n);
88 clean(p.n);87 return try answer(out, prompt, []const u8, "{s}", p.value);
89 return answer(prompt, []const u8, "{s}", p.value);
90}88}
9189
92pub fn forConfirm(comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool {90pub fn forConfirm(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool {
93 return (try forEnum(prompt, alloc, enum { y, n }, .y)) == .y;91 return (try forEnum(out, in, prompt, alloc, enum { y, n }, .y)) == .y;
94}92}
9593
96// pub fn forNumber(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime T: type, default: ?T) !T {94// pub fn forNumber(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime T: type, default: ?T) !T {
97// std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{});95// try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
98// std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});96// try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
9997
100// if (default != null) {98// if (default != null) {
101// std.debug.print(ansi.style.Faint ++ "(", .{});99// try out.print(ansi.style.Faint ++ "(", .{});
102// std.debug.print("{d}", .{default.?});100// try out.print("{d}", .{default.?});
103// std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});101// try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
104// }102// }
105103
106// const stdin = std.io.getStdIn().reader();
107// var value: T = undefined;104// var value: T = undefined;
108// var i: usize = 0;105// var i: usize = 0;
109// while (true) {106// while (true) {
src/main.zig+10-7
...@@ -9,22 +9,25 @@ pub fn main() !void {...@@ -9,22 +9,25 @@ pub fn main() !void {
9 var gpa = std.heap.GeneralPurposeAllocator(.{}){};9 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
10 const alloc = &gpa.allocator;10 const alloc = &gpa.allocator;
1111
12 _ = try inquirer.forConfirm("Is this for delivery?", alloc);12 const in = std.io.getStdIn().reader();
13 const out = std.io.getStdOut().writer();
1314
14 _ = try inquirer.forString("What's your phone number?", alloc, null);15 _ = try inquirer.forConfirm(out, in, "Is this for delivery?", alloc);
1516
16 _ = try inquirer.forEnum("What size do you need?", alloc, enum { Small, Medium, Large }, null);17 _ = try inquirer.forString(out, in, "What's your phone number?", alloc, null);
1718
18 // _ = try inquirer.forNumber("How many do you need?", alloc, u32, 1);19 _ = try inquirer.forEnum(out, in, "What size do you need?", alloc, enum { Small, Medium, Large }, null);
20
21 // _ = try inquirer.forNumber(out,in,"How many do you need?", alloc, u32, 1);
19 // TODO forNumber is causing a compiler crash22 // TODO forNumber is causing a compiler crash
2023
21 // TODO toppings for string list24 // TODO toppings for string list
2225
23 _ = try inquirer.forEnum("You also get a free 2L:", alloc, enum { Pepsi, @"7up", Coke }, null);26 _ = try inquirer.forEnum(out, in, "You also get a free 2L:", alloc, enum { Pepsi, @"7up", Coke }, null);
2427
25 const comment = try inquirer.forString("Any comments on your purchase experience?", alloc, "Nope, all good!");28 const comment = try inquirer.forString(out, in, "Any comments on your purchase experience?", alloc, "Nope, all good!");
2629
27 if (std.mem.eql(u8, comment, "")) {30 if (std.mem.eql(u8, comment, "")) {
28 _ = try inquirer.forEnum("For leaving a comment, you get a freebie:", alloc, enum { Cake, Fries }, null);31 _ = try inquirer.forEnum(out, in, "For leaving a comment, you get a freebie:", alloc, enum { Cake, Fries }, null);
29 }32 }
30}33}