shorten traffic display length

This commit is contained in:
pompurin404 2024-08-25 19:19:06 +08:00
parent d4698583ba
commit 334f718f72
No known key found for this signature in database
3 changed files with 13 additions and 12 deletions

View File

@ -6,3 +6,4 @@
### Bug Fixes
- 修改混合端口后系统代理没有更新
- 缩短MacOS网速显示长度

View File

@ -186,11 +186,11 @@ const mihomoTraffic = async (): Promise<void> => {
const json = JSON.parse(data) as IMihomoTrafficInfo
if (showTraffic) {
if (trafficHopping) {
tray?.setTitle('↑' + `${calcTraffic(json.up)}/s`.padStart(12), {
tray?.setTitle('↑' + `${calcTraffic(json.up)}/s`.padStart(9), {
fontType: 'monospaced'
})
} else {
tray?.setTitle('↓' + `${calcTraffic(json.down)}/s`.padStart(12), {
tray?.setTitle('↓' + `${calcTraffic(json.down)}/s`.padStart(9), {
fontType: 'monospaced'
})
}
@ -201,9 +201,9 @@ const mihomoTraffic = async (): Promise<void> => {
if (process.platform !== 'linux') {
tray?.setToolTip(
'↑' +
`${calcTraffic(json.up)}/s`.padStart(12) +
`${calcTraffic(json.up)}/s`.padStart(9) +
'\n↓' +
`${calcTraffic(json.down)}/s`.padStart(12)
`${calcTraffic(json.down)}/s`.padStart(9)
)
}

View File

@ -1,19 +1,19 @@
export function calcTraffic(byte: number): string {
if (byte < 1024) return `${byte} B`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} KB`
if (byte < 1024) return `${Math.round(byte)} KB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} MB`
if (byte < 1024) return `${Math.round(byte)} MB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} GB`
if (byte < 1024) return `${Math.round(byte)} GB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} TB`
if (byte < 1024) return `${Math.round(byte)} TB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} PB`
if (byte < 1024) return `${Math.round(byte)} PB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} EB`
if (byte < 1024) return `${Math.round(byte)} EB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} ZB`
if (byte < 1024) return `${Math.round(byte)} ZB`
byte /= 1024
return `${byte.toFixed(2)} YB`
return `${Math.round(byte)} YB`
}