-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstatus_interactive.c
More file actions
188 lines (152 loc) · 5.22 KB
/
status_interactive.c
File metadata and controls
188 lines (152 loc) · 5.22 KB
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
/*
* Copyright Nico Sonack <nsonack@herrhotzenplotz.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <gcli/cmd/cmd.h>
#include <gcli/cmd/comment.h>
#include <gcli/cmd/interactive.h>
#include <gcli/cmd/issues.h>
#include <gcli/cmd/pulls.h>
#include <gcli/cmd/status_interactive.h>
#include <gcli/cmd/table.h>
#include <gcli/status.h>
#include <string.h>
static void
print_notification_table(struct gcli_notification_list const *list)
{
gcli_tbl *table;
struct gcli_tblcoldef const columns[] = {
{ .name = "NUMBER", .type = GCLI_TBLCOLTYPE_LONG, .flags = 0 },
{ .name = "REPO", .type = GCLI_TBLCOLTYPE_STRING, .flags = 0 },
{ .name = "TYPE", .type = GCLI_TBLCOLTYPE_STRING, .flags = 0 },
{ .name = "REASON", .type = GCLI_TBLCOLTYPE_STRING, .flags = 0 },
};
table = gcli_tbl_begin(columns, ARRAY_SIZE(columns));
for (size_t i = 0; i < list->notifications_size; ++i) {
struct gcli_notification const *n = &list->notifications[i];
gcli_tbl_add_row(table, (long)i + 1, n->repository,
gcli_notification_target_type_str(n->type),
n->reason);
}
gcli_tbl_end(table);
}
static void
refresh_notifications(struct gcli_notification_list *list)
{
int rc = 0;
gcli_free_notifications(list);
rc = gcli_get_notifications(g_clictx, -1, list);
if (rc < 0)
errx(1, "gcli: failed to fetch notifications: %s", gcli_get_error(g_clictx));
}
static struct gcli_cmd_actions *
notification_handlers[MAX_GCLI_NOTIFICATION_TARGET] = {
[GCLI_NOTIFICATION_TARGET_ISSUE] = &gcli_issue_actions,
[GCLI_NOTIFICATION_TARGET_PULL_REQUEST] = &gcli_pull_actions,
};
static void
status_interactive_notification(struct gcli_notification const *const notif)
{
char *user_input = NULL;
if (notif->type >= MAX_GCLI_NOTIFICATION_TARGET) {
fprintf(stderr, "gcli: error: bad notification type\n");
return;
}
if (notification_handlers[notif->type] == NULL) {
fprintf(stderr, "gcli: error: notification type '%s' not supported\n",
gcli_notification_target_type_str(notif->type));
return;
}
struct gcli_cmd_actions const *const actions =
notification_handlers[notif->type];
for (;;) {
int rc = 0;
user_input = gcli_cmd_prompt("Enter action, done or quit", GCLI_PROMPT_RESULT_MANDATORY);
/* plain quit action */
if (strcmp(user_input, "q") == 0 ||
strcmp(user_input, "quit") == 0) {
free(user_input);
return;
}
/* mark as done and quit */
if (strcmp(user_input, "d") == 0 ||
strcmp(user_input, "done") == 0) {
rc = gcli_notification_mark_as_read(g_clictx, notif->id);
if (rc < 0)
fprintf(stderr, "gcli: error: %s\n", gcli_get_error(g_clictx));
free(user_input);
return;
}
/* search and call action handler or error out */
rc = gcli_cmd_action_handle(actions, ¬if->target, user_input);
if (rc < 0) {
fprintf(stderr, "gcli: error: %s\n", gcli_get_error(g_clictx));
}
free(user_input);
}
}
int
gcli_status_interactive(void)
{
struct gcli_notification_list list = {0};
char *user_input = NULL;
refresh_notifications(&list);
print_notification_table(&list);
for (;;) {
user_input = gcli_cmd_prompt("Enter number, list or quit", GCLI_PROMPT_RESULT_MANDATORY);
if (strcmp(user_input, "q") == 0 ||
strcmp(user_input, "quit") == 0) {
goto out;
} else if (strcmp(user_input, "l") == 0 ||
strcmp(user_input, "list") == 0) {
refresh_notifications(&list);
print_notification_table(&list);
} else {
size_t number = 0;
char *endptr = NULL;
number = strtoul(user_input, &endptr, 10);
if (endptr != user_input + strlen(user_input)) {
fprintf(stderr, "gcli: bad notification number: %s\n",
user_input);
goto next;
}
if (number == 0 || number > list.notifications_size) {
fprintf(stderr, "gcli: unknown notification number\n");
goto next;
}
status_interactive_notification(&list.notifications[number-1]);
}
next:
free(user_input);
user_input = NULL;
}
out:
free(user_input);
user_input = NULL;
gcli_free_notifications(&list);
return 0;
}