Add bt_common_get_term_size()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 4 Jul 2019 05:50:02 +0000 (01:50 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 6 Jul 2019 03:47:50 +0000 (23:47 -0400)
Using ioctl() instead of the `COLUMNS` environment variable because
`COLUMNS` is in fact a shell variable which is not exported.

The function is not supported on Windows yet.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I84942830f0392ff1aca3e1aff54fffc534ddda05
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1621
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
src/common/common.c
src/common/common.h

index aef36b2802a5580df0afab1982e3337a8501eef4..ed1d1c277f687d614d3795698e5e29044888d8b6 100644 (file)
@@ -46,6 +46,7 @@
 
 #ifndef __MINGW32__
 #include <pwd.h>
+#include <sys/ioctl.h>
 #endif
 
 #define SYSTEM_PLUGIN_PATH     INSTALL_LIBDIR "/babeltrace2/plugins"
@@ -1791,3 +1792,35 @@ end:
 
        return folded;
 }
+
+#ifdef __MINGW32__
+BT_HIDDEN
+int bt_common_get_term_size(unsigned int *width, unsigned int *height)
+{
+       /* Not supported on Windows yet */
+       return -1;
+}
+#else /* __MINGW32__ */
+BT_HIDDEN
+int bt_common_get_term_size(unsigned int *width, unsigned int *height)
+{
+       int ret = 0;
+       struct winsize winsize;
+
+       if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) < 0) {
+               ret = -1;
+               goto end;
+       }
+
+       if (width) {
+               *width = (unsigned int) winsize.ws_col;
+       }
+
+       if (height) {
+               *height = (unsigned int) winsize.ws_row;
+       }
+
+end:
+       return ret;
+}
+#endif /* __MINGW32__ */
index 0f2736f00642782a3588b7675b94e7c97e136342..1b952913f3fa96e1f94f8be41d89b1de4eaca5bf 100644 (file)
@@ -348,6 +348,13 @@ BT_HIDDEN
 GString *bt_common_fold(const char *str, unsigned int total_length,
                unsigned int indent);
 
+/*
+ * Writes the terminal's width to `*width`, its height to `*height`,
+ * and returns 0 on success, or returns -1 on error.
+ */
+BT_HIDDEN
+int bt_common_get_term_size(unsigned int *width, unsigned int *height);
+
 /*
  * Wraps read() function to handle EINTR and partial reads.
  * On success, it returns `count` received as parameter. On error, it returns a
This page took 0.026541 seconds and 4 git commands to generate.