-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.c
201 lines (169 loc) · 4.93 KB
/
render.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <curses.h>
#include <stdbool.h>
#include <string.h>
#include "colors.h"
#include "render.h"
#include "config.h"
#include "conchview-render.h"
#include "listview-render.h"
#include "detailview-render.h"
#define ORIGIN_Y 0
#define ORIGIN_X 0
#define CHROME_BORDER_HEIGHT 1
#define CHROME_PADDING_X 1
// Placeholder for status message
#define STATUS_MAXLEN 64
static char status[STATUS_MAXLEN];
// 64 characters provides space for a 14-character status with the clock
#define MIN_WIDTH_FOR_CLOCK 64
#define MAX_LENGTH_FOR_CLOCK 1024
// Should we display the spinner?
static bool show_spinner = false;
static void render_clock(WINDOW *window, char *clock_text) {
int max_x = getmaxx(window);
mvwaddstr(window, ORIGIN_Y, max_x - strlen(clock_text) - CHROME_PADDING_X,
clock_text);
}
static void render_help(WINDOW *window, char *help_text) {
int last_line = getmaxy(window) - 1;
mvwaddstr(window, last_line, CHROME_PADDING_X, help_text);
}
static unsigned int spinner_state = 0;
// Forgive me father, for I have sinned.
//
// - NS 2015-0528
//
// clang-format off
static char const *spinner[] = {
" ",
" ",
" ",
" \\",
" -",
" /",
" |d",
" \\d",
" -d",
" /de",
" |de",
" \\de",
" -dev",
" /dev",
" |dev",
" \\dev\\",
" -dev-",
" /dev/",
" |dev|f",
" \\dev\\f",
" -dev-f",
" /dev/fo",
" |dev|fo",
" \\dev\\fo",
" -dev-for",
" /dev/for",
" |dev|for",
" \\dev\\fort",
" -dev-fort",
" /dev/fort",
" |dev|fort ",
" \\dev\\fort ",
" -dev-fort ",
" /dev/fort 1",
" |dev|fort 1",
" \\dev\\fort 1",
" -dev-fort 11",
" /dev/fort 11",
" |dev|fort 11",
" \\dev\\fort 11 ",
" -dev-fort 11 ",
" /dev/fort 11 ",
" /dev/fort 11 ",
" |dev|fort 11 ",
" \\dev\\fort 11 ",
" -dev-fort 11 ",
" /dev/fort 11 ",
};
// clang-format on
static const int number_spinner_states = sizeof(spinner) / sizeof(char const *);
#define PROGRESS_SPINNER_START_STATE (number_spinner_states - 5)
static void render_watermark(WINDOW *window, bool spin) {
int max_x = getmaxx(window);
int max_y = getmaxy(window) - 1;
// Spin, either if we've been explicitly instructed to spin, or if we have not
// completed a whole revolution of the spinner.
if (spin || spinner_state != (number_spinner_states - 1)) {
spinner_state = (spinner_state + 1) % number_spinner_states;
}
mvwaddstr(window, max_y,
max_x - strlen(spinner[spinner_state]) - CHROME_PADDING_X,
spinner[spinner_state]);
}
static void render_chrome(WINDOW *window, char *title_text) {
int max_x = getmaxx(window);
int last_line = getmaxy(window) - 1;
mvwhline(window, ORIGIN_Y, ORIGIN_X, ACS_HLINE, max_x);
mvwhline(window, last_line, ORIGIN_X, ACS_HLINE, max_x);
mvwaddstr(window, ORIGIN_Y, CHROME_PADDING_X, title_text);
}
void conch_status_clear() {
status[0] = '\0';
}
void conch_status_set(const char *msg) {
strncpy(status, msg, STATUS_MAXLEN);
}
void conch_spinner_hide() {
show_spinner = false;
}
void conch_spinner_show() {
if (show_spinner == false && spinner_state == number_spinner_states - 1) {
spinner_state = PROGRESS_SPINNER_START_STATE;
}
show_spinner = true;
}
static void render_status_message(WINDOW *window) {
size_t len = strlen(status);
if (len == 0) {
return;
}
int center_offset = (getmaxx(window) - len + 2 /* whitespace padding */) / 2;
mvwprintw(window, ORIGIN_Y, center_offset, " %s ", status);
}
void render_view(WINDOW *window, view_type current_view, void *view_state) {
int max_x = getmaxx(window);
int max_y = getmaxy(window);
if (max_y < 2 * CHROME_BORDER_HEIGHT) {
mvwaddstr(window, ORIGIN_Y, ORIGIN_X, "Window too small! Embiggen!");
return;
}
// The two -1s here are because ncurses co-ordinates are *inclusive*
winrect rect = {
.top = CHROME_BORDER_HEIGHT,
.left = ORIGIN_X,
.bottom = max_y - CHROME_BORDER_HEIGHT - 1,
.right = max_x - 1,
.width = max_x,
.height = max_y - 2 * CHROME_BORDER_HEIGHT,
};
conch_spinner_hide();
conch_status_clear();
switch (current_view) {
case VIEW_CONCH:
conch_conchview_render((conchview *)view_state, window, &rect);
break;
case VIEW_LIST:
conch_listview_render((listview *)view_state, window, &rect);
break;
case VIEW_DETAIL:
conch_detailview_render((detailview *)view_state, window, &rect);
break;
}
render_chrome(window, " conch 螺 ");
if (MIN_WIDTH_FOR_CLOCK <= max_x) {
char clock_text[MAX_LENGTH_FOR_CLOCK];
generate_clock_text(MAX_LENGTH_FOR_CLOCK, clock_text);
render_clock(window, clock_text);
}
render_status_message(window);
render_help(window, " i: insert j/k: down/up ?: help q: quit ");
render_watermark(window, show_spinner);
}