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

Support multi-line questions #11

Merged
merged 1 commit into from
Aug 20, 2020
Merged
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
7 changes: 2 additions & 5 deletions drillsrs/cmd/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions drillsrs/cmd/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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('')

Expand Down
14 changes: 14 additions & 0 deletions drillsrs/question.py
Original file line number Diff line number Diff line change
@@ -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}"
8 changes: 8 additions & 0 deletions drillsrs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')