Skip to content

Commit 98a3ebf

Browse files
committed
tui: Remove duplication of locale specific functions
1 parent fc423a0 commit 98a3ebf

3 files changed

Lines changed: 4 additions & 101 deletions

File tree

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,2 @@
1-
export function titlecase(str: string) {
2-
return str.replace(/\b\w/g, (c) => c.toUpperCase())
3-
}
4-
5-
export function time(input: number): string {
6-
const date = new Date(input)
7-
return date.toLocaleTimeString(undefined, { timeStyle: "short" })
8-
}
9-
10-
export function datetime(input: number): string {
11-
const date = new Date(input)
12-
const localTime = time(input)
13-
const localDate = date.toLocaleDateString()
14-
return `${localTime} · ${localDate}`
15-
}
16-
17-
export function datetimeFull(input: number): string {
18-
const date = new Date(input)
19-
const dd = String(date.getDate()).padStart(2, "0")
20-
const mm = String(date.getMonth() + 1).padStart(2, "0")
21-
const yyyy = date.getFullYear()
22-
const hh = String(date.getHours()).padStart(2, "0")
23-
const min = String(date.getMinutes()).padStart(2, "0")
24-
return `${dd}/${mm}/${yyyy} ${hh}:${min}`
25-
}
26-
27-
export function todayTimeOrDateTime(input: number): string {
28-
const date = new Date(input)
29-
const now = new Date()
30-
const isToday =
31-
date.getFullYear() === now.getFullYear() && date.getMonth() === now.getMonth() && date.getDate() === now.getDate()
32-
33-
if (isToday) {
34-
return time(input)
35-
} else {
36-
return datetime(input)
37-
}
38-
}
39-
40-
export function number(num: number): string {
41-
if (num >= 1000000) {
42-
return (num / 1000000).toFixed(1) + "M"
43-
} else if (num >= 1000) {
44-
return (num / 1000).toFixed(1) + "K"
45-
}
46-
return num.toString()
47-
}
48-
49-
export function duration(input: number) {
50-
if (input < 1000) {
51-
return `${input}ms`
52-
}
53-
if (input < 60000) {
54-
return `${(input / 1000).toFixed(1)}s`
55-
}
56-
if (input < 3600000) {
57-
const minutes = Math.floor(input / 60000)
58-
const seconds = Math.floor((input % 60000) / 1000)
59-
return `${minutes}m ${seconds}s`
60-
}
61-
if (input < 86400000) {
62-
const hours = Math.floor(input / 3600000)
63-
const minutes = Math.floor((input % 3600000) / 60000)
64-
return `${hours}h ${minutes}m`
65-
}
66-
const hours = Math.floor(input / 3600000)
67-
const days = Math.floor((input % 3600000) / 86400000)
68-
return `${days}d ${hours}h`
69-
}
70-
71-
export function truncate(str: string, len: number): string {
72-
if (str.length <= len) return str
73-
return str.slice(0, len - 1) + "…"
74-
}
75-
76-
export function truncateLeft(str: string, len: number): string {
77-
if (str.length <= len) return str
78-
return "…" + str.slice(-(len - 1))
79-
}
80-
81-
export function truncateMiddle(str: string, maxLength: number = 35): string {
82-
if (str.length <= maxLength) return str
83-
84-
const ellipsis = "…"
85-
const keepStart = Math.ceil((maxLength - ellipsis.length) / 2)
86-
const keepEnd = Math.floor((maxLength - ellipsis.length) / 2)
87-
88-
return str.slice(0, keepStart) + ellipsis + str.slice(-keepEnd)
89-
}
90-
91-
export function pluralize(count: number, singular: string, plural: string): string {
92-
const template = count === 1 ? singular : plural
93-
return template.replace("{}", count.toString())
94-
}
95-
96-
export * as Locale from "./locale"
1+
export * from "@opencode-ai/tui/util/locale"
2+
export { Locale } from "@opencode-ai/tui/util/locale"

packages/tui/src/routes/session/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ function UserMessage(props: {
15161516
<Show when={ctx.showTimestamps()}>
15171517
<text fg={theme.textMuted}>
15181518
<span style={{ fg: theme.textMuted }}>
1519-
{Locale.todayTimeOrDateTime(props.message.time.created)}
1519+
{Locale.datetimeFull(props.message.time.created)}
15201520
</span>
15211521
</text>
15221522
</Show>

packages/tui/src/util/locale.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ export function datetime(input: number): string {
1616

1717
export function datetimeFull(input: number): string {
1818
const date = new Date(input)
19-
return date.toLocaleString(undefined, {
20-
dateStyle: "medium",
21-
timeStyle: "medium",
22-
})
19+
return `${String(date.getDate()).padStart(2, "0")}/${String(date.getMonth() + 1).padStart(2, "0")}/${date.getFullYear()} ${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`
2320
}
2421

2522
export function todayTimeOrDateTime(input: number): string {

0 commit comments

Comments
 (0)