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 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'ssh/client.c') 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]; -- cgit v1.2.3-54-g00ecf