feat: v2.9.0 终端实时面板,学习时长与学分统计

新增 runner/tui.mjs(~470 行):前台运行时自动开一块实时面板,
框线布局显示状态 / 课程 / 学时 / 课时 / 视频进度条 / 进度 / 最近日志。

- 学习统计 runner/study.mjs:只累计视频真在播的时长(暂停、重试退避、
  页面加载都不计),学分按课程去重(重试 / 刷新不重复计)
- 页面侧新增 credit 快照:平台只把本门学分写在倒计时文案里,
  学完文案就消失,所以边播边记
- log.mjs 增加 setConsoleSink:面板接管终端那一路,日志文件仍是纯文本
- 面板里按 q 等价 Ctrl+C,走 stop.request 同一条优雅收尾路径
- 尺寸自适应:宽度跟随终端(不封顶 120 列),无 resize 事件的平台
  靠定时刷新发现尺寸变化;变窄时按旧帧在新宽度下的折行数回退光标,
  折完超过窗口高就整屏重置;宽度 / 高度不足按重要性丢字段
- 非 TTY(重定向 / 管道 / 计划任务 / --check)自动降级为逐行日志,
  不写 ANSI 控制字符;--no-tui / YUN_XUE_TANG_TUI=0 可手动关闭,
  --no-color / NO_COLOR 关颜色
- 监控轮询从 2000ms 调到 1000ms,与面板刷新对齐
- supervisor 收尾时先收面板再逐行打退出日志,并汇总本次学习时长 / 学分
- check.mjs 增加裸 CR 检查:脚本里的 config\runner 曾被写成裸 CR,
  变成 config<CR>unner 导致找不到配置文件

自测:npm run test:tui(渲染 / 列宽 / 极端尺寸)、npm run test:study(假时钟口径)
This commit is contained in:
void committed 2026-10-26 21:49:13 +08:00
1 parent c1e883d67f
commit 65bf63ea6a
17 files changed
+1608 -28

No files matched your search

+19 -1
View File
@@ -5,11 +5,25 @@
// openLog(file, maxBytes); // 不调用就只打终端
// log('信息A', { x: 1 }); // → [2026-01-01 00:00:00] [信息] 信息A {"x":1}
//
// 终端面板(tui.mjs)启用时会把「终端」这一路改道到面板的日志区(setConsoleSink),
// 文件那一路始终是纯文本逐行 —— 面板不会污染日志文件,grep / 采集不受影响。
//
// 注意:openLog 必须在任何写文件之前调用;closeLog 在进程退出路径上兜底调用。
import fs from 'node:fs';
import path from 'node:path';
let stream = null;
/** 终端输出目标;null = 直接 console.log(未启用面板时的默认行为) */
let consoleSink = null;
/**
* 接管 / 归还终端输出。传 null 恢复逐行 console.log。
* sink(level, text) 收到的是完整的一行(不含换行)。
* @param {null | ((level: string, text: string) => void)} fn
*/
export function setConsoleSink(fn) {
consoleSink = typeof fn === 'function' ? fn : null;
}
/** 打开日志文件;maxBytes 为单文件上限,超出时把现有文件改名成 .1 */
export function openLog(file, maxBytes) {
@@ -42,7 +56,11 @@ function line(level, args) {
if (typeof a === 'string') return a;
try { return JSON.stringify(a); } catch (e) { return String(a); }
}).join(' ');
console.log(text);
if (consoleSink) {
try { consoleSink(level, text); } catch (e) { /* 面板异常不影响日志文件 */ }
} else {
console.log(text);
}
if (stream) { try { stream.write(text + '\n'); } catch (e) { /* ignore */ } }
}