clean up create_password function

This commit is contained in:
Jonathan Wren 2022-05-17 12:34:19 -07:00
parent 70157303d4
commit 1a6afd9f4a
2 changed files with 15 additions and 10 deletions

View file

@ -39,9 +39,10 @@ def list_journals(configuration):
return result
def print_msg(msg: Message) -> Union[None, str]:
def print_msg(msg: Message, **kwargs) -> Union[None, str]:
"""Helper function to print a single message"""
return print_msgs([msg], style=msg.style)
kwargs["style"] = msg.style
return print_msgs([msg], **kwargs)
def print_msgs(
@ -49,6 +50,7 @@ def print_msgs(
delimiter: str = "\n",
style: MsgStyle = MsgStyle.NORMAL,
get_input: bool = False,
screen_input: bool = False,
) -> Union[None, str]:
# Same as print_msg, but for a list
text = Text("", end="")
@ -72,7 +74,7 @@ def print_msgs(
# Always print messages to stderr
console = Console(stderr=True)
if get_input:
return str(console.input(prompt=decorated_text))
return str(console.input(prompt=decorated_text, password=screen_input))
console.print(decorated_text, new_line_start=style.prepend_newline)

View file

@ -1,8 +1,5 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
import getpass
from jrnl.messages import Message
from jrnl.messages import MsgText
from jrnl.messages import MsgStyle
@ -11,20 +8,26 @@ from jrnl.output import print_msgs
def create_password(journal_name: str) -> str:
kwargs = {
"get_input": True,
"screen_input": True,
}
while True:
pw = getpass.getpass(str(MsgText.PasswordFirstEntry))
pw = print_msg(Message(MsgText.PasswordFirstEntry, MsgStyle.PROMPT), **kwargs)
if not pw:
print_msg(Message(MsgText.PasswordCanNotBeEmpty, MsgStyle.PLAIN))
print_msg(Message(MsgText.PasswordCanNotBeEmpty, MsgStyle.WARNING))
continue
elif pw == getpass.getpass(str(MsgText.PasswordConfirmEntry)):
elif pw == print_msg(Message(MsgText.PasswordConfirmEntry, MsgStyle.PROMPT), **kwargs):
break
print_msg(Message(MsgText.PasswordDidNotMatch, MsgStyle.ERROR))
if yesno(Message(MsgText.PasswordStoreInKeychain), default=True):
from .EncryptedJournal import set_keychain
set_keychain(journal_name, pw)
return pw