Terminal control/Dimensions: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(31 intermediate revisions by 24 users not shown)
Line 2:
Determine the height and width of the terminal, and store this information into variables for subsequent use.
[[Terminal Control::task| ]]
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program terminalSize64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ TIOCGWINSZ, 0x5413
.equ IOCTL, 0x1D // Linux syscall
/*******************************************/
/* Structures */
/********************************************/
/* structure terminal size */
.struct 0
term_s_lines: // input modes
.struct term_s_lines + 2
term_s_cols: // output modes
.struct term_s_cols + 2
term_s_filler: // control modes
.struct term_s_filler + 12
term_fin:
/*******************************************/
/* Initialized data */
/*******************************************/
.data
szMessStartPgm: .asciz "Program start \n"
szMessEndPgm: .asciz "Program normal end.\n"
szMessResult: .asciz "Terminal lines : @ cols : @ \n"
szMessErreur: .asciz "\033[31mError IOCTL.\n"
szCarriageReturn: .asciz "\n"
/*******************************************/
/* UnInitialized data */
/*******************************************/
.bss
.align 4
sZoneConv: .skip 24
stTerminal: .skip term_fin // structure terminal
/*******************************************/
/* code section */
/*******************************************/
.text
.global main
main:
ldr x0,qAdrszMessStartPgm //display start message
bl affichageMess
 
/* read terminal dimensions */
mov x0,STDIN // input console
mov x1,TIOCGWINSZ // code IOCTL
ldr x2,qAdrstTerminal // structure address
mov x8,IOCTL // call system Linux
svc 0
cbnz x0,98f // error ?
 
ldr x2,qAdrstTerminal
ldrh w0,[x2,term_s_lines] // load two bytes
ldr x1,qAdrsZoneConv
bl conversion10 // and decimal conversion
ldr x0,qAdrszMessResult
bl strInsertAtChar // and insertion in message
mov x5,x0 // save address of new message
ldrh w0,[x2,term_s_cols] // load two bytes
ldr x1,qAdrsZoneConv
bl conversion10 // and decimal conversion
mov x0,x5 // restaur address of message
bl strInsertAtChar // and insertion in message
bl affichageMess
ldr x0,qAdrszMessEndPgm //display end message
bl affichageMess
b 100f
98: // error display
ldr x0,qAdrszMessErreur
bl affichageMess
mov x0,-1
100: //standard end of the program
mov x0,0 //return code
mov x8,EXIT //request to exit program
svc 0 //perform system call
qAdrszMessStartPgm: .quad szMessStartPgm
qAdrszMessEndPgm: .quad szMessEndPgm
qAdrszMessErreur: .quad szMessErreur
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrstTerminal: .quad stTerminal
qAdrszMessResult: .quad szMessResult
qAdrsZoneConv: .quad sZoneConv
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Program start
Terminal lines : 24 cols : 80
Program normal end.
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Main()
BYTE ROWCRS=$0054 ;Current cursor row
CARD COLCRS=$0055 ;Current cursor column
CARD width
BYTE height
 
Graphics(0)
Position(0,0) ;go to the top-left corner
Put(28) Put(30) ;go up and left - the bottom-right corner
width=COLCRS+1
height=ROWCRS+1
 
Position(2,1)
PrintF("Number of colums: %U%E",width)
PrintF("Number of rows: %B%E",height)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Terminal_control_dimensions.png Screenshot from Atari 8-bit computer]
<pre>
Number of colums: 40
Number of rows: 24
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="AppleScript">local numColumns, numRows
tell application "Terminal"
if not running then activate
set {numColumns, numRows} to {number of columns, number of rows} of tab 1 of window 1
end tell
</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">WIDTH = PEEK(33)
HEIGHT = PEEK(35) - PEEK(34)</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print ["Terminal width:" terminal\width]
print ["Terminal height:" terminal\height]</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 11 ⟶ 149:
{{trans|C}}
AutoHotkey is not built for the console (it is GUI oriented) so we must call the WinAPI directly.
<langsyntaxhighlight AHKlang="ahk">DllCall( "AllocConsole" ) ; create a console if not launched from one
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
 
Line 25 ⟶ 163:
columns := right - left + 1
rows := bottom - top + 1
MsgBox %columns% columns and %rows% rows</langsyntaxhighlight>
 
=={{header|Axe}}==
Since Axe currently only supports the TI-83/84, the home screen dimensions are fixed at 16 columns by 8 rows.
 
=={{header|BaCon}}==
BaCon supports functions to retrieve the current number of <code>ROWS</code> and <code>COLUMNS</code>.
 
<syntaxhighlight lang="freebasic">' ANSI terminal dimensions
X = COLUMNS
Y = ROWS
 
PRINT "X,Y: ", X, ",", Y</syntaxhighlight>
 
=={{header|Batch File}}==
'''Screen Buffer Size:'''
<langsyntaxhighlight lang="dos">@echo off
 
for /f "tokens=1,2 delims= " %%A in ('mode con') do (
Line 41 ⟶ 188:
echo Lines: %line%
echo Columns: %cols%
exit /b 0</langsyntaxhighlight>
{{Out}}
<pre>>Size.Bat
Line 51 ⟶ 198:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> dx% = @vdu.tr%-@vdu.tl% : REM Width of text viewport in pixels
dy% = @vdu.tb%-@vdu.tt% : REM Height of text viewport in pixels</langsyntaxhighlight>
{{works with|all BBC BASIC}}
<syntaxhighlight lang="bbcbasic"> PRINT:VDU 8:dx%=POS+1:PRINT : REM Width of text viewport in characters
REPEAT:dy%=VPOS:PRINT:UNTIL VPOS=dy%:dy%=dy%+1 : REM Height of text viewport in characters</syntaxhighlight>
This has the side effect of scrolling the viewport up one line.
{{works with|BBC BASIC where VDU() is supported}}
<syntaxhighlight lang="bbcbasic"> dx% = VDU(10)-VDU(8)+1 : REM Width of text viewport in characters
dy% = VDU(9)-VDU(11)+1 : REM Height of text viewport in characters</syntaxhighlight>
This has the side effect of scrolling the viewport up one line.
 
=={{header|C}}==
C provides no standard way to find the size of a terminal.
 
=== {{libheader|BSD libc}} ===
[[BSD]] systems (and some other [[Unix]] clones) have TIOCGWINSZ. This ioctl(2) call gets the "window size" of a tty(4) device.
 
Line 63 ⟶ 218:
 
{{works with|BSD|4.4}}
<langsyntaxhighlight lang="c">#include <sys/ioctl.h> /* ioctl, TIOCGWINSZ */
#include <err.h> /* err */
#include <fcntl.h> /* open */
Line 89 ⟶ 244:
close(fd);
return 0;
}</langsyntaxhighlight>
 
=== [[Windows]] ===
Line 95 ⟶ 250:
 
{{works with|MinGW}}
<langsyntaxhighlight lang="c">#include <windows.h>
#include <wchar.h>
 
Line 122 ⟶ 277:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
Line 129 ⟶ 284:
WindowHeight and WindowWidth are simply the size of the window, it only represents the active viewing area which may be larger or more commonly smaller than the size of the buffer.
 
<langsyntaxhighlight lang="csharp">
static void Main(string[] args)
{
Line 147 ⟶ 302:
Console.ReadLine();
}
</syntaxhighlight>
</lang>
 
On the author's system this results in the following output:
Line 160 ⟶ 315:
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. terminal-dimensions.
 
Line 186 ⟶ 341:
 
GOBACK
.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
==={{header|ncurses}}===
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
<syntaxhighlight lang="lisp">(defun screen-dimensions ()
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
(let ((width (width scr))
(height (height scr)))
(format scr "The current terminal screen is ~A lines high, ~A columns wide.~%~%" height width)
(refresh scr)
;; wait for keypress
(get-char scr))))</syntaxhighlight>
 
=={{header|Euphoria}}==
<langsyntaxhighlight Euphorialang="euphoria">include graphics.e
 
sequence vc
Line 200 ⟶ 367:
 
printf(1,"Terminal height is %d\n",term_height)
printf(1,"Terminal width is %d\n",term_width)</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
let bufferHeight = Console.BufferHeight
Line 219 ⟶ 386:
Console.Write("Window Width: ")
Console.WriteLine(windowWidth)
Console.ReadLine()</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|GNU Forth}} {{works with|SwiftForth}}
 
<langsyntaxhighlight lang="forth">variable term-width
variable term-height
 
Line 232 ⟶ 399:
get-size ( width height ) swap
[then]
term-width ! term-height !</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Dim As Integer w, h, p, bpp, tasa
Dim driver_name As String
Screeninfo w, h, p, bpp, , tasa
 
Print !"Informaci¢n sobre el escritorio (terminal):\n"
Print " Ancho de la terminal: "; w; " (pixel)"
Print "Altura de la terminal: "; h; " (pixel)"
Print " Profundidad de color: "; p; " (bits)"
Print " Tasa de refresco: "; tasa; " (Hz)"
 
' Sets screen mode 13 (640*480, 8bpp)
Screen 12
Screeninfo w, h, p, bpp, , tasa, driver_name
 
Print !"Informaci¢n sobre el modo gr fico:\n"
Print " Ancho de la pantalla: "; w; " (pixel)"
Print "Altura de la pantalla: "; h; " (pixel)"
Print " Profundidad de color: "; p; " (bits)"
Print " Bytes por pixel: "; bpp
Print " Tasa de refresco: "; tasa; " (Hz)"
Print " Nombre del driver: "; driver_name
Sleep</syntaxhighlight>
 
=={{header|Go}}==
===Sub-repository===
{{libheader|Go sub-repositories}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 253 ⟶ 445:
}
fmt.Println(h, w)
}</langsyntaxhighlight>
 
===External command===
<langsyntaxhighlight lang="go">package main
 
import (
Line 271 ⟶ 463:
fmt.Sscan(string(d), &h, &w)
fmt.Println(h, w)
}</langsyntaxhighlight>
===Ncurses===
{{libheader|curses}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 291 ⟶ 483:
height, width := s.MaxYX()
fmt.Println(height, width)
}</langsyntaxhighlight>
 
=={{header|J}}==
Line 299 ⟶ 491:
Nevertheless, assuming J version 6 in its usual environment, to determine its width and height, in pixels, you can use:
 
<syntaxhighlight lang ="j">_2 {.qsmsize_jijs_''</langsyntaxhighlight>
 
Note also that this will typically include 37 extra pixels horizontally and 79 extra pixels vertically, which are not available to display text. In other words, if the result was 700 500 you would really have 663 pixels of width and 421 pixels of height.
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
julia> using Gtk
 
julia> screen_size()
(3840, 1080)
 
julia>
</syntaxhighlight>
 
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="scala">// version 1.1.2
 
/*
I needed to execute the terminal command: 'export COLUMNS LINES'
before running this program for it to work (returned 'null' sizes otherwise).
*/
 
fun main(args: Array<String>) {
val lines = System.getenv("LINES")
val columns = System.getenv("COLUMNS")
println("Lines = $lines")
println("Columns = $columns")
}</syntaxhighlight>
 
{{out}}
<pre>
Lines = 24
Columns = 80
</pre>
 
=={{header|Locomotive Basic}}==
Line 307 ⟶ 531:
Locomotive BASIC has no built-in command to get window dimensions, but there is a firmware call to &bb69 (TXT_GET_WINDOW) for this. So we have to use a snippet of Z80 machine code to call the firmware and copy the results from the DE and HL registers to RAM. It looks like this when disassembled:
 
<langsyntaxhighlight lang="z80">4000 d5 push de
4001 e5 push hl
4002 cd 69 bb call &bb69
Line 314 ⟶ 538:
400c e1 pop hl
400d d1 pop de
400e c9 ret</langsyntaxhighlight>
 
This routine gets POKEd into RAM (starting at address &4000) and CALLed from Locomotive BASIC, then the results are retrieved with PEEK:
 
<langsyntaxhighlight lang="locobasic">10 s=&4000:SYMBOL AFTER 256:MEMORY s-1
20 FOR i=0 to 14:READ a:POKE s+i,a:NEXT
30 DATA &d5,&e5,&cd,&69,&bb,&ed,&53,&20,&40,&22,&22,&40,&e1,&d1,&c9
Line 324 ⟶ 548:
50 h=PEEK(&4020)-PEEK(&4022)+1
60 w=PEEK(&4021)-PEEK(&4023)+1
70 PRINT "window width:"; w; ", height:"; h</langsyntaxhighlight>
 
In practice, one would prefer to write the machine code routine as a slightly more elaborate RSX ('''r'''esident '''s'''ystem e'''x'''tension) which is a freely relocatable and therefore more reusable Locomotive BASIC extension. The RSX routine might be called "getwh" and accept pointers to integers, which would simplify the BASIC code to:
 
<langsyntaxhighlight lang="locobasic">10 w%=0:h%=0 ' initialize and force integer type
20 |getwh,@w%,@h% ' call RSX and pass variables as pointers
30 PRINT "window width:"; w%; ", height:"; h%</langsyntaxhighlight>
 
But the fastest way is still to use the system values ​​for the current window :
=={{header|Mathematica}}==
 
<lang Mathematica>WIDTH=RunThrough["tput cols", ""];
<syntaxhighlight lang="locobasic">10 PRINT "window width:"PEEK(&B72C)+1", height:"PEEK(&B72B)+1
HEIGHT=RunThrough["tput lines", ""];</lang>
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">WIDTH=RunThrough["tput cols", ""];
HEIGHT=RunThrough["tput lines", ""];</syntaxhighlight>
 
=={{header|Nim}}==
 
<syntaxhighlight lang="nim">import terminal
 
let (width, height) = terminalSize()
 
echo "Terminal width: ", width
echo "Terminal height: ", height</syntaxhighlight>
 
{{out}}
<pre>Terminal width: 80
Terminal height: 24</pre>
 
=={{header|OCaml}}==
Line 340 ⟶ 582:
Using the library [http://forge.ocamlcore.org/projects/ansiterminal/ ANSITerminal] in the interactive loop:
 
<langsyntaxhighlight lang="ocaml">$ ocaml unix.cma -I +ANSITerminal ANSITerminal.cma
 
# let width, height = ANSITerminal.size () ;;
val width : int = 126
val height : int = 47</langsyntaxhighlight>
 
=={{header|Perl}}==
 
<langsyntaxhighlight lang="perl">use Term::Size;
 
($cols, $rows) = Term::Size::chars;
print "The terminal has $cols columns and $rows lines\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
The buffer is usually somewhat larger (and never smaller) than the current physical screen size. I would guess that most applications are more interested in the latter.
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (video_config)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">vc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">video_config</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Terminal buffer height is %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">VC_LINES</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Terminal buffer width is %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">VC_COLUMNS</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Terminal screen height is %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vc</span><span style="color: #0000FF;">[</span><span style="color: #004600;">VC_SCRNLINES</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Terminal screen width is %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">VC_SCRNCOLS</span><span style="color: #0000FF;">])</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Terminal buffer height is 196
Terminal buffer width is 132
Terminal screen height is 25
Terminal screen width is 80
</pre>
 
=={{header|Perl 6}}==
Using <i>stty</i> just for the heck of it.
<lang perl6>my $stty = qx[stty -a];
my $lines = $stty.match(/ 'rows ' <( \d+/);
my $cols = $stty.match(/ 'columns ' <( \d+/);
say "$lines $cols";</lang>
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(setq
Width (in '(tput cols) (read))
Height (in '(tput lines) (read)) )</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Line 368 ⟶ 622:
 
This code is for Windows only.
<langsyntaxhighlight PureBasiclang="purebasic">Macro ConsoleHandle()
GetStdHandle_( #STD_OUTPUT_HANDLE )
EndMacro
Line 392 ⟶ 646:
;
Print(#CRLF$+"Press ENTER to exit"):Input()
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
Line 400 ⟶ 654:
This uses the [http://python.net/crew/theller/ctypes/ ctypes library] in order to get the console dimensions on Windows. This code is a slight refactoring of an [http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/ ActiveState Recipe]. For Linux, the tput utility is used.
 
<langsyntaxhighlight lang="python">import os
 
def get_windows_terminal():
Line 426 ⟶ 680:
 
print get_linux_terminal() if os.name == 'posix' else get_windows_terminal()
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require (planet neil/charterm:3:0))
(with-charterm
(charterm-screen-size))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Using <i>stty</i> just for the heck of it.
<syntaxhighlight lang="raku" line>my $stty = qx[stty -a];
my $lines = $stty.match(/ 'rows ' <( \d+/);
my $cols = $stty.match(/ 'columns ' <( \d+/);
say "$lines $cols";</syntaxhighlight>
 
=={{header|Retro}}==
This information is provided by Retro in the '''ch''' (height) and '''cw''' (width) variables. You can manually obtain it using the io ports.
 
<langsyntaxhighlight Retrolang="retro">-3 5 out wait 5 in !cw
-4 5 out wait 5 in !ch</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 449 ⟶ 711:
 
Some REXX interpreters don't provide basic [[terminal control]] as part of the language. However, it's possible to determine the size of the terminal window by using external system commands:
<langsyntaxhighlight lang="rexx">width = 'tput'( 'cols' )
height = 'tput'( 'lines' )
 
say 'The terminal is' width 'characters wide'
say 'and has' height 'lines'</langsyntaxhighlight>
 
===LINESIZE===
The <code>LINESIZE</code> built-in function returns the (terminal) screen's width. &nbsp; It is supported by most (classic) REXX interpreters (and some others) such as: CMS REXX, TSO REXX, VSE REXX, the IBM REXX compiler, PC/REXX, Personal REXX, REXX/imc, R4 and ROO. &nbsp; A sample usage of it is:
<langsyntaxhighlight lang="rexx">width=linesize()</langsyntaxhighlight>
The above example makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here ──► [[LINESIZE.REX]].
Line 463 ⟶ 725:
===SCRSIZE===
<code>SCRSIZE</code> is another built-in function, and returns two integers: the screen depth and the screen width. &nbsp; A few classic REXX interpreters support it: &nbsp; PC/REXX, Personal REXX, R4 and ROO.
<langsyntaxhighlight lang="rexx"> parse value scrsize() with sd sw</langsyntaxhighlight>
The above example makes use of &nbsp; '''SCRSIZE''' &nbsp; REXX program (of BIF) which is used to determine the screen size of the terminal (console).
<br>The &nbsp; '''SCRSIZE.REX''' &nbsp; REXX program is included here ──► [[SCRSIZE.REX]].<br>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
system("mode 50,20")
</syntaxhighlight>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def winsize
# Ruby 1.9.3 added 'io/console' to the standard library.
require 'io/console'
Line 479 ⟶ 746:
 
rows, cols = winsize
printf "%d rows by %d columns\n", rows, cols</langsyntaxhighlight>
 
==={{libheader|curses}}===
<code>Curses.lines</code> and <code>Curses.cols</code> return the size of the terminal. The program ''must'' call <code>Curses.init_screen</code>, because without this call, Curses might report 0 lines and 0 columns. Beware that <code>Curses.init_screen</code> also switches the terminal to screen-oriented mode, and fails on those terminals that cannot support curses.
 
<langsyntaxhighlight lang="ruby">require 'curses'
 
begin
Line 496 ⟶ 763:
ensure
Curses.close_screen
end</langsyntaxhighlight>
 
=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="scala"> /*
First execute the terminal command: 'export COLUMNS LINES'
before running this program for it to work (returned 'null' sizes otherwise).
*/
 
val (lines, columns) = (System.getenv("LINES"), System.getenv("COLUMNS"))
println(s"Lines = $lines, Columns = $columns")</syntaxhighlight>
 
=={{header|Seed7}}==
Line 504 ⟶ 781:
''Height'' and ''width'' are based on terminfo respectively the Windows console API.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "console.s7i";
 
Line 518 ⟶ 795:
# the program waits until Return/Enter is pressed.
readln;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">var stty = `stty -a`;
var lines = stty.match(/\brows\h+(\d+)/);
var cols = stty.match(/\bcolumns\h+(\d+)/);
say "#{lines} #{cols}";</syntaxhighlight>
{{out}}
<pre>
24 80
</pre>
 
=={{header|Tcl}}==
{{trans|UNIX Shell}}
<langsyntaxhighlight lang="tcl">set width [exec tput cols]
set height [exec tput lines]
puts "The terminal is $width characters wide and has $height lines"</langsyntaxhighlight>
 
=== Alternative ===
'''Requires''': GNU coreutils
<syntaxhighlight lang="tcl">lassign [exec stty size] width height
puts "The terminal is $width characters wide and has $height lines"</syntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">#!/bin/sh
WIDTH=`tput cols`
HEIGHT=`tput lines`
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines."</langsyntaxhighlight>
 
==={{libheader|termcap}}===
<div style="background-color: #ffc;">termcap is obsolete.</div>
<langsyntaxhighlight lang="bash">#!/bin/sh
WIDTH=`tput co`
HEIGHT=`tput li`
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines."</langsyntaxhighlight>
 
==={{header|C Shell}}===
<langsyntaxhighlight lang="csh">#!/bin/csh -f
set WIDTH=`tput cols`
set HEIGHT=`tput lines`
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines."</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vb">Module Module1
 
Sub Main()
Line 566 ⟶ 859:
End Sub
 
End Module</langsyntaxhighlight>
{{Out}}
I put the built application in Desktop:
Line 584 ⟶ 877:
 
\Desktop></pre>
 
=={{header|Wren}}==
As there is currently no way to obtain this information via Wren CLI, we instead embed a Wren script in a C application and ask the host program to get it for us.
<syntaxhighlight lang="wren">/* Terminal_control_Dimensions.wren */
 
class C {
foreign static terminalWidth
foreign static terminalHeight
}
 
var w = C.terminalWidth
var h = C.terminalHeight
System.print("The dimensions of the terminal are:")
System.print(" Width = %(w)")
System.print(" Height = %(h)")</syntaxhighlight>
<br>
We now embed this Wren script in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc Terminal_control_Dimensions.c -o Terminal_control_Dimensions -lwren -lm */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "wren.h"
 
void C_terminalWidth(WrenVM* vm) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
wrenSetSlotDouble(vm, 0, (double)w.ws_col);
}
 
void C_terminalHeight(WrenVM* vm) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
wrenSetSlotDouble(vm, 0, (double)w.ws_row);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "terminalWidth") == 0) {
return C_terminalWidth;
} else if (isStatic && strcmp(signature, "terminalHeight") == 0) {
return C_terminalHeight;
}
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main() {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Terminal_control_Dimensions.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
The dimensions of the terminal are:
Width = 80
Height = 24
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
int W, H;
[W:= Peek($40, $4A); \IBM-PC BIOS data
Line 592 ⟶ 1,003:
Text(0, "Terminal width and height = ");
IntOut(0, W); ChOut(0, ^x); IntOut(0, H);
]</langsyntaxhighlight>
 
Output:
Line 598 ⟶ 1,009:
80x25
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">clear screen
w = peek("screenwidth")
h = peek("screenheight")
print "Informaci¢n sobre el escritorio (terminal):"
print " Ancho de la terminal: ", w, " (pixel)"
print "Altura de la terminal: ", h, " (pixel)"
 
open window 640,480
w = peek("winheight")
h = peek("winwidth")
print "\n\nInformaci¢n sobre el modo gr fico:"
print " Ancho de la pantalla: ", w, " (pixel)"
print "Altura de la pantalla: ", h, " (pixel)\n"
close window</syntaxhighlight>
 
 
=={{header|zkl}}==
Unix specific solution:
{{trans|GO}}
<langsyntaxhighlight lang="zkl">h,w:=System.popen("stty size","r").readln().split();
println(w," x ",h);</langsyntaxhighlight>
{{out}}<pre>91 x 24</pre>
 
Line 610 ⟶ 1,038:
{{omit from|Maxima}}
{{omit from|PARI/GP}}
{{omit from|TI-83 BASIC|The terminal dimensions are constant.}}
9,476

edits