-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlufetch.py
More file actions
172 lines (161 loc) · 4.94 KB
/
lufetch.py
File metadata and controls
172 lines (161 loc) · 4.94 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
import os, platform, shutil, subprocess, socket
from pathlib import Path
def read_ascii(path):
try:
text = path.read_text(encoding='utf-8')
except Exception:
return []
return text.rstrip('\n').split('\n') if text else []
def get_os_name():
p = Path('/etc/os-release')
if p.exists():
try:
for line in p.read_text(encoding='utf-8').splitlines():
if line.startswith('PRETTY_NAME='):
return line.split('=',1)[1].strip().strip('"')
except Exception:
pass
return platform.system() + ' ' + platform.release()
def get_uptime():
try:
with open('/proc/uptime','r') as f:
secs = float(f.readline().split()[0])
m, s = divmod(int(secs), 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
if d:
return f"{d}d {h}h {m}m"
if h:
return f"{h}h {m}m"
return f"{m}m"
except Exception:
return ''
def count_packages():
cmds = [['dpkg-query','-f','${binary:Package}\n','-W'], ['rpm','-qa'], ['pacman','-Qq'], ['apk','info']]
for cmd in cmds:
if shutil.which(cmd[0]):
try:
out = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
return str(len(out.splitlines()))
except Exception:
continue
return ''
def get_cpu():
try:
if Path('/proc/cpuinfo').exists():
for line in Path('/proc/cpuinfo').read_text(encoding='utf-8').splitlines():
if line.lower().startswith('model name'):
return line.split(':',1)[1].strip()
except Exception:
pass
return platform.processor() or ''
def get_shell():
shell = os.environ.get('SHELL')
if shell:
return os.path.basename(shell)
return ''
def get_mem():
try:
for line in Path('/proc/meminfo').read_text(encoding='utf-8').splitlines():
if line.startswith('MemTotal:'):
parts = line.split()
kb = int(parts[1])
mb = kb // 1024
gib = kb / (1024 * 1024)
return f"{mb}mb ({gib:.1f}GiB)"
except Exception:
pass
return ''
def get_localip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return ''
def get_disk():
try:
total, used, free = shutil.disk_usage('/')
used_gib = used / (2**30)
total_gib = total / (2**30)
percent = (used / total * 100) if total else 0.0
fstype = ''
p = Path('/proc/mounts')
if p.exists():
try:
for line in p.read_text(encoding='utf-8').splitlines():
parts = line.split()
if len(parts) >= 3 and parts[1] == '/':
fstype = parts[2]
break
except Exception:
pass
s = f"{used_gib:.2f} GiB / {total_gib:.2f} GiB ({percent:.0f}%)"
if fstype:
s += f" - {fstype}"
return s
except Exception:
pass
return ''
def main():
base = Path(__file__).parent
art_dir = base / 'ascii'
custom = art_dir / 'custom_ascii.txt'
default = art_dir / 'default_ascii.txt'
art = read_ascii(custom) if custom.exists() else []
if not art and default.exists():
art = read_ascii(default)
info = []
info.append(('OS', get_os_name()))
info.append(('Host', platform.node()))
info.append(('Kernel', platform.release()))
up = get_uptime()
if up:
info.append(('Uptime', up))
pk = count_packages()
if pk:
info.append(('Packages', pk))
sh = get_shell()
if sh:
info.append(('Shell', sh))
cpu = get_cpu()
if cpu:
info.append(('CPU', cpu))
mem = get_mem()
if mem:
info.append(('Memory', mem))
disk = get_disk()
if disk:
info.append(('Disk', disk))
ip = get_localip()
if ip:
info.append(('Local IP', ip))
PURPLE = '\033[38;5;93m'
RESET = '\033[0m'
def format_line(k, v):
name = k
colored = False
if name.startswith('(colored)'):
name = name[len('(colored)'):]
colored = True
colored = True
if colored:
name = f"{PURPLE}{name}{RESET}"
return f"{name}: {v}"
info_lines = [format_line(k, v) for k, v in info]
max_art = max((len(x) for x in art), default=0)
lines = max(len(art), len(info_lines))
art_start = (lines - len(art)) // 2 if art else 0
for i in range(lines):
art_idx = i - art_start
left = art[art_idx] if 0 <= art_idx < len(art) else ''
right = info_lines[i] if i < len(info_lines) else ''
if max_art:
print(f"{left:<{max_art}} {right}")
else:
print(right)
if __name__ == '__main__':
main()