peekfetch 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +175 -2
- package/THIRD_PARTY_NOTICES.md +29 -0
- package/bin/peekfetch.js +14 -0
- package/package.json +44 -4
- package/src/ansi.js +89 -0
- package/src/cache.js +52 -0
- package/src/cli.js +91 -0
- package/src/config.js +65 -0
- package/src/context.js +113 -0
- package/src/exec.js +93 -0
- package/src/format.js +95 -0
- package/src/index.js +137 -0
- package/src/logos/index.js +44 -0
- package/src/logos/logos.json +1 -0
- package/src/modules/desktop.js +152 -0
- package/src/modules/hardware.js +156 -0
- package/src/modules/index.js +41 -0
- package/src/modules/packages.js +96 -0
- package/src/modules/system.js +131 -0
- package/src/modules/terminal.js +206 -0
- package/src/parsers.js +294 -0
- package/src/render.js +113 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { run, readText, which } from '../exec.js';
|
|
4
|
+
import { parseIni, parseXrandr } from '../parsers.js';
|
|
5
|
+
|
|
6
|
+
const isWayland = (env) => Boolean(env.WAYLAND_DISPLAY) || env.XDG_SESSION_TYPE === 'wayland';
|
|
7
|
+
|
|
8
|
+
// Window managers / compositors recognised by process name: name -> display name.
|
|
9
|
+
const LINUX_WMS = {
|
|
10
|
+
sway: 'Sway', hyprland: 'Hyprland', Hyprland: 'Hyprland', river: 'river', wayfire: 'Wayfire', niri: 'niri', labwc: 'labwc',
|
|
11
|
+
'kwin_wayland': 'KWin', 'kwin_x11': 'KWin', kwin: 'KWin', 'gnome-shell': 'Mutter', mutter: 'Mutter', weston: 'Weston',
|
|
12
|
+
'cosmic-comp': 'cosmic-comp', i3: 'i3', bspwm: 'bspwm', openbox: 'Openbox', xfwm4: 'Xfwm4', awesome: 'awesome', dwm: 'dwm',
|
|
13
|
+
herbstluftwm: 'herbstluftwm', qtile: 'Qtile', xmonad: 'xmonad', fluxbox: 'Fluxbox', icewm: 'IceWM', marco: 'Marco',
|
|
14
|
+
muffin: 'Muffin', 'cinnamon': 'Muffin', budgie_wm: 'Budgie WM', 'budgie-wm': 'Budgie WM', spectrwm: 'spectrwm', leftwm: 'LeftWM',
|
|
15
|
+
dwl: 'dwl', 'gala': 'Gala', enlightenment: 'Enlightenment', compiz: 'Compiz', 'wmaker': 'Window Maker',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const MAC_WMS = { yabai: 'yabai', AeroSpace: 'AeroSpace', Amethyst: 'Amethyst', Rectangle: 'Rectangle', chunkwm: 'chunkwm', Spectacle: 'Spectacle', Magnet: 'Magnet', Moom: 'Moom' };
|
|
19
|
+
|
|
20
|
+
// Desktop names as they appear in XDG_CURRENT_DESKTOP -> display name. WM-only sessions map to null.
|
|
21
|
+
const DES = {
|
|
22
|
+
gnome: 'GNOME', kde: 'Plasma', plasma: 'Plasma', 'x-cinnamon': 'Cinnamon', cinnamon: 'Cinnamon', xfce: 'Xfce', mate: 'MATE',
|
|
23
|
+
lxqt: 'LXQt', lxde: 'LXDE', budgie: 'Budgie', unity: 'Unity', pantheon: 'Pantheon', deepin: 'Deepin', cosmic: 'COSMIC',
|
|
24
|
+
enlightenment: 'Enlightenment', trinity: 'Trinity', ukui: 'UKUI',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const DE_VERSION = {
|
|
28
|
+
Plasma: ['plasmashell', ['--version']],
|
|
29
|
+
GNOME: ['gnome-shell', ['--version']],
|
|
30
|
+
Xfce: ['xfce4-session', ['--version']],
|
|
31
|
+
Cinnamon: ['cinnamon', ['--version']],
|
|
32
|
+
MATE: ['mate-session', ['--version']],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
async function detectDe(ctx) {
|
|
36
|
+
if (ctx.platform === 'darwin') return 'Aqua';
|
|
37
|
+
if (ctx.platform === 'win32') return 'Fluent';
|
|
38
|
+
const raw = ctx.env.XDG_CURRENT_DESKTOP ?? ctx.env.DESKTOP_SESSION ?? (ctx.env.KDE_FULL_SESSION ? 'KDE' : null);
|
|
39
|
+
if (!raw) return null;
|
|
40
|
+
const parts = raw.split(':').map((p) => p.trim().toLowerCase());
|
|
41
|
+
const known = parts.map((p) => DES[p] ?? DES[p.replace(/^ubuntu-?/, '')]).find(Boolean);
|
|
42
|
+
if (!known) return Object.values(LINUX_WMS).some((w) => w.toLowerCase() === parts[0]) ? null : raw.split(':')[0];
|
|
43
|
+
const probe = DE_VERSION[known];
|
|
44
|
+
if (probe && (await which(probe[0]))) {
|
|
45
|
+
const version = /(\d+(?:\.\d+)+)/.exec((await run(probe[0], probe[1], { timeout: 1000 })) ?? '')?.[1];
|
|
46
|
+
if (version) return `${known} ${version}`;
|
|
47
|
+
}
|
|
48
|
+
return known;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function detectWm(ctx) {
|
|
52
|
+
const procs = await ctx.processes();
|
|
53
|
+
const names = new Set([...(procs?.values() ?? [])].map((p) => p.name));
|
|
54
|
+
if (ctx.platform === 'darwin') {
|
|
55
|
+
const tiling = Object.keys(MAC_WMS).find((n) => names.has(n));
|
|
56
|
+
return tiling ? MAC_WMS[tiling] : 'Quartz Compositor';
|
|
57
|
+
}
|
|
58
|
+
if (ctx.platform === 'win32') return names.has('komorebi') ? 'komorebi' : 'Desktop Window Manager';
|
|
59
|
+
|
|
60
|
+
if (!isWayland(ctx.env) && ctx.env.DISPLAY && (await which('xprop'))) {
|
|
61
|
+
// EWMH: the root window points at a child window that carries the WM's name.
|
|
62
|
+
const root = await run('xprop', ['-root', '-notype', '_NET_SUPPORTING_WM_CHECK']);
|
|
63
|
+
const id = /\s(0x[0-9a-f]+)/i.exec(root ?? '')?.[1];
|
|
64
|
+
if (id) {
|
|
65
|
+
const out = await run('xprop', ['-id', id, '-notype', '-len', '100', '-f', '_NET_WM_NAME', '8t']);
|
|
66
|
+
const name = /_NET_WM_NAME = "([^"]+)"/.exec(out ?? '')?.[1];
|
|
67
|
+
if (name) return name;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const hit = Object.keys(LINUX_WMS).find((n) => names.has(n));
|
|
71
|
+
if (hit) return LINUX_WMS[hit];
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const MAC_ACCENTS = { '-1': 'Graphite', 0: 'Red', 1: 'Orange', 2: 'Yellow', 3: 'Green', 4: 'Blue', 5: 'Purple', 6: 'Pink' };
|
|
76
|
+
|
|
77
|
+
async function detectTheme(ctx) {
|
|
78
|
+
if (ctx.platform === 'darwin') {
|
|
79
|
+
const read = (key) => run('defaults', ['read', '-g', key], { timeout: 1000 }).then((o) => o?.trim() ?? null);
|
|
80
|
+
const [style, accent, auto] = await Promise.all([read('AppleInterfaceStyle'), read('AppleAccentColor'), read('AppleInterfaceStyleSwitchesAutomatically')]);
|
|
81
|
+
const mode = auto === '1' ? 'Auto' : style === 'Dark' ? 'Dark' : 'Light';
|
|
82
|
+
const color = accent === null ? 'Multicolor' : MAC_ACCENTS[accent] ?? 'Blue';
|
|
83
|
+
return `${color} (${mode})`;
|
|
84
|
+
}
|
|
85
|
+
if (ctx.platform === 'win32') {
|
|
86
|
+
const out = await run('reg', ['query', 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize', '/v', 'AppsUseLightTheme']);
|
|
87
|
+
const light = /0x1/.test(out ?? '');
|
|
88
|
+
return out ? (light ? 'Light' : 'Dark') : null;
|
|
89
|
+
}
|
|
90
|
+
return (await gtkSetting(ctx, 'gtk-theme-name', 'gtk-theme')) ?? kdeSetting(ctx, 'General', 'ColorScheme');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function gtkSetting(ctx, iniKey, gsettingsKey) {
|
|
94
|
+
const xdg = ctx.env.XDG_CONFIG_HOME ?? join(ctx.home, '.config');
|
|
95
|
+
if (await which('gsettings')) {
|
|
96
|
+
const out = (await run('gsettings', ['get', 'org.gnome.desktop.interface', gsettingsKey], { timeout: 1000 }))?.trim().replace(/^'|'$/g, '');
|
|
97
|
+
if (out) return `${out} [GTK]`;
|
|
98
|
+
}
|
|
99
|
+
for (const v of ['gtk-4.0', 'gtk-3.0']) {
|
|
100
|
+
const value = parseIni(await readText(join(xdg, v, 'settings.ini')))?.Settings?.[iniKey];
|
|
101
|
+
if (value) return `${value} [GTK${v[4]}]`;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function kdeSetting(ctx, section, key) {
|
|
107
|
+
const xdg = ctx.env.XDG_CONFIG_HOME ?? join(ctx.home, '.config');
|
|
108
|
+
const value = parseIni(await readText(join(xdg, 'kdeglobals')))?.[section]?.[key];
|
|
109
|
+
return value ? `${value} [Qt]` : null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function detectResolution(ctx) {
|
|
113
|
+
if (ctx.platform === 'darwin') {
|
|
114
|
+
const { displays } = (await ctx.darwinDisplays()) ?? { displays: [] };
|
|
115
|
+
return displays.map((d) => `${d.width}x${d.height}${d.refresh ? ` @ ${d.refresh}Hz` : ''}`);
|
|
116
|
+
}
|
|
117
|
+
if (ctx.platform === 'win32') {
|
|
118
|
+
const gpus = (await ctx.windowsInfo())?.gpu ?? [];
|
|
119
|
+
return gpus
|
|
120
|
+
.filter((g) => g.CurrentHorizontalResolution)
|
|
121
|
+
// A refresh rate of 0 or 1 means "hardware default" (common in VMs), not 1 Hz.
|
|
122
|
+
.map((g) => `${g.CurrentHorizontalResolution}x${g.CurrentVerticalResolution}${g.CurrentRefreshRate > 1 ? ` @ ${g.CurrentRefreshRate}Hz` : ''}`);
|
|
123
|
+
}
|
|
124
|
+
if (ctx.env.DISPLAY && (await which('xrandr'))) {
|
|
125
|
+
const modes = parseXrandr(await run('xrandr', ['--current'], { timeout: 1500 }));
|
|
126
|
+
if (modes.length) return modes;
|
|
127
|
+
}
|
|
128
|
+
// Wayland / no X: read the preferred mode of every connected DRM connector.
|
|
129
|
+
const connectors = (await readdir('/sys/class/drm').catch(() => [])).filter((c) => /^card\d+-/.test(c));
|
|
130
|
+
const out = [];
|
|
131
|
+
for (const c of connectors) {
|
|
132
|
+
if ((await readText(`/sys/class/drm/${c}/status`))?.trim() !== 'connected') continue;
|
|
133
|
+
const mode = (await readText(`/sys/class/drm/${c}/modes`))?.split('\n')[0]?.trim();
|
|
134
|
+
if (mode) out.push(mode);
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default [
|
|
140
|
+
{ key: 'resolution', label: 'Resolution', get: async (ctx) => (await detectResolution(ctx)).join(', ') || null },
|
|
141
|
+
{ key: 'de', label: 'DE', get: detectDe },
|
|
142
|
+
{ key: 'wm', label: 'WM', get: detectWm },
|
|
143
|
+
{ key: 'theme', label: 'Theme', get: detectTheme },
|
|
144
|
+
{
|
|
145
|
+
key: 'icons',
|
|
146
|
+
label: 'Icons',
|
|
147
|
+
async get(ctx) {
|
|
148
|
+
if (ctx.platform !== 'linux') return null;
|
|
149
|
+
return (await gtkSetting(ctx, 'gtk-icon-theme-name', 'icon-theme')) ?? kdeSetting(ctx, 'Icons', 'Theme');
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
];
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import { readdir, statfs } from 'node:fs/promises';
|
|
3
|
+
import { run, readText, which } from '../exec.js';
|
|
4
|
+
import {
|
|
5
|
+
parseVmStat, macMemoryUsed, parseMeminfo, linuxMemory, parseSwapUsage, parsePmset, parseLspci, parseIoregGpu, parseCpuinfoModel,
|
|
6
|
+
} from '../parsers.js';
|
|
7
|
+
import { cleanCpuName, cleanGpuName, formatMHz, formatUsage, colorByLevel, bar } from '../format.js';
|
|
8
|
+
|
|
9
|
+
const usageOpts = (ctx) => ({ useColor: ctx.config.useColor, bars: ctx.config.bars, barWidth: ctx.config.barWidth });
|
|
10
|
+
|
|
11
|
+
async function cpu(ctx) {
|
|
12
|
+
const cpus = os.cpus();
|
|
13
|
+
let model = cpus[0]?.model?.trim() || null;
|
|
14
|
+
let speed = null;
|
|
15
|
+
let cores = cpus.length || null;
|
|
16
|
+
let detail = null;
|
|
17
|
+
|
|
18
|
+
if (ctx.platform === 'darwin') {
|
|
19
|
+
model = (await run('sysctl', ['-n', 'machdep.cpu.brand_string']))?.trim() || model;
|
|
20
|
+
const [p, e] = (await run('sysctl', ['-n', 'hw.perflevel0.logicalcpu', 'hw.perflevel1.logicalcpu']))?.trim().split('\n').map(Number) ?? [];
|
|
21
|
+
if (p && e) detail = `${p}P + ${e}E`;
|
|
22
|
+
// Node reports a bogus clock on Apple Silicon and the real one is not exposed via sysctl.
|
|
23
|
+
if (os.arch() !== 'arm64') speed = Number((await run('sysctl', ['-n', 'hw.cpufrequency_max']))?.trim()) / 1e6 || cpus[0]?.speed || null;
|
|
24
|
+
} else if (ctx.platform === 'linux' || ctx.platform === 'android') {
|
|
25
|
+
const maxKHz = Number((await readText('/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq'))?.trim());
|
|
26
|
+
speed = maxKHz ? Math.round(maxKHz / 1000) : cpus[0]?.speed || null;
|
|
27
|
+
if (!model || model === 'unknown' || /^\d+$/.test(model)) {
|
|
28
|
+
// On ARM, Node reports "unknown"; /proc/cpuinfo has the implementer/part IDs.
|
|
29
|
+
model = parseCpuinfoModel(await readText('/proc/cpuinfo')) ?? null;
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
speed = cpus[0]?.speed || null;
|
|
33
|
+
}
|
|
34
|
+
if (!model) return null;
|
|
35
|
+
|
|
36
|
+
const name = cleanCpuName(model);
|
|
37
|
+
const text = `${name}${cores ? ` (${cores})` : ''}${speed ? ` @ ${formatMHz(speed)}` : ''}${detail ? ` [${detail}]` : ''}`;
|
|
38
|
+
return { text, data: { model: name, cores, speedMHz: speed, layout: detail } };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function gpu(ctx) {
|
|
42
|
+
let gpus = [];
|
|
43
|
+
if (ctx.platform === 'darwin') {
|
|
44
|
+
// ioreg answers in ~30 ms; system_profiler (what neofetch uses) takes ~300 ms.
|
|
45
|
+
gpus = parseIoregGpu(await run('ioreg', ['-rc', 'IOAccelerator', '-d1']));
|
|
46
|
+
if (!gpus.length) gpus = (await ctx.darwinDisplays())?.gpus ?? [];
|
|
47
|
+
return gpus.map((g) => (g.cores ? `${g.name} (${g.cores} cores)` : g.name));
|
|
48
|
+
}
|
|
49
|
+
if (ctx.platform === 'win32') {
|
|
50
|
+
return ((await ctx.windowsInfo())?.gpu ?? []).map((g) => g.Name).filter(Boolean);
|
|
51
|
+
}
|
|
52
|
+
if (await which('lspci')) {
|
|
53
|
+
gpus = parseLspci(await run('lspci', ['-mm'], { timeout: 1500 }));
|
|
54
|
+
if (gpus.length) return gpus.map((g) => cleanGpuName(g.vendor, g.device));
|
|
55
|
+
}
|
|
56
|
+
// No lspci (containers, minimal installs, ARM boards): fall back to the DRM driver name.
|
|
57
|
+
const cards = (await readdir('/sys/class/drm').catch(() => [])).filter((c) => /^card\d+$/.test(c));
|
|
58
|
+
const VENDORS = { '0x10de': 'NVIDIA', '0x1002': 'AMD', '0x8086': 'Intel' };
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const c of cards) {
|
|
61
|
+
const vendor = (await readText(`/sys/class/drm/${c}/device/vendor`))?.trim();
|
|
62
|
+
const driver = /DRIVER=(.+)/.exec((await readText(`/sys/class/drm/${c}/device/uevent`)) ?? '')?.[1];
|
|
63
|
+
const name = [VENDORS[vendor], driver].filter(Boolean).join(' ');
|
|
64
|
+
if (name) out.push(name);
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function memory(ctx) {
|
|
70
|
+
let total = os.totalmem();
|
|
71
|
+
let used = total - os.freemem();
|
|
72
|
+
if (ctx.platform === 'darwin') {
|
|
73
|
+
const vm = await run('vm_stat');
|
|
74
|
+
if (vm) used = macMemoryUsed(parseVmStat(vm));
|
|
75
|
+
} else if (ctx.platform === 'linux' || ctx.platform === 'android') {
|
|
76
|
+
const mem = linuxMemory(parseMeminfo(await readText('/proc/meminfo')));
|
|
77
|
+
if (mem) ({ total, used } = mem);
|
|
78
|
+
}
|
|
79
|
+
return { text: formatUsage(used, total, usageOpts(ctx)), data: { used, total } };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function swap(ctx) {
|
|
83
|
+
let s = null;
|
|
84
|
+
if (ctx.platform === 'darwin') s = parseSwapUsage(await run('sysctl', ['-n', 'vm.swapusage']));
|
|
85
|
+
else if (ctx.platform === 'linux' || ctx.platform === 'android') {
|
|
86
|
+
const m = parseMeminfo(await readText('/proc/meminfo'));
|
|
87
|
+
if (m.SwapTotal !== undefined) s = { total: m.SwapTotal, used: m.SwapTotal - (m.SwapFree ?? 0) };
|
|
88
|
+
}
|
|
89
|
+
if (!s || !s.total) return null;
|
|
90
|
+
return { text: formatUsage(s.used, s.total, usageOpts(ctx)), data: s };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function disk(ctx) {
|
|
94
|
+
const paths = ctx.config.disks ?? [ctx.platform === 'win32' ? `${ctx.env.SystemDrive ?? 'C:'}\\` : '/'];
|
|
95
|
+
const lines = [];
|
|
96
|
+
for (const path of paths) {
|
|
97
|
+
try {
|
|
98
|
+
const s = await statfs(path);
|
|
99
|
+
const total = s.blocks * s.bsize;
|
|
100
|
+
const free = s.bfree * s.bsize;
|
|
101
|
+
const avail = s.bavail * s.bsize;
|
|
102
|
+
const used = total - free;
|
|
103
|
+
// Same math as df: percentage of the space a normal user can use (excludes root-reserved blocks).
|
|
104
|
+
const shown = { used, total: used + avail };
|
|
105
|
+
lines.push({ label: `Disk (${path})`, text: formatUsage(shown.used, shown.total, usageOpts(ctx)), data: { path, used, total, available: avail } });
|
|
106
|
+
} catch {
|
|
107
|
+
// Path missing or not a filesystem: skip it.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return lines;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function battery(ctx) {
|
|
114
|
+
let b = null;
|
|
115
|
+
if (ctx.platform === 'darwin') {
|
|
116
|
+
b = parsePmset(await run('pmset', ['-g', 'batt']));
|
|
117
|
+
} else if (ctx.platform === 'linux' || ctx.platform === 'android') {
|
|
118
|
+
const supplies = (await readdir('/sys/class/power_supply').catch(() => [])).filter((n) => /^(BAT|battery|CMB)/i.test(n));
|
|
119
|
+
for (const s of supplies) {
|
|
120
|
+
const pct = Number((await readText(`/sys/class/power_supply/${s}/capacity`))?.trim());
|
|
121
|
+
if (Number.isFinite(pct)) {
|
|
122
|
+
b = { percent: pct, state: (await readText(`/sys/class/power_supply/${s}/status`))?.trim().toLowerCase() ?? null, remaining: null };
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
} else if (ctx.platform === 'win32') {
|
|
127
|
+
const w = (await ctx.windowsInfo())?.bat?.[0];
|
|
128
|
+
if (w?.EstimatedChargeRemaining !== undefined) b = { percent: w.EstimatedChargeRemaining, state: w.BatteryStatus === 2 ? 'charging' : 'discharging', remaining: null };
|
|
129
|
+
}
|
|
130
|
+
if (!b) return null;
|
|
131
|
+
const details = [b.state, b.remaining && `${b.remaining} left`].filter(Boolean).join(', ');
|
|
132
|
+
// A battery is "good" when full, so invert the usage color scale.
|
|
133
|
+
const pctText = colorByLevel(`${b.percent}%`, 100 - b.percent, ctx.config.useColor);
|
|
134
|
+
const barText = ctx.config.bars ? `${bar(100 - b.percent, ctx.config.barWidth, ctx.config.useColor)} ` : '';
|
|
135
|
+
return { text: `${barText}${pctText}${details ? ` (${details})` : ''}`, data: b };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function localIp() {
|
|
139
|
+
const SKIP = /^(lo|docker|br-|veth|virbr|vmnet|vboxnet|utun|awdl|llw|bridge|tailscale|zt|vEthernet)/i;
|
|
140
|
+
for (const [name, addrs] of Object.entries(os.networkInterfaces())) {
|
|
141
|
+
if (SKIP.test(name)) continue;
|
|
142
|
+
const v4 = addrs?.find((a) => a.family === 'IPv4' && !a.internal);
|
|
143
|
+
if (v4) return { text: `${v4.cidr ?? v4.address} (${name})`, data: { address: v4.address, cidr: v4.cidr, interface: name } };
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export default [
|
|
149
|
+
{ key: 'cpu', label: 'CPU', get: cpu },
|
|
150
|
+
{ key: 'gpu', label: 'GPU', get: gpu },
|
|
151
|
+
{ key: 'memory', label: 'Memory', get: memory },
|
|
152
|
+
{ key: 'swap', label: 'Swap', get: swap },
|
|
153
|
+
{ key: 'disk', label: 'Disk', get: disk },
|
|
154
|
+
{ key: 'battery', label: 'Battery', get: battery },
|
|
155
|
+
{ key: 'ip', label: 'Local IP', get: localIp },
|
|
156
|
+
];
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import system from './system.js';
|
|
2
|
+
import packages from './packages.js';
|
|
3
|
+
import terminal from './terminal.js';
|
|
4
|
+
import desktop from './desktop.js';
|
|
5
|
+
import hardware from './hardware.js';
|
|
6
|
+
|
|
7
|
+
export const MODULES = new Map([...system, ...packages, ...terminal, ...desktop, ...hardware].map((m) => [m.key, m]));
|
|
8
|
+
|
|
9
|
+
/** Layout-only modules, drawn by the renderer rather than detected. */
|
|
10
|
+
export const LAYOUT_MODULES = new Set(['separator', 'break', 'colors']);
|
|
11
|
+
|
|
12
|
+
export const DEFAULT_MODULES = [
|
|
13
|
+
'title', 'separator',
|
|
14
|
+
'os', 'host', 'kernel', 'uptime', 'packages', 'shell', 'resolution', 'de', 'wm', 'theme', 'icons',
|
|
15
|
+
'terminal', 'font', 'gpu', 'cpu', 'memory', 'swap', 'disk', 'battery', 'locale',
|
|
16
|
+
'break', 'colors',
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export const ALL_MODULE_KEYS = [...MODULES.keys(), ...LAYOUT_MODULES];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Turn whatever a module's get() returned into { entries: [{ label, text }], data }.
|
|
23
|
+
* Modules may return: null, a string, an array of strings or entries, or { text, data }.
|
|
24
|
+
*/
|
|
25
|
+
export function normalizeResult(mod, result) {
|
|
26
|
+
const label = mod.label;
|
|
27
|
+
if (result === null || result === undefined || result === '') return { entries: [], data: null };
|
|
28
|
+
if (typeof result === 'string') return { entries: [{ label, text: result }], data: result };
|
|
29
|
+
if (Array.isArray(result)) {
|
|
30
|
+
const entries = result
|
|
31
|
+
.filter((r) => r !== null && r !== undefined && r !== '')
|
|
32
|
+
.map((r) => (typeof r === 'string' ? { label, text: r } : { label: r.label ?? label, text: r.text }));
|
|
33
|
+
const data = result.map((r) => (typeof r === 'string' ? r : r.data ?? r.text));
|
|
34
|
+
return { entries, data: data.length === 1 ? data[0] : data };
|
|
35
|
+
}
|
|
36
|
+
const texts = Array.isArray(result.text) ? result.text : [result.text];
|
|
37
|
+
return {
|
|
38
|
+
entries: texts.filter(Boolean).map((text) => ({ label, text })),
|
|
39
|
+
data: result.data ?? result.text,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { countDir, readText, run, which } from '../exec.js';
|
|
5
|
+
|
|
6
|
+
const lines = (text) => (text ? text.split('\n').filter((l) => l.trim()).length : null);
|
|
7
|
+
|
|
8
|
+
// Each counter resolves to a number (installed packages) or null (manager absent).
|
|
9
|
+
// Where possible we read the package database from disk instead of spawning the
|
|
10
|
+
// package manager. neofetch spawns a process for every manager, which is most of its runtime.
|
|
11
|
+
const COUNTERS = {
|
|
12
|
+
// Debian/Ubuntu: one stanza per package in the dpkg status file.
|
|
13
|
+
dpkg: async () => {
|
|
14
|
+
const text = await readText('/var/lib/dpkg/status');
|
|
15
|
+
return text ? (text.match(/^Status: install ok installed$/gm) ?? []).length : null;
|
|
16
|
+
},
|
|
17
|
+
// Arch: one directory per installed package.
|
|
18
|
+
pacman: () => countDir('/var/lib/pacman/local', (n) => n !== 'ALPM_DB_VERSION' && !n.startsWith('.')),
|
|
19
|
+
// Alpine: one "P:" line per package.
|
|
20
|
+
apk: async () => {
|
|
21
|
+
const text = await readText('/lib/apk/db/installed');
|
|
22
|
+
return text ? (text.match(/^P:/gm) ?? []).length : null;
|
|
23
|
+
},
|
|
24
|
+
// Gentoo: /var/db/pkg/<category>/<package>.
|
|
25
|
+
emerge: async () => {
|
|
26
|
+
const dirs = await readdir('/var/db/pkg').catch(() => null);
|
|
27
|
+
if (!dirs) return null;
|
|
28
|
+
const counts = await Promise.all(dirs.map((d) => countDir(join('/var/db/pkg', d))));
|
|
29
|
+
return counts.reduce((a, b) => a + (b ?? 0), 0);
|
|
30
|
+
},
|
|
31
|
+
rpm: async () => ((await which('rpm')) ? lines(await run('rpm', ['-qa'], { timeout: 4000 })) : null),
|
|
32
|
+
xbps: async () => ((await which('xbps-query')) ? lines(await run('xbps-query', ['-l'])) : null),
|
|
33
|
+
'nix-user': async () => ((await which('nix-store')) ? lines(await run('nix-store', ['-q', '--requisites', join(homedir(), '.nix-profile')], { timeout: 4000 })) : null),
|
|
34
|
+
'nix-system': async () =>
|
|
35
|
+
(await which('nix-store')) ? lines(await run('nix-store', ['-q', '--requisites', '/run/current-system/sw'], { timeout: 4000 })) : null,
|
|
36
|
+
flatpak: async () => {
|
|
37
|
+
const counts = await Promise.all(
|
|
38
|
+
['/var/lib/flatpak/app', '/var/lib/flatpak/runtime', join(homedir(), '.local/share/flatpak/app'), join(homedir(), '.local/share/flatpak/runtime')].map((d) =>
|
|
39
|
+
countDir(d),
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
return counts.some((c) => c !== null) ? counts.reduce((a, b) => a + (b ?? 0), 0) : null;
|
|
43
|
+
},
|
|
44
|
+
snap: () => countDir('/snap', (n) => !['bin', 'README'].includes(n) && !n.startsWith('.')),
|
|
45
|
+
brew: async (ctx) => {
|
|
46
|
+
const prefixes = [ctx.env.HOMEBREW_PREFIX, '/opt/homebrew', '/usr/local', '/home/linuxbrew/.linuxbrew', join(homedir(), '.linuxbrew')].filter(Boolean);
|
|
47
|
+
for (const p of new Set(prefixes)) {
|
|
48
|
+
const n = await countDir(join(p, 'Cellar'));
|
|
49
|
+
if (n !== null) return n;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
},
|
|
53
|
+
'brew-cask': async (ctx) => {
|
|
54
|
+
const prefixes = [ctx.env.HOMEBREW_PREFIX, '/opt/homebrew', '/usr/local'].filter(Boolean);
|
|
55
|
+
for (const p of new Set(prefixes)) {
|
|
56
|
+
const n = await countDir(join(p, 'Caskroom'));
|
|
57
|
+
if (n !== null) return n;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
},
|
|
61
|
+
port: async () => {
|
|
62
|
+
if (!(await which('port'))) return null;
|
|
63
|
+
return lines(await run('port', ['-q', 'installed'], { timeout: 4000 }));
|
|
64
|
+
},
|
|
65
|
+
pkg: async () => ((await which('pkg')) ? lines(await run('pkg', ['info'], { timeout: 4000 })) : null),
|
|
66
|
+
scoop: (ctx) => countDir(join(ctx.env.SCOOP ?? join(homedir(), 'scoop'), 'apps'), (n) => n !== 'scoop' && !n.startsWith('.')),
|
|
67
|
+
choco: (ctx) => countDir(join(ctx.env.ChocolateyInstall ?? 'C:\\ProgramData\\chocolatey', 'lib')),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const BY_PLATFORM = {
|
|
71
|
+
darwin: ['brew', 'brew-cask', 'port', 'nix-user', 'nix-system'],
|
|
72
|
+
linux: ['dpkg', 'pacman', 'rpm', 'apk', 'xbps', 'emerge', 'nix-user', 'nix-system', 'flatpak', 'snap', 'brew'],
|
|
73
|
+
android: ['dpkg', 'apk'],
|
|
74
|
+
freebsd: ['pkg'],
|
|
75
|
+
openbsd: ['pkg'],
|
|
76
|
+
netbsd: ['pkg'],
|
|
77
|
+
win32: ['scoop', 'choco'],
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export default [
|
|
81
|
+
{
|
|
82
|
+
key: 'packages',
|
|
83
|
+
label: 'Packages',
|
|
84
|
+
async get(ctx) {
|
|
85
|
+
const managers = BY_PLATFORM[ctx.platform] ?? [];
|
|
86
|
+
const counts = await Promise.all(managers.map((m) => COUNTERS[m](ctx).catch(() => null)));
|
|
87
|
+
const found = managers.map((m, i) => [m, counts[i]]).filter(([, n]) => n > 0);
|
|
88
|
+
if (!found.length) return null;
|
|
89
|
+
const total = found.reduce((a, [, n]) => a + n, 0);
|
|
90
|
+
return {
|
|
91
|
+
text: found.map(([m, n]) => `${n} (${m})`).join(', '),
|
|
92
|
+
data: { total, managers: Object.fromEntries(found) },
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
];
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import { run, readText } from '../exec.js';
|
|
3
|
+
import { parseSwVers, macosCodename } from '../parsers.js';
|
|
4
|
+
import { formatUptime } from '../format.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Who are we running on? Returns the display string plus the names used to pick a logo,
|
|
8
|
+
* in priority order (neofetch matches its logo `case` against the distro string the same way).
|
|
9
|
+
*/
|
|
10
|
+
export function distro(ctx) {
|
|
11
|
+
return ctx.once('distro', async () => {
|
|
12
|
+
const arch = os.machine?.() ?? os.arch();
|
|
13
|
+
switch (ctx.platform) {
|
|
14
|
+
case 'darwin': {
|
|
15
|
+
const v = parseSwVers(await run('sw_vers'));
|
|
16
|
+
const name = v.name ?? 'macOS';
|
|
17
|
+
const codename = macosCodename(v.version);
|
|
18
|
+
const text = [name, codename, v.version, v.build && `(${v.build})`, arch].filter(Boolean).join(' ');
|
|
19
|
+
return { text, logoNames: ['macOS', 'Darwin'], data: { name, codename, version: v.version, build: v.build, arch } };
|
|
20
|
+
}
|
|
21
|
+
case 'linux':
|
|
22
|
+
case 'android': {
|
|
23
|
+
if (ctx.platform === 'android' || ctx.env.TERMUX_VERSION) {
|
|
24
|
+
const version = (await run('getprop', ['ro.build.version.release']))?.trim();
|
|
25
|
+
return { text: `Android ${version ?? ''} ${arch}`.replace(/\s+/g, ' '), logoNames: ['Android'], data: { name: 'Android', version, arch } };
|
|
26
|
+
}
|
|
27
|
+
const rel = (await ctx.osRelease()) ?? {};
|
|
28
|
+
const name = rel.PRETTY_NAME ?? rel.DISTRIB_DESCRIPTION ?? [rel.NAME ?? rel.DISTRIB_ID, rel.VERSION_ID ?? rel.DISTRIB_RELEASE].filter(Boolean).join(' ');
|
|
29
|
+
const wsl = /microsoft/i.test(os.release()) || ctx.env.WSL_DISTRO_NAME;
|
|
30
|
+
const text = `${name || 'Linux'} ${arch}${wsl ? ' (WSL)' : ''}`;
|
|
31
|
+
const logoNames = [rel.NAME, rel.DISTRIB_ID, rel.ID, ...(rel.ID_LIKE ?? '').split(/\s+/), 'Linux'].filter(Boolean);
|
|
32
|
+
return { text, logoNames, data: { name: rel.NAME ?? null, id: rel.ID ?? null, version: rel.VERSION_ID ?? null, arch, wsl: Boolean(wsl) } };
|
|
33
|
+
}
|
|
34
|
+
case 'win32': {
|
|
35
|
+
let name = os.version?.() ?? 'Windows';
|
|
36
|
+
const build = Number(os.release().split('.')[2] ?? 0);
|
|
37
|
+
// Windows 11 still reports "Windows 10" in its product name; the build number tells them apart.
|
|
38
|
+
if (build >= 22000) name = name.replace('Windows 10', 'Windows 11');
|
|
39
|
+
// neofetch's "Windows"* glob is the old 4-color flag; Windows 8+ (build 9200) gets the flat logo.
|
|
40
|
+
const logoNames = build >= 9200 ? ['windows10', 'Windows'] : ['Windows'];
|
|
41
|
+
return { text: `${name} ${arch}`, logoNames, data: { name, build, arch } };
|
|
42
|
+
}
|
|
43
|
+
default: {
|
|
44
|
+
const name = os.type();
|
|
45
|
+
return { text: `${name} ${os.release()} ${arch}`, logoNames: [name, 'BSD'], data: { name, version: os.release(), arch } };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const JUNK_DMI = /^(To be filled by O\.E\.M\.|To Be Filled By O\.E\.M\.|OEM|O\.E\.M\.|Not Applicable|System Product Name|System Version|Undefined|Default string|Not Specified|Type1ProductConfigId|INVALID|All Series|None)$/i;
|
|
52
|
+
|
|
53
|
+
export default [
|
|
54
|
+
{
|
|
55
|
+
key: 'title',
|
|
56
|
+
label: null,
|
|
57
|
+
async get(ctx) {
|
|
58
|
+
let user;
|
|
59
|
+
try {
|
|
60
|
+
user = os.userInfo().username;
|
|
61
|
+
} catch {
|
|
62
|
+
user = ctx.env.USER ?? ctx.env.USERNAME ?? 'user';
|
|
63
|
+
}
|
|
64
|
+
const host = os.hostname().split('.')[0];
|
|
65
|
+
return { text: `${user}@${host}`, data: { user, host } };
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
key: 'os',
|
|
70
|
+
label: 'OS',
|
|
71
|
+
get: (ctx) => distro(ctx),
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
key: 'host',
|
|
75
|
+
label: 'Host',
|
|
76
|
+
async get(ctx) {
|
|
77
|
+
if (ctx.platform === 'darwin') {
|
|
78
|
+
const model = (await run('sysctl', ['-n', 'hw.model']))?.trim();
|
|
79
|
+
if (!model) return null;
|
|
80
|
+
// The marketing name ("MacBook Air") only comes from system_profiler (~250 ms).
|
|
81
|
+
// Hardware does not change, so cache it per model identifier.
|
|
82
|
+
const marketing = await ctx.cache.remember(`mac-name:${model}`, async () => {
|
|
83
|
+
const json = await run('system_profiler', ['SPHardwareDataType', '-json'], { timeout: 5000 });
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(json).SPHardwareDataType[0].machine_name ?? null;
|
|
86
|
+
} catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return { text: marketing ? `${marketing} (${model})` : model, data: { name: marketing, model } };
|
|
91
|
+
}
|
|
92
|
+
if (ctx.platform === 'linux') {
|
|
93
|
+
if (ctx.env.WSL_DISTRO_NAME || /microsoft/i.test(os.release())) return 'Windows Subsystem for Linux';
|
|
94
|
+
const dt = (await readText('/sys/firmware/devicetree/base/model'))?.replace(/\0/g, '').trim();
|
|
95
|
+
if (dt) return dt;
|
|
96
|
+
const read = async (f) => {
|
|
97
|
+
const v = (await readText(`/sys/devices/virtual/dmi/id/${f}`))?.trim();
|
|
98
|
+
return v && !JUNK_DMI.test(v) ? v : null;
|
|
99
|
+
};
|
|
100
|
+
const [name, version, board, vendor] = await Promise.all(['product_name', 'product_version', 'board_name', 'board_vendor'].map(read));
|
|
101
|
+
if (name) return [name, version].filter(Boolean).join(' ');
|
|
102
|
+
return [vendor, board].filter(Boolean).join(' ') || null;
|
|
103
|
+
}
|
|
104
|
+
if (ctx.platform === 'win32') {
|
|
105
|
+
const cs = (await ctx.windowsInfo())?.cs;
|
|
106
|
+
return cs ? `${cs.Manufacturer ?? ''} ${cs.Model ?? ''}`.trim() : null;
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
key: 'kernel',
|
|
113
|
+
label: 'Kernel',
|
|
114
|
+
get: (ctx) => (ctx.platform === 'win32' ? os.release() : `${os.type()} ${os.release()}`),
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
key: 'uptime',
|
|
118
|
+
label: 'Uptime',
|
|
119
|
+
get() {
|
|
120
|
+
const seconds = Math.floor(os.uptime());
|
|
121
|
+
return { text: formatUptime(seconds), data: { seconds } };
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
key: 'locale',
|
|
126
|
+
label: 'Locale',
|
|
127
|
+
get(ctx) {
|
|
128
|
+
return ctx.env.LC_ALL || ctx.env.LC_MESSAGES || ctx.env.LANG || Intl.DateTimeFormat().resolvedOptions().locale || null;
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
];
|