Terminal control/Dimensions: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(Added solution for Action!) |
m (→{{header|Wren}}: Minor tidy) |
||
(5 intermediate revisions by 5 users not shown) | |||
Line 4:
=={{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 */
Line 98:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Program start
Line 105:
</pre>
=={{header|Action!}}==
<
BYTE ROWCRS=$0054 ;Current cursor row
CARD COLCRS=$0055 ;Current cursor column
Line 120:
PrintF("Number of colums: %U%E",width)
PrintF("Number of rows: %B%E",height)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Terminal_control_dimensions.png Screenshot from Atari 8-bit computer]
Line 127:
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}}==
<
HEIGHT = PEEK(35) - PEEK(34)</
=={{header|Arturo}}==
<
print ["Terminal height:" terminal\height]</
=={{header|AutoHotkey}}==
Line 141 ⟶ 149:
{{trans|C}}
AutoHotkey is not built for the console (it is GUI oriented) so we must call the WinAPI directly.
<
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
Line 155 ⟶ 163:
columns := right - left + 1
rows := bottom - top + 1
MsgBox %columns% columns and %rows% rows</
=={{header|Axe}}==
Line 163 ⟶ 171:
BaCon supports functions to retrieve the current number of <code>ROWS</code> and <code>COLUMNS</code>.
<
X = COLUMNS
Y = ROWS
PRINT "X,Y: ", X, ",", Y</
=={{header|Batch File}}==
'''Screen Buffer Size:'''
<
for /f "tokens=1,2 delims= " %%A in ('mode con') do (
Line 180 ⟶ 188:
echo Lines: %line%
echo Columns: %cols%
exit /b 0</
{{Out}}
<pre>>Size.Bat
Line 190 ⟶ 198:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
dy% = @vdu.tb%-@vdu.tt% : REM Height of text viewport in pixels</
{{works with|all BBC BASIC}}
<
REPEAT:dy%=VPOS:PRINT:UNTIL VPOS=dy%:dy%=dy%+1 : REM Height of text viewport in characters</
This has the side effect of scrolling the viewport up one line.
{{works with|BBC BASIC where VDU() is supported}}
<
dy% = VDU(9)-VDU(11)+1 : REM Height of text viewport in characters</
This has the side effect of scrolling the viewport up one line.
Line 210 ⟶ 218:
{{works with|BSD|4.4}}
<
#include <err.h> /* err */
#include <fcntl.h> /* open */
Line 236 ⟶ 244:
close(fd);
return 0;
}</
=== [[Windows]] ===
Line 242 ⟶ 250:
{{works with|MinGW}}
<
#include <wchar.h>
Line 269 ⟶ 277:
return 0;
}</
=={{header|C sharp|C#}}==
Line 276 ⟶ 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.
<
static void Main(string[] args)
{
Line 294 ⟶ 302:
Console.ReadLine();
}
</syntaxhighlight>
On the author's system this results in the following output:
Line 307 ⟶ 315:
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<
PROGRAM-ID. terminal-dimensions.
Line 333 ⟶ 341:
GOBACK
.</
=={{header|Common Lisp}}==
==={{header|ncurses}}===
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
<
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
(let ((width (width scr))
Line 345 ⟶ 353:
(refresh scr)
;; wait for keypress
(get-char scr))))</
=={{header|Euphoria}}==
<
sequence vc
Line 359 ⟶ 367:
printf(1,"Terminal height is %d\n",term_height)
printf(1,"Terminal width is %d\n",term_width)</
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<
let bufferHeight = Console.BufferHeight
Line 378 ⟶ 386:
Console.Write("Window Width: ")
Console.WriteLine(windowWidth)
Console.ReadLine()</
=={{header|Forth}}==
{{works with|GNU Forth}} {{works with|SwiftForth}}
<
variable term-height
Line 391 ⟶ 399:
get-size ( width height ) swap
[then]
term-width ! term-height !</
=={{header|FreeBASIC}}==
<
Dim driver_name As String
Screeninfo w, h, p, bpp, , tasa
Line 416 ⟶ 424:
Print " Tasa de refresco: "; tasa; " (Hz)"
Print " Nombre del driver: "; driver_name
Sleep</
=={{header|Go}}==
===Sub-repository===
{{libheader|Go sub-repositories}}
<
import (
Line 437 ⟶ 445:
}
fmt.Println(h, w)
}</
===External command===
<
import (
Line 455 ⟶ 463:
fmt.Sscan(string(d), &h, &w)
fmt.Println(h, w)
}</
===Ncurses===
{{libheader|curses}}
<
import (
Line 475 ⟶ 483:
height, width := s.MaxYX()
fmt.Println(height, width)
}</
=={{header|J}}==
Line 483 ⟶ 491:
Nevertheless, assuming J version 6 in its usual environment, to determine its width and height, in pixels, you can use:
<syntaxhighlight lang
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}}==
<
julia> using Gtk
Line 495 ⟶ 503:
julia>
</syntaxhighlight>
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<
/*
Line 511 ⟶ 519:
println("Lines = $lines")
println("Columns = $columns")
}</
{{out}}
Line 523 ⟶ 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:
<
4001 e5 push hl
4002 cd 69 bb call &bb69
Line 530 ⟶ 538:
400c e1 pop hl
400d d1 pop de
400e c9 ret</
This routine gets POKEd into RAM (starting at address &4000) and CALLed from Locomotive BASIC, then the results are retrieved with PEEK:
<
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 540 ⟶ 548:
50 h=PEEK(&4020)-PEEK(&4022)+1
60 w=PEEK(&4021)-PEEK(&4023)+1
70 PRINT "window width:"; w; ", height:"; h</
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:
<
20 |getwh,@w%,@h% ' call RSX and pass variables as pointers
30 PRINT "window width:"; w%; ", height:"; h%</
But the fastest way is still to use the system values for the current window :
<syntaxhighlight lang="locobasic">10 PRINT "window width:"PEEK(&B72C)+1", height:"PEEK(&B72B)+1
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
HEIGHT=RunThrough["tput lines", ""];</
=={{header|Nim}}==
<
let (width, height) = terminalSize()
echo "Terminal width: ", width
echo "Terminal height: ", height</
{{out}}
Line 569 ⟶ 582:
Using the library [http://forge.ocamlcore.org/projects/ansiterminal/ ANSITerminal] in the interactive loop:
<
# let width, height = ANSITerminal.size () ;;
val width : int = 126
val height : int = 47</
=={{header|Perl}}==
<
($cols, $rows) = Term::Size::chars;
print "The terminal has $cols columns and $rows lines\n";</
=={{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>
Line 598 ⟶ 614:
=={{header|PicoLisp}}==
<
Width (in '(tput cols) (read))
Height (in '(tput lines) (read)) )</
=={{header|PureBasic}}==
Line 606 ⟶ 622:
This code is for Windows only.
<
GetStdHandle_( #STD_OUTPUT_HANDLE )
EndMacro
Line 630 ⟶ 646:
;
Print(#CRLF$+"Press ENTER to exit"):Input()
EndIf</
=={{header|Python}}==
Line 638 ⟶ 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.
<
def get_windows_terminal():
Line 664 ⟶ 680:
print get_linux_terminal() if os.name == 'posix' else get_windows_terminal()
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket
(require (planet neil/charterm:3:0))
(with-charterm
(charterm-screen-size))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
Using <i>stty</i> just for the heck of it.
<syntaxhighlight lang="raku"
my $lines = $stty.match(/ 'rows ' <( \d+/);
my $cols = $stty.match(/ 'columns ' <( \d+/);
say "$lines $cols";</
=={{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.
<
-4 5 out wait 5 in !ch</
=={{header|REXX}}==
Line 695 ⟶ 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:
<
height = 'tput'( 'lines' )
say 'The terminal is' width 'characters wide'
say 'and has' height 'lines'</
===LINESIZE===
The <code>LINESIZE</code> built-in function returns the (terminal) screen's width. 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. A sample usage of it is:
<
The above example makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The '''LINESIZE.REX''' REXX program is included here ──► [[LINESIZE.REX]].
Line 709 ⟶ 725:
===SCRSIZE===
<code>SCRSIZE</code> is another built-in function, and returns two integers: the screen depth and the screen width. A few classic REXX interpreters support it: PC/REXX, Personal REXX, R4 and ROO.
<
The above example makes use of '''SCRSIZE''' REXX program (of BIF) which is used to determine the screen size of the terminal (console).
<br>The '''SCRSIZE.REX''' REXX program is included here ──► [[SCRSIZE.REX]].<br>
=={{header|Ring}}==
<
system("mode 50,20")
</syntaxhighlight>
=={{header|Ruby}}==
<
# Ruby 1.9.3 added 'io/console' to the standard library.
require 'io/console'
Line 730 ⟶ 746:
rows, cols = winsize
printf "%d rows by %d columns\n", rows, cols</
==={{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.
<
begin
Line 747 ⟶ 763:
ensure
Curses.close_screen
end</
=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<
First execute the terminal command: 'export COLUMNS LINES'
before running this program for it to work (returned 'null' sizes otherwise).
Line 757 ⟶ 773:
val (lines, columns) = (System.getenv("LINES"), System.getenv("COLUMNS"))
println(s"Lines = $lines, Columns = $columns")</
=={{header|Seed7}}==
Line 765 ⟶ 781:
''Height'' and ''width'' are based on terminfo respectively the Windows console API.
<
include "console.s7i";
Line 779 ⟶ 795:
# the program waits until Return/Enter is pressed.
readln;
end func;</
=={{header|Sidef}}==
{{trans|Raku}}
<
var lines = stty.match(/\brows\h+(\d+)/);
var cols = stty.match(/\bcolumns\h+(\d+)/);
say "#{lines} #{cols}";</
{{out}}
<pre>
Line 794 ⟶ 810:
=={{header|Tcl}}==
{{trans|UNIX Shell}}
<
set height [exec tput lines]
puts "The terminal is $width characters wide and has $height lines"</
=== Alternative ===
'''Requires''': GNU coreutils
<
puts "The terminal is $width characters wide and has $height lines"</
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<
WIDTH=`tput cols`
HEIGHT=`tput lines`
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines."</
==={{libheader|termcap}}===
<div style="background-color: #ffc;">termcap is obsolete.</div>
<
WIDTH=`tput co`
HEIGHT=`tput li`
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines."</
==={{header|C Shell}}===
<
set WIDTH=`tput cols`
set HEIGHT=`tput lines`
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines."</
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Sub Main()
Line 843 ⟶ 859:
End Sub
End Module</
{{Out}}
I put the built application in Desktop:
Line 864 ⟶ 880:
=={{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.
<
class C {
Line 875 ⟶ 891:
System.print("The dimensions of the terminal are:")
System.print(" Width = %(w)")
System.print(" Height = %(h)")</
<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 */
<lang c>#include <stdlib.h>▼
#include <stdio.h>
#include <string.h>
Line 953 ⟶ 971:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 969 ⟶ 987:
free(script);
return 0;
}</
{{out}}
Line 979 ⟶ 997:
=={{header|XPL0}}==
<
int W, H;
[W:= Peek($40, $4A); \IBM-PC BIOS data
Line 985 ⟶ 1,003:
Text(0, "Terminal width and height = ");
IntOut(0, W); ChOut(0, ^x); IntOut(0, H);
]</
Output:
Line 993 ⟶ 1,011:
=={{header|Yabasic}}==
<
w = peek("screenwidth")
h = peek("screenheight")
Line 1,006 ⟶ 1,024:
print " Ancho de la pantalla: ", w, " (pixel)"
print "Altura de la pantalla: ", h, " (pixel)\n"
close window</
Line 1,012 ⟶ 1,030:
Unix specific solution:
{{trans|GO}}
<
println(w," x ",h);</
{{out}}<pre>91 x 24</pre>
|