Skip to content

Commit 12239da

Browse files
committed
Implemented the main functionality
New input added to the prompt(): hide_password False by default, if True, the characters won't be echoed back (as *) in the output
1 parent 45713d4 commit 12239da

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/prompt_toolkit/shortcuts/prompt.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ class PromptSession(Generic[_T]):
249249
When True (the default), automatically wrap long lines instead of
250250
scrolling horizontally.
251251
:param is_password: Show asterisks instead of the actual typed characters.
252+
:param hide_password: Hide the password, rather than showing asterisks.
252253
:param editing_mode: ``EditingMode.VI`` or ``EditingMode.EMACS``.
253254
:param vi_mode: `bool`, if True, Identical to ``editing_mode=EditingMode.VI``.
254255
:param complete_while_typing: `bool` or
@@ -335,10 +336,10 @@ class PromptSession(Generic[_T]):
335336
"lexer",
336337
"completer",
337338
"complete_in_thread",
338-
"is_password",
339339
"editing_mode",
340340
"key_bindings",
341341
"is_password",
342+
"hide_password",
342343
"bottom_toolbar",
343344
"style",
344345
"style_transformation",
@@ -377,6 +378,7 @@ def __init__(
377378
multiline: FilterOrBool = False,
378379
wrap_lines: FilterOrBool = True,
379380
is_password: FilterOrBool = False,
381+
hide_password: bool = False,
380382
vi_mode: bool = False,
381383
editing_mode: EditingMode = EditingMode.EMACS,
382384
complete_while_typing: FilterOrBool = True,
@@ -435,6 +437,7 @@ def __init__(
435437
self.completer = completer
436438
self.complete_in_thread = complete_in_thread
437439
self.is_password = is_password
440+
self.hide_password = hide_password
438441
self.key_bindings = key_bindings
439442
self.bottom_toolbar = bottom_toolbar
440443
self.style = style
@@ -555,13 +558,17 @@ def _create_layout(self) -> Layout:
555558
def display_placeholder() -> bool:
556559
return self.placeholder is not None and self.default_buffer.text == ""
557560

561+
password_character = "" if self.hide_password else "*"
562+
558563
all_input_processors = [
559564
HighlightIncrementalSearchProcessor(),
560565
HighlightSelectionProcessor(),
561566
ConditionalProcessor(
562567
AppendAutoSuggestion(), has_focus(default_buffer) & ~is_done
563568
),
564-
ConditionalProcessor(PasswordProcessor(), dyncond("is_password")),
569+
ConditionalProcessor(
570+
PasswordProcessor(char=password_character), dyncond("is_password")
571+
),
565572
DisplayMultipleCursors(),
566573
# Users can insert processors here.
567574
DynamicProcessor(lambda: merge_processors(self.input_processors or [])),
@@ -866,6 +873,7 @@ def prompt(
866873
completer: Completer | None = None,
867874
complete_in_thread: bool | None = None,
868875
is_password: bool | None = None,
876+
hide_password: bool | None = None,
869877
key_bindings: KeyBindingsBase | None = None,
870878
bottom_toolbar: AnyFormattedText | None = None,
871879
style: BaseStyle | None = None,
@@ -961,6 +969,8 @@ class itself. For these, passing in ``None`` will keep the current
961969
self.complete_in_thread = complete_in_thread
962970
if is_password is not None:
963971
self.is_password = is_password
972+
if hide_password is not None:
973+
self.hide_password = hide_password
964974
if key_bindings is not None:
965975
self.key_bindings = key_bindings
966976
if bottom_toolbar is not None:
@@ -1103,6 +1113,7 @@ async def prompt_async(
11031113
completer: Completer | None = None,
11041114
complete_in_thread: bool | None = None,
11051115
is_password: bool | None = None,
1116+
hide_password: bool | None = None,
11061117
key_bindings: KeyBindingsBase | None = None,
11071118
bottom_toolbar: AnyFormattedText | None = None,
11081119
style: BaseStyle | None = None,
@@ -1155,6 +1166,8 @@ async def prompt_async(
11551166
self.complete_in_thread = complete_in_thread
11561167
if is_password is not None:
11571168
self.is_password = is_password
1169+
if hide_password is not None:
1170+
self.hide_password = hide_password
11581171
if key_bindings is not None:
11591172
self.key_bindings = key_bindings
11601173
if bottom_toolbar is not None:
@@ -1376,6 +1389,7 @@ def prompt(
13761389
completer: Completer | None = None,
13771390
complete_in_thread: bool | None = None,
13781391
is_password: bool | None = None,
1392+
hide_password: bool | None = None,
13791393
key_bindings: KeyBindingsBase | None = None,
13801394
bottom_toolbar: AnyFormattedText | None = None,
13811395
style: BaseStyle | None = None,
@@ -1420,7 +1434,9 @@ def prompt(
14201434
"""
14211435
# The history is the only attribute that has to be passed to the
14221436
# `PromptSession`, it can't be passed into the `prompt()` method.
1423-
session: PromptSession[str] = PromptSession(history=history)
1437+
# The `hide_password` is needed by the layout, so must be provided
1438+
# in the init as well.
1439+
session: PromptSession[str] = PromptSession(history=history, hide_password=hide_password)
14241440

14251441
return session.prompt(
14261442
message,
@@ -1431,6 +1447,7 @@ def prompt(
14311447
completer=completer,
14321448
complete_in_thread=complete_in_thread,
14331449
is_password=is_password,
1450+
# hide_password=hide_password,
14341451
key_bindings=key_bindings,
14351452
bottom_toolbar=bottom_toolbar,
14361453
style=style,

0 commit comments

Comments
 (0)