Skip to content

Select and multiselect spams terminal when redirecting stdout #274

@AumyF

Description

@AumyF

Describe the bug

Some prompts spam terminal when redirecting stdout.
As far as I found out, some prompts include "select", "multiselect", "number", "autosuggest", and "autosuggestMultiselect".

To Reproduce

  1. Create a file and call prompt with { type: "select", stdout: process.stderr }.
const { prompt } = require("prompts");

(async () => {
  const r = await prompt({
    name: "spam",
    type: "select",
    message: "Pick one!",
    stdout: process.stderr,
    choices: [
      { value: "Charmander" },
      { value: "Bulbasaur" },
      { value: "Squirtle" },
      { value: "Pikachu" },
      { value: "Eevee" },
    ],
  });
})();
  1. Run the script and redirect stdout to something (such as /dev/null).
    image

    Type "number" will not output duplicately until some errors occurs.
    image

  2. When removing redirection it runs correctly.
    image

Expected behavior

Should run properly even if stdout is redirected.

System

  • OS: Ubuntu 20.04 on WSL on Windows 10 2004
  • Terminal: Windows Terminal (used in the screenshots), VSCode integrated, and WSL console
  • Node version: both v8.17.0 and v14.11.0

Additional context

This issue seems to be caused by clear function. It uses process.stdout.columns even if it should use process.stderr.columns. When stdout is redirected to something, process.stdout.columns will be undefined, so "clear" clears nothing.

similar issues: #89 #153 #175

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions