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

Refactor and fix multiline text bug #355

Merged
40 changes: 18 additions & 22 deletions src/inquirer/render/console/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import sys

from blessed import Terminal
Expand Down Expand Up @@ -32,8 +33,6 @@ def render(self, question, answers=None):
clazz = self.render_factory(question.kind)
render = clazz(question, terminal=self.terminal, theme=self._theme, show_default=question.show_default)

self.clear_eos()

try:
return self._event_loop(render)
finally:
Expand All @@ -42,19 +41,18 @@ def render(self, question, answers=None):
def _event_loop(self, render):
try:
while True:
self._relocate()
self._print_status_bar(render)
self._relocate_and_clear()
self._print_status_bar()

self._print_header(render)
self._print_options(render)

self._process_input(render)
self._force_initial_column()
except errors.EndOfInput as e:
self._go_to_end(render)
return e.selection

def _print_status_bar(self, render):
def _print_status_bar(self):
if self._previous_error is None:
self.clear_bottombar()
return
Expand All @@ -77,14 +75,12 @@ def _print_header(self, render):
)
show_default = render.question.default and render.show_default
header += default_value if show_default else ""
msg_template = (
"{t.move_up}{t.clear_eol}{tq.brackets_color}[" "{tq.mark_color}?{tq.brackets_color}]{t.normal} {msg}"
)
msg_template = "{tq.brackets_color}[{tq.mark_color}?{tq.brackets_color}]{t.normal} {msg}"

# ensure any user input with { or } will not cause a formatting error
escaped_current_value = str(render.get_current_value()).replace("{", "{{").replace("}", "}}")
self.print_str(
f"\n{msg_template}: {escaped_current_value}",
f"{msg_template}: {escaped_current_value}",
msg=header,
lf=not render.title_inline,
tq=self._theme.Question,
Expand All @@ -104,9 +100,9 @@ def _process_input(self, render):
except errors.ValidationError as e:
self._previous_error = render.handle_validation_error(e)

def _relocate(self):
print(self._position * self.terminal.move_up, end="")
self._force_initial_column()
def _relocate_and_clear(self):
print("\r" + self._position * self.terminal.move_up, end="")
self.clear_eos()
self._position = 0

def _go_to_end(self, render):
Expand All @@ -115,9 +111,6 @@ def _go_to_end(self, render):
print(self._position * self.terminal.move_down, end="")
self._position = 0

def _force_initial_column(self):
self.print_str("\r")

def render_error(self, message):
if message:
symbol = ">> "
Expand All @@ -139,7 +132,8 @@ def clear_bottombar(self):
with self.terminal.location(0, self.height - 2):
self.clear_eos()

def render_factory(self, question_type):
@staticmethod
def render_factory(question_type):
matrix = {
"text": Text,
"editor": Editor,
Expand All @@ -155,17 +149,19 @@ def render_factory(self, question_type):
return matrix.get(question_type)

def print_line(self, base, lf=True, **kwargs):
self.print_str(base + self.terminal.clear_eol(), lf=lf, **kwargs)
self.print_str(base, lf=lf, **kwargs)

def print_str(self, base, lf=False, **kwargs):
msg = base.format(t=self.terminal, **kwargs)
print(msg, end="\n" if lf else "")
sys.stdout.flush()

self._position += math.floor((self.terminal.length(msg) - 1) / self.width)
if lf:
self._position += 1

print(base.format(t=self.terminal, **kwargs), end="\n" if lf else "")
sys.stdout.flush()

def clear_eos(self):
print(self.terminal.clear_eos(), end="")
print(self.terminal.clear_eos, end="")

@property
def width(self):
Expand Down