mirror of
https://github.com/anomalyco/opencode.git
synced 2026-02-01 22:48:16 +00:00
fix(util): change filename truncation to end truncation, add truncateMiddle utility
This commit is contained in:
@@ -22,11 +22,16 @@ export function getFilenameTruncated(path: string | undefined, maxLength: number
|
||||
const filename = getFilename(path)
|
||||
if (filename.length <= maxLength) return filename
|
||||
const lastDot = filename.lastIndexOf(".")
|
||||
const name = lastDot <= 0 ? filename : filename.slice(0, lastDot)
|
||||
const ext = lastDot <= 0 ? "" : filename.slice(lastDot)
|
||||
const available = maxLength - ext.length - 1 // -1 for ellipsis
|
||||
if (available <= 0) return filename.slice(0, maxLength - 1) + "…"
|
||||
return filename.slice(0, available) + "…" + ext
|
||||
}
|
||||
|
||||
export function truncateMiddle(text: string, maxLength: number = 20) {
|
||||
if (text.length <= maxLength) return text
|
||||
const available = maxLength - 1 // -1 for ellipsis
|
||||
const start = Math.ceil(available / 2)
|
||||
const end = Math.floor(available / 2)
|
||||
return name.slice(0, start) + "…" + name.slice(-end) + ext
|
||||
return text.slice(0, start) + "…" + text.slice(-end)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user