Elius94 / console-gui-tools

A simple library to draw option menu or other popup inputs and layout on Node.js console.
MIT License
106 stars 17 forks source link

[Bug]: Input popup doesn't handle special characters properly #65

Closed Compositr closed 1 year ago

Compositr commented 1 year ago

What happened?

When I tried to type an & sign, it just inputted "undefined" image

I typed the following: test&123

Uppercase does not work either (shift key doesn't work)

What should have happened?

I would expect the popup input to handle special characters (e.g. "&") properly

Code

  const input = await new Promise<string>((resolve, reject) => {
    new InputPopup({
      id: "popupInput",
      title: "Input",
      numeric: false,
      value: "",
    })
      .show()
      .on("confirm", (out) => {
        resolve(out);
        GUI.refresh();
      })
      .on("cancel", () => {
        reject();
      });
  }).catch(() => {
    // kill
    process.exit(1);
  });

Library Version

3.0.3

Node Version

18.12.1

What operating system are you using?

Other Linux

Terminal

KDE Konsole on Arch

Interest to fix the bug

Compositr commented 1 year ago

I experienced this while trying to make my own input box. I figured out that key.sequence was the correct value to add rather than key.name (which is undefined for special characters)

These lines of code in particular need key.name to be changed to key.sequence src/components/widgets/InputPopup.ts

            tmp += key.name
            this.value = tmp

Working example:

image

Code:

Show code ```ts inputBox.on("keypress", (key: KeyListenerArgs) => { // let user type in input without typing here if (!inputBox.focused) return; __d_lastKey = JSON.stringify(key); switch (key.name) { case "/": break; case "backspace": if (input.length === 0) isCommand = false; input = input.slice(0, -1); cursorPos = Math.max(0, cursorPos - 1); break; case "delete": { if (cursorPos === 0) return; const leftPart = input.slice(0, cursorPos - 1); const rightPart = input.slice(cursorPos); input = leftPart + rightPart; break; } case "space": input += " "; cursorPos++; break; case "tab": // no-op for now break; case "return": case "enter": // handled in other listener break; case "home": cursorPos = 0; break; case "end": cursorPos = input.length; break; case "left": cursorPos = Math.max(0, cursorPos - 1); break; case "right": cursorPos = Math.min(input.length, cursorPos + 1); break; default: if (key.sequence.length !== 1) break; if (key.sequence === "/" && !isCommand && input.length === 0) { isCommand = true; break; } const leftPart = input.slice(0, cursorPos); const rightPart = input.slice(cursorPos); input = leftPart + key.sequence + rightPart; cursorPos++; break; } drawMessage(); }); ```