opt.: disk size (#252)

This commit is contained in:
lollipopkit
2024-01-21 18:01:11 +08:00
parent 362dcdf288
commit 50d6ed919b
11 changed files with 42 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
extension NumX on num {
String get convertBytes {
String get bytes2Str {
const suffix = ['B', 'KB', 'MB', 'GB', 'TB'];
double value = toDouble();
int squareTimes = 0;
@@ -12,10 +12,12 @@ extension NumX on num {
}
return '$finalValue ${suffix[squareTimes]}';
}
String get kb2Str => (this * 1024).bytes2Str;
}
extension BigIntX on BigInt {
String get convertBytes {
String get bytes2Str {
const suffix = ['B', 'KB', 'MB', 'GB', 'TB'];
double value = toDouble();
int squareTimes = 0;
@@ -28,4 +30,6 @@ extension BigIntX on BigInt {
}
return '$finalValue ${suffix[squareTimes]}';
}
String get kb2Str => (this * BigInt.from(1024)).bytes2Str;
}

View File

@@ -214,7 +214,7 @@ const _statusCmds = [
'cat /proc/stat | grep cpu',
'uptime',
'cat /proc/net/snmp',
'df -h',
'df -B 1K',
"cat /proc/meminfo | grep -E 'Mem|Swap'",
'cat /sys/class/thermal/thermal_zone*/type',
'cat /sys/class/thermal/thermal_zone*/temp',
@@ -246,7 +246,7 @@ const _bsdStatusCmd = [
'uname -or',
'top -l 1 | grep "CPU usage"',
'uptime',
'df -h',
'df -k',
'top -l 1 | grep PhysMem',
//'sysctl -a | grep temperature',
'hostname',

View File

@@ -34,7 +34,7 @@ final class PodmanImg implements ContainerImg {
});
@override
String? get sizeMB => size?.convertBytes;
String? get sizeMB => size?.bytes2Str;
@override
int? get containersCount => containers;

View File

@@ -7,9 +7,9 @@ class Disk {
final String dev;
final String mount;
final int usedPercent;
final String used;
final String size;
final String avail;
final BigInt used;
final BigInt size;
final BigInt avail;
const Disk({
required this.dev,
@@ -20,6 +20,12 @@ class Disk {
required this.avail,
});
/// raw:
/// ```
/// Filesystem 1K-blocks Used Available Use% Mounted on
/// overlay 959122528 154470540 755857572 17% /
/// tmpfs 65536 0 65536 0% /dev
/// ```
static List<Disk> parse(String raw) {
final list = <Disk>[];
final items = raw.split('\n');
@@ -43,9 +49,9 @@ class Disk {
dev: vals[0],
mount: vals[5],
usedPercent: int.parse(vals[4].replaceFirst('%', '')),
used: vals[2],
size: vals[1],
avail: vals[3],
used: BigInt.tryParse(vals[2]) ?? BigInt.zero,
size: BigInt.tryParse(vals[1]) ?? BigInt.one,
avail: BigInt.tryParse(vals[3]) ?? BigInt.one,
));
} catch (e) {
continue;
@@ -79,8 +85,8 @@ class DiskIO extends TimeSeq<DiskIOPiece> {
(String?, String?) getSpeed(String dev) {
final (read_, write_) = _getSpeed(dev);
if (read_ == null || write_ == null) return (null, null);
final read = '${read_.convertBytes}/s';
final write = '${write_.convertBytes}/s';
final read = '${read_.bytes2Str}/s';
final write = '${write_.bytes2Str}/s';
return (read, write);
}
@@ -92,8 +98,8 @@ class DiskIO extends TimeSeq<DiskIOPiece> {
read += read_ ?? 0;
write += write_ ?? 0;
}
final readStr = '${read.convertBytes}/s';
final writeStr = '${write.convertBytes}/s';
final readStr = '${read.bytes2Str}/s';
final writeStr = '${write.bytes2Str}/s';
return (readStr, writeStr);
}

View File

@@ -47,10 +47,10 @@ class NetSpeed extends TimeSeq<NetSpeedPart> {
for (var i = 0; i < now.length; i++) {
size += sizeInBytes(i);
}
return size.convertBytes;
return size.bytes2Str;
}
final idx = deviceIdx(device);
return sizeInBytes(idx).convertBytes;
return sizeInBytes(idx).bytes2Str;
}
String speedOut({String? device, bool all = false}) {
@@ -73,10 +73,10 @@ class NetSpeed extends TimeSeq<NetSpeedPart> {
for (var i = 0; i < now.length; i++) {
size += sizeOutBytes(i);
}
return size.convertBytes;
return size.bytes2Str;
}
final idx = deviceIdx(device);
return sizeOutBytes(idx).convertBytes;
return sizeOutBytes(idx).bytes2Str;
}
int deviceIdx(String? device) {
@@ -90,7 +90,7 @@ class NetSpeed extends TimeSeq<NetSpeedPart> {
return 0;
}
String buildStandardOutput(double speed) => '${speed.convertBytes}/s';
String buildStandardOutput(double speed) => '${speed.bytes2Str}/s';
/// [raw] example:
/// Inter-| Receive | Transmit

View File

@@ -41,13 +41,13 @@ abstract final class InitStatus {
avail: 1,
),
disk: [
const Disk(
Disk(
dev: '/',
mount: '/',
usedPercent: 0,
used: '0',
size: '0',
avail: '0',
used: BigInt.zero,
size: BigInt.one,
avail: BigInt.zero,
)
],
tcp: const Conn(maxConn: 0, active: 0, passive: 0, fail: 0),

View File

@@ -193,8 +193,8 @@ class _PrivateKeyEditPageState extends State<PrivateKeyEditPage> {
context.showSnackBar(
l10n.fileTooLarge(
path,
size.convertBytes,
Miscs.privateKeyMaxSize.convertBytes,
size.bytes2Str,
Miscs.privateKeyMaxSize.bytes2Str,
),
);
return;

View File

@@ -256,7 +256,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
),
UIs.width7,
Text(
'of ${(ss.mem.total * 1024).convertBytes}',
'of ${(ss.mem.total * 1024).bytes2Str}',
style: UIs.text13Grey,
)
],
@@ -297,7 +297,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
Text('${used.toStringAsFixed(0)}%', style: UIs.text27),
UIs.width7,
Text(
'of ${(ss.swap.total * 1024).convertBytes} ',
'of ${(ss.swap.total * 1024).bytes2Str} ',
style: UIs.text13Grey,
)
],
@@ -452,7 +452,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
Widget _buildDiskItem(Disk disk, ServerStatus ss) {
final (read, write) = ss.diskIO.getSpeed(disk.dev);
final text = () {
final use = '${disk.used} / ${disk.size}';
final use = '${disk.used.kb2Str} / ${disk.size.kb2Str}';
if (read == null || write == null) return use;
return '$use\n${l10n.read} $read | ${l10n.write} $write';
}();

View File

@@ -182,7 +182,7 @@ class _LocalStoragePageState extends State<LocalStoragePage> {
title: Text(fileName),
subtitle: isDir
? null
: Text(stat.size.convertBytes, style: UIs.textGrey),
: Text(stat.size.bytes2Str, style: UIs.textGrey),
trailing: Text(
stat.modified
.toString()

View File

@@ -304,7 +304,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
subtitle: isDir
? null
: Text(
(file.attr.size ?? 0).convertBytes,
(file.attr.size ?? 0).bytes2Str,
style: UIs.textGrey,
),
onTap: () {

View File

@@ -94,7 +94,7 @@ class _SftpMissionPageState extends State<SftpMissionPage> {
);
case SftpWorkerStatus.loading:
final percentStr = (status.progress ?? 0.0).toStringAsFixed(2);
final size = (status.size ?? 0).convertBytes;
final size = (status.size ?? 0).bytes2Str;
return _wrapInCard(
status: status,
subtitle: l10n.percentOfSize(percentStr, size),