From cf13a71c9bb73398ad4e722a9fbb37dab788f588 Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Mon, 6 Feb 2023 02:43:29 +0200 Subject: [PATCH 1/3] questions: add `trim_header` option --- src/inquirer/questions.py | 37 ++++++++++++++++++------- src/inquirer/render/console/__init__.py | 9 +++--- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/inquirer/questions.py b/src/inquirer/questions.py index 147dfeb2..f13b7a02 100644 --- a/src/inquirer/questions.py +++ b/src/inquirer/questions.py @@ -43,6 +43,7 @@ def __init__( validate=True, show_default=False, other=False, + trim_header=True, ): self.name = name self._message = message @@ -53,6 +54,7 @@ def __init__( self.answers = {} self.show_default = show_default self._other = other + self.trim_header = trim_header if self._other: self._choices.append(GLOBAL_OTHER_CHOICE) @@ -109,9 +111,13 @@ def _solve(self, prop, *args, **kwargs): class Text(Question): kind = "text" - def __init__(self, name, message="", default=None, autocomplete=None, **kwargs): + def __init__(self, name, message="", default=None, autocomplete=None, trim_header=True, **kwargs): super().__init__( - name, message=message, default=str(default) if default and not callable(default) else default, **kwargs + name, + message=message, + default=str(default) if default and not callable(default) else default, + trim_header=trim_header, + **kwargs, ) self.autocomplete = autocomplete @@ -119,8 +125,8 @@ def __init__(self, name, message="", default=None, autocomplete=None, **kwargs): class Password(Text): kind = "password" - def __init__(self, name, echo="*", **kwargs): - super().__init__(name, **kwargs) + def __init__(self, name, echo="*", trim_header=True, **kwargs): + super().__init__(name, trim_header=trim_header, **kwargs) self.echo = echo @@ -131,8 +137,8 @@ class Editor(Text): class Confirm(Question): kind = "confirm" - def __init__(self, name, default=False, **kwargs): - super().__init__(name, default=default, **kwargs) + def __init__(self, name, default=False, trim_header=True, **kwargs): + super().__init__(name, default=default, trim_header=trim_header, **kwargs) class List(Question): @@ -149,9 +155,10 @@ def __init__( carousel=False, other=False, autocomplete=None, + trim_header=True, ): - super().__init__(name, message, choices, default, ignore, validate, other=other) + super().__init__(name, message, choices, default, ignore, validate, other=other, trim_header=trim_header) self.carousel = carousel self.autocomplete = autocomplete @@ -170,9 +177,10 @@ def __init__( carousel=False, other=False, autocomplete=None, + trim_header=True, ): - super().__init__(name, message, choices, default, ignore, validate, other=other) + super().__init__(name, message, choices, default, ignore, validate, other=other, trim_header=trim_header) self.carousel = carousel self.autocomplete = autocomplete @@ -222,8 +230,17 @@ class Path(Text): kind = "path" - def __init__(self, name, default=None, path_type="any", exists=None, normalize_to_absolute_path=False, **kwargs): - super().__init__(name, default=default, **kwargs) + def __init__( + self, + name, + default=None, + trim_header=True, + path_type="any", + exists=None, + normalize_to_absolute_path=False, + **kwargs, + ): + super().__init__(name, default=default, trim_header=trim_header, **kwargs) self._path_type = path_type self._exists = exists diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index 4b4a6293..7151e8d9 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -78,10 +78,11 @@ def _print_header(self, render): header += f" ({self._theme.Question.default_color}{default_value}{self.terminal.normal})" header_ending = ": " - extra_if_long = "..." - maximum_width = self.width - len(header_ending + extra_if_long) - if self.terminal.length(header) > maximum_width: - header = self.terminal.truncate(header, maximum_width) + extra_if_long + if render.question.trim_header: + extra_if_long = "..." + maximum_width = self.width - len(header_ending + extra_if_long) + if self.terminal.length(header) > maximum_width: + header = self.terminal.truncate(header, maximum_width) + extra_if_long full_header = f"{header}{header_ending}{str(render.get_current_value())}" self.print_str(full_header, lf=not render.title_inline) From 6a02c9d7a5378f377899663b82b3615e0d5cc54e Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Thu, 9 Feb 2023 02:48:00 +0200 Subject: [PATCH 2/3] questions: add `trim_choices` option --- src/inquirer/questions.py | 4 ++++ src/inquirer/render/console/__init__.py | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/inquirer/questions.py b/src/inquirer/questions.py index f13b7a02..68ca1094 100644 --- a/src/inquirer/questions.py +++ b/src/inquirer/questions.py @@ -156,11 +156,13 @@ def __init__( other=False, autocomplete=None, trim_header=True, + trim_choices=False, ): super().__init__(name, message, choices, default, ignore, validate, other=other, trim_header=trim_header) self.carousel = carousel self.autocomplete = autocomplete + self.trim_choices = trim_choices class Checkbox(Question): @@ -178,11 +180,13 @@ def __init__( other=False, autocomplete=None, trim_header=True, + trim_choices=False, ): super().__init__(name, message, choices, default, ignore, validate, other=other, trim_header=trim_header) self.carousel = carousel self.autocomplete = autocomplete + self.trim_choices = trim_choices # Solution for checking valid path based on diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index 7151e8d9..cd63c8b2 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -64,7 +64,13 @@ def _print_options(self, render): for message, symbol, color in render.get_options(): if hasattr(message, "decode"): # python 2 message = message.decode("utf-8") - self.print_line(f" {color}{symbol} {message}{self.terminal.normal}") + choice = f" {color}{symbol} {message}{self.terminal.normal}" + if render.question.trim_choices: + extra_if_long = "..." + maximum_width = self.width - len(extra_if_long) + if self.terminal.length(choice) > maximum_width: + choice = self.terminal.truncate(choice, maximum_width) + extra_if_long + self.print_line(choice) def _print_header(self, render): header = render.get_header() From c3f05ca6036506cfe473e6651c0a1473a712b8ac Mon Sep 17 00:00:00 2001 From: Guy Salton Date: Thu, 9 Feb 2023 02:59:47 +0200 Subject: [PATCH 3/3] ConsoleRender: add `trim_str` function --- src/inquirer/render/console/__init__.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/inquirer/render/console/__init__.py b/src/inquirer/render/console/__init__.py index cd63c8b2..6640cdc2 100644 --- a/src/inquirer/render/console/__init__.py +++ b/src/inquirer/render/console/__init__.py @@ -66,10 +66,7 @@ def _print_options(self, render): message = message.decode("utf-8") choice = f" {color}{symbol} {message}{self.terminal.normal}" if render.question.trim_choices: - extra_if_long = "..." - maximum_width = self.width - len(extra_if_long) - if self.terminal.length(choice) > maximum_width: - choice = self.terminal.truncate(choice, maximum_width) + extra_if_long + choice = self.trim_str(choice) self.print_line(choice) def _print_header(self, render): @@ -85,12 +82,11 @@ def _print_header(self, render): header_ending = ": " if render.question.trim_header: - extra_if_long = "..." - maximum_width = self.width - len(header_ending + extra_if_long) - if self.terminal.length(header) > maximum_width: - header = self.terminal.truncate(header, maximum_width) + extra_if_long + header = self.trim_str(header, extra_after_trimming=header_ending) + else: + header += header_ending - full_header = f"{header}{header_ending}{str(render.get_current_value())}" + full_header = f"{header}{str(render.get_current_value())}" self.print_str(full_header, lf=not render.title_inline) def _process_input(self, render): @@ -176,3 +172,10 @@ def width(self): @property def height(self): return self.terminal.width or 24 + + def trim_str(self, msg: str, extra_after_trimming: str = ""): + extra_if_long = "..." + maximum_width = self.width - len(extra_if_long + extra_after_trimming) + if self.terminal.length(msg) > maximum_width: + return self.terminal.truncate(msg, maximum_width) + extra_if_long + extra_after_trimming + return msg + extra_after_trimming