commit:     f3e254127ebad7c8774081e3834653effe1966d0
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue May 14 08:33:09 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed May 15 10:28:30 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f3e25412

Have _eend() remember the cursor position before printing

The act of printing a LF character after the indicator moves the cursor
back to the row beneath - as intended - but also results in the cursor
being positioned at the first column, even though it may have been
elsewhere initially. Address this by using the DECSC sequence to save
the cursor position prior to printing the indicator, then the DECRC
sequence to restore it afterwards. My testing shows no measurable
performance impact.

Consider the following script as a test case.

#!/bin/sh
. ./functions.sh
ebegin Testing
einfon more output
eend 0

Below is a depicted invocation which clearly demonstrates the beneficial
effect of this change.

$ /.testcase; printf done
 * Testing ...                                                    [ ok ]
 * more outputdone$ ▉

Whereas, previously, the outcome would have been as shown below.

$ /.testcase; printf done
 * Testing ...                                                    [ ok ]
done$ ▉ output

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 functions.sh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/functions.sh b/functions.sh
index 6d77f59..7bd4b11 100644
--- a/functions.sh
+++ b/functions.sh
@@ -227,10 +227,10 @@ _eend()
        fi
 
        if [ "${genfun_tty}" -eq 2 ]; then
-               # Move the cursor up by one line with CUU before positioning it
-               # horizontally with CHA. Both are formal ECMA-48 CSI sequences.
-               # Print the indicator afterwards.
-               printf '\033[1A\033[%dG %s\n' "$(( genfun_cols - 6 + 
genfun_offset ))" "${msg}"
+               # Save the cursor position with DECSC, move it up by one line
+               # with CUU, position it horizontally with CHA, print the
+               # indicator, then restore the cursor position with DECRC.
+               printf '\0337\033[1A\033[%dG %s\0338' "$(( genfun_cols - 6 + 
genfun_offset ))" "${msg}"
        else
                # The standard output does not refer to a sufficiently capable
                # terminal. Print only the indicator.

Reply via email to