1

I want to print logs with colored keywords. Previously I used:

cd ~/Code/Docker/somerepo && docker-compose logs -f my_service \
  | grep -v "Successfully connected" \
  | grep -v "this operation breaks ==" \
  | sed "s/at com\.example\.product/at \x1b[32mcom\.example\.product\x1b[0m/g" \
  | sed "s/caused by/\x1b[31mcaused by\x1b[0m/g"

as a function and it works.

Now I want to extract the color definition and put it into a function named get_color and print_in_color:

get_color() {
    number=
    case "$1" in
        RED)
        number='31'
        ;;
        GREEN)
        number='32'
        ;;
        YELLOW)
        number='33'
        ;;
        BLUE)
        number='34'
        ;;
        PURPLE)
        number='35'
        ;;
        *)
        number='38'
        ;;
    esac
    echo '\x1b['$number'm'
}

# $1 string, $2 color(RED, YELLOW, BLUE, GREEN, PURPLE)
print_in_color() {
    while read data;
    do
        color=$(get_color $2)
        nocolor='\x1b[0m'
        echo $1 | sed "s/$1/${color}$1${nocolor}/g"
    done;
}

And use it like

log_color() {
    cd ~/Code/Docker/somerepo && docker-compose logs -f my_service \
    | grep -v "Successfully connected" \
    | grep -v "this operation breaks ==" \
    | print_in_color 'com\.example\.product' BLUE \
    | print_in_color 'caused by' RED
}

But now it only prints caused by in red.

How could I define a function to return a pipe to pipe into another function?

1 Answer 1

1

Just simplify your print_in_color function:

print_in_color() {
    local -r LINE="${1//\//\\\/}"
    local -r color=$(get_color "$2")
    local -r nocolor='\x1b[0m'
    sed "s/\(${LINE}\)/${color}\1${nocolor}/g"

}

You do not need to loop on input data. The sed command accept data on pipe input and write the result on pipe output so you can chained many print_in_color calls

UPDATE

Without get_color function:

#! /bin/bash

declare -g -r ANSI_CSI="\x1b["
declare -g -r ANSI_RESET="${ANSI_CSI}0m"
declare -g -r -A H_COLORS=(
    [RED]="${ANSI_CSI}31m"
    [GREEN]="${ANSI_CSI}32m"
    [YELLOW]="${ANSI_CSI}33m"
    [BLUE]="${ANSI_CSI}34m"
    [PURPLE]="${ANSI_CSI}35m"
    [OTHER]="${ANSI_CSI}38m"
)

# $1 string, $2 color(RED, YELLOW, BLUE, GREEN, PURPLE)
print_in_color() {
    local -r LINE="${1//\//\\\/}"
    local -r COLOR=${H_COLORS[${2:-OTHER}]}
    sed "s/\(${LINE}\)/${COLOR:-${H_COLORS[OTHER]}}\1${ANSI_RESET}/g"
}
5
  • Fast and to the point! Thanks!
    – WesternGun
    Jul 4, 2022 at 10:33
  • 1
    Note that this will fail if $1 contains /. Not sure if this is a problem, of course.
    – terdon
    Jul 4, 2022 at 11:13
  • yes... well I will try to not use "/" in the text to render; or just escape it beforehand.
    – WesternGun
    Jul 4, 2022 at 11:21
  • 1
    @WesternGun fair enough, it's just a limitation to be aware of. You could also use a different delimiter if you know of a character that will never be used as part of $1. Something like sed 's%foo%bar% for example. Any character will do.
    – terdon
    Jul 4, 2022 at 11:29
  • Thank you for your feed back. I have fixed my solution Jul 4, 2022 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .