From b5f5fdefbbee3ae75bb032774263885c46d63a7f Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 13 Jul 2020 19:26:17 +0200 Subject: ssh/client: More ergonomic interface with / commands and focusing --- ssh/client.c | 30 ++++++++++++++++++++++++++++++ ssh/string_view.c | 7 +++++++ ssh/string_view.h | 3 +++ 3 files changed, 40 insertions(+) diff --git a/ssh/client.c b/ssh/client.c index 3fe0869..52f26c6 100644 --- a/ssh/client.c +++ b/ssh/client.c @@ -149,6 +149,7 @@ static bool parse_args( struct state { char **rooms; size_t num_rooms; + char *focus_room; }; static void autocomplete_roomname(const struct state *state, char **namep) { @@ -193,6 +194,24 @@ static bool handle_line( struct tomsg_client *client, struct string_view line ) { + if (line.len == 0) return false; + if (line.s[0] != '/' || (line.len >= 2 && line.s[0] == '/' && line.s[1] == '/')) { + if (state->focus_room != NULL) { + char *message = NULL; + sv_copy(line, &message); + enum tomsg_retval ret = tomsg_send(client, state->focus_room, message, NULL); + free(message); + if (ret != TOMSG_OK) return true; + return false; + } else { + printf("Can't send directly, no room is /focus'ed\n"); + return false; + } + } + + // drop the '/' + sv_skip(&line, 1); + const struct string_view command = tokenise_greedy(&line); char *args[10]; int num_args = 0; @@ -274,6 +293,16 @@ static bool handle_line( } else if (sv_equals(command, "quit") || sv_equals(command, "exit")) { quit_requested = true; + } else if (sv_equals(command, "focus")) { + if (parse_args(line, args, num_args = 1, false)) { + autocomplete_roomname(state, &args[0]); + if (state->focus_room != NULL) free(state->focus_room); + state->focus_room = args[0]; + args[0] = NULL; + printf("Focused room set to %s.\n", state->focus_room); + ret = TOMSG_OK; + } + } else if (sv_equals(command, "help")) { printf( "Commands:\n" @@ -532,6 +561,7 @@ int main(int argc, char **argv) { struct state state = (struct state){ .rooms = NULL, .num_rooms = 0, + .focus_room = NULL, }; struct pollfd pollfds[2]; diff --git a/ssh/string_view.c b/ssh/string_view.c index 2f4f22a..ba05236 100644 --- a/ssh/string_view.c +++ b/ssh/string_view.c @@ -70,3 +70,10 @@ void sv_skip_whitespace(struct string_view *line) { line->len--; } } + +void sv_skip(struct string_view *line, size_t num) { + if (!line->s) return; + if (num > line->len) num = line->len; + line->s += num; + line->len -= num; +} diff --git a/ssh/string_view.h b/ssh/string_view.h index 659dad4..6c3f47a 100644 --- a/ssh/string_view.h +++ b/ssh/string_view.h @@ -33,3 +33,6 @@ struct string_view sv_tokenise_word(struct string_view *line); // Skips all isspace() at the beginning of the string void sv_skip_whitespace(struct string_view *line); + +// Skips the given number of characters +void sv_skip(struct string_view *line, size_t num); -- cgit v1.2.3-54-g00ecf