[gjs/ewlsh/whatwg-console: 22/22] modules: Implement console.clear()
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/ewlsh/whatwg-console: 22/22] modules: Implement console.clear()
- Date: Tue, 29 Jun 2021 08:08:10 +0000 (UTC)
commit d9276bd540600512ba31c26fb83e5c4ef6adaf65
Author: Evan Welsh <contact evanwelsh com>
Date: Tue Jun 29 01:07:13 2021 -0700
modules: Implement console.clear()
meson.build | 10 ++++++++++
modules/console.cpp | 38 ++++++++++++++++++++++++++++++++++++++
modules/esm/console.js | 6 +++++-
3 files changed, 53 insertions(+), 1 deletion(-)
---
diff --git a/meson.build b/meson.build
index 267ab9ad..3280f4e8 100644
--- a/meson.build
+++ b/meson.build
@@ -124,6 +124,10 @@ gi = dependency('gobject-introspection-1.0', version: '>= 1.66.0',
fallback: ['gobject-introspection', 'girepo_dep'])
spidermonkey = dependency('mozjs-78', version: '>= 78.2.0')
+gio_unix = dependency('gio-unix-2.0', version: glib_required_version, required: false)
+
+has_gio_unix = gio_unix.found()
+
# We might need to look for the headers and lib's for Cairo
# manually on MSVC/clang-cl builds...
cairo = dependency('cairo', required: get_option('cairo').enabled() and cxx.get_argument_syntax() != 'msvc')
@@ -313,6 +317,8 @@ header_conf.set('HAVE_SIGNAL_H', cxx.check_header('signal.h',
# enable GNU extensions on systems that have them
header_conf.set('_GNU_SOURCE', 1)
+header_conf.set('HAVE_GIO_UNIX', has_gio_unix)
+
configure_file(output: 'config.h', configuration: header_conf)
### Check for environment ######################################################
@@ -483,6 +489,10 @@ if build_readline
libgjs_dependencies += readline_deps
endif
+if has_gio_unix
+ libgjs_dependencies += gio_unix
+endif
+
libgjs_cpp_args = ['-DGJS_COMPILATION'] + directory_defines
# Check G-I and/or Meson on this one.
diff --git a/modules/console.cpp b/modules/console.cpp
index d01eaea0..b8cda17b 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -39,6 +39,11 @@
# endif
#endif
+#ifdef HAVE_GIO_UNIX
+# include <gio/gio.h>
+# include <gio/gunixoutputstream.h>
+#endif
+
#include <js/CallArgs.h>
#include <js/CompilationAndEvaluation.h>
#include <js/CompileOptions.h>
@@ -328,6 +333,36 @@ bool gjs_console_get_terminal_size(JSContext* cx, unsigned argc,
return true;
}
+constexpr const char* ANSI_CODE [[maybe_unused]] = "\x1b[2J";
+constexpr size_t ANSI_CODE_LEN [[maybe_unused]] = strlen(ANSI_CODE);
+
+bool gjs_console_clear_terminal(JSContext* cx, unsigned argc, JS::Value* vp) {
+ JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+
+#ifdef HAVE_GIO_UNIX
+ int fd = fileno(stdout);
+ if (fd >= 0) {
+ GjsAutoUnref<GOutputStream> ostream(
+ g_unix_output_stream_new(fd, false));
+ size_t bytes_written;
+ GError* error = nullptr;
+
+ if (!g_output_stream_write_all(ostream, ANSI_CODE, ANSI_CODE_LEN,
+ &bytes_written, nullptr, &error))
+ return gjs_throw_gerror_message(cx, error);
+
+ if (!g_output_stream_flush(ostream, nullptr, &error))
+ return gjs_throw_gerror_message(cx, error);
+
+ if (!g_output_stream_close(ostream, nullptr, &error))
+ return gjs_throw_gerror_message(cx, error);
+ }
+#endif
+
+ args.rval().setUndefined();
+ return true;
+}
+
bool
gjs_define_console_stuff(JSContext *context,
JS::MutableHandleObject module)
@@ -337,6 +372,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 a479959f..e22b236a 100644
--- a/modules/esm/console.js
+++ b/modules/esm/console.js
@@ -9,7 +9,7 @@
// @ts-expect-error
import GLib from 'gi://GLib';
-const {getTerminalSize: getNativeTerminalSize } =
+const {getTerminalSize: getNativeTerminalSize, clearTerminal } =
// @ts-expect-error
import.meta.importSync('console');
@@ -182,6 +182,10 @@ export class Console {
// 1.1.2 clear()
clear() {
+ try {
+ // clearTerminal can throw Gio-related errors.
+ clearTerminal();
+ } catch {}
}
// 1.1.3 debug(...data)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]