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

Add trimming options #381

Open
wants to merge 3 commits into
base: dev/renderer-update
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/inquirer/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
validate=True,
show_default=False,
other=False,
trim_header=True,
):
self.name = name
self._message = message
Expand All @@ -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)
Expand Down Expand Up @@ -109,18 +111,22 @@ 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


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


Expand All @@ -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):
Expand All @@ -149,11 +155,14 @@ def __init__(
carousel=False,
other=False,
autocomplete=None,
trim_header=True,
trim_choices=False,
):

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
self.trim_choices = trim_choices


class Checkbox(Question):
Expand All @@ -170,11 +179,14 @@ def __init__(
carousel=False,
other=False,
autocomplete=None,
trim_header=True,
trim_choices=False,
):

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
self.trim_choices = trim_choices


# Solution for checking valid path based on
Expand Down Expand Up @@ -222,8 +234,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
Expand Down
22 changes: 16 additions & 6 deletions src/inquirer/render/console/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ 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:
choice = self.trim_str(choice)
self.print_line(choice)

def _print_header(self, render):
header = render.get_header()
Expand All @@ -78,12 +81,12 @@ 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:
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):
Expand Down Expand Up @@ -169,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