Skip to content

Instantly share code, notes, and snippets.

@Kodehawa
Last active August 12, 2020 22:10
Show Gist options
  • Save Kodehawa/0876bdacea3c8a9bff6f86038bbc046f to your computer and use it in GitHub Desktop.
Save Kodehawa/0876bdacea3c8a9bff6f86038bbc046f to your computer and use it in GitHub Desktop.
Fish meme (kafetch ported to fish but I made a dictionary in the process)
#!/bin/fish
set -l bold (tput bold)
set -l normal (tput sgr0)
set -g count 0
set -g index 1
# To get the current/used memory usage I ended up implementing dictionaries in fish because I was bored and couldn't come up with a better solution at the time.
# Hint: a friend came up with a better and shorter solution in less time.
# But this was an interesting exercise on implementing something that doesn't exist on an interpreted language, I guess.
function increment_key
set -a mem_key (echo $argv[1]:$argv[2])
set index (math $index + 1)
end
set mem_key
set mem_value
for i in (string replace -a " " "" (string split : (cat /proc/meminfo)))
if [ (math $count % 2) = '0' ]
increment_key $i $index
else
set -a mem_value (echo "$i")
end
set count (math $count + 1)
end
function get_from_key
for key_value in $mem_key
set -l split (string split : $key_value)
if [ $split[1] = $argv ]
echo $mem_value[$split[2]]
end
end
end
# End of dict implementation.
function replace_kb
echo (string replace 'kB' '' $argv)
end
function get_readable_value
echo (replace_kb (get_from_key $argv))
end
# Actually get memory values.
function get_total_memory
echo (get_readable_value MemTotal)
end
function get_readable_total_memory
echo (math -s0 (get_total_memory) / 1024)
end
function get_used_memory
set -l mem_used 0
set mem_used (math $mem_used + (get_total_memory))
set mem_used (math $mem_used + (get_readable_value Shmem))
set -l mem_total_substract (get_readable_value MemFree) (get_readable_value Buffers) (get_readable_value Cached) (get_readable_value SReclaimable)
for value in $mem_total_substract
set mem_used (math $mem_used - (replace_kb $value))
end
echo $mem_used
end
function get_readable_used_memory
echo (math -s0 (get_used_memory) / 1024)
end
# End of memory stuff.
# All of the stuff that's NOT memory usage.
set -l kernel (uname -r)
# Pacman packages.
set -l pkg (pacman -Q | count)
# AUR packages (foreign)
# Foreign package lookup is slow (~200ms, so I'll comment it out)
#set -l aur_pkg (pacman -Qm | count)
# Explicitly installed packages.
set -l ex_pkg (pacman -Qqe | count)
# You know what this is.
set username (echo "$USER" | sed 's/.*/\u&/')
set readable_total_mem (get_readable_total_memory)
set readable_used_mem (get_readable_used_memory)
# Greeting
echo " $bold❤️$normal Hello $username <3 $bold($USER@$hostname) $normal"
# Kernel
echo "$bold Kernel: $normal$kernel"
# Memory
echo "$bold Memory: $normal$readable_used_mem MB / $readable_total_mem MB"
# Packages
# Foreign package lookup is slow (~200ms, so I'll comment it out)
#echo " $bold Packages: $normal$pkg (Explicit: $ex_pkg, AUR: $aur_pkg)"
echo "$bold Packages: $normal$pkg (Explicit: $ex_pkg)"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment