snips.sh

  1# vim:ft=bash
  2# See this wiki page for more info:
  3# https://github.com/dylanaraps/neofetch/wiki/Customizing-Info
  4
  5# Prepare ------------------------------------------------------------------------------------------
  6
  7pokemon=${1:-$(shuf -i 1-905 -n 1)}
  8
  9data_pokemon=$(curl -fsLS "https://pokeapi.co/api/v2/pokemon/${pokemon}")
 10data_species=$(curl -fsLS "$(echo "${data_pokemon}" | jq --raw-output .species.url)")
 11
 12id=$(echo "${data_species}" | jq --raw-output .id)
 13name=$(echo "${data_species}" | jq --raw-output '.names | .[] | select(.language.name == "en").name')
 14category=$(echo "${data_species}" | jq --raw-output '.genera | .[] | select(.language.name == "en").genus')
 15title="▐ No.$(printf '%03d' "${id}") ▌ ${name} - ${category}"
 16
 17for type in $(echo "${data_pokemon}" | jq --raw-output '.types | .[].type.name' | tr '[:lower:]' '[:upper:]'); do
 18  case "${type}" in
 19    NORMAL)   color=7  ;;
 20    FIRE)     color=9  ;;
 21    WATER)    color=12 ;;
 22    ELECTRIC) color=11 ;;
 23    GRASS)    color=10 ;;
 24    ICE)      color=14 ;;
 25    FIGHTING) color=1  ;;
 26    POISON)   color=5  ;;
 27    GROUND)   color=11 ;;
 28    FLYING)   color=6  ;;
 29    PSYCHIC)  color=13 ;;
 30    BUG)      color=2  ;;
 31    ROCK)     color=3  ;;
 32    GHOST)    color=4  ;;
 33    DRAGON)   color=4  ;;
 34    DARK)     color=3  ;;
 35    STEEL)    color=8  ;;
 36    FAIRY)    color=13 ;;
 37  esac
 38
 39  types="${types} [7;38;5;${color}m ${type}  "
 40done
 41
 42height=$(awk "BEGIN{ print $(echo "${data_pokemon}" | jq --raw-output .height) / 10 }")
 43weight=$(awk "BEGIN{ print $(echo "${data_pokemon}" | jq --raw-output .weight) / 10 }")
 44status=$(gum style --align center --width 44 "${types}" "Height: ${height}m      Weight: ${weight}kg")
 45
 46info=$(gum join --vertical "${title}" '' "${status}")
 47
 48pokemon_path=$(echo "${data_pokemon}" | jq --raw-output .name)
 49art=$(gum style "$(curl -fsLS "https://gitlab.com/phoneybadger/pokemon-colorscripts/-/raw/main/colorscripts/small/regular/${pokemon_path}" | sed -e 's/$//g')")
 50
 51terminal_size=$(stty size)
 52terminal_height=${terminal_size% *}
 53terminal_width=${terminal_size#* }
 54
 55prompt_height=${PROMPT_HEIGHT:-1}
 56
 57print_test() {
 58  no_color=$(printf '%b' "${1}" | sed -e 's/\x1B\[[0-9;]*[JKmsu]//g')
 59
 60  [ "$(printf '%s' "${no_color}" | wc --lines)" -gt $(( terminal_height - prompt_height )) ] && return 1
 61  [ "$(printf '%s' "${no_color}" | wc --max-line-length)" -gt "${terminal_width}" ] && return 1
 62
 63  gum style --align center --width="${terminal_width}" "${1}" ''
 64  printf '%b' "\033[A"
 65
 66  exit 0
 67}
 68
 69# Display ------------------------------------------------------------------------------------------
 70prin() {
 71    # If $2 doesn't exist we format $1 as info.
 72    if [[ "$(trim "$1")" && "$2" ]]; then
 73        [[ "$json" ]] && { printf '    %s\n' "\"${1}\" \"${2}\","; return; }
 74
 75        string="${1}${2:+: $2}"
 76    else
 77        string="${2:-$1}"
 78        local subtitle_color="$info_color"
 79    fi
 80    # disabling trim function so can make alained
 81    #string="$(trim "${string//$'\e[0m'}")"
 82    length="$(strip_sequences "$string")"
 83    length="${#length}"
 84
 85    # Format the output.
 86    string="${string/:/${reset}${colon_color}${separator:=:}${info_color}}"
 87    string="${subtitle_color}${bold}${string}"
 88
 89    # Print the info.
 90    printf '%b\n' "${text_padding:+\e[${text_padding}C}${zws}${string//\\n}${reset} "
 91
 92    # Calculate info height.
 93    ((++info_height))
 94
 95    # Log that prin was used.
 96    prin=1
 97}
 98
 99get_packages() {
100    # to adjust the number of pkgs per pkg manager
101    pkgs_h=0
102
103    # has: Check if package manager installed.
104    # dir: Count files or dirs in a glob.
105    # pac: If packages > 0, log package manager name.
106    # tot: Count lines in command output.
107    has() { type -p "$1" >/dev/null && manager=$1; }
108    # globbing is intentional here
109    # shellcheck disable=SC2206
110    dir() { pkgs=($@); ((packages+=${#pkgs[@]})); pac "$((${#pkgs[@]}-pkgs_h))"; }
111    pac() { (($1 > 0)) && { managers+=("${manager} $1"); manager_string+="${manager}, "; }; }
112    tot() {
113        IFS=$'\n' read -d "" -ra pkgs <<< "$("$@")";
114        ((packages+=${#pkgs[@]}));
115        pac "$((${#pkgs[@]}-pkgs_h))";
116    }
117
118    case $os in
119        Linux|BSD|"iPhone OS"|Solaris)
120            # Package Manager Programs.
121            has kiss       && tot kiss l
122            has cpt-list   && tot cpt-list
123            has pacman-key && tot pacman -Qq --color never
124            has dpkg       && tot dpkg-query -f '.\n' -W
125            has xbps-query && tot xbps-query -l
126            has apk        && tot apk info
127            has opkg       && tot opkg list-installed
128            has pacman-g2  && tot pacman-g2 -Q
129            has lvu        && tot lvu installed
130            has tce-status && tot tce-status -i
131            has pkg_info   && tot pkg_info
132            has pkgin      && tot pkgin list
133            has tazpkg     && pkgs_h=6 tot tazpkg list && ((packages-=6))
134            has sorcery    && tot gaze installed
135            has alps       && tot alps showinstalled
136            has butch      && tot butch list
137            has swupd      && tot swupd bundle-list --quiet
138            has pisi       && tot pisi li
139            has pacstall   && tot pacstall -L
140
141            # Using the dnf package cache is much faster than rpm.
142            if has dnf && type -p sqlite3 >/dev/null && [[ -f /var/cache/dnf/packages.db ]]; then
143                pac "$(sqlite3 /var/cache/dnf/packages.db "SELECT count(pkg) FROM installed")"
144            else
145                has rpm && tot rpm -qa
146            fi
147
148            # 'mine' conflicts with minesweeper games.
149            [[ -f /etc/SDE-VERSION ]] &&
150                has mine && tot mine -q
151
152            # Counting files/dirs.
153            # Variables need to be unquoted here. Only Bedrock Linux is affected.
154            # $br_prefix is fixed and won't change based on user input so this is safe either way.
155            # shellcheck disable=SC2086
156            {
157            shopt -s nullglob
158            has brew    && dir "$(brew --cellar)/* $(brew --caskroom)/*"
159            has emerge  && dir "/var/db/pkg/*/*"
160            has Compile && dir "/Programs/*/"
161            has eopkg   && dir "/var/lib/eopkg/package/*"
162            has crew    && dir "${CREW_PREFIX:-/usr/local}/etc/crew/meta/*.filelist"
163            has pkgtool && dir "/var/log/packages/*"
164            has scratch && dir "/var/lib/scratchpkg/index/*/.pkginfo"
165            has kagami  && dir "/var/lib/kagami/pkgs/*"
166            has cave    && dir "/var/db/paludis/repositories/cross-installed/*/data/*/ \
167                                /var/db/paludis/repositories/installed/data/*/"
168            shopt -u nullglob
169            }
170
171            # Other (Needs complex command)
172            has kpm-pkg && ((packages+=$(kpm  --get-selections | grep -cv deinstall$)))
173
174            has guix && {
175                manager=guix-system && tot guix package -p "/run/current-system/profile" -I
176                manager=guix-user   && tot guix package -I
177            }
178
179            has nix-store && {
180                nix-user-pkgs() {
181                    nix-store -qR ~/.nix-profile
182                    nix-store -qR /etc/profiles/per-user/"$USER"
183                }
184                manager=nix-system  && tot nix-store -qR /run/current-system/sw
185                manager=nix-user    && tot nix-user-pkgs
186                manager=nix-default && tot nix-store -qR /nix/var/nix/profiles/default
187            }
188
189            # pkginfo is also the name of a python package manager which is painfully slow.
190            # TODO: Fix this somehow.
191            has pkginfo && tot pkginfo -i
192
193            # List these last as they accompany regular package managers.
194            has flatpak && tot flatpak list
195            has spm     && tot spm list -i
196            has puyo    && dir ~/.puyo/installed
197
198            # Snap hangs if the command is run without the daemon running.
199            # Only run snap if the daemon is also running.
200            has snap && ps -e | grep -qFm 1 snapd >/dev/null && \
201            pkgs_h=1 tot snap list && ((packages-=1))
202
203            # This is the only standard location for appimages.
204            # See: https://github.com/AppImage/AppImageKit/wiki
205            manager=appimage && has appimaged && dir ~/.local/bin/*.appimage
206        ;;
207
208        "Mac OS X"|"macOS"|MINIX)
209            has brew  && {
210                manager="$(color 3)${reset}" && dir "$(brew --cellar)/* $(brew --caskroom)/*"
211            }
212
213            has nix-store && {
214                nix-user-pkgs() {
215                    nix-store -qR ~/.nix-profile
216                    nix-store -qR /etc/profiles/per-user/"$USER"
217                }
218                manager="$(color 4)$(color 1)S${reset}" && tot nix-store -qR /run/current-system/sw
219                manager="$(color 4) U${reset}"   && tot nix-user-pkgs
220            }
221        ;;
222    esac
223
224    if ((packages == 0)); then
225        unset packages
226
227    elif [[ $package_managers == on ]]; then
228        printf -v packages '%s, ' "${managers[@]}"
229        packages=${packages%,*}
230
231    elif [[ $package_managers == tiny ]]; then
232        packages+=" (${manager_string%,*})"
233    fi
234
235    packages=${packages/pacman-key/pacman}
236}
237
238print_info() {
239    poke_info=$(gum style --border normal --padding '0 1' --width 42 "\
240$(echo "$(info "$(color 1)    OS  " distro)" | tr -d "\n") \
241$(echo -e "\n$(info "$(color 2)    UP  " uptime)") \
242$(echo -e "\n$(info "$(color 3)   PKG  " packages)") \
243$(echo -e "\n$(info "$(color 4)    WM  " wm)") \
244$(echo -e "\n$(info "$(color 5) SHELL  " shell)") \
245$(echo -e "\n$(info "$(color 6)  TERM  " term)") \
246$(echo -e "\n$(color 9)  EDIT  $(color 7)  $EDITOR") \
247$(echo -e "\n$(info "$(color 8)   MEM  " memory)") ")
248  group_info_entries=$(gum join --vertical "${info}" '' "${poke_info}")
249  print_test "$(gum join --horizontal --align center "${art}" '  ' "${group_info_entries}")"
250}
251
252title_fqdn="off"
253kernel_shorthand="on"
254distro_shorthand="tiny"
255os_arch="off"
256uptime_shorthand="tiny"
257memory_percent="off"
258memory_unit="gib"
259package_managers="on"
260shell_path="off"
261shell_version="off"
262speed_type="bios_limit"
263speed_shorthand="on"
264cpu_brand="on"
265cpu_speed="on"
266cpu_cores="logical"
267cpu_temp="off"
268gpu_brand="on"
269gpu_type="all"
270refresh_rate="on"
271gtk_shorthand="off"
272gtk2="on"
273gtk3="on"
274public_ip_host="http://ident.me"
275public_ip_timeout=2
276de_version="on"
277disk_show=('/' "/dev/sda1")
278disk_subtitle="mount"
279disk_percent="on"
280music_player="auto"
281song_format="%artist% - %album% - %title%"
282song_shorthand="off"
283mpc_args=()
284colors=(distro)
285bold="on"
286underline_enabled="on"
287underline_char="─"
288separator=" "
289block_range=(0 15)
290color_blocks="on"
291block_width=3
292block_height=1
293col_offset="auto"
294bar_char_elapsed="-"
295bar_char_total="="
296bar_border="on"
297bar_length=15
298bar_color_elapsed="distro"
299bar_color_total="distro"
300cpu_display="off"
301memory_display="off"
302battery_display="off"
303disk_display="off"
304image_backend="off"
305image_source="auto"
306ascii_distro="auto"
307ascii_colors=(distro)
308ascii_bold="on"
309image_loop="off"
310thumbnail_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/thumbnails/neofetch"
311crop_mode="normal"
312crop_offset="center"
313image_size="auto"
314gap=3
315yoffset=0
316xoffset=0
317background_color=
318stdout="off"