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
1818![image](https://user-images.githubusercontent.com/5464072/127479686-fda8f860-a705-4fd6-9768-a3e1f53a6bc7.png)
1919
2020## 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`
2222 - 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`
2424 - 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`
2626 - 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`
2828 - Calls `forEnum` with `y/n`
2929
3030## TODO
src/lib.zig+35-38
......@@ -2,10 +2,10 @@ const std = @import("std");
22const ansi = @import("ansi");
33const range = @import("range").range;
44
5pub fn answer(comptime prompt: []const u8, comptime T: type, comptime valfmt: []const u8, value: T) T {
6 std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{});
7 std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});
8 std.debug.print(comptime ansi.color.Fg(.Cyan, valfmt ++ "\n"), .{value});
5pub fn answer(out: anytype, comptime prompt: []const u8, comptime T: type, comptime valfmt: []const u8, value: T) !T {
6 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
7 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
8 try out.print(comptime ansi.color.Fg(.Cyan, valfmt ++ "\n"), .{value});
99 return value;
1010}
1111
......@@ -14,11 +14,11 @@ const PromptRet = struct {
1414 value: []const u8,
1515};
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 {
1818 var n: usize = 1;
1919 var value: []const u8 = undefined;
2020 while (true) : (n += 1) {
21 std.debug.print(comptime ansi.color.Faint("> "), .{});
21 try out.print(comptime ansi.color.Faint("> "), .{});
2222 const input = try in.readUntilDelimiterAlloc(alloc, '\n', 100);
2323
2424 if (input.len == 0) if (default) |d| {
......@@ -33,32 +33,31 @@ fn doprompt(in: std.fs.File.Reader, alloc: *std.mem.Allocator, default: ?[]const
3333 return PromptRet{ .n = n, .value = value };
3434}
3535
36fn clean(n: usize) void {
36fn clean(out: anytype, n: usize) !void {
3737 for (range(n)) |_| {
38 std.debug.print(comptime ansi.csi.CursorUp(1), .{});
39 std.debug.print(comptime ansi.csi.EraseInLine(0), .{});
38 try out.print(comptime ansi.csi.CursorUp(1), .{});
39 try out.print(comptime ansi.csi.EraseInLine(0), .{});
4040 }
4141}
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 {
4444 comptime std.debug.assert(@typeInfo(options) == .Enum);
4545
46 std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{});
47 std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});
46 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
47 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
4848
49 std.debug.print(ansi.style.Faint ++ "(", .{});
49 try out.print(ansi.style.Faint ++ "(", .{});
5050 inline for (std.meta.fields(options)) |f, i| {
51 if (i != 0) std.debug.print("/", .{});
52 std.debug.print(f.name, .{});
51 if (i != 0) try out.print("/", .{});
52 try out.print(f.name, .{});
5353 }
54 std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
54 try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
5555
56 const stdin = std.io.getStdIn().reader();
5756 var value: options = undefined;
5857 var i: usize = 0;
5958 while (true) {
6059 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);
6261 defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value);
6362
6463 i += p.n;
......@@ -67,43 +66,41 @@ pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime
6766 break;
6867 }
6968 }
70 clean(i);
71 _ = answer(prompt, []const u8, "{s}", @tagName(value));
69 try clean(out, i);
70 _ = try answer(out, prompt, []const u8, "{s}", @tagName(value));
7271
7372 return value;
7473}
7574
76pub fn forString(comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8 {
77 std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{});
78 std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});
75pub fn forString(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8 {
76 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
77 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
7978
8079 if (default != null) {
81 std.debug.print(ansi.style.Faint ++ "(", .{});
82 std.debug.print("{s}", .{default.?});
83 std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
80 try out.print(ansi.style.Faint ++ "(", .{});
81 try out.print("{s}", .{default.?});
82 try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
8483 }
8584
86 const stdin = std.io.getStdIn().reader();
87 const p = try doprompt(stdin, alloc, default);
88 clean(p.n);
89 return answer(prompt, []const u8, "{s}", p.value);
85 const p = try doprompt(out, in, alloc, default);
86 try clean(out, p.n);
87 return try answer(out, prompt, []const u8, "{s}", p.value);
9088}
9189
92pub fn forConfirm(comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool {
93 return (try forEnum(prompt, alloc, enum { y, n }, .y)) == .y;
90pub fn forConfirm(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool {
91 return (try forEnum(out, in, prompt, alloc, enum { y, n }, .y)) == .y;
9492}
9593
9694// 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, "? "), .{});
98// std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{});
95// try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
96// try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
9997
10098// if (default != null) {
101// std.debug.print(ansi.style.Faint ++ "(", .{});
102// std.debug.print("{d}", .{default.?});
103// std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
99// try out.print(ansi.style.Faint ++ "(", .{});
100// try out.print("{d}", .{default.?});
101// try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
104102// }
105103
106// const stdin = std.io.getStdIn().reader();
107104// var value: T = undefined;
108105// var i: usize = 0;
109106// while (true) {
src/main.zig+10-7
......@@ -9,22 +9,25 @@ pub fn main() !void {
99 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
1010 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);
1922 // TODO forNumber is causing a compiler crash
2023
2124 // 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
2730 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);
2932 }
3033}