Differences From
Artifact [2416d97ee0]:
1 1 #include <k/cli.h>
2 2 #include <k/io.h>
3 3 #include <k/str.h>
4 4
5 -typedef struct buffer {
6 - char* cur;
7 - kiochan channel;
8 - sz run;
9 - char buf [];
10 -} buffer;
11 -
12 -static buffer*
13 -buffer_new(void* mem, kiochan channel, sz run) {
14 - buffer* r = mem;
15 - r -> cur = r -> buf;
16 - r -> channel = channel;
17 - r -> run = run;
18 - return r;
19 -}
20 -
21 -static kiocond
22 -buffer_flush(buffer* b) {
23 - ksraw str = {b -> cur - b -> buf, b -> buf};
24 - b -> cur = b -> buf;
25 - return kiosend(b -> channel, str, null);
26 -}
27 -
28 -static kcond
29 -buffer_send(buffer* b, const char* str) {
30 - ksmut buf = { b->run - (b->cur - b->buf), b->cur };
31 - ksraw src = { 0, str };
32 - kscond sc = kscp(src, buf, &src.size);
33 - if (sc != kscond_ok) return sc;
34 -
35 - b->cur += src.size;
36 - if (b->cur >= (b->buf + b->run)) {
37 - return buffer_flush(b);
38 - } else return kiocond_ok;
39 -}
40 -
41 5 kcond
42 6 kcli_usage(kcli_set prg, kiochan ch) {
43 - ubyte buf_space [sizeof(buffer) + 256];
44 - buffer* out = buffer_new(buf_space, ch, 256);
7 + ubyte buf_space [sizeof(ksbuf) + 256];
8 + ksbuf* out = ksbufmk(buf_space, ch, 256);
45 9
46 10 const char* msg [] = {
47 11 prg.name, " v", prg.version, "\n\n",
48 12 prg.desc, "\n\n",
49 13 };
50 - for (sz i = 0; i != sizeof msg / sizeof msg[0]; ++ i) {
51 - kcond c = buffer_send(out, msg[i]);
52 - if (!kokay(c)) return c;
14 + for (sz i = 0; i != Kmsz(msg); ++ i) {
15 + ksraw str = { 0, msg[i] };
16 + kcond c = ksbufput(out, str);
53 17 }
54 18
55 - return buffer_flush(out);
19 + return ksbufflush(out);
56 20 }