Fixed human-readable file size representation (#137)

(as it follows units of IEC 60027-2 A.2 )
This commit is contained in:
0x6d61726b
2024-03-03 15:48:56 +01:00
committed by GitHub
parent f763b9657f
commit 51bcd82ea7
5 changed files with 20 additions and 13 deletions

View File

@@ -17,7 +17,7 @@ import {
} from '@mui/material' } from '@mui/material'
import { useCallback } from 'react' import { useCallback } from 'react'
import { RPCResult } from '../types' import { RPCResult } from '../types'
import { ellipsis, formatSpeedMiB, mapProcessStatus, roundMiB } from '../utils' import { ellipsis, formatSpeedMiB, mapProcessStatus, formatSize } from '../utils'
type Props = { type Props = {
download: RPCResult download: RPCResult
@@ -86,7 +86,7 @@ const DownloadCard: React.FC<Props> = ({ download, onStop, onCopy }) => {
{!isCompleted() ? formatSpeedMiB(download.progress.speed) : ''} {!isCompleted() ? formatSpeedMiB(download.progress.speed) : ''}
</Typography> </Typography>
<Typography> <Typography>
{roundMiB(download.info.filesize_approx ?? 0)} {formatSize(download.info.filesize_approx ?? 0)}
</Typography> </Typography>
<Resolution resolution={download.info.resolution} /> <Resolution resolution={download.info.resolution} />
</Stack> </Stack>

View File

@@ -14,7 +14,7 @@ import {
import { useRecoilValue } from 'recoil' import { useRecoilValue } from 'recoil'
import { activeDownloadsState } from '../atoms/downloads' import { activeDownloadsState } from '../atoms/downloads'
import { useRPC } from '../hooks/useRPC' import { useRPC } from '../hooks/useRPC'
import { ellipsis, formatSpeedMiB, roundMiB } from "../utils" import { ellipsis, formatSpeedMiB, formatSize } from "../utils"
const DownloadsListView: React.FC = () => { const DownloadsListView: React.FC = () => {
@@ -74,7 +74,7 @@ const DownloadsListView: React.FC = () => {
/> />
</TableCell> </TableCell>
<TableCell>{formatSpeedMiB(download.progress.speed)}</TableCell> <TableCell>{formatSpeedMiB(download.progress.speed)}</TableCell>
<TableCell>{roundMiB(download.info.filesize_approx ?? 0)}</TableCell> <TableCell>{formatSize(download.info.filesize_approx ?? 0)}</TableCell>
<TableCell> <TableCell>
<Button <Button
variant="contained" variant="contained"

View File

@@ -1,7 +1,7 @@
import StorageIcon from '@mui/icons-material/Storage' import StorageIcon from '@mui/icons-material/Storage'
import { useRecoilValue } from 'recoil' import { useRecoilValue } from 'recoil'
import { freeSpaceBytesState } from '../atoms/status' import { freeSpaceBytesState } from '../atoms/status'
import { formatGiB } from '../utils' import { formatSize } from '../utils'
const FreeSpaceIndicator = () => { const FreeSpaceIndicator = () => {
const freeSpace = useRecoilValue(freeSpaceBytesState) const freeSpace = useRecoilValue(freeSpaceBytesState)
@@ -15,7 +15,7 @@ const FreeSpaceIndicator = () => {
}}> }}>
<StorageIcon /> <StorageIcon />
<span> <span>
{formatGiB(freeSpace)} {formatSize(freeSpace)}
</span> </span>
</div> </div>
) )

View File

@@ -42,14 +42,21 @@ export function toFormatArgs(codes: string[]): string {
return '' return ''
} }
export const formatGiB = (bytes: number) => export function formatSize(bytes: number): string {
`${(bytes / 1_000_000_000).toFixed(0)}GiB` const threshold = 1024
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
export const roundMiB = (bytes: number) => let i = 0
`${(bytes / 1_000_000).toFixed(2)} MiB` while (bytes >= threshold) {
bytes /= threshold
i = i + 1
}
return `${bytes.toFixed(i == 0 ? 0 : 2)} ${units.at(i)}`
}
export const formatSpeedMiB = (val: number) => export const formatSpeedMiB = (val: number) =>
`${roundMiB(val)}/s` `${(val / 1_048_576).toFixed(2)} MiB/s`
export const datetimeCompareFunc = (a: string, b: string) => export const datetimeCompareFunc = (a: string, b: string) =>
new Date(a).getTime() - new Date(b).getTime() new Date(a).getTime() - new Date(b).getTime()

View File

@@ -41,7 +41,7 @@ import { useToast } from '../hooks/toast'
import { useI18n } from '../hooks/useI18n' import { useI18n } from '../hooks/useI18n'
import { ffetch } from '../lib/httpClient' import { ffetch } from '../lib/httpClient'
import { DirectoryEntry } from '../types' import { DirectoryEntry } from '../types'
import { base64URLEncode, roundMiB } from '../utils' import { base64URLEncode, formatSize } from '../utils'
export default function Downloaded() { export default function Downloaded() {
const [menuPos, setMenuPos] = useState({ x: 0, y: 0 }) const [menuPos, setMenuPos] = useState({ x: 0, y: 0 })
@@ -237,7 +237,7 @@ export default function Downloaded() {
variant="caption" variant="caption"
component="span" component="span"
> >
{roundMiB(file.size)} {formatSize(file.size)}
</Typography> </Typography>
} }
{!file.isDirectory && <> {!file.isDirectory && <>