fidian / ansi

ANSI escape codes in pure bash - change text color, position the cursor, much more

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ansi::insertLines does not insert newlines as expected

mtjrider opened this issue · comments

After calling source ansi, inserting new lines via ansi::insertLines N does not insert any new lines.
Likewise, ansi --insert-lines=N does not insert any new lines.

I'm not sure if I'm using this function correctly, so I provide some concrete examples with precise output below. If you could give me some guidance regarding my use of your scripts, that'd be great.

# using ansi
...$ ansi --insert-lines=5 hello world
hello world
...$ ...
# using ansi::insertLines
...$ ansi::insertLines 5
...$ ...

I am using Ubuntu 22.04 under WSL. My version of bash is provided below. I will also include some potentially useful information regarding ANSI_* variables defined ansi.

...$ echo "$ANSI_ESC"

...$ echo "$ANSI_CSI"

...$ echo "$ANSI_OSC"
...$ echo "$ANSI_ST"

...$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Thanks!
Matthew J

I didn't converse with the person that opened the ticket, but here are a couple useful tips for others.

Displaying the ANSI variables won't work well with echo. Use set instead.

$ set | grep ^ANSI
ANSI_CSI=$'\E['
ANSI_ESC=$'\E'
ANSI_OSC=$'\E]'
ANSI_REPORT=
ANSI_ST=$'\E\\'

Also, --insert-lines will insert lines at the current cursor position. That option difficult to normally see and I understand why this issue was opened. Here's an image showing the cursor being saved near the top of the terminal, then the terminal is filled with content. The cursor position is restored, then --insert-lines is used to move other content down in the terminal window.

ezgif-3-c09e9a707b

Hi @fidian,

Yes, I realized this was user-error, and not a real bug.
I assumed its behavior was to printf a user-provided number of newline characters at the "current cursor position." Something like this:

...$ printf '%0.s\n' $(seq 1 ${num_newlines})

I appreciate your thoughtful and descriptive reply. It was very helpful.