Sample Header Ad - 728x90

How to show a line number in bash TUI?

1 vote
1 answer
398 views
I'm trying to add support for line number on a bash project i like (mainly as a fun exercise). To start, i looked into the function that was setting up the status line (since i thought of inspiring myself from it, since it would be printing a constant line, at the bottom of the window, horizontally):
status_line() {
    # '\e7'        : Save cursor position.
    #                This is more widely supported than '\e[s'.
    # '\e[%sH'     : Move cursor to bottom of the terminal.
    # '\e[30;41m'  : Set foreground and background colors.
    # '%*s'        : Insert enough spaces to fill the screen width.
    #                This sets the background color to the whole line
    #                and fixes issues in 'screen' where '\e[K' doesn't work.
    # '\r'         : Move cursor back to column 0 (was at EOL due to above).
    # '\e[m'       : Reset text formatting.
    # '\e[%sH\e[K' : Clear line below status_line.
    # '\e8'        : Restore cursor position.
    #                This is more widely supported than '\e[u'.

    buffer_name="${file_name:-New Buffer}"
    (( modified == 1 )) && buffer_name+="*"
    counter="Ln $((file_line+1)), Col $((file_column+1))"

    printf "\e7\e[%sH%s%*s%s\e[%sH\e[K\e8"\
            "$((LINES-1))"\
            "[$buffer_name]"\
            "$((COLUMNS-${#buffer_name}-${#counter}-4))" ""\
            "[$counter]"\
            "$LINES"

}
A part of the project's code. Now I'm aware i could just use \n and replace the H in the printf statements to something that would make it work vertically (since i need a line number, it would obviously be preferable *vertically*), but I don't know much about printf syntax (in this context, not talking about the C function, but what is used in shell script). Any way i could do it here vertically? (don't necessarily need the full feature made, just some indication/hint or implementation that work without doing a for-loop/loop). I'm aware i could just do something along the line of:
printf '%s\n' {1..10}
But I'm not sure how to make it work horizontally, without rewriting/writing over the text displayed when a file is opened (contrary to what the status line is doing, which work as a separate text object, from my own understanding).
Asked by Nordine Lotfi (2472 rep)
Nov 1, 2020, 03:09 PM
Last activity: Nov 1, 2020, 03:28 PM