How to get the number of rows available (unused) in a terminal?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I can get the total number of rows and columns with stty or tput, but how can I get either the number of rows available (not used) or the index/number of the current row/line in bash or any other shell?



Example:



$ ls
foo bar baz
$ (cursor is here)
.
.


This terminal has 5 rows. The "current row" is 3 and the number of available rows/blank lines after the cursor is 2.







share|improve this question


















  • 2




    What exactly do you mean by available (not used)? Do you mean blank lines below the cursor? It would also be useful to show what you've already researched so far.
    – Anthony Geoghegan
    Nov 11 '17 at 18:57










  • As per this part of your question: "* ... the number of rows available... of the current row ...*": what is your definition of "current"? I.e. isn't that like "the row being processed"? If so, then that is always 1. Must be me who doesn't get it ...
    – Pierre.Vriens
    Nov 11 '17 at 18:59










  • Anthony Geoghegan: yes, that's what I need. Pierre.Vriens: I've added an example.
    – snahor
    Nov 11 '17 at 20:16







  • 2




    Depends on the terminal, obviously. Nowaday, most people say "terminal" when they mean a "terminal emulator", and most terminal emulators are more or less compatible with Xterm. Read the escape sequences understood by Xterm. You may also be interested in the terminfo database, and in the ncurses library. And anyway, 99.9% of the time the cursor is on the last line of the terminal...
    – AlexP
    Nov 11 '17 at 20:16











  • @AlexP thanks! If you post your comment as an answer, I'll accept it.
    – snahor
    Nov 11 '17 at 20:56














up vote
-1
down vote

favorite












I can get the total number of rows and columns with stty or tput, but how can I get either the number of rows available (not used) or the index/number of the current row/line in bash or any other shell?



Example:



$ ls
foo bar baz
$ (cursor is here)
.
.


This terminal has 5 rows. The "current row" is 3 and the number of available rows/blank lines after the cursor is 2.







share|improve this question


















  • 2




    What exactly do you mean by available (not used)? Do you mean blank lines below the cursor? It would also be useful to show what you've already researched so far.
    – Anthony Geoghegan
    Nov 11 '17 at 18:57










  • As per this part of your question: "* ... the number of rows available... of the current row ...*": what is your definition of "current"? I.e. isn't that like "the row being processed"? If so, then that is always 1. Must be me who doesn't get it ...
    – Pierre.Vriens
    Nov 11 '17 at 18:59










  • Anthony Geoghegan: yes, that's what I need. Pierre.Vriens: I've added an example.
    – snahor
    Nov 11 '17 at 20:16







  • 2




    Depends on the terminal, obviously. Nowaday, most people say "terminal" when they mean a "terminal emulator", and most terminal emulators are more or less compatible with Xterm. Read the escape sequences understood by Xterm. You may also be interested in the terminfo database, and in the ncurses library. And anyway, 99.9% of the time the cursor is on the last line of the terminal...
    – AlexP
    Nov 11 '17 at 20:16











  • @AlexP thanks! If you post your comment as an answer, I'll accept it.
    – snahor
    Nov 11 '17 at 20:56












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I can get the total number of rows and columns with stty or tput, but how can I get either the number of rows available (not used) or the index/number of the current row/line in bash or any other shell?



Example:



$ ls
foo bar baz
$ (cursor is here)
.
.


This terminal has 5 rows. The "current row" is 3 and the number of available rows/blank lines after the cursor is 2.







share|improve this question














I can get the total number of rows and columns with stty or tput, but how can I get either the number of rows available (not used) or the index/number of the current row/line in bash or any other shell?



Example:



$ ls
foo bar baz
$ (cursor is here)
.
.


This terminal has 5 rows. The "current row" is 3 and the number of available rows/blank lines after the cursor is 2.









share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '17 at 20:10

























asked Nov 11 '17 at 18:48









snahor

1022




1022







  • 2




    What exactly do you mean by available (not used)? Do you mean blank lines below the cursor? It would also be useful to show what you've already researched so far.
    – Anthony Geoghegan
    Nov 11 '17 at 18:57










  • As per this part of your question: "* ... the number of rows available... of the current row ...*": what is your definition of "current"? I.e. isn't that like "the row being processed"? If so, then that is always 1. Must be me who doesn't get it ...
    – Pierre.Vriens
    Nov 11 '17 at 18:59










  • Anthony Geoghegan: yes, that's what I need. Pierre.Vriens: I've added an example.
    – snahor
    Nov 11 '17 at 20:16







  • 2




    Depends on the terminal, obviously. Nowaday, most people say "terminal" when they mean a "terminal emulator", and most terminal emulators are more or less compatible with Xterm. Read the escape sequences understood by Xterm. You may also be interested in the terminfo database, and in the ncurses library. And anyway, 99.9% of the time the cursor is on the last line of the terminal...
    – AlexP
    Nov 11 '17 at 20:16











  • @AlexP thanks! If you post your comment as an answer, I'll accept it.
    – snahor
    Nov 11 '17 at 20:56












  • 2




    What exactly do you mean by available (not used)? Do you mean blank lines below the cursor? It would also be useful to show what you've already researched so far.
    – Anthony Geoghegan
    Nov 11 '17 at 18:57










  • As per this part of your question: "* ... the number of rows available... of the current row ...*": what is your definition of "current"? I.e. isn't that like "the row being processed"? If so, then that is always 1. Must be me who doesn't get it ...
    – Pierre.Vriens
    Nov 11 '17 at 18:59










  • Anthony Geoghegan: yes, that's what I need. Pierre.Vriens: I've added an example.
    – snahor
    Nov 11 '17 at 20:16







  • 2




    Depends on the terminal, obviously. Nowaday, most people say "terminal" when they mean a "terminal emulator", and most terminal emulators are more or less compatible with Xterm. Read the escape sequences understood by Xterm. You may also be interested in the terminfo database, and in the ncurses library. And anyway, 99.9% of the time the cursor is on the last line of the terminal...
    – AlexP
    Nov 11 '17 at 20:16











  • @AlexP thanks! If you post your comment as an answer, I'll accept it.
    – snahor
    Nov 11 '17 at 20:56







2




2




What exactly do you mean by available (not used)? Do you mean blank lines below the cursor? It would also be useful to show what you've already researched so far.
– Anthony Geoghegan
Nov 11 '17 at 18:57




What exactly do you mean by available (not used)? Do you mean blank lines below the cursor? It would also be useful to show what you've already researched so far.
– Anthony Geoghegan
Nov 11 '17 at 18:57












As per this part of your question: "* ... the number of rows available... of the current row ...*": what is your definition of "current"? I.e. isn't that like "the row being processed"? If so, then that is always 1. Must be me who doesn't get it ...
– Pierre.Vriens
Nov 11 '17 at 18:59




As per this part of your question: "* ... the number of rows available... of the current row ...*": what is your definition of "current"? I.e. isn't that like "the row being processed"? If so, then that is always 1. Must be me who doesn't get it ...
– Pierre.Vriens
Nov 11 '17 at 18:59












Anthony Geoghegan: yes, that's what I need. Pierre.Vriens: I've added an example.
– snahor
Nov 11 '17 at 20:16





Anthony Geoghegan: yes, that's what I need. Pierre.Vriens: I've added an example.
– snahor
Nov 11 '17 at 20:16





2




2




Depends on the terminal, obviously. Nowaday, most people say "terminal" when they mean a "terminal emulator", and most terminal emulators are more or less compatible with Xterm. Read the escape sequences understood by Xterm. You may also be interested in the terminfo database, and in the ncurses library. And anyway, 99.9% of the time the cursor is on the last line of the terminal...
– AlexP
Nov 11 '17 at 20:16





Depends on the terminal, obviously. Nowaday, most people say "terminal" when they mean a "terminal emulator", and most terminal emulators are more or less compatible with Xterm. Read the escape sequences understood by Xterm. You may also be interested in the terminfo database, and in the ncurses library. And anyway, 99.9% of the time the cursor is on the last line of the terminal...
– AlexP
Nov 11 '17 at 20:16













@AlexP thanks! If you post your comment as an answer, I'll accept it.
– snahor
Nov 11 '17 at 20:56




@AlexP thanks! If you post your comment as an answer, I'll accept it.
– snahor
Nov 11 '17 at 20:56










1 Answer
1






active

oldest

votes

















up vote
3
down vote













Try this:



#!/bin/bash

if ! termios="$(stty -g 2>/dev/null)"
then
echo "ERROR: Not running in a terminal"
exit 1
fi


# Get max rows and columns
maxrows=$(tput lines)
maxcols=$(tput cols)

# Disable ICANON ECHO
stty -icanon -echo

# Get cursor position
tput u7
read -d "R" rowcol

# Revert to original settings
stty "$termios"

# clean up response
rowcol="$rowcol//[^0-9;]/"
rowcol="$rowcol//;/ "

printf 'maxrows: %d maxcols: %d currow: %d curcol: %dn' $maxrows $maxcols $rowcol[0] $rowcol[1]

exit 0





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403935%2fhow-to-get-the-number-of-rows-available-unused-in-a-terminal%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    Try this:



    #!/bin/bash

    if ! termios="$(stty -g 2>/dev/null)"
    then
    echo "ERROR: Not running in a terminal"
    exit 1
    fi


    # Get max rows and columns
    maxrows=$(tput lines)
    maxcols=$(tput cols)

    # Disable ICANON ECHO
    stty -icanon -echo

    # Get cursor position
    tput u7
    read -d "R" rowcol

    # Revert to original settings
    stty "$termios"

    # clean up response
    rowcol="$rowcol//[^0-9;]/"
    rowcol="$rowcol//;/ "

    printf 'maxrows: %d maxcols: %d currow: %d curcol: %dn' $maxrows $maxcols $rowcol[0] $rowcol[1]

    exit 0





    share|improve this answer
























      up vote
      3
      down vote













      Try this:



      #!/bin/bash

      if ! termios="$(stty -g 2>/dev/null)"
      then
      echo "ERROR: Not running in a terminal"
      exit 1
      fi


      # Get max rows and columns
      maxrows=$(tput lines)
      maxcols=$(tput cols)

      # Disable ICANON ECHO
      stty -icanon -echo

      # Get cursor position
      tput u7
      read -d "R" rowcol

      # Revert to original settings
      stty "$termios"

      # clean up response
      rowcol="$rowcol//[^0-9;]/"
      rowcol="$rowcol//;/ "

      printf 'maxrows: %d maxcols: %d currow: %d curcol: %dn' $maxrows $maxcols $rowcol[0] $rowcol[1]

      exit 0





      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        Try this:



        #!/bin/bash

        if ! termios="$(stty -g 2>/dev/null)"
        then
        echo "ERROR: Not running in a terminal"
        exit 1
        fi


        # Get max rows and columns
        maxrows=$(tput lines)
        maxcols=$(tput cols)

        # Disable ICANON ECHO
        stty -icanon -echo

        # Get cursor position
        tput u7
        read -d "R" rowcol

        # Revert to original settings
        stty "$termios"

        # clean up response
        rowcol="$rowcol//[^0-9;]/"
        rowcol="$rowcol//;/ "

        printf 'maxrows: %d maxcols: %d currow: %d curcol: %dn' $maxrows $maxcols $rowcol[0] $rowcol[1]

        exit 0





        share|improve this answer












        Try this:



        #!/bin/bash

        if ! termios="$(stty -g 2>/dev/null)"
        then
        echo "ERROR: Not running in a terminal"
        exit 1
        fi


        # Get max rows and columns
        maxrows=$(tput lines)
        maxcols=$(tput cols)

        # Disable ICANON ECHO
        stty -icanon -echo

        # Get cursor position
        tput u7
        read -d "R" rowcol

        # Revert to original settings
        stty "$termios"

        # clean up response
        rowcol="$rowcol//[^0-9;]/"
        rowcol="$rowcol//;/ "

        printf 'maxrows: %d maxcols: %d currow: %d curcol: %dn' $maxrows $maxcols $rowcol[0] $rowcol[1]

        exit 0






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '17 at 5:50









        fpmurphy1

        2,231915




        2,231915



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403935%2fhow-to-get-the-number-of-rows-available-unused-in-a-terminal%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?