fetchpy-null: next gen. of fetch scripts
This commit is contained in:
parent
ee846ef9e7
commit
3eb85bbcb3
78
fetchpy0
Executable file
78
fetchpy0
Executable file
@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Ferit Yiğit BALABAN <fyb@fybx.dev>, 2024
|
||||
#
|
||||
# fetchpy-null, fetch script alternative to neofetch
|
||||
# revision 1
|
||||
import os
|
||||
import sys
|
||||
import subprocess as sp
|
||||
|
||||
|
||||
def add_color(text, props):
|
||||
built = ''
|
||||
_clr_start = '\u001b[31m'
|
||||
_clr_end = '\u001b[0m'
|
||||
|
||||
i = 0
|
||||
p = 0
|
||||
char = lambda: text[i]
|
||||
while i < len(text):
|
||||
if char() == '{':
|
||||
built += f"{_clr_start}{props[p]}"
|
||||
p += 1
|
||||
elif char() == '}':
|
||||
built += _clr_end
|
||||
else:
|
||||
built += char()
|
||||
|
||||
i += 1
|
||||
return built
|
||||
|
||||
|
||||
def find_username():
|
||||
return sp.run(['whoami'], text=True, capture_output=True).stdout[0:-1]
|
||||
|
||||
|
||||
def find_hostname():
|
||||
return sp.run(['uname', '-n'], text=True, capture_output=True).stdout[0:-1]
|
||||
|
||||
|
||||
def main():
|
||||
props = []
|
||||
|
||||
props.append(find_username()) # username
|
||||
props.append(find_hostname()) # hostname
|
||||
props.append('Arch GNU+Linux') # distro_name
|
||||
props.append(os.uname().release) # kernel_version
|
||||
|
||||
installed_packages = sp.run(['pacman', '-Q'], text=True, capture_output=True).stdout.splitlines()
|
||||
props.append(str(len(installed_packages))) # package_count
|
||||
|
||||
total_mem, used_mem, _, _, _, _ = map(int, os.popen('free -m').readlines()[1].split()[1:])
|
||||
def rnd(x): return round(x, 1)
|
||||
props.append(f'{rnd(used_mem / 1000)} GB / {rnd(total_mem / 1000)} GB') # mem_usage
|
||||
|
||||
with open('/proc/uptime', 'r', encoding='utf8') as file:
|
||||
uptime_seconds = float(file.readline().split()[0])
|
||||
file.close()
|
||||
|
||||
if uptime_seconds < 60:
|
||||
uptime = str(uptime_seconds).split('.', maxsplit=1)[0] + ' seconds'
|
||||
elif uptime_seconds < 3600:
|
||||
number = str(uptime_seconds / 60).split('.', maxsplit=1)[0]
|
||||
uptime = f'{number} minute' if number == '1' else f'{number} minutes'
|
||||
else:
|
||||
number = str(uptime_seconds / 3600).split('.', maxsplit=1)[0]
|
||||
uptime = f'{number} hour' if number == '1' else f'{number} hours'
|
||||
props.append(uptime)
|
||||
|
||||
txt = \
|
||||
"""Hi {}, welcome to {}. I see that you're on {},
|
||||
running with Linux {}. You've {} packages installed.
|
||||
Your RAM usage is {} and uptime is {}.\n"""
|
||||
print(add_color(txt, props))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user