Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Erase the prompt before validating #523

Open
RuiNtD opened this issue Dec 21, 2022 · 4 comments
Open

Suggestion: Erase the prompt before validating #523

RuiNtD opened this issue Dec 21, 2022 · 4 comments
Labels
enhancement New feature or request module:prompt prompt module

Comments

@RuiNtD
Copy link

RuiNtD commented Dec 21, 2022

Prompts should be erased (in the console) before running the validate function, in case something logs to console while validating.

@c4spar
Copy link
Owner

c4spar commented Dec 22, 2022

Not sure if it's a good idea to erase the prompt by default. If the validation takes some time, the prompt dissapears for a while.

I could provide a function to clear the prompt manuelly or an option to clear it automatically.

But this is theoretically already possible by checking the permission state and using cliffy's tty module (you don't need to clear the prompt, it's enough to move the cursor up. the prompt module will then automatically clear everything after and below the cursor):

await Input.prompt({
  message: "Select a file",
  validate: async (path) => {
    const { state } = await Deno.permissions.query({
      name: "read",
      path,
    });

    state === "prompt" && console.log();
    const { isFile } = await Deno.stat(path);
    state === "prompt" && tty.cursorUp(2);

    return isFile;
  },
});
 ? Select a file › deno.jsonc
⚠️ ┌ Deno requests read access to "deno.jsonc".
   ├ Requested by `Deno.stat()` API
   ├ Run again with --allow-read to bypass this prompt.
   └ Allow? [y/n] (y = yes, allow; n = no, deny) > y

But i also noticed the same issue happens if an error accours during validation.

 ? Select a file › deno.jsoncerror: Uncaught (in promise) Error: some error
    throw new Error("some error");

i think atleast it would be a good idea to move the cursor to the next line before validating. So it looks like this:

 ? Select a file › sdgsdfsdf
error: Uncaught (in promise) Error: some error
    throw new Error("some error");

With this change, the workaround could look like this:

await Input.prompt({
  message: "Select a file",
  validate: async (path) => {
    const { state } = await Deno.permissions.query({
      name: "read",
      path,
    });

    const { isFile } = await Deno.stat(path);
    state === "prompt" && tty.cursorUp(1);

    return isFile;
  },
});

@RuiNtD
Copy link
Author

RuiNtD commented Dec 22, 2022

Tried logging an empty line and moving the cursor up. Didn't clear the prompt, unfortunately.

(Screenshot is taken during the await delay(1000))

@c4spar
Copy link
Owner

c4spar commented Dec 22, 2022

This might work for you as a workaround. But since you have activated the hint, you need to move the cursor down 2 lines.

await Input.prompt({
  message: "Where is your legacy instance?",
  default: Deno.args[0],
  hint: "This should be a .minecraft folder containing Wynntils for 1.12.2",
  validate: async (legacyPath) => {
    legacyPath = join(legacyPath, "wynntils", "configs");

    const { state } = await Deno.permissions.query({
      name: "read",
      path: legacyPath,
    });

    if (state === "prompt") {
      console.log();
      console.log();
    }

    try {
      const uuids = [...Deno.readDirSync(legacyPath)]
        .filter((v) => v.isDirectory)
        .map((v) => v.name);
      if (uuids.length === 0) {
        return "Failed to find any legacy Wynntils configs in this folder.";
      }
      state === "prompt" && tty.cursorUp(3);
      return true;
    } catch (e) {
      state === "prompt" && tty.cursorUp(3);
      return e.message;
    }
  },
});

I will think about a solution how we can improve this.

@c4spar
Copy link
Owner

c4spar commented Dec 22, 2022

Or if you want to clear the prompt completely than you can just call tty.cursorLeft.eraseDown();:

await Input.prompt({
  message: "Where is your legacy instance?",
  default: Deno.args[0],
  hint: "This should be a .minecraft folder containing Wynntils for 1.12.2",
  validate: async (legacyPath) => {
    legacyPath = join(legacyPath, "wynntils", "configs");

    tty.cursorLeft.eraseDown();

    try {
      const uuids = [...Deno.readDirSync(legacyPath)]
        .filter((v) => v.isDirectory)
        .map((v) => v.name);
      if (uuids.length === 0) {
        return "Failed to find any legacy Wynntils configs in this folder.";
      }
      return true;
    } catch (e) {
      return e.message;
    }
  },
});

@c4spar c4spar added enhancement New feature or request module:prompt prompt module labels Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request module:prompt prompt module
Projects
None yet
Development

No branches or pull requests

2 participants