AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.07k stars 349 forks source link

Clear to end of screen before redrawing prompt #476

Open zimeg opened 1 year ago

zimeg commented 1 year ago

Summary

This PR adds a terminal.EraseScreen function to support the "Erase in Display" ANSI control sequence. This function replaces calls to terminal.ClearLine in resetPrompt in order to clear previous prompt output entirely and atomically, instead of clearing each line in an iterative manner.

Updating resetPrompt to clear to the end of the screen seems to significantly reduce the "flickering" effect noted in #436 since fewer updates to the terminal screen are made between each render.

Additionally, the MultiLine prompt was updated as a result of changes to the resetPrompt logic. This prompt now preserves leading and trailing spaces in answers and replaces the input template with an answered template after submission. The template was also updated to begin inputs on a newline, which happens to be a request of #336.

Preview

Before

https://user-images.githubusercontent.com/18134219/205465447-cda292c1-bb7b-4ef3-af26-aa450b00791b.mp4

After

https://user-images.githubusercontent.com/18134219/205465499-ed0040e4-80ec-49e8-b0df-9223258a1b10.mp4

Reviewers

The following steps can be used to inspect these changes:

  1. Checkout this branch

  2. In a project using survey and survey.Select, survey.MultiSelect, or survey.MultiLine, append the following to your go.mod, pointing to your local survey source:

    replace github.com/AlecAivazis/survey/v2 v2.3.6 => ../../go-survey/survey
  3. Run your project and rapidly page through select options in an attempt to reproduce the flickering effect.

Notes