add pretty output to swaymsg

eventually this will pass a flag to json-c to ignore slashes
This commit is contained in:
Cole Mickens 2016-01-23 20:25:41 -08:00
parent c8cf3f70f9
commit c6561f88e4
2 changed files with 27 additions and 2 deletions

View file

@ -1,9 +1,14 @@
include_directories(
${JSONC_INCLUDE_DIRS}
)
add_executable(swaymsg add_executable(swaymsg
main.c main.c
) )
target_link_libraries(swaymsg target_link_libraries(swaymsg
sway-common sway-common
${JSONC_LIBRARIES}
) )
install( install(

View file

@ -6,6 +6,8 @@
#include <sys/un.h> #include <sys/un.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <json-c/json.h>
#include <json-c/printbuf.h>
#include "stringop.h" #include "stringop.h"
#include "ipc-client.h" #include "ipc-client.h"
#include "readline.h" #include "readline.h"
@ -17,6 +19,7 @@ void sway_terminate(void) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
static int quiet = 0; static int quiet = 0;
static int pretty = 0;
char *socket_path = NULL; char *socket_path = NULL;
char *cmdtype = NULL; char *cmdtype = NULL;
@ -35,6 +38,7 @@ int main(int argc, char **argv) {
"Usage: swaymsg [options] [message]\n" "Usage: swaymsg [options] [message]\n"
"\n" "\n"
" -h, --help Show help message and quit.\n" " -h, --help Show help message and quit.\n"
" -p, --pretty Decode the JSON response and format it.\n"
" -q, --quiet Be quiet.\n" " -q, --quiet Be quiet.\n"
" -v, --version Show the version number and quit.\n" " -v, --version Show the version number and quit.\n"
" -s, --socket <socket> Use the specified socket.\n" " -s, --socket <socket> Use the specified socket.\n"
@ -43,7 +47,7 @@ int main(int argc, char **argv) {
int c; int c;
while (1) { while (1) {
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, "hqvs:t:", long_options, &option_index); c = getopt_long(argc, argv, "hpqvs:t:", long_options, &option_index);
if (c == -1) { if (c == -1) {
break; break;
} }
@ -51,6 +55,9 @@ int main(int argc, char **argv) {
case 'q': // Quiet case 'q': // Quiet
quiet = 1; quiet = 1;
break; break;
case 'p':
pretty = 1;
break;
case 's': // Socket case 's': // Socket
socket_path = strdup(optarg); socket_path = strdup(optarg);
break; break;
@ -112,7 +119,20 @@ int main(int argc, char **argv) {
int socketfd = ipc_open_socket(socket_path); int socketfd = ipc_open_socket(socket_path);
uint32_t len = strlen(command); uint32_t len = strlen(command);
char *resp = ipc_single_command(socketfd, type, command, &len); char *resp = ipc_single_command(socketfd, type, command, &len);
if (!quiet) {
if (pretty) {
struct printbuf *pb;
if(!(pb = printbuf_new())) {
return -1;
}
printbuf_memappend(pb, resp, sizeof(char)*strlen(resp));
struct json_object *obj = json_tokener_parse(pb->buf);
const char *resp_json = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY);
printf("%s\n", resp_json);
free((char*)resp_json);
printbuf_free(pb);
} else if (!quiet) {
printf("%s\n", resp); printf("%s\n", resp);
} }
close(socketfd); close(socketfd);