Skip to content

Commit cc1bae1

Browse files
author
farfarfun
committed
refactor: replace _print_table with csv/tab functions for status output
1 parent 0e3505f commit cc1bae1

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

scripts/services/nlt-services.sh

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,39 @@ http_probe() {
9090
fi
9191
}
9292

93-
# 制表符行 → column -t 对齐(无 column 时原样输出)
94-
_print_table() {
93+
# CSV 单元格(RFC 风格双引号,避免内容含逗号时错乱)
94+
_csv_cell() {
95+
local x="${1//\"/\"\"}"
96+
printf '"%s"' "$x"
97+
}
98+
99+
_status_csv_line() {
100+
printf '%s,%s,%s,%s,%s\n' \
101+
"$(_csv_cell "$1")" "$(_csv_cell "$2")" "$(_csv_cell "$3")" "$(_csv_cell "$4")" "$(_csv_cell "$5")"
102+
}
103+
104+
# Tab 行 → column -t(无 gum 时的回退)
105+
_status_tsv_line() {
106+
printf '%s\t%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4" "$5"
107+
}
108+
109+
# stdin 为 CSV → gum table -p;无 gum 时勿调用本函数,应改送 TSV 给 column
110+
_render_status_table_from_csv() {
111+
PATH="${HOME}/opt/gum/bin:${PATH}"
112+
command -v gum >/dev/null 2>&1 || return 1
113+
gum table -p -s ',' \
114+
--columns '服务,状态,PID,端口/访问,HTTP' \
115+
--border rounded
116+
}
117+
118+
_print_status_rows_column() {
95119
if command -v column >/dev/null 2>&1; then
96120
column -t -s $'\t'
97121
else
98122
cat
99123
fi
100124
}
101125

102-
# 单行五列(服务、状态、PID、端口/访问、HTTP),字段内勿含制表符
103-
_status_row() {
104-
printf '%s\t%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$4" "$5"
105-
}
106-
107126
_mark_alive() {
108127
proc_alive "$1" && printf '%s' '' || printf '%s' '×'
109128
}

0 commit comments

Comments
 (0)