Last active
September 12, 2023 19:15
-
-
Save linuxrootok/ae08e12bfb73ad3ddfdd6a065c079197 to your computer and use it in GitHub Desktop.
[shell output the table] #shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
sep="#" | |
# write the table border | |
function segmentation(){ | |
local seg="" | |
local i | |
for i in $(seq $column_count) | |
do | |
seg+="+${sep}" | |
done | |
seg+="+\n" | |
echo $seg | |
} | |
# set_title Column_0 "Column 1" "" Column3 | |
function set_title(){ | |
[ -n "$title" ] && echo "Warring:表头已经定义过,重写表头和内容" | |
column_count=0 | |
title="" | |
local i | |
for i in "$@" | |
do | |
title+="|${i}${sep}" | |
let column_count++ | |
done | |
title+="|\n" | |
seg=`segmentation` | |
title="${seg}${title}${seg}" | |
content="" | |
} | |
# 表格追加行 | |
function append_line(){ | |
check_line | |
line="" | |
local i | |
for i in "$@" | |
do | |
line+="|$i${sep}" | |
done | |
check_line | |
line="" | |
} | |
# 检查行 | |
function check_line(){ | |
if [ -n "$line" ] | |
then | |
c_c=$(echo $line|tr -cd "${sep}"|wc -c) | |
difference=$((${column_count}-${c_c})) | |
if [ $difference -gt 0 ] | |
then | |
line+=$(seq -s " " $difference|sed -r s/[0-9]\+/\|${sep}/g|sed -r s/${sep}\ /${sep}/g) | |
fi | |
content+="${line}|\n" | |
fi | |
} | |
# 输出表格内容 | |
function output_table(){ | |
if [ ! -n "${title}" ] | |
then | |
echo "未设置表头,退出" && return 1 | |
fi | |
table="${title}${content}$(segmentation)" | |
echo -e $table|column -s "${sep}" -t|awk '{if($0 ~ /^+/){gsub(" ","-",$0);print $0}else{gsub("\\(\\*\\)","\033[31m(*)\033[0m",$0);print $0}}' | |
} | |
if [ "$SHLVL" -eq "2" ] | |
then | |
set_title ID Name "Creation time" | |
append_line 1 "TF" "2017-01-01" | |
append_line 2 "" "2017-01-02(*)" | |
append_line 3 "SF" | |
append_line 3 "SF" "(*)" | |
append_line 4 "TS" | |
append_line 5 | |
output_table | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment