[gjs/ewlsh/whatwg-console: 3/7] util: Add console handling utility




commit eadb9929d6ef79d807828971f30dc6c121bf0a73
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Aug 7 00:21:15 2021 -0700

    util: Add console handling utility

 gjs/debugger.cpp    | 15 ++++-----------
 meson.build         |  1 +
 modules/console.cpp |  1 +
 util/console.cpp    | 44 ++++++++++++++++++++++++++++++++++++++++++++
 util/console.h      | 18 ++++++++++++++++++
 5 files changed, 68 insertions(+), 11 deletions(-)
---
diff --git a/gjs/debugger.cpp b/gjs/debugger.cpp
index 585024d3..8bf2d676 100644
--- a/gjs/debugger.cpp
+++ b/gjs/debugger.cpp
@@ -12,15 +12,6 @@
 #    include <readline/readline.h>
 #endif
 
-#ifdef HAVE_UNISTD_H
-#    include <unistd.h>  // for isatty, STDIN_FILENO
-#elif defined(_WIN32)
-#    include <io.h>
-#    ifndef STDIN_FILENO
-#        define STDIN_FILENO 0
-#    endif
-#endif
-
 #include <glib.h>
 
 #include <js/CallArgs.h>
@@ -39,6 +30,8 @@
 #include "gjs/jsapi-util.h"
 #include "gjs/macros.h"
 
+#include "util/console.h"
+
 GJS_JSAPI_RETURN_CONVENTION
 static bool quit(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@@ -64,7 +57,7 @@ static bool do_readline(JSContext* cx, unsigned argc, JS::Value* vp) {
     do {
         const char* real_prompt = prompt ? prompt.get() : "db> ";
 #ifdef HAVE_READLINE_READLINE_H
-        if (isatty(STDIN_FILENO)) {
+        if (Gjs::Console::is_tty(Gjs::Console::stdin_fd)) {
             line = readline(real_prompt);
         } else {
 #else
@@ -77,7 +70,7 @@ static bool do_readline(JSContext* cx, unsigned argc, JS::Value* vp) {
                 buf[0] = '\0';
             line.reset(g_strdup(g_strchomp(buf)));
 
-            if (!isatty(STDIN_FILENO)) {
+            if (!Gjs::Console::is_tty(Gjs::Console::stdin_fd)) {
                 if (feof(stdin)) {
                     g_print("[quit due to end of input]\n");
                     line.reset(g_strdup("quit"));
diff --git a/meson.build b/meson.build
index 5b2bcdce..b6f6b40e 100644
--- a/meson.build
+++ b/meson.build
@@ -428,6 +428,7 @@ libgjs_jsapi_sources = [
     'gjs/jsapi-util-root.h',
     'gjs/jsapi-util-string.cpp',
     'gjs/jsapi-util.cpp', 'gjs/jsapi-util.h',
+    'util/console.cpp', 'util/console.h',
     'util/log.cpp', 'util/log.h',
     'util/misc.cpp', 'util/misc.h',
 ]
diff --git a/modules/console.cpp b/modules/console.cpp
index 2ef511aa..70d0b7a1 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -42,6 +42,7 @@
 #include "gjs/context-private.h"
 #include "gjs/jsapi-util.h"
 #include "modules/console.h"
+#include "util/console.h"
 
 namespace mozilla {
 union Utf8Unit;
diff --git a/util/console.cpp b/util/console.cpp
new file mode 100644
index 00000000..2323189a
--- /dev/null
+++ b/util/console.cpp
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+#include <config.h>
+
+#include <stdio.h>
+
+#ifdef HAVE_UNISTD_H
+#    include <unistd.h>
+#elif defined(_WIN32)
+#    include <io.h>
+#endif
+
+#include "util/console.h"
+
+namespace Gjs {
+namespace Console {
+
+#ifdef HAVE_UNISTD_H
+const int stdin_fd = STDIN_FILENO;
+const int stdout_fd = STDOUT_FILENO;
+const int stderr_fd = STDERR_FILENO;
+#elif defined(_WIN32)
+const int stdin_fd = _fileno(stdin);
+const int stdout_fd = _fileno(stdout);
+const int stderr_fd = _fileno(stderr);
+#else
+const int stdin_fd = 0;
+const int stdout_fd = 1;
+const int stderr_fd = 2;
+#endif
+
+bool is_tty(int fd) {
+#ifdef HAVE_UNISTD_H
+    return isatty(fd);
+#elif defined(_WIN32)
+    return _isatty(fd);
+#else
+    return false;
+#endif
+}
+
+}  // namespace Console
+}  // namespace Gjs
\ No newline at end of file
diff --git a/util/console.h b/util/console.h
new file mode 100644
index 00000000..275c6c7c
--- /dev/null
+++ b/util/console.h
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+#ifndef UTIL_CONSOLE_H_
+#define UTIL_CONSOLE_H_
+
+namespace Gjs {
+namespace Console {
+extern const int stdout_fd;
+extern const int stdin_fd;
+extern const int stderr_fd;
+
+[[nodiscard]] bool is_tty(int fd = stdout_fd);
+
+};  // namespace Console
+};  // namespace Gjs
+
+#endif  // UTIL_CONSOLE_H_


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]