style: 格式化代码
This commit is contained in:
@@ -4,15 +4,8 @@ import pathlib
|
|||||||
from textual.app import ComposeResult
|
from textual.app import ComposeResult
|
||||||
from textual.containers import ScrollableContainer
|
from textual.containers import ScrollableContainer
|
||||||
from textual.screen import Screen
|
from textual.screen import Screen
|
||||||
from textual.widgets import (
|
from textual.widgets import (Button, Footer, Header, Label, ListItem, ListView,
|
||||||
Button,
|
Static)
|
||||||
Footer,
|
|
||||||
Header,
|
|
||||||
Label,
|
|
||||||
ListItem,
|
|
||||||
ListView,
|
|
||||||
Static,
|
|
||||||
)
|
|
||||||
|
|
||||||
import heurams.services.timer as timer
|
import heurams.services.timer as timer
|
||||||
import heurams.services.version as version
|
import heurams.services.version as version
|
||||||
@@ -28,9 +21,9 @@ logger = get_logger(__name__)
|
|||||||
|
|
||||||
class DashboardScreen(Screen):
|
class DashboardScreen(Screen):
|
||||||
"""主仪表盘屏幕"""
|
"""主仪表盘屏幕"""
|
||||||
|
|
||||||
SUB_TITLE = "仪表盘"
|
SUB_TITLE = "仪表盘"
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name: str | None = None,
|
name: str | None = None,
|
||||||
@@ -41,7 +34,7 @@ class DashboardScreen(Screen):
|
|||||||
self.nextdates = {}
|
self.nextdates = {}
|
||||||
self.texts = {}
|
self.texts = {}
|
||||||
self.stay_enabled = {}
|
self.stay_enabled = {}
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
"""组合界面组件"""
|
"""组合界面组件"""
|
||||||
yield Header(show_clock=True)
|
yield Header(show_clock=True)
|
||||||
@@ -54,94 +47,94 @@ class DashboardScreen(Screen):
|
|||||||
ListView(id="union-list", classes="union-list-view"),
|
ListView(id="union-list", classes="union-list-view"),
|
||||||
Label(
|
Label(
|
||||||
f'"潜进" 启发式辅助记忆调度器 | 版本 {version.ver} '
|
f'"潜进" 启发式辅助记忆调度器 | 版本 {version.ver} '
|
||||||
f'{version.codename.capitalize()} 2025'
|
f"{version.codename.capitalize()} 2025"
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
yield Footer()
|
yield Footer()
|
||||||
|
|
||||||
def analyser(self, filename: str) -> dict:
|
def analyser(self, filename: str) -> dict:
|
||||||
"""分析文件状态以生成显示文本
|
"""分析文件状态以生成显示文本
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename: 要分析的文件名
|
filename: 要分析的文件名
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: 包含显示文本的字典,键为行号
|
dict: 包含显示文本的字典,键为行号
|
||||||
"""
|
"""
|
||||||
from heurams.kernel.particles.loader import load_electron, load_nucleon
|
from heurams.kernel.particles.loader import load_electron, load_nucleon
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
filestem = pathlib.Path(filename).stem
|
filestem = pathlib.Path(filename).stem
|
||||||
|
|
||||||
# 构建电子文件路径
|
# 构建电子文件路径
|
||||||
electron_dir = config_var.get()["paths"]["electron_dir"]
|
electron_dir = config_var.get()["paths"]["electron_dir"]
|
||||||
electron_file_path = pathlib.Path(electron_dir) / f"{filestem}.json"
|
electron_file_path = pathlib.Path(electron_dir) / f"{filestem}.json"
|
||||||
|
|
||||||
logger.debug(f"电子文件路径: {electron_file_path}")
|
logger.debug(f"电子文件路径: {electron_file_path}")
|
||||||
|
|
||||||
# 确保电子文件存在
|
# 确保电子文件存在
|
||||||
if not electron_file_path.exists():
|
if not electron_file_path.exists():
|
||||||
electron_file_path.touch()
|
electron_file_path.touch()
|
||||||
electron_file_path.write_text("{}")
|
electron_file_path.write_text("{}")
|
||||||
|
|
||||||
# 加载电子数据
|
# 加载电子数据
|
||||||
electron_dict = load_electron(path=electron_file_path)
|
electron_dict = load_electron(path=electron_file_path)
|
||||||
logger.debug(f"电子数据: {electron_dict}")
|
logger.debug(f"电子数据: {electron_dict}")
|
||||||
|
|
||||||
# 分析电子状态
|
# 分析电子状态
|
||||||
is_due = 0
|
is_due = 0
|
||||||
is_activated = 0
|
is_activated = 0
|
||||||
nextdate = 0x3F3F3F3F
|
nextdate = 0x3F3F3F3F
|
||||||
|
|
||||||
for electron in electron_dict.values():
|
for electron in electron_dict.values():
|
||||||
logger.debug(f"{electron}, 是否到期: {electron.is_due()}")
|
logger.debug(f"{electron}, 是否到期: {electron.is_due()}")
|
||||||
|
|
||||||
if electron.is_due():
|
if electron.is_due():
|
||||||
is_due = 1
|
is_due = 1
|
||||||
if electron.is_activated():
|
if electron.is_activated():
|
||||||
is_activated = 1
|
is_activated = 1
|
||||||
nextdate = min(nextdate, electron.nextdate())
|
nextdate = min(nextdate, electron.nextdate())
|
||||||
|
|
||||||
# 检查是否需要更多复习
|
# 检查是否需要更多复习
|
||||||
nucleon_dir = config_var.get()["paths"]["nucleon_dir"]
|
nucleon_dir = config_var.get()["paths"]["nucleon_dir"]
|
||||||
nucleon_path = pathlib.Path(nucleon_dir) / f"{filestem}.toml"
|
nucleon_path = pathlib.Path(nucleon_dir) / f"{filestem}.toml"
|
||||||
nucleon_count = len(load_nucleon(nucleon_path))
|
nucleon_count = len(load_nucleon(nucleon_path))
|
||||||
electron_count = len(electron_dict)
|
electron_count = len(electron_dict)
|
||||||
is_more = not (electron_count >= nucleon_count)
|
is_more = not (electron_count >= nucleon_count)
|
||||||
|
|
||||||
logger.debug(f"是否需要更多复习: {is_more}")
|
logger.debug(f"是否需要更多复习: {is_more}")
|
||||||
|
|
||||||
# 更新状态
|
# 更新状态
|
||||||
self.nextdates[filename] = nextdate
|
self.nextdates[filename] = nextdate
|
||||||
self.stay_enabled[filename] = (is_due or is_more)
|
self.stay_enabled[filename] = is_due or is_more
|
||||||
|
|
||||||
# 构建返回结果
|
# 构建返回结果
|
||||||
result[0] = f"{filename}\0"
|
result[0] = f"{filename}\0"
|
||||||
|
|
||||||
if not is_activated:
|
if not is_activated:
|
||||||
result[1] = " 尚未激活"
|
result[1] = " 尚未激活"
|
||||||
else:
|
else:
|
||||||
status_text = "需要复习" if is_due else "当前无需复习"
|
status_text = "需要复习" if is_due else "当前无需复习"
|
||||||
result[1] = f"下一次复习: {nextdate}\n{status_text}"
|
result[1] = f"下一次复习: {nextdate}\n{status_text}"
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def on_mount(self) -> None:
|
def on_mount(self) -> None:
|
||||||
"""挂载组件时初始化"""
|
"""挂载组件时初始化"""
|
||||||
union_list_widget = self.query_one("#union-list", ListView)
|
union_list_widget = self.query_one("#union-list", ListView)
|
||||||
probe = probe_all(0)
|
probe = probe_all(0)
|
||||||
|
|
||||||
# 分析所有文件
|
# 分析所有文件
|
||||||
for file in probe["nucleon"]:
|
for file in probe["nucleon"]:
|
||||||
self.texts[file] = self.analyser(file)
|
self.texts[file] = self.analyser(file)
|
||||||
|
|
||||||
# 按下次复习时间排序
|
# 按下次复习时间排序
|
||||||
nucleon_files = sorted(
|
nucleon_files = sorted(
|
||||||
probe["nucleon"],
|
probe["nucleon"],
|
||||||
key=lambda f: self.nextdates[f],
|
key=lambda f: self.nextdates[f],
|
||||||
reverse=True,
|
reverse=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 填充列表
|
# 填充列表
|
||||||
if not probe["nucleon"]:
|
if not probe["nucleon"]:
|
||||||
union_list_widget.append(
|
union_list_widget.append(
|
||||||
@@ -154,63 +147,61 @@ class DashboardScreen(Screen):
|
|||||||
)
|
)
|
||||||
union_list_widget.disabled = True
|
union_list_widget.disabled = True
|
||||||
return
|
return
|
||||||
|
|
||||||
for file in nucleon_files:
|
for file in nucleon_files:
|
||||||
text = self.texts[file]
|
text = self.texts[file]
|
||||||
list_item = ListItem(
|
list_item = ListItem(Label(f"{text[0]}\n{text[1]}"))
|
||||||
Label(f"{text[0]}\n{text[1]}")
|
|
||||||
)
|
|
||||||
union_list_widget.append(list_item)
|
union_list_widget.append(list_item)
|
||||||
|
|
||||||
if not self.stay_enabled[file]:
|
if not self.stay_enabled[file]:
|
||||||
list_item.disabled = True
|
list_item.disabled = True
|
||||||
|
|
||||||
def on_list_view_selected(self, event) -> None:
|
def on_list_view_selected(self, event) -> None:
|
||||||
"""处理列表项选择事件"""
|
"""处理列表项选择事件"""
|
||||||
if not isinstance(event.item, ListItem):
|
if not isinstance(event.item, ListItem):
|
||||||
return
|
return
|
||||||
|
|
||||||
selected_label = event.item.query_one(Label)
|
selected_label = event.item.query_one(Label)
|
||||||
label_text = str(selected_label.renderable)
|
label_text = str(selected_label.renderable)
|
||||||
|
|
||||||
if "未找到任何 .toml 文件" in label_text:
|
if "未找到任何 .toml 文件" in label_text:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 提取文件名
|
# 提取文件名
|
||||||
selected_filename = pathlib.Path(
|
selected_filename = pathlib.Path(label_text.partition("\0")[0].replace("*", ""))
|
||||||
label_text.partition("\0")[0].replace("*", "")
|
|
||||||
)
|
|
||||||
|
|
||||||
# 构建文件路径
|
# 构建文件路径
|
||||||
nucleon_dir = config_var.get()["paths"]["nucleon_dir"]
|
nucleon_dir = config_var.get()["paths"]["nucleon_dir"]
|
||||||
electron_dir = config_var.get()["paths"]["electron_dir"]
|
electron_dir = config_var.get()["paths"]["electron_dir"]
|
||||||
|
|
||||||
nucleon_file_path = pathlib.Path(nucleon_dir) / selected_filename
|
nucleon_file_path = pathlib.Path(nucleon_dir) / selected_filename
|
||||||
electron_file_path = pathlib.Path(electron_dir) / f"{selected_filename.stem}.json"
|
electron_file_path = (
|
||||||
|
pathlib.Path(electron_dir) / f"{selected_filename.stem}.json"
|
||||||
# 跳转到准备屏幕
|
|
||||||
self.app.push_screen(
|
|
||||||
PreparationScreen(nucleon_file_path, electron_file_path)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 跳转到准备屏幕
|
||||||
|
self.app.push_screen(PreparationScreen(nucleon_file_path, electron_file_path))
|
||||||
|
|
||||||
def on_button_pressed(self, event) -> None:
|
def on_button_pressed(self, event) -> None:
|
||||||
"""处理按钮点击事件"""
|
"""处理按钮点击事件"""
|
||||||
button_id = event.button.id
|
button_id = event.button.id
|
||||||
|
|
||||||
if button_id == "new_nucleon_button":
|
if button_id == "new_nucleon_button":
|
||||||
from .nucreator import NucleonCreatorScreen
|
from .nucreator import NucleonCreatorScreen
|
||||||
|
|
||||||
new_screen = NucleonCreatorScreen()
|
new_screen = NucleonCreatorScreen()
|
||||||
self.app.push_screen(new_screen)
|
self.app.push_screen(new_screen)
|
||||||
|
|
||||||
elif button_id == "precache_all_button":
|
elif button_id == "precache_all_button":
|
||||||
from .precache import PrecachingScreen
|
from .precache import PrecachingScreen
|
||||||
|
|
||||||
precache_screen = PrecachingScreen()
|
precache_screen = PrecachingScreen()
|
||||||
self.app.push_screen(precache_screen)
|
self.app.push_screen(precache_screen)
|
||||||
|
|
||||||
elif button_id == "about_button":
|
elif button_id == "about_button":
|
||||||
about_screen = AboutScreen()
|
about_screen = AboutScreen()
|
||||||
self.app.push_screen(about_screen)
|
self.app.push_screen(about_screen)
|
||||||
|
|
||||||
def action_quit_app(self) -> None:
|
def action_quit_app(self) -> None:
|
||||||
"""退出应用程序"""
|
"""退出应用程序"""
|
||||||
self.app.exit()
|
self.app.exit()
|
||||||
|
|||||||
Reference in New Issue
Block a user