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 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""" """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( def print_msgs(
@ -49,6 +50,7 @@ def print_msgs(
delimiter: str = "\n", delimiter: str = "\n",
style: MsgStyle = MsgStyle.NORMAL, style: MsgStyle = MsgStyle.NORMAL,
get_input: bool = False, get_input: bool = False,
screen_input: bool = False,
) -> Union[None, str]: ) -> Union[None, str]:
# Same as print_msg, but for a list # Same as print_msg, but for a list
text = Text("", end="") text = Text("", end="")
@ -72,7 +74,7 @@ def print_msgs(
# Always print messages to stderr # Always print messages to stderr
console = Console(stderr=True) console = Console(stderr=True)
if get_input: 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) console.print(decorated_text, new_line_start=style.prepend_newline)

View file

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