LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2010, 07:14 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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?
 
Old 03-19-2010, 07:24 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 03-20-2010, 09:10 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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?
 
Old 03-20-2010, 09:33 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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 ?
 
Old 03-20-2010, 09:49 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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?
 
Old 03-20-2010, 11:59 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 03-22-2010, 05:28 PM   #7
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 03-22-2010, 07:06 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
Old 03-22-2010, 07:22 PM   #9
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
echo $COLUMNS
it's an environement variable

also you can find out how many lines by
echo $LINES
 
Old 03-22-2010, 08:11 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
But I am not just talking about Bash scripts, but for other scripting and compiled languages.
 
Old 03-23-2010, 10:25 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 03-23-2010, 10:58 AM   #12
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
Quote:
Originally Posted by MTK358 View Post
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)

Last edited by frieza; 03-23-2010 at 10:59 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gnome-terminal and console column width jhwilliams Linux - Software 3 08-16-2007 10:22 PM
fake larger width in terminal Four Linux - Newbie 7 07-26-2007 05:12 AM
How can a bash script findout the width of the terminal window alex.e.c Linux - Software 2 10-14-2004 11:20 AM
Scripting question: the width of the terminal window Bebo Programming 13 11-06-2003 04:41 PM
Changing Text Size/Terminal Width in Bash TastyWheat Linux - General 2 11-02-2003 11:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration