完成afetch

master
yutent 2024-01-08 14:47:14 +08:00
parent e3893a8511
commit 75ff0b9af3
1 changed files with 135 additions and 58 deletions

185
src/fetch
View File

@ -15,16 +15,16 @@ _cbd=$(tput bold) # 加粗
_cnc=$(tput sgr0) # 重置(结束) _cnc=$(tput sgr0) # 重置(结束)
# 一些变量 # 一些变量
FULL=─ FULL=""
EMPTY=─ EMPTY=""
IN_DOCKER='N' IN_DOCKER='N'
ARCH_NAME='amd64' ARCH_NAME='amd64'
name=$USER
host=$(uname -n) host=$(uname -n)
distro=$(uname -o | awk -F '"' '/PRETTY_NAME/ { print $2 }' /etc/os-release) distro=$(uname -o | awk -F '"' '/PRETTY_NAME/ { print $2 }' /etc/os-release)
distro_version=$(lsb_release -sr)
packages=$(apt list --installed | wc -l) packages=$(apt list --installed | wc -l)
wm=$(xprop -id $(xprop -root -notype | awk '$1=="_NET_SUPPORTING_WM_CHECK:"{print $5}') -notype -f _NET_WM_NAME 8t | grep "WM_NAME" | cut -f2 -d \") wm=$(xprop -id $(xprop -root -notype | awk '$1=="_NET_SUPPORTING_WM_CHECK:"{print $5}') -notype -f _NET_WM_NAME 8t | grep "WM_NAME" | cut -f2 -d \")
storage=$(df -h --output=used,size / | awk 'NR == 2 { print $1,"/",$2 }') storage=$(df -h --output=used,size / | awk 'NR == 2 { print $1,"/",$2 }')
@ -32,7 +32,7 @@ term=$(ps -o sid= -p "$$" | xargs ps -o ppid= -p | xargs ps -o comm= -p)
uptm=$(uptime -p | sed -e 's/up //') uptm=$(uptime -p | sed -e 's/up //')
check_is_docker(){ function check_is_docker(){
if [ -f /.dockerenv ]; then if [ -f /.dockerenv ]; then
IN_DOCKER="Y" IN_DOCKER="Y"
elif [ -f /proc/cmdline ]; then elif [ -f /proc/cmdline ]; then
@ -42,8 +42,58 @@ check_is_docker(){
fi fi
} }
function pad_start() {
input_string=$1
max_len=$2
while [ ${#input_string} -lt $max_len ]
do
input_string=" ${input_string}"
done
echo -n "$input_string"
}
function pad_end() {
input_string=$1
max_len=$2
while [ ${#input_string} -lt $max_len ]
do
input_string+=" "
done
echo -n "$input_string"
}
# progress bar
function draw() {
perc=$1
size=20
inc=$((perc * size / 100))
out=
for v in $(seq 0 $((size - 1))); do
test "$v" -le "$inc" &&
out="${out}${_ccy}${_cbd}${FULL}" ||
out="${out}${_cwh}${EMPTY}"
done
out+="${_cnc}"
echo -n $out
}
function trim() {
str=$1
str="${str#"${str%%[![:space:]]*}"}" # 前空格
str="${str%"${str##*[![:space:]]}"}" # 后空格
echo -n "$str"
}
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# 获取设备信息 # 获取设备信息
get_model() { function get_model() {
model="" model=""
if [[ -d /system/app/ && -d /system/priv-app ]]; then if [[ -d /system/app/ && -d /system/priv-app ]]; then
model="$(getprop ro.product.brand) $(getprop ro.product.model)" model="$(getprop ro.product.brand) $(getprop ro.product.model)"
@ -65,13 +115,11 @@ get_model() {
else else
model="@${host}" model="@${host}"
fi fi
else
model="@${model}"
fi fi
} }
# 获取debian的内核版本 # 获取debian的内核版本
get_kernel() { function get_kernel() {
if [ $IN_DOCKER == "Y" ]; then if [ $IN_DOCKER == "Y" ]; then
kernel_version="$(uname -r)-${ARCH_NAME}" kernel_version="$(uname -r)-${ARCH_NAME}"
else else
@ -80,7 +128,7 @@ get_kernel() {
fi fi
} }
get_shell(){ function get_shell(){
shell_name=${SHELL##*/} shell_name=${SHELL##*/}
shell_version=" " shell_version=" "
@ -97,36 +145,60 @@ get_shell(){
;; ;;
esac esac
shell_version=${shell_version/\(*\)}
} }
# progress bar function get_cpu() {
draw() { cpu=$(lscpu | awk -F '\\s*: | @' '/Model name/ {print $2}')
perc=$1 cpu_cores=$(lscpu | awk -F '\\s*: | @' '/^CPU\(s\):/ {print $2}')
size=$2 cpu_speed=$(lscpu | awk -F '\\s*: | @' '/CPU max MHz/ {print $2}')
inc=$((perc * size / 100))
out= cpu_cores=$(trim "$cpu_cores")
color="$3" cpu_speed=$(trim "$cpu_speed")
for v in $(seq 0 $((size - 1))); do cpu_speed=${cpu_speed%.*}
test "$v" -le "$inc" && cpu_speed="$((cpu_speed / 1000)).${cpu_speed:1}"
out="${out}\e[1;${color}m${FULL}" ||
out="${out}\e[0;37m${CWH}${EMPTY}" cpu=$(trim "$cpu" | sed -e 's/ Gen//')
done cpu+=" (${cpu_cores}) ${cpu_speed}GHz"
printf "$out"
} }
free() { function get_gpu() {
printf "0" gpu=$(lspci -mm | awk -F '\"|\" \"|\\(' '/"Display|"3D|"VGA/ { print $(NF-1) }')
} }
function get_ram() {
ram_total=$(free -m | awk '/Mem:/ {printf "%.2fGB", $2/1024}')
ram_used=$(free -m | awk '/Mem:/ {printf "%.2fGB", $3/1024}')
ram_perc=$(free -m | awk '/Mem:/ {print int($3/$2 * 100.0)}')
}
function get_screen() {
if type -p xrandr >/dev/null; then
resolution="$(xrandr --nograb --current | awk 'match($0,/[0-9]*\.[0-9]*\*/) {
printf $1 "@" int($2) ", "}')"
resolution="${resolution:0: -2}"
elif type -p xwininfo >/dev/null; then
read -r w h <<< "$(xwininfo -root | awk -F':' '/Width|Height/ {printf $2}')"
resolution="${w}x${h}"
elif type -p xdpyinfo >/dev/null; then
resolution="$(xdpyinfo | awk '/dimensions:/ {printf $2}')"
fi
}
check_is_docker check_is_docker
get_model get_model
get_kernel get_kernel
get_shell get_shell
get_cpu
get_gpu
ram=$(free | awk '/Mem:/ {print int($3/$2 * 100.0)}') get_ram
get_screen
block_1=" ▓▓ " block_1=" ▓▓ "
@ -134,18 +206,23 @@ block_2=" ▒▒ "
block_3=" ░░ " block_3=" ░░ "
logo_1="${_cre}${block_1}${block_1}${block_1}${block_1}${block_1}${block_1}${_cnc}" logo_1="${_cbd}${_cre}${block_1}${block_1}${block_1}${block_1}${block_1}${block_1}${_cnc}"
logo_2="${_cbl}${block_1}${_cma}${block_1}${block_1}${block_1}${block_1}${_cgr}${block_1}${_cnc}"
logo_3="${_cbl}${block_1}${_cre}${block_1}${_cbk}${block_3}${block_3}${_ccy}${block_1}${_cgr}${block_1}${_cnc}"
logo_4="${_cbl}${block_1}${_cre}${block_1}${_cbk}${block_3}${block_3}${_ccy}${block_1}${_cgr}${block_1}${_cnc}"
logo_5="${_cbl}${block_1}${_cwh}${block_1}${block_1}${block_1}${_ccy}${block_1}${_cgr}${block_1}${_cnc}"
logo_6="${_cye}${block_1}${block_1}${block_1}${block_1}${block_1}${_cgr}${block_1}${_cnc}"
deb_1=" _ _ _ " logo_2="${_cbd}${_cbl}${block_1}${_cma}${block_1}${block_1}${block_1}${block_1}${_cgr}${block_1}${_cnc}"
deb_2=" __| | ___| |__ (_) __ _ _ ___"
deb_3=" / _\` |/ _ \ '_ \| |/ _\` | '_ \ " logo_3=${_cbd}"${_cbl}${block_1}${_cwh}${block_1}${_cbk}${block_3}${block_3}${_ccy}${block_1}${_cgr}${block_1}${_cnc}"
deb_4=" | (_| | __/ |_) | | (_| | | | |"
deb_5=" \__,_|\___|_.__/|_|\__,_|_| |_|" logo_4=${_cbd}"${_cbl}${block_1}${_cwh}${block_1}${_cbk}${block_3}${block_3}${_ccy}${block_1}${_cgr}${block_1}${_cnc}"
logo_5=${_cbd}"${_cbl}${block_1}${_cwh}${block_1}${block_1}${block_1}${_ccy}${block_1}${_cgr}${block_1}${_cnc}"
logo_6=${_cbd}"${_cye}${block_1}${block_1}${block_1}${block_1}${block_1}${_cgr}${block_1}${_cnc}"
deb_1="${_cre}${_cbd} _ _ _ ${_cnc}"
deb_2="${_cre}${_cbd} __| | ___| |__ (_) __ _ _ ___${_cnc}"
deb_3="${_cre}${_cbd} / _\` |/ _ \ '_ \| |/ _\` | '_ \ ${_cnc}"
deb_4="${_cre}${_cbd} | (_| | __/ |_) | | (_| | | | |${_cnc}"
deb_5="${_cre}${_cbd} \__,_|\___|_.__/|_|\__,_|_| |_|${_cnc}"
# 输出前清屏 # 输出前清屏
@ -160,26 +237,26 @@ cat <<EOF
│ │ ${deb_5} │ │ │ ${deb_5} │
│ ${logo_3} │ │ │ ${logo_3} │ │
│ │ │ │ │ │
│ ${logo_4} ├─────────────────────────────────────────────────┤ │ ${logo_4} ├─────────────────────────────────────────────────┤
│ │ │ │ │ │ │ │
│ ${logo_5} │ Host │ ${model} │ ${logo_5} │ ${_cre}${_cbd}Host${_cnc} │ $(pad_end "$model" 35)
│ │ OS │ │ │ ${_cre}${_cbd}OS${_cnc} │ $(pad_end "$distro" 35)
│ ${logo_6} │ Uptime │ ${uptm} │ ${logo_6} │ ${_cre}${_cbd}Uptime${_cnc} │ $(pad_end "$uptm" 35)
│ │ │ │ │ │ │ │
├──────────────────────────┼──────────────┴───────────────────────────────────┤ ├─────────────────┬────────┴──────────┴───────────────────────────────────────┤
│ │ │ │ │ │
${_cbl}Kernel${_cnc} │ ${kernel_version} ${_cre}${_cbd}$(pad_start "Kernel" 13)${_cnc} │ $(pad_end "$kernel_version" 54)
${_cma}Packages${_cnc} │ ${packages} (apt) ${_cre}${_cbd}$(pad_start "Packages" 13)${_cnc} │ $(pad_end "${packages} (apt)" 54)
${_cgr}Shell${_cnc} │ ${shell_name} ${shell_version} │ ${_cre}${_cbd}$(pad_start "Shell" 13)${_cnc} │ $(pad_end "${shell_name} ${shell_version}" 54) │
${_ccy}Resolution${_cnc} │ 2048x1280, 2048x1280 ${_cre}${_cbd}$(pad_start "Resolution" 13)${_cnc} │ $(pad_end "$resolution" 54)
${_cye}WM${_cnc} │ ${wm} ${_cre}${_cbd}$(pad_start "WM" 13)${_cnc} │ $(pad_end "${wm}" 54)
${_cbl}Terminal${_cnc} │ ${term} ${_cre}${_cbd}$(pad_start "Terminal" 13)${_cnc} │ $(pad_end "${term}" 54)
${_cbl}CPU${_cnc} │ ${term} ${_cre}${_cbd}$(pad_start "CPU" 13)${_cnc} │ $(pad_end "${cpu}" 54)
${_cbl}GPU${_cnc} │ ${term} ${_cre}${_cbd}$(pad_start "GPU" 13)${_cnc} │ $(pad_end "${gpu}" 54)
${_cbl}Disk${_cnc} │ $(draw "$ram" 15 35) ${ram}% ${_cre}${_cbd}$(pad_start "Disk" 13)${_cnc} │ $(pad_end "${storage}" 54)
${_cbl}Memory${_cnc} │ $(draw "$ram" 15 35) ${ram}% ${_cre}${_cbd}$(pad_start "Memory" 13)${_cnc} │ $(draw $ram_perc)$(pad_end " ${ram_perc}% (${ram_used} / ${ram_total})" 34)
│ │ │ │ │ │
└────────────────────────────────────────────────────────────────────────────┘ └────────────────────────────────────────────────────────────────────────────┘
EOF EOF