1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include "curses.h"
#include "match.h"
#include <ncurses.h>
#include <stdlib.h>
WINDOW *w, *titlew, *statw, *promptw;
void init_curses()
{
initscr();
refresh();
start_color();
init_colours();
curs_set(0);
noecho();
w = newwin(LINES-3, COLS, 1, 0);
titlew = newwin(1, COLS, 0, 0);
statw = newwin(1, COLS, LINES-2, 0);
promptw = newwin(1, COLS, LINES-1, 0);
wbkgd(titlew, COLOR_PAIR(C_STATUS));
wbkgd(statw, COLOR_PAIR(C_STATUS));
waddstr(titlew, " dartboat");
wrefresh(titlew);
}
void free_curses()
{
delwin(promptw);
delwin(statw);
delwin(titlew);
delwin(w);
endwin();
}
void init_colours()
{
FOREACH_COLOUR(GEN_COLOUR_INIT_PAIR)
}
void curses_status(char *status)
{
wmove(statw, 0, 0);
wclrtoeol(statw);
waddstr(statw, status);
wrefresh(statw);
}
void curses_prompt(char *prompt)
{
wmove(promptw, 0, 0);
wclrtoeol(promptw);
waddstr(promptw, prompt);
wrefresh(promptw);
}
void flushbuf(char *buf, int *buflen, int col)
{
if (!(*buflen)) return;
if (col) wattron(w, col);
waddstr(w, buf);
if (col) wattroff(w, col);
*buflen = 0;
}
int points_colour(int points)
{
if (points >= 180)
return COLOR_PAIR(C_140) | A_BOLD;
else if (points >= 140)
return COLOR_PAIR(C_140);
else if (points >= 100)
return COLOR_PAIR(C_100);
else if (points >= 60)
return COLOR_PAIR(C_60);
else if (points >= 40)
return COLOR_PAIR(C_40);
else if (points >= 20)
return COLOR_PAIR(C_20);
else if (points > 0)
return COLOR_PAIR(C_0);
else
return COLOR_PAIR(C_0) | A_BOLD;
}
void curses_draw(struct leg *l1, struct leg *l2)
{
werase(w);
char buf[100];
int buflen = 0;
int wlines = LINES - 3;
int n_visits = l1->n_visits > l2->n_visits ? l1->n_visits :
l2->n_visits;
int start_visit = (wlines > n_visits) ? 0 : (n_visits - wlines);
int offset = 0;
if (start_visit == 0 && wlines > n_visits) {
wmove(w, (offset++)-start_visit, 1);
buflen = sprintf(buf, "(%2d)", 0);
flushbuf(buf, &buflen, COLOR_PAIR(C_VISIT));
buflen = sprintf(buf, " %3d %3d", l1->start, l2->start);
flushbuf(buf, &buflen, 0);
}
for (int i = start_visit; i < n_visits; ++i) {
wmove(w, offset+i-start_visit, 1);
buflen += sprintf(buf + buflen, "(%2d)", i+1);
flushbuf(buf, &buflen, COLOR_PAIR(C_VISIT));
buflen += sprintf(buf + buflen, " ");
flushbuf(buf, &buflen, 0);
struct visit *v = l1->visits + i;
flushbuf(buf, &buflen, 0);
buflen += sprintf(buf + buflen, "%3d", v->points);
flushbuf(buf, &buflen, points_colour(v->points));
buflen += sprintf(buf + buflen, " %3d", v->rem);
if (i < l2->n_visits) {
v = l2->visits + i;
buflen += sprintf(buf + buflen, " %3d ", v->rem);
flushbuf(buf, &buflen, 0);
buflen += sprintf(buf + buflen, "%3d", v->points);
flushbuf(buf, &buflen, points_colour(v->points));
for (int j = 0; j < v->n_darts; ++j)
buflen += sprintf(buf + buflen,
j == 0 ? " %4s" : " %4s",
segment_name(v->darts[j]));
flushbuf(buf, &buflen, COLOR_PAIR(C_DARTS));
}
flushbuf(buf, &buflen, 0);
}
wrefresh(w);
}
|