[gjs/ewlsh/whatwg-console: 3/4] modules: Implement console.clear()




commit aa514fe6d4b9a3b9292e8c4fc302338c76d56a05
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 |  9 ++++++---
 util/console.cpp       | 18 ++++++++++++++++++
 util/console.h         |  2 ++
 4 files changed, 47 insertions(+), 3 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 a9ac6b9f..34eb702b 100644
--- a/modules/esm/console.js
+++ b/modules/esm/console.js
@@ -9,11 +9,11 @@
 // @ts-expect-error
 import GLib from 'gi://GLib';
 
-const {getTerminalSize: getNativeTerminalSize } =
+const {getTerminalSize: getNativeTerminalSize, clearTerminal} =
     // @ts-expect-error
     import.meta.importSync('console');
 
-export { getNativeTerminalSize };
+export {getNativeTerminalSize};
 
 /**
  * @typedef TerminalSize
@@ -146,7 +146,10 @@ export class Console {
 
     // 1.1.2 clear()
     clear() {
-        throw new Error('clear() is not implemented.');
+        try {
+            // clearTerminal can throw Gio-related errors.
+            clearTerminal();
+        } catch {}
     }
 
     // 1.1.3 debug(...data)
diff --git a/util/console.cpp b/util/console.cpp
index 4f25b19e..63d3e557 100644
--- a/util/console.cpp
+++ b/util/console.cpp
@@ -65,6 +65,24 @@ bool is_tty(int fd) {
 #endif
 }
 
+bool supports_ansi() {
+#ifdef _WIN32
+    DWORD mode;
+    GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode);
+    return mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == mode;
+#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]