[gjs/ewlsh/nova-repl: 113/114] Refactor console utility




commit 1871a239c3f681d1510125562676af6f0024db53
Author: Evan Welsh <contact evanwelsh com>
Date:   Fri Sep 3 21:55:18 2021 -0700

    Refactor console utility
    
    It appears disabling pch was not being passed correctly.

 gjs/debugger.cpp          |  4 ++--
 libgjs-private/gjs-util.c | 13 -------------
 meson.build               |  2 +-
 modules/console.cpp       | 20 +++++++++++++++++++-
 modules/esm/console.js    |  5 +++--
 modules/esm/repl.js       |  3 +--
 util/console.cpp          | 11 -----------
 util/console.h            | 33 +++++++++++++++------------------
 util/console.hh           | 27 ---------------------------
 9 files changed, 41 insertions(+), 77 deletions(-)
---
diff --git a/gjs/debugger.cpp b/gjs/debugger.cpp
index 5d815c8f3..8bf2d6767 100644
--- a/gjs/debugger.cpp
+++ b/gjs/debugger.cpp
@@ -57,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 (gjs_console_is_tty(stdin_fd)) {
+        if (Gjs::Console::is_tty(Gjs::Console::stdin_fd)) {
             line = readline(real_prompt);
         } else {
 #else
@@ -70,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 (!gjs_console_is_tty(stdin_fd)) {
+            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/libgjs-private/gjs-util.c b/libgjs-private/gjs-util.c
index f656b6dbf..55b3b4f19 100644
--- a/libgjs-private/gjs-util.c
+++ b/libgjs-private/gjs-util.c
@@ -17,7 +17,6 @@
 #include <glib/gi18n.h> /* for bindtextdomain, bind_textdomain_codeset, textdomain */
 
 #include "libgjs-private/gjs-util.h"
-#include "util/console.h"
 
 char *
 gjs_format_int_alternative_output(int n)
@@ -324,15 +323,3 @@ void gjs_log_set_writer_func(GjsGLogWriterFunc func, void* user_data,
 
     g_log_set_writer_func(gjs_log_writer_func_wrapper, func, NULL);
 }
-
-/**
- * gjs_clear_terminal:
- *
- * Clears the terminal, if possible.
- */
-void gjs_clear_terminal(void) {
-    if (!gjs_console_is_tty(stdout_fd))
-        return;
-
-    gjs_console_clear();
-}
diff --git a/meson.build b/meson.build
index b8972e7c9..9bf031a31 100644
--- a/meson.build
+++ b/meson.build
@@ -448,7 +448,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/console.hh',
+    '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 5099b9c28..edc99112b 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -26,7 +26,7 @@
 #include "gjs/jsapi-util-args.h"
 #include "gjs/jsapi-util.h"
 #include "modules/console.h"
-#include "util/console.hh"
+#include "util/console.h"
 
 namespace mozilla {
 union Utf8Unit;
@@ -180,7 +180,25 @@ static bool gjs_console_is_valid_js(JSContext* cx, unsigned argc,
     return true;
 }
 
+GJS_JSAPI_RETURN_CONVENTION
+static bool gjs_console_clear_terminal(JSContext* cx, unsigned argc,
+                                       JS::Value* vp) {
+    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+    if (!gjs_parse_call_args(cx, "eval", args, ""))
+        return false;
+
+    if (!Gjs::Console::is_tty(Gjs::Console::stdout_fd)) {
+        args.rval().setBoolean(false);
+        return true;
+    }
+
+    args.rval().setBoolean(Gjs::Console::clear());
+    return true;
+}
+
 static JSFunctionSpec console_module_funcs[] = {
+    JS_FN("clearTerminal", gjs_console_clear_terminal, 1,
+          GJS_MODULE_PROP_FLAGS),
     JS_FN("interact", gjs_console_interact, 1, GJS_MODULE_PROP_FLAGS),
     JS_FN("enableRawMode", gjs_console_enable_raw_mode, 0,
           GJS_MODULE_PROP_FLAGS),
diff --git a/modules/esm/console.js b/modules/esm/console.js
index bdbfc69dd..210298c1e 100644
--- a/modules/esm/console.js
+++ b/modules/esm/console.js
@@ -2,7 +2,8 @@
 // SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
 
 import GLib from 'gi://GLib';
-import GjsPrivate from 'gi://GjsPrivate';
+
+const NativeConsole = import.meta.importSync('_consoleNative');
 
 const sLogger = Symbol('Logger');
 const sPrinter = Symbol('Printer');
@@ -151,7 +152,7 @@ class Console {
      */
     clear() {
         this[sGroupIndentation] = '';
-        GjsPrivate.clear_terminal();
+        NativeConsole.clearTerminal();
     }
 
     /**
diff --git a/modules/esm/repl.js b/modules/esm/repl.js
index e1c31216d..67f593095 100644
--- a/modules/esm/repl.js
+++ b/modules/esm/repl.js
@@ -362,8 +362,7 @@ class ReplInput {
                 this.moveCursorRight();
                 return;
             case 'l':
-                // TODO: Consider using internal API instead...
-                console.clear();
+                Console.clearTerminal();
                 return;
             case 'n':
                 this.historyDown();
diff --git a/util/console.cpp b/util/console.cpp
index e3fbfdbe2..82ddcc098 100644
--- a/util/console.cpp
+++ b/util/console.cpp
@@ -19,7 +19,6 @@
 #include <glib.h>
 
 #include "util/console.h"
-#include "util/console.hh"
 
 /**
  * ANSI escape code sequences to manipulate terminals.
@@ -131,13 +130,3 @@ bool clear() {
 
 }  // namespace Console
 }  // namespace Gjs
-
-// C compatibility definitions...
-
-const int stdin_fd = Gjs::Console::stdin_fd;
-const int stdout_fd = Gjs::Console::stdout_fd;
-const int stderr_fd = Gjs::Console::stderr_fd;
-
-bool gjs_console_is_tty(int fd) { return Gjs::Console::is_tty(fd); }
-
-bool gjs_console_clear() { return Gjs::Console::clear(); }
diff --git a/util/console.h b/util/console.h
index df27f305e..306721ed2 100644
--- a/util/console.h
+++ b/util/console.h
@@ -1,31 +1,28 @@
-/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
-/*
- * SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
- * SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
- */
+// 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_
 
-/* This file has to be valid C, because it's used in libgjs-private */
-
-#include <stdbool.h> /* IWYU pragma: keep */
-
-#include <glib.h>
-
-#include "gjs/macros.h"
-
-G_BEGIN_DECLS
+#include <config.h>
 
+namespace Gjs {
+namespace Console {
 extern const int stdout_fd;
 extern const int stdin_fd;
 extern const int stderr_fd;
 
-GJS_USE
-bool gjs_console_is_tty(int fd);
+[[nodiscard]] bool is_tty(int fd = stdout_fd);
+
+[[nodiscard]] bool clear();
+
+[[nodiscard]] bool enable_raw_mode();
+
+[[nodiscard]] bool disable_raw_mode();
 
-bool gjs_console_clear(void);
+[[nodiscard]] bool get_size(int* width, int* height);
 
-G_END_DECLS
+};  // namespace Console
+};  // namespace Gjs
 
 #endif  // UTIL_CONSOLE_H_


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