LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to find out width of terminal (https://www.linuxquestions.org/questions/programming-9/how-to-find-out-width-of-terminal-796592/)

MTK358 03-19-2010 07:14 PM

How to find out width of terminal
 
I wonder how CLI applications know the width of the terminal. For example, how does ls know how many columns to use?

How do package managers like pacman know what length to make the download progress bar?

Also, how do they redraw the progress bar in the same line?

Sergei Steshenko 03-19-2010 07:24 PM

Quote:

Originally Posted by MTK358 (Post 3905067)
I wonder how CLI applications know the width of the terminal. For example, how does ls know how many columns to use?

How do package managers like pacman know what length to make the download progress bar?

Also, how do they redraw the progress bar in the same line?

man 1 stty
man 2 stty

, etc.

MTK358 03-20-2010 09:10 AM

That didn't really help.

Also, I want to be able to do this from C, Perl, Python, etc.

I was thinking more like ANSI escape codes, I know that there is a code to set the cursor position, isn't there some escape code that tells the terminal to report back it's size?

Sergei Steshenko 03-20-2010 09:33 AM

Quote:

Originally Posted by MTK358 (Post 3905520)
That didn't really help.

Also, I want to be able to do this from C, Perl, Python, etc.

I was thinking more like ANSI escape codes, I know that there is a code to set the cursor position, isn't there some escape code that tells the terminal to report back it's size?

On my machine in a 'konsole' session:


Code:

sergei@amdam2:~> stty -a
speed 38400 baud; rows 69; columns 174; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff -iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
sergei@amdam2:~>

- what else do you need and what prevents you from parsing the output in C/Perl/Python ?

MTK358 03-20-2010 09:49 AM

Good, but can stty set the cursor position (not that it really matters, because I know how to do it using ANSI codes).

ANd is it possible to get this info without executing an external program?

Sergei Steshenko 03-20-2010 11:59 AM

Quote:

Originally Posted by MTK358 (Post 3905548)
Good, but can stty set the cursor position (not that it really matters, because I know how to do it using ANSI codes).

ANd is it possible to get this info without executing an external program?

Do some web searching, really.

CoderMan 03-22-2010 05:28 PM

Quote:

Originally Posted by MTK358 (Post 3905520)
That didn't really help.

Also, I want to be able to do this from C, Perl, Python, etc.

I was thinking more like ANSI escape codes, I know that there is a code to set the cursor position, isn't there some escape code that tells the terminal to report back it's size?

Are you interested in the actual programming part? libncurses is pretty easy to learn, in my experience:

http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

And there are ncurses wrappers in Perl, Python, and heck, probably every other language ever written.

MTK358 03-22-2010 07:06 PM

I am not talking about applications that make text "GUI"s.

I am talking about applications that draw progress bars in the sommand line, like most package managers do, or wget, for example.

frieza 03-22-2010 07:22 PM

echo $COLUMNS
it's an environement variable

also you can find out how many lines by
echo $LINES

MTK358 03-22-2010 08:11 PM

But I am not just talking about Bash scripts, but for other scripting and compiled languages.

theNbomr 03-23-2010 10:25 AM

You can get/set environment variables from most languages. I think certain versions of Java disallow this, but most Linux hosted languages that I've used are environmentally friendly.
--- rod.

frieza 03-23-2010 10:58 AM

Quote:

Originally Posted by MTK358 (Post 3908241)
But I am not just talking about Bash scripts, but for other scripting and compiled languages.

i know that i was simply pointing you at where the information is available, i dont know any more specifics about other programming languages tho but i would look into writing programs that read environment variables (the method depends on the language and you specified more then one)


All times are GMT -5. The time now is 04:05 PM.