1

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).

Nordine Lotfi
  • 2,200
  • 12
  • 45

1 Answers1

2

Instead of doing this in the status_line function, I would use the draw_line function: it knows what the current line is, so it can be changed to output the line number at the start of each line.

The printf arguments in the shell are close to those in C, including %d to display numbers and the size specifiers you’d probably want to use here. You’d also need to reduce COLUMNS as appropriate, or account for the line number in the width calculations.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • didn't thought of looking more deeply at `draw_line` yet :D. That's a good idea yeah. I did thought of using the fact it was counting the line (like what `status_line` does). Wouldn't that make it inefficient though, if i print the line number for each line, one at a time, instead of all at once? (say, if i already know the last line, then i can do the rest using my example). – Nordine Lotfi Nov 01 '20 at 15:33
  • 1
    I doubt it would make much difference in terms of efficiency, `draw_line` already prints every line one at a time anyway. – Stephen Kitt Nov 01 '20 at 17:01