chore: update sentence loading error

This commit is contained in:
duorameng
2026-04-30 09:01:02 +08:00
parent d4a93e9bdc
commit 7a8333f915
3 changed files with 48 additions and 21 deletions
+24 -11
View File
@@ -1,7 +1,6 @@
package constant package constant
import ( import (
"bufio"
"bytes" "bytes"
_ "embed" _ "embed"
"encoding/json" "encoding/json"
@@ -26,13 +25,25 @@ var (
// initSentences 初始化:统计行数和记录每行偏移 // initSentences 初始化:统计行数和记录每行偏移
func initSentences() { func initSentences() {
sentenceOnce.Do(func() { sentenceOnce.Do(func() {
scanner := bufio.NewScanner(bytes.NewReader(sentenceData)) data := sentenceData
var offset int64 = 0 var offset int64 = 0
for scanner.Scan() { for {
// 记录当前行的起始位置
lineOffsets = append(lineOffsets, offset) lineOffsets = append(lineOffsets, offset)
// scanner.Bytes() returns the line without newline
offset += int64(len(scanner.Bytes())) + 1 // +1 for newline
lineCount++ lineCount++
// 寻找下一个换行符
idx := bytes.IndexByte(data[offset:], '\n')
if idx == -1 {
// 最后一行没有换行符
break
}
// 移动到下一行的起始位置
offset += int64(idx) + 1
if offset >= int64(len(data)) {
break
}
} }
}) })
} }
@@ -40,24 +51,26 @@ func initSentences() {
// GetRandomSentence 随机获取一条古诗词 // GetRandomSentence 随机获取一条古诗词
func GetRandomSentence() string { func GetRandomSentence() string {
initSentences() initSentences()
if lineCount == 0 { if lineCount <= 0 {
return "欢迎使用白虎面板" return "欢迎使用白虎面板"
} }
targetIndex := rand.Intn(lineCount) targetIndex := rand.Intn(lineCount)
start := lineOffsets[targetIndex] start := lineOffsets[targetIndex]
// 找到行结束位置(通过下一个偏移量或文件末尾) // 确定当前行的结束位置
var end int64 var end int64
if targetIndex < lineCount-1 { if targetIndex < lineCount-1 {
end = lineOffsets[targetIndex+1] - 1 // -1 移除换行符 end = lineOffsets[targetIndex+1]
} else { } else {
end = int64(len(sentenceData)) end = int64(len(sentenceData))
} }
// 确保不越界并移除末尾可能的回车符 (Windows \r\n) // 提取行并清理两端的空白字符(包括 \r, \n
line := sentenceData[start:end] line := bytes.TrimSpace(sentenceData[start:end])
line = bytes.TrimRight(line, "\r\n") if len(line) == 0 {
return "欢迎使用白虎面板"
}
var sData []string var sData []string
if err := json.Unmarshal(line, &sData); err == nil && len(sData) >= 1 { if err := json.Unmarshal(line, &sData); err == nil && len(sData) >= 1 {
+22 -8
View File
@@ -95,12 +95,12 @@
--sidebar-border: #e5e7eb; --sidebar-border: #e5e7eb;
--sidebar-ring: #9ca3af; --sidebar-ring: #9ca3af;
/* 字体栈定义 */ /* 字体栈定义 - 优先使用 Inter Variable 以获得最佳的可变字体支持 */
--font-main: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif; --font-main: 'Inter Variable', 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif;
} }
.is-windows { .is-windows {
--font-main: 'Inter', 'Noto Sans SC', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif; --font-main: 'Inter Variable', 'Inter', 'Noto Sans SC', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans CN", sans-serif;
} }
/* OKLCH support targeting modern browsers */ /* OKLCH support targeting modern browsers */
@@ -216,12 +216,23 @@
} }
body { body {
@apply bg-background text-foreground antialiased; @apply bg-background text-foreground;
/* 增强字体栈:根据平台差异化 Noto Sans SC 与 system-ui 的优先级 */ /* 增强字体栈与高级特性优化 */
font-family: var(--font-main); font-family: var(--font-main);
font-size: 14px; font-size: 14px;
line-height: 1.5; line-height: 1.5;
letter-spacing: -0.01em; letter-spacing: -0.011em;
/* Inter 字体高级特性优化:
cv05: l 增加尾钩 (提升识别)
cv08: G 增加尖角
cv11: 单层小写 a
ss01: 开口数字 (更现代)
ss03: 圆形 0
*/
font-feature-settings: "cv05", "cv08", "cv11", "ss01", "ss03";
font-optical-sizing: auto;
-webkit-font-smoothing: antialiased; -webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
text-rendering: optimizeLegibility; text-rendering: optimizeLegibility;
@@ -247,8 +258,11 @@
body { body {
font-size: 15.5px; font-size: 15.5px;
line-height: 1.55; line-height: 1.55;
letter-spacing: -0.01em; letter-spacing: -0.011em;
/* 移动端使用 antialiased 以匹配高 DPI 渲染 */ /* 移动端增强字体渲染 */
font-family: var(--font-main);
font-feature-settings: "cv05", "cv08", "cv11", "ss01", "ss03";
font-optical-sizing: auto;
-webkit-font-smoothing: antialiased; -webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
} }
+2 -2
View File
@@ -22,7 +22,7 @@ function handleClick() {
</script> </script>
<template> <template>
<span v-bind="$attrs" class="truncate block cursor-pointer hover:text-primary transition-colors font-mono" :title="text || '-'" <span v-bind="$attrs" class="truncate block cursor-pointer hover:text-primary transition-colors" :title="text || '-'"
@click.stop="handleClick"> @click.stop="handleClick">
{{ text || '-' }} {{ text || '-' }}
</span> </span>
@@ -30,7 +30,7 @@ function handleClick() {
<BaihuDialog v-model:open="showDialog" :title="title"> <BaihuDialog v-model:open="showDialog" :title="title">
<div class="max-h-[60vh] overflow-y-auto custom-scrollbar"> <div class="max-h-[60vh] overflow-y-auto custom-scrollbar">
<div class="p-4 bg-muted/30 rounded-xl border border-border/50"> <div class="p-4 bg-muted/30 rounded-xl border border-border/50">
<p class="text-[13.5px] leading-relaxed text-foreground/90 break-all whitespace-pre-wrap font-mono"> <p class="text-[13.5px] leading-relaxed text-foreground/90 break-all whitespace-pre-wrap">
{{ text }} {{ text }}
</p> </p>
</div> </div>