Archived
0
0

style: 代码格式化

This commit is contained in:
2025-12-13 21:47:37 +08:00
parent a0b327cdbb
commit baa7ac8ee9
64 changed files with 755 additions and 573 deletions

View File

@@ -10,10 +10,29 @@ import copy
import random
from textual.message import Message
class ClozePuzzle(BasePuzzleWidget):
def __init__(self, *children: Widget, atom: pt.Atom, alia: str = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
def __init__(
self,
*children: Widget,
atom: pt.Atom,
alia: str = "",
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
markup: bool = True,
) -> None:
super().__init__(
*children,
atom=atom,
name=name,
id=id,
classes=classes,
disabled=disabled,
markup=markup,
)
self.inputlist = list()
self.hashtable = {}
self.alia = alia
@@ -21,7 +40,11 @@ class ClozePuzzle(BasePuzzleWidget):
def _work(self):
cfg = self.atom.registry["orbital"]["puzzles"][self.alia]
self.puzzle = pz.ClozePuzzle(text=cfg["content"], delimiter=cfg["delimiter"], min_denominator=cfg["min_denominator"])
self.puzzle = pz.ClozePuzzle(
text=cfg["content"],
delimiter=cfg["delimiter"],
min_denominator=cfg["min_denominator"],
)
self.puzzle.refresh()
self.ans = copy.copy(self.puzzle.answer)
random.shuffle(self.ans)
@@ -35,6 +58,7 @@ class ClozePuzzle(BasePuzzleWidget):
class InputChanged(Message):
"""输入变化消息"""
def __init__(self, current_input: list, max_length: int) -> None:
self.current_input = current_input # 当前输入
self.max_length = max_length # 最大长度
@@ -51,16 +75,17 @@ class ClozePuzzle(BasePuzzleWidget):
def update_preview(self):
preview = self.query_one("#inputpreview")
preview.update(f"当前输入: {self.inputlist}") # type: ignore
self.post_message(self.InputChanged(
current_input=self.inputlist.copy(),
max_length=len(self.puzzle.answer)
))
preview.update(f"当前输入: {self.inputlist}") # type: ignore
self.post_message(
self.InputChanged(
current_input=self.inputlist.copy(), max_length=len(self.puzzle.answer)
)
)
def on_button_pressed(self, event: Button.Pressed) -> None:
button_id = event.button.id
if button_id == "delete":
if len(self.inputlist) > 0:
self.inputlist.pop()
@@ -69,17 +94,17 @@ class ClozePuzzle(BasePuzzleWidget):
answer_text = self.hashtable[button_id]
self.inputlist.append(answer_text)
self.update_preview()
if len(self.inputlist) >= len(self.puzzle.answer):
is_correct = self.inputlist == self.puzzle.answer
rating = 4 if is_correct else 2
self.post_message(self.RatingChanged(
atom=self.atom,
rating=rating,
is_correct=is_correct
))
self.post_message(
self.RatingChanged(
atom=self.atom, rating=rating, is_correct=is_correct
)
)
if not is_correct:
self.inputlist = []
self.update_preview()
self.update_preview()