Skip to content

Commit 45156d9

Browse files
committed
feat(display): add COUNT command
1 parent 1cc27b7 commit 45156d9

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

display/threads/commands.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static char *act_bezier2(
7171

7272
static char *act_term(struct ui_ctx *, char *target, size_t argc, char **argv);
7373
static char *act_save(struct ui_ctx *, char *target, size_t argc, char **argv);
74+
static char *act_count(struct ui_ctx *, char *target, size_t argc, char **argv);
7475

7576
static bool parse_color(const char *in, struct color *out);
7677
static char *parse_args(const char *fmt, size_t argc, char **argv, ...);
@@ -88,6 +89,7 @@ static const struct action_container actions[] = {
8889
static const struct action_container root_actions[] = {
8990
{ "TERMINATE", act_term },
9091
{ "SAVE", act_save },
92+
{ "COUNT", act_count },
9193
};
9294

9395
void *cmd_thread(void *arg)
@@ -574,6 +576,21 @@ static char *act_save(
574576
return err_buf;
575577
}
576578

579+
static char *act_count(
580+
struct ui_ctx *ctx, char *target, size_t argc, char **argv)
581+
{
582+
(void)target;
583+
584+
char *ret_buf = NULL;
585+
if ((ret_buf = parse_args("", argc, argv)))
586+
return ret_buf;
587+
588+
ret_buf = malloc(1024);
589+
snprintf(ret_buf, 1024, "%d", ui_pane_count(ctx));
590+
591+
return ret_buf;
592+
}
593+
577594
static bool parse_color(const char *in, struct color *out)
578595
{
579596
if (in[0] != '#')

display/threads/ui.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ struct ui_ctx {
4444
};
4545

4646
enum sleep_result {
47-
SHOULD_CANCEL, SHOULD_SWITCH, SHOULD_SYNC,
47+
SHOULD_CANCEL,
48+
SHOULD_SWITCH,
49+
SHOULD_SYNC,
4850
};
4951

5052
static void *rotate_panes(void *);
@@ -160,7 +162,8 @@ void ui_sync(struct ui_ctx *ctx)
160162
fprintf(stderr, "failed to write to sync_fd_tx: %s", STR_ERR);
161163
}
162164

163-
static enum sleep_result cancellable_sleep(int cancellation_fd, int sync_fd, int *sleep_time)
165+
static enum sleep_result cancellable_sleep(
166+
int cancellation_fd, int sync_fd, int *sleep_time)
164167
{
165168
// If sleep_time is too low during a barrage of requests, poll(2) might
166169
// just not exhuast its timeout, and delta will be zero we'll never
@@ -182,27 +185,30 @@ static enum sleep_result cancellable_sleep(int cancellation_fd, int sync_fd, int
182185
clock_gettime(CLOCK_MONOTONIC_RAW, &a);
183186

184187
int r = poll(fds, sizeof(fds) / sizeof(*fds), *sleep_time);
185-
if (r == -1) FATAL_ERR("cancellable_sleep: poll(2) failed: %s", STR_ERR);
188+
if (r == -1)
189+
FATAL_ERR("cancellable_sleep: poll(2) failed: %s", STR_ERR);
186190

187191
if (fds[0].revents &= POLLIN) {
188192
// Cancellation fd.
189193
if (read(fds[0].fd, buf, 1) != 1)
190-
fprintf(stderr, "cancellable_sleep: failed to read from cancellation fd.\n");
194+
fprintf(stderr,
195+
"cancellable_sleep: failed to read from cancellation fd.\n");
191196

192197
*sleep_time = PANE_DELAY;
193198
return SHOULD_CANCEL;
194199
} else if (fds[1].revents &= POLLIN) {
195200
// Sync fd.
196201
if (read(fds[1].fd, buf, 1) != 1)
197-
fprintf(stderr, "cancellable_sleep: failed to read from sync fd.\n");
202+
fprintf(stderr,
203+
"cancellable_sleep: failed to read from sync fd.\n");
198204

199205
clock_gettime(CLOCK_MONOTONIC_RAW, &b);
200206

201207
uint64_t prior = (a.tv_sec * 1000) + (a.tv_nsec / 1000 / 1000);
202208
uint64_t post = (b.tv_sec * 1000) + (b.tv_nsec / 1000 / 1000);
203-
int delta = (int) post - prior;
209+
int delta = (int)post - prior;
204210

205-
if (delta < 20)
211+
if (delta < 20)
206212
delta += 20;
207213

208214
if (delta >= *sleep_time) {
@@ -234,7 +240,8 @@ static void *rotate_panes(void *arg)
234240
FATAL_ERR("ui: couldn't take lock: %s", strerror(r));
235241

236242
i += switching;
237-
if (i >= ctx->panes.count) i = 0;
243+
if (i >= ctx->panes.count)
244+
i = 0;
238245

239246
struct pane *p = &ctx->panes.panes[i];
240247

@@ -245,7 +252,8 @@ static void *rotate_panes(void *arg)
245252
if (r != 0)
246253
FATAL_ERR("ui: couldn't return lock: %s", strerror(r));
247254

248-
enum sleep_result r = cancellable_sleep(ctx->cancellation_fd, ctx->sync_fd_rx, &sleep_time);
255+
enum sleep_result r = cancellable_sleep(
256+
ctx->cancellation_fd, ctx->sync_fd_rx, &sleep_time);
249257
switch (r) {
250258
case SHOULD_SWITCH:
251259
switching = true;
@@ -314,6 +322,18 @@ enum ui_failure ui_pane_create(
314322
return UI_OK;
315323
}
316324

325+
size_t ui_pane_count(struct ui_ctx *ctx)
326+
{
327+
int r = pthread_mutex_lock(&ctx->panes.lock);
328+
if (r != 0)
329+
FATAL_ERR(
330+
"ui_pane_count: failed to take lock: %s", strerror(r));
331+
332+
size_t ret = ctx->panes.count;
333+
pthread_mutex_unlock(&ctx->panes.lock);
334+
return ret;
335+
}
336+
317337
enum ui_failure ui_pane_remove(struct ui_ctx *ctx, char *name)
318338
{
319339
int r;

display/threads/ui.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void ui_sync(struct ui_ctx *ctx);
2727
enum ui_failure ui_pane_create(
2828
struct ui_ctx *ctx, char *name, struct color fill);
2929

30+
size_t ui_pane_count(struct ui_ctx *ctx);
31+
3032
enum ui_failure ui_pane_remove(struct ui_ctx *ctx, char *name);
3133

3234
enum ui_failure ui_pane_draw_shape(

0 commit comments

Comments
 (0)