Select edges properly
[clav.git] / ui-cli.c
blob0b1eed36e2cfc0477f79ac4f512f3a9902a9398a
1 #include <errno.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 #include "macros.h"
7 #include "quiver.h"
8 #include "ui.h"
10 /* Whether we actually need to draw this frame or not */
11 static uint_fast8_t need_to_draw = 1;
13 /* The quiver */
14 struct quiver *q;
16 /* The event we'll give back */
17 static struct ui_event pass_back = { 0 };
19 /* Intialize CLI */
20 int ui_init(struct quiver *i_q)
22 q = i_q;
24 return 0;
27 /* Deal with the fact that the quiver was changed */
28 int ui_respond_quiver_change(void)
30 return 0;
33 /* Tear down CLI */
34 int ui_teardown(void)
36 return 0;
39 /* Record that a frame has been started */
40 int ui_start_frame(void)
42 return 0;
45 /* Draw a frame, sleep to framelimit */
46 int ui_finish_frame(void)
48 size_t i = 0;
49 size_t j = 0;
50 struct rational *e = 0;
51 size_t line_length = 0;
53 if (!need_to_draw) {
54 return 0;
57 printf("%*s| Name\n----------\n", 4, "i");
59 for (j = 0; j < q->v_num; ++j) {
60 printf("%*llu | %s\n", 4, (long long unsigned) j, q->v[j].name);
63 line_length = printf("%*s| ", 4, "i\\j");
65 for (j = 0; j < q->v_num; ++j) {
66 line_length += printf("%*llu", 5, (long long unsigned) j);
69 putchar('\n');
71 for (j = 0; j < line_length; ++j) {
72 putchar('-');
75 for (i = 0; i < q->v_num; ++i) {
76 printf("\n %*llu| ", 3, (long long unsigned) i);
78 for (j = 0; j < q->v_num; ++j) {
79 e = &(q->e[i * q->v_len + j]);
81 if (e->p == 0) {
82 printf("%*s", 5, "");
83 } else if (e->q == 1) {
84 printf("%*lld", 5, (long long int) e->p);
85 } else {
86 printf("%*lld/%llu", 3, (long long int) e->p,
87 (long long unsigned) e->q);
92 printf("\n> ");
93 fflush(stdout);
94 need_to_draw = 0;
96 return 0;
99 int ui_confirm_deny(const char *s)
101 int c = 0;
103 printf("%s [y/n]\n", s);
105 while ((c = fgetc(stdin))) {
106 if (c == EOF) {
107 pass_back = (struct ui_event) { .type = ET_FORCE_QUIT };
108 break;
111 if (c == 'y' ||
112 c == 'Y') {
113 pass_back = (struct ui_event) { .type = ET_CONFIRM };
114 break;
115 } else if (c == 'n' ||
116 c == 'N') {
117 pass_back = (struct ui_event) { .type = ET_DENY };
118 break;
122 return 0;
125 int ui_get_event(struct ui_event *e, uint_fast8_t *more)
127 *e = pass_back;
128 *more = 0;
129 pass_back = (struct ui_event) { 0 };
131 return 0;