From ca606d555bcd9f89e0a91ccef7ba64d95ac7e4df Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Sun, 5 Feb 2023 02:03:54 +0200 Subject: [PATCH 1/7] ConsoleRender: remove redundant parameter from `_print_status_bar` --- src/inquirer/render/console/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index c5658023..4286bf0c 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -43,7 +43,7 @@ def _event_loop(self, render): try: while True: self._relocate() - self._print_status_bar(render) + self._print_status_bar() self._print_header(render) self._print_options(render) @@ -54,7 +54,7 @@ def _event_loop(self, render): 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 From 511003b26071a22ffac21e2084e944aa4ce90dbc Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Sun, 5 Feb 2023 02:21:45 +0200 Subject: [PATCH 2/7] ConsoleRender: remove redundant code in `_print_header` --- src/inquirer/render/console/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index 4286bf0c..13773489 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -77,14 +77,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, From f6a9ff90de5401e070137f9e9501a24df973da42 Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Sun, 5 Feb 2023 02:48:09 +0200 Subject: [PATCH 3/7] ConsoleRender: remove redundant round brackets --- src/inquirer/render/console/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index 13773489..f186419d 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -153,7 +153,7 @@ 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 + self.terminal.clear_eol, lf=lf, **kwargs) def print_str(self, base, lf=False, **kwargs): if lf: @@ -163,7 +163,7 @@ def print_str(self, base, lf=False, **kwargs): sys.stdout.flush() def clear_eos(self): - print(self.terminal.clear_eos(), end="") + print(self.terminal.clear_eos, end="") @property def width(self): From 44ea1c816476e644b408bd629eebe8c82eed8d32 Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Sun, 5 Feb 2023 02:24:13 +0200 Subject: [PATCH 4/7] ConsoleRender: remove function `_force_initial_column` --- src/inquirer/render/console/__init__.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index f186419d..46d257a1 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -49,7 +49,6 @@ def _event_loop(self, 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 @@ -103,8 +102,7 @@ def _process_input(self, render): self._previous_error = render.handle_validation_error(e) def _relocate(self): - print(self._position * self.terminal.move_up, end="") - self._force_initial_column() + print("\r" + self._position * self.terminal.move_up, end="") self._position = 0 def _go_to_end(self, render): @@ -113,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 = ">> " From 13ef9a743c998a490f85634b5d4ebe3ed7cf51a7 Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Sun, 5 Feb 2023 02:44:09 +0200 Subject: [PATCH 5/7] ConsoleRender: adding handling of lines bigger than terminal width --- src/inquirer/render/console/__init__.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index 46d257a1..1fb34149 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -1,3 +1,4 @@ +import math import sys from blessed import Terminal @@ -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: @@ -42,7 +41,7 @@ def render(self, question, answers=None): def _event_loop(self, render): try: while True: - self._relocate() + self._relocate_and_clear() self._print_status_bar() self._print_header(render) @@ -101,8 +100,9 @@ def _process_input(self, render): except errors.ValidationError as e: self._previous_error = render.handle_validation_error(e) - def _relocate(self): + 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): @@ -151,12 +151,14 @@ def print_line(self, base, lf=True, **kwargs): self.print_str(base + self.terminal.clear_eol, 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="") From d17914755536410a0fd5e88bf562139ada9eda1f Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Mon, 6 Feb 2023 01:46:47 +0200 Subject: [PATCH 6/7] ConsoleRender: remove redundant `terminal.clear_eol` from `print_line` --- src/inquirer/render/console/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index 1fb34149..5cd7e2c6 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -148,7 +148,7 @@ 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) From 63af9ac0dee7da07a9d9ed85b6221d6bd47780f8 Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Mon, 6 Feb 2023 01:47:59 +0200 Subject: [PATCH 7/7] ConsoleRender: make `render_factory` static --- src/inquirer/render/console/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index 5cd7e2c6..e74e550e 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -132,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,