How to get the number of rows available (unused) in a terminal?
Clash 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.
shell terminal
add a comment |Â
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.
shell terminal
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 theterminfo
database, and in thencurses
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
add a comment |Â
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.
shell terminal
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.
shell terminal
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 theterminfo
database, and in thencurses
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
add a comment |Â
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 theterminfo
database, and in thencurses
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Nov 12 '17 at 5:50
fpmurphy1
2,231915
2,231915
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 thencurses
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