diff --git a/swaymsg/CMakeLists.txt b/swaymsg/CMakeLists.txt index 06939991..53f251c5 100644 --- a/swaymsg/CMakeLists.txt +++ b/swaymsg/CMakeLists.txt @@ -1,9 +1,14 @@ +include_directories( + ${JSONC_INCLUDE_DIRS} +) + add_executable(swaymsg main.c ) target_link_libraries(swaymsg sway-common + ${JSONC_LIBRARIES} ) install( diff --git a/swaymsg/main.c b/swaymsg/main.c index 22572b6f..ed144f38 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "stringop.h" #include "ipc-client.h" #include "readline.h" @@ -17,6 +19,7 @@ void sway_terminate(void) { int main(int argc, char **argv) { static int quiet = 0; + static int pretty = 0; char *socket_path = NULL; char *cmdtype = NULL; @@ -35,6 +38,7 @@ int main(int argc, char **argv) { "Usage: swaymsg [options] [message]\n" "\n" " -h, --help Show help message and quit.\n" + " -p, --pretty Decode the JSON response and format it.\n" " -q, --quiet Be quiet.\n" " -v, --version Show the version number and quit.\n" " -s, --socket Use the specified socket.\n" @@ -43,7 +47,7 @@ int main(int argc, char **argv) { int c; while (1) { 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) { break; } @@ -51,6 +55,9 @@ int main(int argc, char **argv) { case 'q': // Quiet quiet = 1; break; + case 'p': + pretty = 1; + break; case 's': // Socket socket_path = strdup(optarg); break; @@ -112,7 +119,20 @@ int main(int argc, char **argv) { int socketfd = ipc_open_socket(socket_path); uint32_t len = strlen(command); 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); } close(socketfd);