[gjs/ewlsh/nova-repl] Remove unused Deno functionality, cleanup paths.



commit 047f760a7c0ff00605d3ca414273b396a28c7bd0
Author: Evan Welsh <contact evanwelsh com>
Date:   Wed Sep 1 22:50:40 2021 -0700

    Remove unused Deno functionality, cleanup paths.

 cliffy/ansi.js                              |   6 ++
 cliffy/bundle.sh                            |   4 +-
 cliffy/lib.js                               |  28 +++++++-
 cliffy/transform.js                         |   2 +-
 js.gresource.xml                            |   6 +-
 modules/esm/_denoCompatLayer.js             | 102 ----------------------------
 modules/esm/{_cliffy.js => _repl/cliffy.js} |  87 ++++++------------------
 modules/esm/_repl/deno.js                   |  28 ++++++++
 modules/esm/repl.js                         |  45 ++++++++----
 9 files changed, 117 insertions(+), 191 deletions(-)
---
diff --git a/cliffy/ansi.js b/cliffy/ansi.js
new file mode 100644
index 00000000..7149b357
--- /dev/null
+++ b/cliffy/ansi.js
@@ -0,0 +1,6 @@
+export {setColorEnabled, getColorEnabled} from 'https://deno.land/std@0.104.0/fmt/colors.ts';
+export * from 'https://deno.land/x/cliffy@v0.19.5/ansi/ansi.ts';
+export * from 'https://deno.land/x/cliffy@v0.19.5/ansi/ansi_escapes.ts';
+export * from 'https://deno.land/x/cliffy@v0.19.5/ansi/chain.ts';
+export * from 'https://deno.land/x/cliffy@v0.19.5/ansi/colors.ts';
+export * from 'https://deno.land/x/cliffy@v0.19.5/ansi/cursor_position.ts';
\ No newline at end of file
diff --git a/cliffy/bundle.sh b/cliffy/bundle.sh
index 3de97ca4..6182d1ae 100755
--- a/cliffy/bundle.sh
+++ b/cliffy/bundle.sh
@@ -1,4 +1,4 @@
 #!/bin/sh
 
-deno bundle --unstable ./cliffy/lib.js ./modules/esm/_cliffy.js
-gjs -m ./cliffy/transform.js ./modules/esm/_cliffy.js
\ No newline at end of file
+deno bundle --unstable ./cliffy/lib.js ./modules/esm/_repl/cliffy.js
+gjs -m ./cliffy/transform.js ./modules/esm/_repl/cliffy.js
\ No newline at end of file
diff --git a/cliffy/lib.js b/cliffy/lib.js
index c0819522..5458fc3e 100644
--- a/cliffy/lib.js
+++ b/cliffy/lib.js
@@ -1,4 +1,26 @@
-export * as AnsiColors from 'https://deno.land/std@0.104.0/fmt/colors.ts';
-export * as Ansi from 'https://deno.land/x/cliffy@v0.19.5/ansi/mod.ts';
+export * as Ansi from './ansi.js';
+
 export * as Keycode from 'https://deno.land/x/cliffy@v0.19.5/keycode/mod.ts';
-export { Figures } from 'https://deno.land/x/cliffy@v0.19.5/prompt/figures.ts';
\ No newline at end of file
+export { Figures } from 'https://deno.land/x/cliffy@v0.19.5/prompt/figures.ts';
+
+
+/**
+ * Ported from Cliffy, originally
+ * depended on Deno.stdin/Deno.stdout
+ *
+ * @param {object} _
+ * @param {Gio.UnixInputStream} _.stdin
+ * @param {Gio.UnixOutputStream} _.stdout
+ */
+export function getCursorPosition({ stdin, stdout }) {
+    stdout.write_bytes(new TextEncoder().encode(cursorPosition), null);
+    const data = stdin.read_bytes(8, null).toArray();
+    const [y, x] = new TextDecoder().decode(data).match(/\[(\d+);(\d+)R/)?.slice(1, 3).map(Number) ?? [
+        0,
+        0
+    ];
+    return {
+        x,
+        y
+    };
+}
\ No newline at end of file
diff --git a/cliffy/transform.js b/cliffy/transform.js
index 4183fe47..15e1b89f 100644
--- a/cliffy/transform.js
+++ b/cliffy/transform.js
@@ -5,7 +5,7 @@ const [, contents] = file.load_contents(null);
 let source = new TextDecoder().decode(contents);
 source = 
 `
-import * as Deno from './_denoCompatLayer.js';
+import * as Deno from './deno.js';
 ${source.replace(/\#/g, '$').replace('const { Deno: Deno1  } = globalThis;', 'const Deno1 = Deno;')}
 `
 
diff --git a/js.gresource.xml b/js.gresource.xml
index a5f564fa..c95d6cd9 100644
--- a/js.gresource.xml
+++ b/js.gresource.xml
@@ -13,6 +13,9 @@
     <file>modules/esm/_encoding/encoding.js</file>
     <file>modules/esm/_encoding/encodingMap.js</file>
     <file>modules/esm/_encoding/util.js</file>
+
+    <file>modules/esm/_repl/deno.js</file>
+    <file>modules/esm/_repl/cliffy.js</file>
   
     <file>modules/esm/cairo.js</file>
     <file>modules/esm/gettext.js</file>
@@ -21,9 +24,6 @@
     <file>modules/esm/repl.js</file>
     <file>modules/esm/system.js</file>
 
-    <file>modules/esm/_denoCompatLayer.js</file>
-    <file>modules/esm/_cliffy.js</file>
-
     <!-- Script-based Modules -->
     <file>modules/script/_bootstrap/debugger.js</file>
     <file>modules/script/_bootstrap/default.js</file>
diff --git a/modules/esm/_cliffy.js b/modules/esm/_repl/cliffy.js
similarity index 92%
rename from modules/esm/_cliffy.js
rename to modules/esm/_repl/cliffy.js
index 30e613c7..5f833140 100644
--- a/modules/esm/_cliffy.js
+++ b/modules/esm/_repl/cliffy.js
@@ -1,5 +1,5 @@
 
-import * as Deno from './_denoCompatLayer.js';
+import * as Deno from './deno.js';
 const Deno1 = Deno;
 const noColor = typeof Deno1?.noColor === "boolean" ? Deno1.noColor : true;
 let enabled = !noColor;
@@ -431,7 +431,7 @@ const CSI = `${ESC}[`;
 const OSC = `${ESC}]`;
 const SEP = ";";
 const bel = "\u0007";
-const cursorPosition = `${CSI}6n`;
+const cursorPosition1 = `${CSI}6n`;
 function cursorTo(x, y) {
     if (typeof y !== "number") {
         return `${CSI}${x}G`;
@@ -533,7 +533,7 @@ function image(buffer, options) {
 const mod1 = function() {
     return {
         bel: bel,
-        cursorPosition: cursorPosition,
+        cursorPosition: cursorPosition1,
         cursorTo: cursorTo,
         cursorMove: cursorMove,
         cursorUp: cursorUp,
@@ -648,11 +648,11 @@ function factory2(stack = []) {
     colors1._stack = stack;
     return colors1;
 }
-function getCursorPosition({ stdin =Deno.stdin , stdout =Deno.stdout  } = {
+function getCursorPosition2({ stdin =Deno.stdin , stdout =Deno.stdout  } = {
 }) {
     const data = new Uint8Array(8);
     Deno.setRaw(stdin.rid, true);
-    stdout.writeSync(new TextEncoder().encode(cursorPosition));
+    stdout.writeSync(new TextEncoder().encode(cursorPosition1));
     stdin.readSync(data);
     Deno.setRaw(stdin.rid, false);
     const [y, x] = new TextDecoder().decode(data).match(/\[(\d+);(\d+)R/)?.slice(1, 3).map(Number) ?? [
@@ -664,67 +664,13 @@ function getCursorPosition({ stdin =Deno.stdin , stdout =Deno.stdout  } = {
         y
     };
 }
-const tty = factory3();
-function factory3(options) {
-    let result = "";
-    let stack = [];
-    const stdout = options?.stdout ?? Deno.stdout;
-    const stdin = options?.stdin ?? Deno.stdin;
-    const tty1 = function(...args) {
-        if (this) {
-            update(args);
-            stdout.writeSync(new TextEncoder().encode(result));
-            return this;
-        }
-        return factory3(args[0] ?? options);
-    };
-    tty1.text = function(text) {
-        stack.push([
-            text,
-            []
-        ]);
-        update();
-        stdout.writeSync(new TextEncoder().encode(result));
-        return this;
-    };
-    tty1.getCursorPosition = ()=>getCursorPosition({
-            stdout,
-            stdin
-        })
-    ;
-    const methodList = Object.entries(mod1);
-    for (const [name1, method] of methodList){
-        if (name1 === "cursorPosition") {
-            continue;
-        }
-        Object.defineProperty(tty1, name1, {
-            get () {
-                stack.push([
-                    method,
-                    []
-                ]);
-                return this;
-            }
-        });
-    }
-    return tty1;
-    function update(args) {
-        if (!stack.length) {
-            return;
-        }
-        if (args) {
-            stack[stack.length - 1][1] = args;
-        }
-        result = stack.reduce((prev, [cur, args1])=>prev + (typeof cur === "string" ? cur : cur.call(tty1, 
...args1))
-        , "");
-        stack = [];
-    }
-}
 const mod2 = function() {
     return {
+        setColorEnabled: setColorEnabled,
+        getColorEnabled: getColorEnabled,
         ansi,
         bel,
-        cursorPosition,
+        cursorPosition: cursorPosition1,
         cursorTo,
         cursorMove,
         cursorUp,
@@ -752,8 +698,7 @@ const mod2 = function() {
         link,
         image,
         colors,
-        getCursorPosition,
-        tty,
+        getCursorPosition: getCursorPosition2,
         mod: mod1
     };
 }();
@@ -1010,7 +955,19 @@ const win = {
 };
 const Figures1 = Deno.build.os === "windows" ? win : main;
 export { Figures1 as Figures };
-export { mod as AnsiColors };
+function getCursorPosition1({ stdin , stdout  }) {
+    stdout.write_bytes(new TextEncoder().encode(cursorPosition), null);
+    const data = stdin.read_bytes(8, null).toArray();
+    const [y, x] = new TextDecoder().decode(data).match(/\[(\d+);(\d+)R/)?.slice(1, 3).map(Number) ?? [
+        0,
+        0
+    ];
+    return {
+        x,
+        y
+    };
+}
 export { mod2 as Ansi };
 export { mod3 as Keycode };
+export { getCursorPosition1 as getCursorPosition };
 
diff --git a/modules/esm/_repl/deno.js b/modules/esm/_repl/deno.js
new file mode 100644
index 00000000..dd459287
--- /dev/null
+++ b/modules/esm/_repl/deno.js
@@ -0,0 +1,28 @@
+import GLib from 'gi://GLib';
+
+export { exit } from 'system';
+
+export const stdin = null;
+export const stdout = null;
+export const stderr = null;
+
+// A very minimal shim for Cliffy's Deno-specific functionality.
+
+export const build = {};
+Object.defineProperty(build, 'os', {
+    get() {
+        const name = GLib.get_os_info('NAME').toLowerCase();
+
+        if (name === 'macos')
+            return 'darwin';
+
+        if (name === 'windows')
+            return 'windows';
+
+        // Linux distributions all have differing names...
+        return 'linux';
+    }
+});
+
+// '1' is assumed to be STDOUT
+export const noColor = !GLib.log_writer_supports_color(1);
diff --git a/modules/esm/repl.js b/modules/esm/repl.js
index c1442eaa..6869ab89 100644
--- a/modules/esm/repl.js
+++ b/modules/esm/repl.js
@@ -1,6 +1,6 @@
 import Gio from 'gi://Gio';
 
-import { Ansi, AnsiColors, Keycode, Figures } from './_cliffy.js';
+import { Ansi, Keycode, Figures } from './_repl/cliffy.js';
 
 const stdin = Gio.UnixInputStream.new(0, false);
 const stdout = Gio.UnixOutputStream.new(1, false);
@@ -8,6 +8,7 @@ const stderr = Gio.UnixOutputStream.new(2, false);
 
 const Console = import.meta.importSync('_consoleNative');
 
+// TODO: Integrate with new printer...
 function toString(value) {
     if (typeof value === 'function') {
         return value.toString();
@@ -21,14 +22,17 @@ function toString(value) {
     return `${value}`;
 }
 
-AnsiColors.setColorEnabled(true);
+// Enable color support...
+Ansi.setColorEnabled(true);
 
 class ReplInput {
     constructor() {
-        this.$promptPrefix = 'gjs';
-        this.$promptPointer = Figures.POINTER_SMALL;
-        this.$promptLength = `${this.$promptPrefix} ${this.$promptPointer} `.length;
-        this.$prompt = `${Ansi.colors.yellow(this.$promptPrefix)} ${this.$promptPointer} `;
+        this._promptPrefix = 'gjs';
+        this._promptPointer = Figures.POINTER_SMALL;
+        this._promptLength = `${this._promptPrefix} ${this._promptPointer} `.length;
+        this._prompt = `${Ansi.colors.yellow(this._promptPrefix)} ${this._promptPointer} `;
+
+        this._cancelled = false;
 
         this.history = [];
         this.inputChars = [];
@@ -354,14 +358,14 @@ class ReplInput {
         if (this.lines.length > 0) {
             // Create a prefix like '... ' that matches the current
             // prompt length...
-            const prefix = ' '.padStart(this.$promptLength, '.');
+            const prefix = ' '.padStart(this._promptLength, '.');
             this.writeSync(prefix + value);
         } else {
-            this.writeSync(this.$prompt + value);
+            this.writeSync(this._prompt + value);
         }
 
         this.updateInputIndex(value);
-        this.writeSync(Ansi.cursorTo(this.$promptLength + this.inputIndex + 1));
+        this.writeSync(Ansi.cursorTo(this._promptLength + this.inputIndex + 1));
         this.writeSync(Ansi.cursorShow);
         this.flush();
     }
@@ -380,12 +384,19 @@ class ReplInput {
     }
 
     _read(stdin, result) {
+        if (this._cancelled)
+            return;
+
         if (result) {
             const gbytes = stdin.read_bytes_finish(result);
             const bytes = gbytes.toArray();
             if (bytes.length > 0) {
                 for (const event of Keycode.parse(bytes)) {
                     this.handleEvent(event);
+
+                    if (this._cancelled)
+                        break;
+
                     this.render();
                 }
             }
@@ -395,8 +406,12 @@ class ReplInput {
         stdin.read_bytes_async(8, 0, this.cancellable, this._read.bind(this));
     }
 
-    stop() {
-        this.cancellable.cancel();
+    cancel() {
+        this._cancelled = true;
+        this.cancellable?.cancel();
+
+        this.writeSync('\n');
+        this.flush();
     }
 
     read() {
@@ -406,6 +421,7 @@ class ReplInput {
 
     prompt(input) {
         this._input = input;
+        this._cancelled = false;
         this.read();
     }
 }
@@ -440,7 +456,7 @@ export class Repl {
     startPrompt() {
         this.input.prompt((input) => {
             if (input === 'exit()') {
-                this.stop();
+                this.exit();
             } else {
                 this.evaluateJs(input);
             }
@@ -466,9 +482,8 @@ export class Repl {
         }
     }
 
-    stop() {
-        this.writeSync('\n');
-        this.flush();
+    exit() {
+        this.input.cancel();
 
         const mainloop = this._mainloop;
         this._mainloop = null;


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