Skip to content
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
20 changes: 12 additions & 8 deletions src-tauri/src/notices/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,16 +1138,20 @@ fn window_label(w: &str) -> String {
}

/// 「,约 3 小时后重置」(英文是 ` and resets in about 3 hours`,接在句子中间)。
/// 满一天按天说:每周的窗口离重置常常还有好几天,「约 150 小时」没法读
/// 满一天按天说:每周的窗口离重置常常还有好几天,「约 150 小时」没法读。
///
/// **四舍五入、取整之后再定单位**,和菜单栏的 `resets_in` 一样:1 小时 59 分是「约 2 小时」,
/// 不是「约 1 小时」;59.5 分钟是「约 1 小时」,不是「约 60 分钟」
fn after(secs: u64) -> String {
let (n, unit, unit_en) = if secs >= 86_400 {
(secs / 86_400, "天", "day")
} else if secs >= 3600 {
(secs / 3600, "小时", "hour")
} else if secs >= 60 {
(secs / 60, "分钟", "minute")
} else {
let [m, h, d] = [60.0, 3600.0, 86_400.0].map(|unit| (secs as f64 / unit).round() as u64);
let (n, unit, unit_en) = if secs < 60 {
return tr!(",即将重置", " and resets shortly").into();
} else if m < 60 {
(m, "分钟", "minute")
} else if h < 24 {
(h, "小时", "hour")
} else {
(d, "天", "day")
};
let plural = if n == 1 { "" } else { "s" };
tr!(
Expand Down
11 changes: 10 additions & 1 deletion src-tauri/src/notices/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,11 +1473,20 @@ fn an_english_notice_reads_as_whole_sentences() {
body("weekly", Some(3_600))
);
assert!(
body("7d", Some(90)).starts_with(
body("7d", Some(70)).starts_with(
"The 7-day usage limit has been reached and resets in about 1 minute."
)
);
assert!(body("weekly", Some(30)).contains("and resets shortly."));
// 四舍五入、取整之后再定单位:1 小时 59 分说 2 小时,59.5 分钟说 1 小时,23.5 小时说 1 天
for (secs, said) in [
(7_140, "resets in about 2 hours."),
(3_570, "resets in about 1 hour."),
(84_600, "resets in about 1 day."),
] {
let b = body("weekly", Some(secs));
assert!(b.contains(said), "{secs}: {b}");
}
assert!(body("weekly", None).starts_with("The weekly usage limit has been reached. "));

let s = &rules::from_event(&tw_api::Event::ConfigRejected {
Expand Down
16 changes: 11 additions & 5 deletions src/settings/RetentionSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,15 @@ export function capText(n: number): string {
return String(Number(g.toPrecision(3)));
}

function bytes(n: number): string {
if (n >= GIB) return `${gib(n)} GB`;
if (n >= 1024 * 1024) return `${Math.round(n / (1024 * 1024))} MB`;
if (n >= 1024) return `${Math.round(n / 1024)} KB`;
return `${n} B`;
/**
* 占用写成 B / KB / MB / GB。**先取整再定单位**:差一点满 1 MB 的取整是 1024 KB,该写成
* 「1 MB」,不是「1024 KB」;MB 到 GB 同理。导出给测试用。
*/
export function bytes(n: number): string {
if (n < 1024) return `${n} B`;
const kb = Math.round(n / 1024);
if (kb < 1024) return `${kb} KB`;
const mb = Math.round(n / (1024 * 1024));
if (mb < 1024) return `${mb} MB`;
return `${gib(n)} GB`;
}
18 changes: 17 additions & 1 deletion src/settings/retention.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import type { RetentionView } from "@/types";
import { capText, checks, draftOf } from "./RetentionSection";
import { bytes, capText, checks, draftOf } from "./RetentionSection";

const MIB = 1024 * 1024;
const GIB = 1024 * MIB;
Expand Down Expand Up @@ -45,3 +45,19 @@ describe("报文上限", () => {
expect(checks({ ...saved, body_days: "0" }, saved).body_days).toBe(false);
});
});

/** 占用的写法:取整进了位的,按下一档写 —— 不写「1024 KB」「1024 MB」 */
describe("报文占用", () => {
it("按 B、KB、MB、GB", () => {
expect(bytes(512)).toBe("512 B");
expect(bytes(300 * 1024)).toBe("300 KB");
expect(bytes(412 * MIB)).toBe("412 MB");
expect(bytes(Math.round(1.5 * GIB))).toBe("1.5 GB");
});

it("进了位就按下一档写", () => {
expect(bytes(MIB - 100)).toBe("1 MB");
expect(bytes(GIB - 100 * 1024)).toBe("1 GB");
expect(bytes(MIB - 600)).toBe("1023 KB");
});
});
Loading