I am using the following bash function to print a multiline strings in colour.
desc="
Syp:
{-W,-w,--wht}
-f FILE
Print Syp brief
Red:
Some red details here.
Code:
Some code here
More code
Red:
Some red details here.
Blu:
Some blue details here.
Some blue details here.
Mgn:
Magonta
Cyn:
Some details here.
Code:
Code here
More code
Rst:
Wore"
fire "$desc"
Things work as expected when using Wht:, Grn:, Blu:, Ylw:, Red:, Amb:, Cyn:, Mgn: but not when using Syp: and Code:. I have difficulty understanding how they would behave differently.
Syp: and Code: are getting called by !kl.
fire ()
{
awk 'BEGIN { kl=0
("tput sgr0") |& getline rst
ka = "Wht 15 Grn 34 Blu 39 Ylw 11 Red 196 Amb 214"
kb = "Cyn 51 Mgn 201 Syp 196 Code 196"
ks = sprintf("%s %s", ka, kb)
n = split(ks, kaggr)
for ( i=1; i<n; i+=2 ) {
knam = kaggr[i] ":"
knum = kaggr[i+1]
("tput bold; tput setaf " knum) |& getline tseq[knam]
}
}
($1 in tseq) { kl=1 ; ctp=$1 ; next }
/Rst:/ { kl=0 ; next }
kl { printf("%s%s%s\n", tseq[ctp], $0, rst) }
!kl { printf("%s\n", $0) }
' <<< "$@"
}
("tput sgr0") |& getline rst, etc. - you're still using a coprocess unnecessarily, not closing it when done, and not checking forgetlinefailure as I showed and discussed in my previous answer](unix.stackexchange.com/a/735256/133219). Also,ks = sprintf("%s %s", ka, kb)=ks = ka " " kb, no need to complicate it. You don't need to do that, though, you could just use a backsalash at the end of the first line to define a string across 2 lines if you prefer that over defining a single long string.Syp:andCode:are getting called by!kl. There should not be anygetlinefailure for"tput sgr0".getlinecan fail so just write your code to handle it if/when it does - I already showed you how to do that, its just a few more characters.klis set to1in your code, it cannot be reset to0untilRst:is seen in your input so it's impossible forklto be1for the key strings aboveCode:and then somehow get set to0again (making!kltrue) whenCode:is present, even if you did have control chars in your input so that rules that out.