klabels can now filter key & value
This commit is contained in:
parent
b19df9bdf3
commit
47afa44ed3
@ -11,6 +11,8 @@ function klabels {
|
|||||||
local max_label_length=0
|
local max_label_length=0
|
||||||
local key=
|
local key=
|
||||||
local value=
|
local value=
|
||||||
|
local filter_value="$1"
|
||||||
|
local filter_key="$2"
|
||||||
|
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
if [ -z "$header" ]; then
|
if [ -z "$header" ]; then
|
||||||
@ -32,20 +34,30 @@ function klabels {
|
|||||||
max_key_length=$(( ${#key} > max_key_length ? ${#key} : max_key_length ))
|
max_key_length=$(( ${#key} > max_key_length ? ${#key} : max_key_length ))
|
||||||
done
|
done
|
||||||
|
|
||||||
# Print a line separator based on the longest key length
|
|
||||||
separator_length=$((max_key_length + trim_length))
|
|
||||||
printf "%${separator_length}s\n" | tr ' ' "="
|
|
||||||
|
|
||||||
# Loop through labels and align them
|
# Loop through labels and align them
|
||||||
for label in "${labels[@]}"; do
|
for label in "${labels[@]}"; do
|
||||||
key="${label%%=*}"
|
key="${label%%=*}"
|
||||||
value="${label#*=}"
|
value="${label#*=}"
|
||||||
|
|
||||||
|
if [ -n "$filter_key" ] && ! [[ "$key" == *$filter_key* ]] ; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$filter_value" ] && ! [[ "$value" == *$filter_value* ]] ; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Format the label to align the '=' sign
|
# Format the label to align the '=' sign
|
||||||
printf -v formatted_label "%-${max_key_length}s = %s" "$key" "$value"
|
printf -v formatted_label "%-${max_key_length}s = %s" "$key" "$value"
|
||||||
|
|
||||||
if [ -z "$first_line" ]; then
|
if [ -z "$first_line" ]; then
|
||||||
first_line=1
|
first_line=1
|
||||||
|
|
||||||
|
# Print a line separator based on the longest key length
|
||||||
|
separator_length=$((max_key_length + trim_length))
|
||||||
|
printf "%${separator_length}s\n" | tr ' ' "-"
|
||||||
|
|
||||||
echo "$line_text$formatted_label"
|
echo "$line_text$formatted_label"
|
||||||
else
|
else
|
||||||
echo "$padding$formatted_label"
|
echo "$padding$formatted_label"
|
||||||
|
Loading…
Reference in New Issue
Block a user