-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathutils.ts
More file actions
113 lines (104 loc) · 3.21 KB
/
Copy pathutils.ts
File metadata and controls
113 lines (104 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { t } from '@nextcloud/l10n'
/**
* Formats a size in megabytes to a human-readable string.
*
* @param mb size in megabytes
*/
export function formatMegabytes(mb: number): string {
const units = ['MB', 'GB', 'TB', 'PB', 'EB']
let value = mb
let i = 0
while (value >= 1024 && i < units.length - 1) {
value /= 1024
i++
}
return value.toFixed(2) + ' ' + units[i]
}
/**
* Adds alpha to a CSS color string (handles #RRGGBB and rgb()).
*
* @param color base colour as #RRGGBB or rgb(...)
* @param alpha opacity between 0 and 1
*/
export function withAlpha(color: string, alpha: number): string {
if (color.startsWith('#') && color.length === 7) {
const r = parseInt(color.slice(1, 3), 16)
const g = parseInt(color.slice(3, 5), 16)
const b = parseInt(color.slice(5, 7), 16)
return `rgba(${r}, ${g}, ${b}, ${alpha})`
}
if (color.startsWith('rgb(')) {
return color.replace('rgb(', 'rgba(').replace(')', `, ${alpha})`)
}
return color
}
/**
* Formats a size in bytes to a human-readable string.
*
* @param bytes size in bytes
*/
export function formatBytes(bytes: number): string {
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
let value = Math.abs(bytes)
let i = 0
while (value >= 1024 && i < units.length - 1) {
value /= 1024
i++
}
return value.toFixed(1) + ' ' + units[i]
}
/**
* Reads a themed colour from the active theme's CSS custom properties, so that
* canvas drawing (which cannot resolve `var(...)` itself) follows light/dark
* mode and custom themes.
*
* @param name custom property name, including the leading dashes
* @param fallback colour to use when the property is not set
*/
export function cssColor(name: string, fallback: string): string {
return getComputedStyle(document.documentElement)
.getPropertyValue(name)
.trim() || fallback
}
/**
* The themed primary accent colour.
*/
export function primaryColor(): string {
return cssColor('--color-primary-element', '#3681c3')
}
/**
* Shortens a job class for a table column: for an `OCA\` job the second segment
* is the app id, so `OCA\Talk\BackgroundJob\RemoveEmptyRooms` becomes
* "Talk RemoveEmptyRooms".
*
* @param className fully qualified class name
*/
export function jobName(className: string): string {
const parts = className.split('\\')
const name = parts.pop() ?? className
return parts[0] === 'OCA' && parts[1] ? `${parts[1]} ${name}` : name
}
/**
* @param duration run time in milliseconds, null when the job reported none
*/
export function formatDuration(duration: number | null): string {
if (duration === null) {
return '–'
}
if (duration < 1000) {
// TRANSLATORS: {duration} is a number of milliseconds, e.g. "412 ms"
return t('serverinfo', '{duration} ms', { duration })
}
// TRANSLATORS: {duration} is a number of seconds with one decimal, e.g. "1.4 s"
return t('serverinfo', '{duration} s', { duration: (duration / 1000).toFixed(1) })
}
/**
* @param memoryPeak peak memory in kilobytes, base 10 as the job runner reports it
*/
export function formatKilobytes(memoryPeak: number | null): string {
return memoryPeak === null ? '–' : formatBytes(memoryPeak * 1000)
}