What a way to spend an evening.
As I attempted to rewrite some Gitlab automation from bash to python, I stumbled over what should have been a trivial thing: collapsing section headers. They were not trivial.
In bash the code to do a section header looks like this:
section_start() { name="$1" description="${2:-$name}" echo -e "\e[0Ksection_start:`date +%s`:${name}[collapsed=true]\r\e[0K${description}" } |
But those escape sequences are not valid escape sequences in python. What they turn in to is non-intuitive either. Here is the code I ended up with:
def section_start(name, description=None): if description is None: description = name escape = bytearray([27, 91, 48, 75]) log_date = local["date"]("+%s").strip() message = "section_start:" + log_date + ":" + name + "[collapsed=true]\r" b = escape + message.encode('utf-8') + escape + description.encode('utf-8') sys.stdout.buffer.write(b) print() |
The print call in python accepts on a limited set of escape characters. It turns out that the “\e” in my original echo command is not one of them.
To find out what was actually sent to the screen, I fell back on strace. Specifically, this call:
strace -xx -e trace=write echo -e "\e[0Ksection_start:`date +%s`:name[collapsed=false]\r\e[0Kdescription" 2> /tmp/trace.txt |
The output from that was:
write(1, "\x1b\x5b\x30\x4b\x73\x65\x63....
The \x1b became the 27 in my program: the ascii escape character.
With that, when I print output to the screen, the name portion is covered up and I only see the description. Since this is what happens with the bash code, I think I have something that will work on gitlab.