[erlang-questions] Getting the number of columns of a terminal

Gustav Simonsson <>
Tue Jan 17 15:17:35 CET 2012


Good question. 
If you can find out which terminal device you're in you can do something like this:

15:14:12 belegost ~> tty
/dev/pts/1
15:14:16 belegost ~> erl
Erlang R14B04 (erts-5.8.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.8.5  (abort with ^G)
1> 
1> open_port({spawn_executable, "/bin/sh"}, [{args,["-c", "stty -aF /dev/pts/1"]}]).
#Port<0.581>
2> flush().
Shell got {#Port<0.581>,
           {data,"speed 38400 baud; rows 70; columns 119; line = 0;\nintr = ^C; quit = ^\\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?;\nswtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;\nlnext = ^V; flush = ^O; min = 1; time = 0;\n-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts\n-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff\n-iuclc ixany imaxbel iutf8\n-opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0\nisig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt\nechoctl echoke\n"}}
ok
3> 

The problem is if you call tty or stty directly from within Erlang then they will
fail because they cannot know which terminal device they are executed from. 
The good thing about stty though if you can find a way to use it is that it works on linux, mac os x and BSD.

Regards,
Gustav Simonsson



Sent from my PC

----- Original Message -----
From: "Juan Jose Comellas" <>
To: "erlang-questions" <>
Sent: Tuesday, January 17, 2012 1:54:54 PM
Subject: [erlang-questions] Getting the number of columns of a terminal

Does anybody know if there's any way of getting the number of columns
in a terminal in a portable way in Erlang (i.e. not using curses or
anything else that's not available on other platforms)? I need to wrap
some lines that I'm showing on a terminal and I'd rather use the real
terminal's width instead hardcoding it to 80 characters or so.

Thanks,

Juanjo
_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list