mirror of
https://github.com/tw93/Mole.git
synced 2026-02-11 20:34:02 +00:00
add cpu temp display and optimize user check
This commit is contained in:
38
bin/purge.sh
38
bin/purge.sh
@@ -63,12 +63,12 @@ start_purge() {
|
|||||||
perform_purge() {
|
perform_purge() {
|
||||||
local stats_dir="${XDG_CACHE_HOME:-$HOME/.cache}/mole"
|
local stats_dir="${XDG_CACHE_HOME:-$HOME/.cache}/mole"
|
||||||
local monitor_pid=""
|
local monitor_pid=""
|
||||||
|
|
||||||
# Cleanup function
|
# Cleanup function
|
||||||
cleanup_monitor() {
|
cleanup_monitor() {
|
||||||
# Remove scanning file to stop monitor
|
# Remove scanning file to stop monitor
|
||||||
rm -f "$stats_dir/purge_scanning" 2> /dev/null || true
|
rm -f "$stats_dir/purge_scanning" 2> /dev/null || true
|
||||||
|
|
||||||
if [[ -n "$monitor_pid" ]]; then
|
if [[ -n "$monitor_pid" ]]; then
|
||||||
kill "$monitor_pid" 2> /dev/null || true
|
kill "$monitor_pid" 2> /dev/null || true
|
||||||
wait "$monitor_pid" 2> /dev/null || true
|
wait "$monitor_pid" 2> /dev/null || true
|
||||||
@@ -77,45 +77,45 @@ perform_purge() {
|
|||||||
printf '\r\033[K\n\033[K\033[A'
|
printf '\r\033[K\n\033[K\033[A'
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set up trap for cleanup
|
# Set up trap for cleanup
|
||||||
trap cleanup_monitor INT TERM
|
trap cleanup_monitor INT TERM
|
||||||
|
|
||||||
# Show scanning with spinner on same line as title
|
# Show scanning with spinner on same line as title
|
||||||
if [[ -t 1 ]]; then
|
if [[ -t 1 ]]; then
|
||||||
# Print title first
|
# Print title first
|
||||||
printf '%s' "${PURPLE_BOLD}Purge Project Artifacts${NC} "
|
printf '%s' "${PURPLE_BOLD}Purge Project Artifacts${NC} "
|
||||||
|
|
||||||
# Start background monitor with ASCII spinner
|
# Start background monitor with ASCII spinner
|
||||||
(
|
(
|
||||||
local spinner_chars="|/-\\"
|
local spinner_chars="|/-\\"
|
||||||
local spinner_idx=0
|
local spinner_idx=0
|
||||||
local last_path=""
|
local last_path=""
|
||||||
|
|
||||||
# Set up trap to exit cleanly
|
# Set up trap to exit cleanly
|
||||||
trap 'exit 0' INT TERM
|
trap 'exit 0' INT TERM
|
||||||
|
|
||||||
# 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 max_len=80
|
||||||
|
|
||||||
if [[ ${#path} -le $max_len ]]; then
|
if [[ ${#path} -le $max_len ]]; then
|
||||||
echo "$path"
|
echo "$path"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Calculate how much to show on each side
|
# Calculate how much to show on each side
|
||||||
local side_len=$(( (max_len - 3) / 2 ))
|
local side_len=$(((max_len - 3) / 2))
|
||||||
local start="${path:0:$side_len}"
|
local start="${path:0:$side_len}"
|
||||||
local end="${path: -$side_len}"
|
local end="${path: -$side_len}"
|
||||||
echo "${start}...${end}"
|
echo "${start}...${end}"
|
||||||
}
|
}
|
||||||
|
|
||||||
while [[ -f "$stats_dir/purge_scanning" ]]; do
|
while [[ -f "$stats_dir/purge_scanning" ]]; do
|
||||||
local current_path=$(cat "$stats_dir/purge_scanning" 2> /dev/null || echo "")
|
local current_path=$(cat "$stats_dir/purge_scanning" 2> /dev/null || echo "")
|
||||||
local display_path=""
|
local display_path=""
|
||||||
|
|
||||||
if [[ -n "$current_path" ]]; then
|
if [[ -n "$current_path" ]]; then
|
||||||
display_path="${current_path/#$HOME/~}"
|
display_path="${current_path/#$HOME/~}"
|
||||||
display_path=$(truncate_path "$display_path")
|
display_path=$(truncate_path "$display_path")
|
||||||
@@ -123,11 +123,11 @@ perform_purge() {
|
|||||||
elif [[ -n "$last_path" ]]; then
|
elif [[ -n "$last_path" ]]; then
|
||||||
display_path="$last_path"
|
display_path="$last_path"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get current spinner character
|
# Get current spinner character
|
||||||
local spin_char="${spinner_chars:$spinner_idx:1}"
|
local spin_char="${spinner_chars:$spinner_idx:1}"
|
||||||
spinner_idx=$(( (spinner_idx + 1) % ${#spinner_chars} ))
|
spinner_idx=$(((spinner_idx + 1) % ${#spinner_chars}))
|
||||||
|
|
||||||
# Show title on first line, spinner and scanning info on second line
|
# Show title on first line, spinner and scanning info on second line
|
||||||
if [[ -n "$display_path" ]]; then
|
if [[ -n "$display_path" ]]; then
|
||||||
printf '\r%s\n%s %sScanning %s\033[K\033[A' \
|
printf '\r%s\n%s %sScanning %s\033[K\033[A' \
|
||||||
@@ -140,7 +140,7 @@ perform_purge() {
|
|||||||
"${BLUE}${spin_char}${NC}" \
|
"${BLUE}${spin_char}${NC}" \
|
||||||
"${GRAY}"
|
"${GRAY}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep 0.05
|
sleep 0.05
|
||||||
done
|
done
|
||||||
exit 0
|
exit 0
|
||||||
@@ -149,14 +149,14 @@ perform_purge() {
|
|||||||
else
|
else
|
||||||
echo -e "${PURPLE_BOLD}Purge Project Artifacts${NC}"
|
echo -e "${PURPLE_BOLD}Purge Project Artifacts${NC}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
clean_project_artifacts
|
clean_project_artifacts
|
||||||
local exit_code=$?
|
local exit_code=$?
|
||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
trap - INT TERM
|
trap - INT TERM
|
||||||
cleanup_monitor
|
cleanup_monitor
|
||||||
|
|
||||||
if [[ -t 1 ]]; then
|
if [[ -t 1 ]]; then
|
||||||
echo -e "${PURPLE_BOLD}Purge Project Artifacts${NC}"
|
echo -e "${PURPLE_BOLD}Purge Project Artifacts${NC}"
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ func getScoreStyle(score int) lipgloss.Style {
|
|||||||
|
|
||||||
func buildCards(m MetricsSnapshot, _ int) []cardData {
|
func buildCards(m MetricsSnapshot, _ int) []cardData {
|
||||||
cards := []cardData{
|
cards := []cardData{
|
||||||
renderCPUCard(m.CPU),
|
renderCPUCard(m.CPU, m.Thermal),
|
||||||
renderMemoryCard(m.Memory),
|
renderMemoryCard(m.Memory),
|
||||||
renderDiskCard(m.Disks, m.DiskIO),
|
renderDiskCard(m.Disks, m.DiskIO),
|
||||||
renderBatteryCard(m.Batteries, m.Thermal),
|
renderBatteryCard(m.Batteries, m.Thermal),
|
||||||
@@ -225,9 +225,18 @@ func hasSensorData(sensors []SensorReading) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderCPUCard(cpu CPUStatus) cardData {
|
func renderCPUCard(cpu CPUStatus, thermal ThermalStatus) cardData {
|
||||||
var lines []string
|
var lines []string
|
||||||
lines = append(lines, fmt.Sprintf("Total %s %5.1f%%", progressBar(cpu.Usage), cpu.Usage))
|
|
||||||
|
// Line 1: Usage + Temp (Format: 15% @ 30.4°C)
|
||||||
|
usageBar := progressBar(cpu.Usage)
|
||||||
|
|
||||||
|
headerText := fmt.Sprintf("%5.1f%%", cpu.Usage)
|
||||||
|
if thermal.CPUTemp > 0 {
|
||||||
|
headerText += fmt.Sprintf(" @ %s°C", colorizeTemp(thermal.CPUTemp))
|
||||||
|
}
|
||||||
|
|
||||||
|
lines = append(lines, fmt.Sprintf("Total %s %s", usageBar, headerText))
|
||||||
|
|
||||||
if cpu.PerCoreEstimated {
|
if cpu.PerCoreEstimated {
|
||||||
lines = append(lines, subtleStyle.Render("Per-core data unavailable (using averaged load)"))
|
lines = append(lines, subtleStyle.Render("Per-core data unavailable (using averaged load)"))
|
||||||
@@ -527,12 +536,7 @@ func renderBatteryCard(batts []BatteryStatus, thermal ThermalStatus) cardData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if thermal.CPUTemp > 0 {
|
if thermal.CPUTemp > 0 {
|
||||||
tempText := fmt.Sprintf("%.0f°C", thermal.CPUTemp)
|
tempText := colorizeTemp(thermal.CPUTemp) + "°C" // Reuse common color logic
|
||||||
if thermal.CPUTemp > 80 {
|
|
||||||
tempText = dangerStyle.Render(tempText)
|
|
||||||
} else if thermal.CPUTemp > 60 {
|
|
||||||
tempText = warnStyle.Render(tempText)
|
|
||||||
}
|
|
||||||
healthParts = append(healthParts, tempText)
|
healthParts = append(healthParts, tempText)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -641,12 +645,12 @@ func colorizeBattery(percent float64, s string) string {
|
|||||||
|
|
||||||
func colorizeTemp(t float64) string {
|
func colorizeTemp(t float64) string {
|
||||||
switch {
|
switch {
|
||||||
case t >= 85:
|
case t >= 76:
|
||||||
return dangerStyle.Render(fmt.Sprintf("%.1f", t))
|
return dangerStyle.Render(fmt.Sprintf("%.1f", t))
|
||||||
case t >= 70:
|
case t >= 56:
|
||||||
return warnStyle.Render(fmt.Sprintf("%.1f", t))
|
return warnStyle.Render(fmt.Sprintf("%.1f", t))
|
||||||
default:
|
default:
|
||||||
return subtleStyle.Render(fmt.Sprintf("%.1f", t))
|
return okStyle.Render(fmt.Sprintf("%.1f", t))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -375,7 +375,6 @@ scan_purge_targets() {
|
|||||||
while IFS= read -r item; do
|
while IFS= read -r item; do
|
||||||
# Check if we should abort (scanning file removed by Ctrl+C)
|
# Check if we should abort (scanning file removed by Ctrl+C)
|
||||||
if [[ ! -f "$stats_dir/purge_scanning" ]]; then
|
if [[ ! -f "$stats_dir/purge_scanning" ]]; then
|
||||||
rm -f "$output_file.raw"
|
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -413,7 +412,6 @@ scan_purge_targets() {
|
|||||||
while IFS= read -r item; do
|
while IFS= read -r item; do
|
||||||
# Check if we should abort (scanning file removed by Ctrl+C)
|
# Check if we should abort (scanning file removed by Ctrl+C)
|
||||||
if [[ ! -f "$stats_dir/purge_scanning" ]]; then
|
if [[ ! -f "$stats_dir/purge_scanning" ]]; then
|
||||||
rm -f "$output_file.raw"
|
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -260,11 +260,20 @@ get_user_home() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get_invoking_user() {
|
get_invoking_user() {
|
||||||
if [[ -n "${SUDO_USER:-}" && "${SUDO_USER:-}" != "root" ]]; then
|
if [[ -n "${_MOLE_INVOKING_USER_CACHE:-}" ]]; then
|
||||||
echo "$SUDO_USER"
|
echo "$_MOLE_INVOKING_USER_CACHE"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
echo "${USER:-}"
|
|
||||||
|
local user
|
||||||
|
if [[ -n "${SUDO_USER:-}" && "${SUDO_USER:-}" != "root" ]]; then
|
||||||
|
user="$SUDO_USER"
|
||||||
|
else
|
||||||
|
user="${USER:-}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
export _MOLE_INVOKING_USER_CACHE="$user"
|
||||||
|
echo "$user"
|
||||||
}
|
}
|
||||||
|
|
||||||
get_invoking_uid() {
|
get_invoking_uid() {
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ setup() {
|
|||||||
|
|
||||||
elapsed=$(( (end - start) / 1000000 ))
|
elapsed=$(( (end - start) / 1000000 ))
|
||||||
|
|
||||||
[ "$elapsed" -lt 200 ]
|
[ "$elapsed" -lt 500 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "get_darwin_major caches correctly" {
|
@test "get_darwin_major caches correctly" {
|
||||||
|
|||||||
Reference in New Issue
Block a user