1
1
<script setup lang="ts">
2
2
import type { DiskStat , LoadStat , MemStat } from ' @/api/analytic'
3
3
import analytic from ' @/api/analytic'
4
- import upgrade from ' @/api/upgrade'
5
4
import { formatDateTime } from ' @/lib/helper'
6
5
7
6
interface StatusData {
@@ -38,30 +37,27 @@ function formatUptime(uptime: number) {
38
37
// Format memory usage as "used percentage"
39
38
function formatMemoryUsage(memory : MemStat | null ) {
40
39
if (! memory )
41
- return ' 0B0 %'
40
+ return ' 0B 0 %'
42
41
43
- // Use the pressure value as percentage (since you said we can get it directly)
44
- const percentage = memory .pressure . toFixed (1 )
42
+ // Use percentage provided by the backend; keep 1 decimal place
43
+ const percentage = typeof memory .pressure === ' number ' ? memory . pressure . toFixed (1 ) : ' 0.0 '
45
44
46
- // Remove space from used size and combine without space
47
- const usedSize = memory .used . replace ( ' ' , ' ' )
45
+ // Preserve space in formatted size and add a space before percentage
46
+ const usedSize = memory .used
48
47
49
- return ` ${usedSize }${percentage }% `
48
+ return ` ${usedSize } ${percentage }% `
50
49
}
51
50
52
51
// Format disk usage as "used percentage"
53
52
function formatDiskUsage(disk : DiskStat | null ) {
54
53
if (! disk )
55
- return ' 0B0 %'
54
+ return ' 0B 0 %'
56
55
57
- // Value is already formatted string like "39 GiB"
58
- // Use the pre-calculated percentage from the API
59
- const percentage = disk .percentage . toFixed ( 1 )
56
+ // Already a formatted string like "39 GiB"; percentage comes from API
57
+ const percentage = typeof disk . percentage === ' number ' ? disk . percentage . toFixed ( 1 ) : ' 0.0 '
58
+ const usedSize = disk .used
60
59
61
- // Remove space from used size and combine without space
62
- const usedSize = disk .used .replace (' ' , ' ' )
63
-
64
- return ` ${usedSize }${percentage }% `
60
+ return ` ${usedSize } ${percentage }% `
65
61
}
66
62
67
63
// Format CPU frequency
@@ -75,50 +71,46 @@ function formatCpuFreq(freq: number) {
75
71
return ` ${freq .toFixed (0 )}MHz `
76
72
}
77
73
74
+ // Format CPU text as "<count>x<freq>" with graceful fallbacks
75
+ function formatCpu(cpuCount : number , freq : number ) {
76
+ const hasCount = typeof cpuCount === ' number' && cpuCount > 0
77
+ const hasFreq = typeof freq === ' number' && freq > 0
78
+
79
+ if (hasCount && hasFreq )
80
+ return ` ${cpuCount }x${formatCpuFreq (freq )} `
81
+ if (hasCount && ! hasFreq )
82
+ return ` ${cpuCount }x `
83
+ if (! hasCount && hasFreq )
84
+ return ` ${formatCpuFreq (freq )} `
85
+ return ' N/A'
86
+ }
87
+
78
88
// Update current timestamp
79
89
function updateTimestamp() {
80
90
statusData .value .timestamp = formatDateTime (new Date ().toISOString ())
81
91
}
82
92
83
- // Initialize data from analytic init API
93
+ // Initialize data from analytic init API (no fallbacks)
84
94
async function initializeData() {
85
95
try {
86
96
const analyticData = await analytic .init ()
87
97
88
- // Set system info with fallbacks
89
98
statusData .value .uptime = analyticData ?.host ?.uptime || 0
90
99
statusData .value .loadAvg = analyticData ?.loadavg || null
91
100
statusData .value .memory = analyticData ?.memory || null
92
101
statusData .value .disk = analyticData ?.disk || null
93
102
94
- // Set CPU info with fallbacks
103
+ // System version is taken directly from host.platformVersion
104
+ statusData .value .version = analyticData ?.host ?.platformVersion || ' '
105
+
95
106
const cpuInfo = analyticData ?.cpu ?.info || []
96
107
statusData .value .cpuCount = cpuInfo .length || 0
97
-
98
- // Get CPU frequency from first CPU info with fallback
99
- if (cpuInfo .length > 0 && cpuInfo [0 ].mhz ) {
100
- statusData .value .cpuFreq = cpuInfo [0 ].mhz
101
- }
102
- else {
103
- statusData .value .cpuFreq = 0
104
- }
105
-
106
- // Try to get version from upgrade API, fallback to host platform version
107
- try {
108
- const versionData = await upgrade .current_version ()
109
- statusData .value .version = versionData ?.cur_version ?.version || analyticData ?.host ?.platformVersion || ' unknown'
110
- }
111
- catch (versionError ) {
112
- console .warn (' Failed to get app version, using platform version:' , versionError )
113
- statusData .value .version = analyticData ?.host ?.platformVersion || ' unknown'
114
- }
108
+ statusData .value .cpuFreq = (cpuInfo .length > 0 && cpuInfo [0 ].mhz ) ? cpuInfo [0 ].mhz : 0
115
109
116
110
updateTimestamp ()
117
111
}
118
112
catch (error ) {
119
113
console .error (' Failed to initialize terminal status bar:' , error )
120
- // Set default values on error
121
- statusData .value .version = ' error'
122
114
updateTimestamp ()
123
115
}
124
116
}
@@ -200,7 +192,7 @@ onMounted(() => {
200
192
201
193
<div class =" status-item cpu" >
202
194
<span class =" icon i-tabler-cpu" />
203
- <span class =" value" >{{ statusData.cpuCount || 0 }}x{{ formatCpuFreq( statusData.cpuFreq || 0) }}</span >
195
+ <span class =" value" >{{ formatCpu( statusData.cpuCount || 0, statusData.cpuFreq || 0) }}</span >
204
196
</div >
205
197
206
198
<div class =" status-item memory" >
0 commit comments