gum/examples/diyfetch
2022-09-28 11:59:11 -04:00

100 lines
3.8 KiB
Bash
Executable file
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# ____ _____ ____ _ _
# | _ \_ _\ \ / / _| ___| |_ ___| |__
# | | | | | \ V / |_ / _ \ __/ __| '_ \
# | |_| | | | || _| __/ || (__| | | |
# |____/___| |_||_| \___|\__\___|_| |_|
#
# About:
# DIYfetch it the shell script template for writing fetch tool
# utilizing `gum join` command (https://github.com/charmbracelet/gum#join).
#
# This script is written in POSIX-shell for portability
# feel free to switch it to any scripting language that you prefer.
#
# Note:
# When copy ANSI string from random script make sure to replace "\033" and "\e" to ""
# or wrap it in `$(printf '%b' "<ansi_string>")`.
#
# URL: https://github.com/info-mono/diyfetch
# Prepare ------------------------------------------------------------------------------------------
# You can lookup the color codes on https://wikipedia.org/wiki/ANSI_escape_code#8-bit
main_color=4
# You can add some eye candy icons with Emoji of use Nerd Fonts (https://www.nerdfonts.com).
info=$(gum style "[1;38;5;${main_color}m${USER}@[1;38;5;${main_color}m$(hostname)
----------------
[1;38;5;${main_color}mOS: <your_os>
[1;38;5;${main_color}mKERNEL: $(uname -sr)
[1;38;5;${main_color}mUPTIME: $(uptime -p | cut -c 4-)
[1;38;5;${main_color}mSHELL: $(basename "${SHELL}")
[1;38;5;${main_color}mEDITOR: $(basename "${EDITOR:-<your_editor>}")
[1;38;5;${main_color}mDE: <your_de>
[1;38;5;${main_color}mWM: <your_wm>
[1;38;5;${main_color}mTERMINAL: <your_terminal>")
# You can get OS arts on https://github.com/info-mono/os-ansi
# copy the raw data of the .ansi file then paste it down below.
art=$(gum style ' ___
(.. |
(<> |
/ __ \
( / \/ |
_/\ __)/_)
\/-____\/')
# You can generate colorstrip using https://github.com/info-mono/colorstrip
color=$(gum style '████████████████████████
████████████████████████')
# Display ------------------------------------------------------------------------------------------
# The code in this section is to display the fetch adaptively to the terminal's size.
# If you just want a static fetch display, you can just use something like this:
#
# group_info_color=$(gum join --vertical "${info}" '' "${color}")
# gum join --horizontal --align center ' ' "${art}" ' ' "${group_info_color}"
terminal_size=$(stty size)
terminal_height=${terminal_size% *}
terminal_width=${terminal_size#* }
# Acknowledge of how high the shell prompt is so the prompt don't push the fetch out.
prompt_height=${PROMPT_HEIGHT:-1}
print_test() {
no_color=$(printf '%b' "${1}" | sed -e 's/\x1B\[[0-9;]*[JKmsu]//g')
[ "$(printf '%s' "${no_color}" | wc --lines)" -gt $(( terminal_height - prompt_height )) ] && return 1
[ "$(printf '%s' "${no_color}" | wc --max-line-length)" -gt "${terminal_width}" ] && return 1
gum style --align center --width="${terminal_width}" "${1}" ''
printf '%b' "\033[A"
exit 0
}
# Paper layout
print_test "$(gum join --vertical --align center "${art}" '' "${info}" '' "${color}")"
# Classic layout
group_info_color=$(gum join --vertical "${info}" '' "${color}")
print_test "$(gum join --horizontal --align center "${art}" ' ' "${group_info_color}")"
# Hybrid layout
group_art_info=$(gum join --horizontal --align center "${art}" ' ' "${info}")
print_test "$(gum join --vertical --align center "${group_art_info}" '' "${color}")"
# Other layout
print_test "$(gum join --vertical --align center "${art}" '' "${info}")"
print_test "${group_art_info}"
print_test "${group_info_color}"
print_test "${info}"
exit 1