~kris/dots

srice

ref: 0e7f2c2785a36c2c4d5f7214411d3746a1667303 srice/.config/eww/scripts/get-laptop-info.sh -rwxr-xr-x 3.6 KiB
0e7f2c27 — Kris Yotam xprofile: bump laptop HiDPI to 180 and scale cursor for M1 Pro retina 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash

# CACHE FILE PATH

CACHE_FILE="/tmp/eww_laptop_info_cache.json"
FALLBACK_LOGO="${HOME}/.config/eww/assets/brand.png"
LOGO_CACHE_DIR="${HOME}/.cache/eww/logos"

# --- 1. CEK CACHE UTAMA (FAST PATH) ---
if [ -s "$CACHE_FILE" ]; then
    cat "$CACHE_FILE"
    exit 0
fi

# --- 2. JIKA CACHE KOSONG, JALANKAN DETEKSI (HEAVY PATH) ---
# Bagian ini hanya jalan 1x setelah laptop nyala.

get_brand() {
    local vendor=$(cat /sys/class/dmi/id/sys_vendor 2>/dev/null | tr '[:upper:]' '[:lower:]')
    case "$vendor" in
        *lenovo*) echo "lenovo" ;;
        *asus*) echo "asus" ;;
        *dell*) echo "dell" ;;
        *hp*|*hewlett*) echo "hp" ;;
        *acer*) echo "acer" ;;
        *msi*) echo "msi" ;;
        *apple*) echo "apple" ;;
        *samsung*) echo "samsung" ;;
        *toshiba*) echo "toshiba" ;;
        *sony*) echo "sony" ;;
        *razer*) echo "razer" ;;
        *gigabyte*) echo "gigabyte" ;;
        *framework*) echo "framework" ;;
        *) echo "unknown" ;;
    esac
}

get_logo_path() {
    local brand=$1
    local domain=""
    mkdir -p "$LOGO_CACHE_DIR"
    local logo_file="${LOGO_CACHE_DIR}/${brand}.png"

    # Cek cache logo
    if [ -f "$logo_file" ] && [ -s "$logo_file" ]; then
        echo "$logo_file"
        return
    fi

    # Mapping domain
    case "$brand" in
        lenovo) domain="lenovo.com" ;;
        asus) domain="asus.com" ;;
        dell) domain="dell.com" ;;
        hp) domain="hp.com" ;;
        acer) domain="acer.com" ;;
        msi) domain="msi.com" ;;
        apple) domain="apple.com" ;;
        samsung) domain="samsung.com" ;;
        toshiba) domain="toshiba.com" ;;
        sony) domain="sony.com" ;;
        razer) domain="razer.com" ;;
        gigabyte) domain="gigabyte.com" ;;
        framework) domain="frame.work" ;;
        *) domain="" ;;
    esac

    # Download Async (Background)
    if [ -n "$domain" ]; then
        (
            logo_url="https://t1.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https%3A%2F%2F${domain}&size=128"
            curl -s -m 10 -o "$logo_file" "$logo_url" 2>/dev/null
        ) &
    fi

    echo "$FALLBACK_LOGO"
}

get_model() {
    local model=$(cat /sys/class/dmi/id/product_name 2>/dev/null)
    if [ -z "$model" ] || [[ "$model" == *"System Product"* ]]; then
        model=$(cat /sys/class/dmi/id/board_name 2>/dev/null)
    fi
    echo "${model:-Unknown}"
}

get_cpu() {
    lscpu | grep "Model name" | cut -d':' -f2 | sed -e 's/(R)//g' -e 's/(TM)//g' -e 's/CPU //g' -e 's/Core //g' | xargs
}

get_gpu() {
    lspci | grep -i 'vga\|3d\|display' | cut -d':' -f3 | head -n1 | sed -e 's/Corporation //g' -e 's/\[.*\]//g' | xargs
}

get_ram() {
    free -h | awk '/^Mem:/ {print $2}'
}

get_disk() {
    local disk=$(df -h / | awk 'NR==2 {print $2}')
    local disk_type="SSD"
    if [ -f "/sys/block/sda/queue/rotational" ]; then
        [ "$(cat /sys/block/sda/queue/rotational)" -eq 1 ] && disk_type="HDD"
    fi
    echo "${disk} ${disk_type}"
}

# --- GENERATE DATA ---
BRAND=$(get_brand)
LOGO_PATH=$(get_logo_path "$BRAND")
MODEL=$(get_model)
CPU=$(get_cpu)
GPU=$(get_gpu)
RAM=$(get_ram)
DISK=$(get_disk)

# --- SAVE TO JSON CACHE ---
# Kita simpan output json ke variabel dulu
JSON_OUTPUT=$(jq -n \
  --arg brand "$BRAND" \
  --arg logo_path "$LOGO_PATH" \
  --arg model "$MODEL" \
  --arg cpu "$CPU" \
  --arg gpu "$GPU" \
  --arg ram "$RAM" \
  --arg disk "$DISK" \
  '{
    brand: $brand,
    logo_path: $logo_path,
    model: $model,
    cpu: $cpu,
    gpu: $gpu,
    ram: $ram,
    disk: $disk
  }')

# Tulis ke file cache
echo "$JSON_OUTPUT" > "$CACHE_FILE"

# Print ke stdout untuk Eww
echo "$JSON_OUTPUT"