From cb730e49e11649c5e4f01994fb9e9c3b8d696a81 Mon Sep 17 00:00:00 2001 From: resolritter Date: Mon, 17 Aug 2020 06:16:01 -0300 Subject: [PATCH] support multiline questions --- drillsrs/cmd/review.py | 7 ++----- drillsrs/cmd/study.py | 6 ++---- drillsrs/question.py | 14 ++++++++++++++ drillsrs/util.py | 8 ++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 drillsrs/question.py diff --git a/drillsrs/cmd/review.py b/drillsrs/cmd/review.py index 9e25a22..d1a7967 100644 --- a/drillsrs/cmd/review.py +++ b/drillsrs/cmd/review.py @@ -5,6 +5,7 @@ from drillsrs.cmd.command_base import CommandBase from drillsrs import db, scheduler, util from drillsrs.cli_args import Mode +from drillsrs.question import render_question_prompt def _review_single_card( @@ -24,11 +25,7 @@ def _review_single_card( if mode is Mode.reversed or mode is Mode.mixed and random.random() > 0.5: raw_question, raw_answers = random.choice(raw_answers), [raw_question] - print('Question: %s' % raw_question, end='') - if card.tags: - print(' [%s]' % util.format_card_tags(card.tags), end='') - print() - + print(render_question_prompt(raw_question, raw_answers, card.tags)) while True: answer_text = util.ask('Answer: ') if answer_text: diff --git a/drillsrs/cmd/study.py b/drillsrs/cmd/study.py index 985dcab..025537a 100644 --- a/drillsrs/cmd/study.py +++ b/drillsrs/cmd/study.py @@ -4,6 +4,7 @@ from drillsrs.cmd.command_base import CommandBase from drillsrs import db, scheduler, util from drillsrs.cli_args import Mode +from drillsrs.question import render_question_prompt def _learn_single_card( @@ -21,10 +22,7 @@ def _learn_single_card( if mode is Mode.reversed or mode is Mode.mixed and random.random() > 0.5: raw_question, raw_answers = random.choice(raw_answers), [raw_question] - question = 'Question: %s' % raw_question - if card.tags: - question += ' [%s]' % util.format_card_tags(card.tags) - util.ask(question) + util.ask(render_question_prompt(raw_question, raw_answers, card.tags)) util.ask('Answers: %s' % ', '.join(raw_answers)) print('') diff --git a/drillsrs/question.py b/drillsrs/question.py new file mode 100644 index 0000000..a17c833 --- /dev/null +++ b/drillsrs/question.py @@ -0,0 +1,14 @@ +from drillsrs import util + + +question_prompt = "Question:" + + +def render_question_prompt(raw_question, raw_answers, tags): + rendered_tags = "[%s]" % util.format_card_tags(tags) if tags else "" + + if util.is_raw_multiline(raw_question): + rendered_question = util.from_raw_to_multiline(raw_question) + return f"{question_prompt} {rendered_tags}\n{rendered_question}" + else: + return f"{question_prompt} {raw_question} {rendered_tags}" diff --git a/drillsrs/util.py b/drillsrs/util.py index 40f5013..4547677 100644 --- a/drillsrs/util.py +++ b/drillsrs/util.py @@ -77,3 +77,11 @@ def get_data(file_name: str) -> str: template_path = os.path.join(here, 'data', file_name) with open(template_path, 'r') as handle: return handle.read() + + +def is_raw_multiline(raw_str: str) -> bool: + return r'\n' in raw_str + + +def from_raw_to_multiline(raw_str: str) -> str: + return raw_str.replace(r'\n', '\n')