mirror of
https://github.com/tw93/Mole.git
synced 2026-02-16 03:46:11 +00:00
fix: precision issue in battery metrics and adaptive purge UI
- cmd/status: use ParseUint to handle negative battery power values correctly - bin/purge: make path truncation adaptive to terminal width
This commit is contained in:
@@ -98,7 +98,14 @@ perform_purge() {
|
|||||||
# Function to truncate path in the middle
|
# Function to truncate path in the middle
|
||||||
truncate_path() {
|
truncate_path() {
|
||||||
local path="$1"
|
local path="$1"
|
||||||
local max_len=80
|
local term_cols
|
||||||
|
term_cols=$(tput cols 2>/dev/null || echo 80)
|
||||||
|
# Reserve some space for the spinner and text (approx 20 chars)
|
||||||
|
local max_len=$((term_cols - 20))
|
||||||
|
# Ensure a reasonable minimum width
|
||||||
|
if (( max_len < 40 )); then
|
||||||
|
max_len=40
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ ${#path} -le $max_len ]]; then
|
if [[ ${#path} -le $max_len ]]; then
|
||||||
echo "$path"
|
echo "$path"
|
||||||
|
|||||||
@@ -241,12 +241,14 @@ func collectThermal() ThermalStatus {
|
|||||||
valStr, _, _ = strings.Cut(valStr, ",")
|
valStr, _, _ = strings.Cut(valStr, ",")
|
||||||
valStr, _, _ = strings.Cut(valStr, "}")
|
valStr, _, _ = strings.Cut(valStr, "}")
|
||||||
valStr = strings.TrimSpace(valStr)
|
valStr = strings.TrimSpace(valStr)
|
||||||
if powerMW, err := strconv.ParseFloat(valStr, 64); err == nil {
|
|
||||||
// macOS ioreg may return negative values as uint64 (two's complement overflow)
|
// Use ParseUint to handle potential 64-bit two's complement negative values correctly.
|
||||||
// If value > 2^63, convert from uint64 representation to proper signed value
|
// ioreg might return a large generic integer for negative values.
|
||||||
if powerMW > float64(1<<63) {
|
if valUint, err := strconv.ParseUint(valStr, 10, 64); err == nil {
|
||||||
powerMW = powerMW - 18446744073709551616.0 // 2^64
|
// Cast to int64 to interpret 2's complement correctly (e.g. MaxUint64 -> -1)
|
||||||
}
|
valInt := int64(valUint)
|
||||||
|
powerMW := float64(valInt)
|
||||||
|
|
||||||
// Validate reasonable battery power range: -200W to 200W
|
// Validate reasonable battery power range: -200W to 200W
|
||||||
if powerMW > -200000 && powerMW < 200000 {
|
if powerMW > -200000 && powerMW < 200000 {
|
||||||
thermal.BatteryPower = powerMW / 1000.0
|
thermal.BatteryPower = powerMW / 1000.0
|
||||||
|
|||||||
Reference in New Issue
Block a user