[gjs/ewlsh/whatwg-console: 5/8] modules: Implement console.clear()




commit 91e061b25f91badecdd06df7cf22a0635df47664
Author: Evan Welsh <contact evanwelsh com>
Date:   Tue Jun 29 01:07:13 2021 -0700

    modules: Implement console.clear()

 modules/console.cpp    | 21 +++++++++++++++++++++
 modules/esm/console.js |  7 +++++--
 util/console.cpp       | 22 ++++++++++++++++++++++
 util/console.h         |  2 ++
 4 files changed, 50 insertions(+), 2 deletions(-)
---
diff --git a/modules/console.cpp b/modules/console.cpp
index b9f8d7de..44624e68 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -303,6 +303,24 @@ bool gjs_console_get_terminal_size(JSContext* cx, unsigned argc,
     return true;
 }
 
+
+bool gjs_console_clear_terminal(JSContext* cx, unsigned argc, JS::Value* vp) {
+    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+
+    if (!Gjs::Console::is_tty()) {
+        gjs_throw(cx, "Output is not a tty.");
+        return false;
+    }
+
+    if (!Gjs::Console::clear()) {
+        gjs_throw(cx, "Output does not support ANSI escape codes.");
+        return false;
+    }
+
+    args.rval().setUndefined();
+    return true;
+}
+
 bool
 gjs_define_console_stuff(JSContext              *context,
                          JS::MutableHandleObject module)
@@ -312,6 +330,9 @@ gjs_define_console_stuff(JSContext              *context,
     return JS_DefineFunction(context, module, "getTerminalSize",
                              gjs_console_get_terminal_size, 1,
                              GJS_MODULE_PROP_FLAGS) &&
+           JS_DefineFunction(context, module, "clearTerminal",
+                             gjs_console_clear_terminal, 1,
+                             GJS_MODULE_PROP_FLAGS) &&
            JS_DefineFunctionById(context, module, atoms.interact(),
                                  gjs_console_interact, 1,
                                  GJS_MODULE_PROP_FLAGS);
diff --git a/modules/esm/console.js b/modules/esm/console.js
index 659f3b79..22542d42 100644
--- a/modules/esm/console.js
+++ b/modules/esm/console.js
@@ -6,7 +6,7 @@ import GLib from 'gi://GLib';
 // @ts-expect-error importSync is not configured.
 const {getTerminalSize: getNativeTerminalSize, clearTerminal} = import.meta.importSync('console');
 
-export { getNativeTerminalSize };
+export {getNativeTerminalSize};
 
 /**
  * @typedef TerminalSize
@@ -163,7 +163,10 @@ export class Console {
      * @returns {void}
      */
     clear() {
-        throw new Error('clear() is not implemented.');
+        try {
+            // clearTerminal will throw on unsupported platforms.
+            clearTerminal();
+        } catch {}
     }
 
     /**
diff --git a/util/console.cpp b/util/console.cpp
index 02509561..0f34b918 100644
--- a/util/console.cpp
+++ b/util/console.cpp
@@ -65,6 +65,28 @@ bool is_tty(int fd) {
 #endif
 }
 
+bool supports_ansi() {
+#ifdef _WIN32
+    DWORD mode;
+    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
+    if (handle == INVALID_HANDLE_VALUE)
+        return false;
+
+    GetConsoleMode(handle, &mode);
+    return !!SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
+#else
+    // For now we'll assume all TTYs on other operating systems support ANSI.
+    return is_tty(stdout_fd);
+#endif
+}
+
+bool clear() {
+    if (stdout_fd < 0 || !supports_ansi())
+        return false;
+
+    return fputs(ANSICode::ESCAPE, stdout) > 0 && fflush(stdout) > 0;
+}
+
 void size(int* width, int* height) {
     {
         const char* lines = g_getenv("LINES");
diff --git a/util/console.h b/util/console.h
index d3265e31..c38a213e 100644
--- a/util/console.h
+++ b/util/console.h
@@ -11,6 +11,8 @@ extern const int stdin_fd;
 extern const int stderr_fd;
 
 [[nodiscard]] bool is_tty(int fd = stdout_fd);
+[[nodiscard]] bool supports_ansi();
+[[nodiscard]] bool clear();
 void size(int* width, int* height);
 
 };  // namespace Console


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