Terminal control/Cursor movement: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Kotlin}}: Assumed terminal size is in fact 80 x 24)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(33 intermediate revisions by 21 users not shown)
Line 19:
This task has no specific requirements to trap or correct cursor movement beyond the terminal boundaries, so the implementer should decide what behavior fits best in terms of the chosen language.   Explanatory notes may be added to clarify how an out of bounds action would behave and the generation of error messages relating to an out of bounds cursor position is permitted.
<br><br>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program cursorMove64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/* Initialized data */
.data
szMessStartPgm: .asciz "Program start \n"
szMessEndPgm: .asciz "Program normal end.\n"
szMessColorRed: .asciz "Color red.\n"
szCodeInit: .asciz "\033[0m" // color reinit
szCodeRed: .asciz "\033[31m" // color red
szCodeBlue: .asciz "\033[34m" // color blue
szMessMove: .asciz "\033[A\033[6CBlue Message up and 6 location right."
szMessMoveDown: .asciz "\033[31m\033[BRed text location down"
szMessTopLeft: .asciz "\033[;HTOP LEFT"
szCarriageReturn: .asciz "\n"
/* UnInitialized data */
.bss
/* code section */
.text
.global main
main:
ldr x0,qAdrszMessStartPgm // display start message
bl affichageMess
ldr x0,qAdrszCodeRed // color red
bl affichageMess
ldr x0,qAdrszMessColorRed
bl affichageMess
ldr x0,qAdrszCodeBlue
bl affichageMess
ldr x0,qAdrszMessMove
bl affichageMess
ldr x0,qAdrszMessMoveDown // move pointer down
bl affichageMess
ldr x0,qAdrszMessTopLeft
bl affichageMess
ldr x0,qAdrszCarriageReturn // start next line
bl affichageMess
ldr x0,qAdrszCodeInit // color reinitialize
bl affichageMess
ldr x0,qAdrszMessEndPgm // display end message
bl affichageMess
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
qAdrszCodeInit: .quad szCodeInit
qAdrszCodeRed: .quad szCodeRed
qAdrszCodeBlue: .quad szCodeBlue
qAdrszMessColorRed: .quad szMessColorRed
qAdrszMessMove: .quad szMessMove
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessMoveDown: .quad szMessMoveDown
qAdrszMessTopLeft: .quad szMessTopLeft
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Wait(BYTE frames)
BYTE RTCLOK=$14
frames==+RTCLOK
WHILE frames#RTCLOK DO OD
RETURN
 
PROC Main()
BYTE
d=[50],
CH=$02FC, ;Internal hardware value for last key pressed
ROWCRS=$0054 ;Current cursor row
CARD COLCRS=$0055 ;Current cursor column
 
Graphics(0)
Position(2,2)
Print("Press any key to start demonstration.")
Position(20,10)
Put(28) Put(29) ;trick to show the new cursor pos
DO UNTIL CH#$FF OD
CH=$FF
 
Wait(d) Put(30) ;move cursor left
Wait(d) Put(31) ;move cursor right
Wait(d) Put(28) ;move cursor up
Wait(d) Put(29) ;move cursor down
 
Wait(d) Position(0,ROWCRS) ;move to the beginning of the line
Put(28) Put(29) ;trick to show the new cursor pos
 
Wait(d) Position(39,ROWCRS) ;move to the end of the line
Put(28) Put(29) ;trick to show the new cursor pos
Wait(d) Position(0,0) ;move to the top-left corner
Put(28) Put(29) ;trick to show the new cursor pos
 
Wait(d) Position(39,23) ;move to the bottom-right corner
Put(29) Put(28) ;trick to show the new cursor pos
 
Wait(d)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cursor_movement.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
{{libheader|ANSIAda}}
<syntaxhighlight lang="ada">with Ada.Text_Io;
 
with Ansi;
 
procedure Movement is
use Ada.Text_Io;
begin
Put (Ansi.Store);
Put (Ansi.Position (Row => 20, Column => 40));
 
for A in 1 .. 15 loop
Put (Ansi.Back);
delay 0.020;
end loop;
 
for A in 1 .. 15 loop
Put (Ansi.Up);
delay 0.050;
end loop;
 
for A in 1 .. 15 loop
Put (Ansi.Forward);
delay 0.020;
end loop;
 
for A in 1 .. 15 loop
Put (Ansi.Down);
delay 0.050;
end loop;
 
delay 1.000;
Put (Ansi.Horizontal (Column => 1));
delay 2.000;
Put (Ansi.Position (1, 1));
delay 2.000;
Put (Ansi.Restore);
end Movement;</syntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
 
/* ARM assembly Raspberry PI */
/* program cursorMove.s */
 
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
 
/* Initialized data */
.data
szMessStartPgm: .asciz "Program start \n"
szMessEndPgm: .asciz "Program normal end.\n"
szMessColorRed: .asciz "Color red.\n"
szCodeInit: .asciz "\033[0m" @ color reinit
szCodeRed: .asciz "\033[31m" @ color red
szCodeBlue: .asciz "\033[34m" @ color blue
szMessMove: .asciz "\033[A\033[6CBlue Message up and 6 location right."
szMessMoveDown: .asciz "\033[31m\033[BRed text location down"
szMessTopLeft: .asciz "\033[;HTOP LEFT"
szCarriageReturn: .asciz "\n"
 
/* UnInitialized data */
.bss
 
/* code section */
.text
.global main
main:
 
ldr r0,iAdrszMessStartPgm @ display start message
bl affichageMess
ldr r0,iAdrszCodeRed @ color red
bl affichageMess
ldr r0,iAdrszMessColorRed
bl affichageMess
ldr r0,iAdrszCodeBlue
bl affichageMess
ldr r0,iAdrszMessMove
bl affichageMess
ldr r0,iAdrszMessMoveDown @ move pointer down
bl affichageMess
ldr r0,iAdrszMessTopLeft
bl affichageMess
ldr r0,iAdrszCarriageReturn @ start next line
bl affichageMess
ldr r0,iAdrszCodeInit @ color reinitialize
bl affichageMess
ldr r0,iAdrszMessEndPgm @ display end message
bl affichageMess
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform system call
iAdrszMessStartPgm: .int szMessStartPgm
iAdrszMessEndPgm: .int szMessEndPgm
iAdrszCodeInit: .int szCodeInit
iAdrszCodeRed: .int szCodeRed
iAdrszCodeBlue: .int szCodeBlue
iAdrszMessColorRed: .int szMessColorRed
iAdrszMessMove: .int szMessMove
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessMoveDown: .int szMessMoveDown
iAdrszMessTopLeft: .int szMessTopLeft
 
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registers
mov r2,#0 @ counter length */
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call system
pop {r0,r1,r2,r7,lr} @ restaur registers
bx lr @ return
 
</syntaxhighlight>
 
=={{header|AutoHotkey}}==
 
<langsyntaxhighlight lang="autohotkey">DllCall("AllocConsole")
hConsole:=DllCall("GetConsoleWindow","UPtr")
Stdout:=FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n")
Line 94 ⟶ 343:
bufferheight:=NumGet(&struct,2,"UShort")
return 1
}</langsyntaxhighlight>
 
=={{header|Axe}}==
Axe does not allow relative movement of the cursor. However, if the current position is known in the X and Y variables, the behavior can be simulated.
<langsyntaxhighlight lang="axe">Output(X-1,Y)
Output(X+1,Y)
Output(X,Y-1)
Line 105 ⟶ 354:
Output(15,Y)
Output(0,0)
Output(15,7)</langsyntaxhighlight>
 
=={{header|BaCon}}==
 
<syntaxhighlight lang="freebasic">' ANSI terminal cursor movement
' Default number of positions, if not specified, is 1.
 
' Left 1 position, blocks at first position
CURSOR BACK 1
 
' Right 1, blocks at end of line
CURSOR FORWARD 1
 
' Up 1, column uneffected, blocks at top
CURSOR UP 1
 
' Down 1, column uneffected, until scroll at bottom
CURSOR DOWN 1
 
' First column of current row
R = GETY
GOTOXY 1,R
 
' Last column of current row
C = COLUMNS
GOTOXY C,R
 
' Home position, top left
GOTOXY 1,1
 
' Bottom right
R = ROWS
GOTOXY C,R</syntaxhighlight>
 
=={{header|BASIC}}==
Line 111 ⟶ 392:
{{works with|QBasic}}
 
<langsyntaxhighlight lang="qbasic">10 'move left
20 LOCATE , POS(0) - 1
30 'move right
Line 126 ⟶ 407:
140 LOCATE 1, 1
150 'bottom right corner; requires knowledge of screen dimensions (80x25 here)
160 LOCATE 25, 80</langsyntaxhighlight>
==={{header|Applesoft BASIC}}===
80-Column Text Card: Applesoft Control Codes
Line 133 ⟶ 414:
Apple II Family Identification
http://www.umich.edu/~archive/apple2/technotes/tn/misc/TN.MISC.007
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">REM APPLE II GS ?
100 DATA56,32,31,254,160,0
110 DATA176,1,136,140,13,3,96
Line 190 ⟶ 471:
430 HTAB PEEK(33)
440 GET A$
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> VDU 8 : REM Move one position to the left
VDU 9 : REM Move one position to the right
VDU 11 : REM Move up one line
Line 202 ⟶ 483:
VDU 13,8,10 : REM Move to the end of the line
VDU 30,8 : REM Move to the bottom right corner
VDU 23,16,0;0;0;0; : REM Enable scrolling</langsyntaxhighlight>
 
=={{header|Befunge}}==
 
Assuming a terminal with support for ANSI escape sequences, you can move the cursor position with the code fragments below.
 
{|style="text-align: left;"
!Move left one column:
|<code>"D["39*,,,</code>
|-
!Move right one column:
|<code>"C["39*,,,</code>
|-
!Move up one line:
|<code>"A["39*,,,</code>
|-
!Move down one line:
|<code>"B["39*,,,</code>
|-
!Move to start of line:
|<code>94+,</code>
|-
!Move to end of line:
|<code>"C999["39*,,,,,,</code>
|-
!Move to top left:
|<code>"H["39*,,,</code>
|-
!Move to bottom right:
|<code>0"H999;999["39*>:#,_$</code>
|}
 
Note that the ''end of line'' movement is achieved by moving right 999 columns, and relies on the fact that the terminal will clamp the movement range to the width of the screen. Similarly, the ''bottom right'' movement is achieved by setting the cursor position to the 999th column of the 999th row, which again is clamped to the maximum width and height of the screen.
 
The ''start of line'' movement is simply a carriage return (ASCII 13).
 
=={{header|C}}==
The conio.h header file in Borland's Turbo C makes keyboard interaction very simple. The following is an interactive program which has been tested with Turbo C, the delay function takes milliseconds and has been used to animate the involved cases.
 
<syntaxhighlight lang="c">
<lang C>
/*Abhishek Ghosh, 7th November 2013, Rotterdam*/
#include<conio.h>
#include<dos.h>
Line 292 ⟶ 606:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
==={{header|ncurses}}===
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
<syntaxhighlight lang="lisp">(defun cursor-movement ()
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible t)
;; display the screen and wait for a keypress
(refresh scr) (get-char scr)
 
(move-direction scr :right) (refresh scr) (get-char scr)
(move-direction scr :left) (refresh scr) (get-char scr)
(move-direction scr :down) (refresh scr) (get-char scr)
(move-direction scr :up) (refresh scr) (get-char scr)
;; end of line
(move scr (car (cursor-position scr)) (1- (width scr))) (refresh scr) (get-char scr)
;; beginning of line
(move scr (car (cursor-position scr)) 0) (refresh scr) (get-char scr)
;; bottom right corner
(move scr (1- (height scr)) (1- (width scr))) (refresh scr) (get-char scr)
;; top left corner
(move scr 0 0) (refresh scr) (get-char scr)))</syntaxhighlight>
 
=={{header|C sharp|C#}}==
{{works with|Mono|1.2}}
{{works with|Visual C sharp|Visual C#|2003}}
<langsyntaxhighlight lang="csharp">static void Main(string[] args)
{
//There will be a 3 second pause between each cursor movement.
Line 324 ⟶ 659:
System.Threading.Thread.Sleep(3000);
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
ANS/ISO Forth has a cursor positioning function called AT-XY. The standard language does not define how the cursor is managed however most systems give the programmer direct access to a pair of variables for cursor row and column position.
 
The following example assumes we are using a terminal that accepts ANSI escape codes. It defines the ANSI codes as Forth words with a markup language look. With this code compiled into the Forth system, the commands are used like native Forth commands.
<syntaxhighlight lang="forth">( ANSI terminal control lexicon )
DECIMAL
 
( support routines)
27 CONSTANT ESC
: <##> ( n -- ) ( sends n, radix 10, no spaces)
BASE @ >R DECIMAL 0 <# #S #> TYPE R> BASE ! ;
 
: ESC[ ( -- ) ESC EMIT ." [" ;
 
( ANSI terminal commands as Forth words)
: <CUU> ( row --) ESC[ <##> ." A" ;
: <CUD> ( row --) ESC[ <##> ." B" ;
: <CUF> ( col --) ESC[ <##> ." C" ;
: <CUB> ( col --) ESC[ <##> ." D" ;
: <CPL> ( -- ) ESC[ <##> ." F" ;
: <CHA> ( n --) ESC[ <##> ." G" ;
: <EL> ( -- ) ESC[ ." K" ;
: <ED> ( -- ) ESC[ ." 2J" ;
: <CUP> ( row col -- ) SWAP ESC[ <##> ." ;" <##> ." H" ;
 
( Define ANSI Forth names for these functions using our markup words)
: AT-XY ( col row -- ) SWAP <CUP> ;
: PAGE ( -- ) <ED> 1 1 <CUP> ;</syntaxhighlight>
Rosetta Task
<syntaxhighlight lang="forth">( move the cursor one position to the left) 1 <CUB>
( move the cursor one position to the right) 1 <CUF>
( move the cursor up one line ) 1 <CUU>
( move the cursor down one line) 1 <CUD>
( move the cursor to the beginning of the line) 1 <CHA>
( move the cursor to the end of the line ) 80 <CHA>
( move the cursor to the top left corner of the screen) 1 1 <CUP>
( move the cursor to the bottom right corner of the screen) 80 24 <CUP>
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vb">Dim As Integer w, h
 
Screeninfo w, h
'move left
Locate , Pos(0) - 1
'move right
Locate , Pos(0) + 1
'move up
Locate Csrlin - 1
'move down
Locate Csrlin + 1
'beginning of line
Locate , 1
'end of line
Locate , h
'top left corner
Locate 1, 1
'bottom right corner
Locate w, h</syntaxhighlight>
 
=={{header|Go}}==
===External commands===
<langsyntaxhighlight lang="go">package main
 
import (
Line 375 ⟶ 769:
cmd.Stdout = os.Stdout
return cmd.Run()
}</langsyntaxhighlight>
===ANSI escape codes===
Not meeting all task requirements. Some of the movements are awkward with ANSI escape codes alone and are best done with one of the other techniques shown.
<langsyntaxhighlight lang="go">package main
 
import (
Line 398 ⟶ 792:
fmt.Print("\033[;H") // top left
time.Sleep(1 * time.Second)
}</langsyntaxhighlight>
===Ncurses===
{{libheader|curses}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 460 ⟶ 854:
s.Refresh()
s.GetChar()
}</langsyntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq'''
 
The following relies on the ANSI terminal control codes. Unfortunately, as best I can tell,
horizontal cursor positioning via ANSI codes does not work in Terminal.app on a Mac.
 
Invocation: jq -nr --unbuffered -f cursor-movement.jq
<syntaxhighlight lang=jq>
# Be busy for at least the given number of seconds,
# and emit the actual number of seconds that have elapsed.
# The reason for defining sleep/1 is that it allows the idiom:
# E | F, (sleep(1) as $elapsed | CONTINUE_WITH_E_AS_INPUT)
def sleep($seconds):
now
| . as $now
| until( . - $now >= $seconds; now)
| . - $now ;
 
def demo:
def ESC: "\u001B";
def s: sleep(2) | empty;
"\(ESC)[2J", # clear terminal
"\(ESC)[12;40H", # move to (12, 40)
s,
"\(ESC)[D", # move left
s,
"\(ESC)[C", # move right
s,
"\(ESC)[A", # move up
s,
"\(ESC)[B", # move down
s,
"\(ESC)[G", # move to beginning of line
s,
"\(ESC)[79C", # move to end of line (assuming 80 column terminal)
s,
"\(ESC)[1;1H", # move to top left corner
s,
"\(ESC)[24;80H", # move to bottom right corner (assuming 80 x 24 terminal)
s,
"\(ESC)[1;1H" # home cursor again before quitting
;
 
demo
</syntaxhighlight>
=={{header|Julia}}==
{{trans|Kotlin}}
<syntaxhighlight lang="julia">const ESC = "\u001B" # escape code
const moves = Dict( "left" => "[1D", "right" => "[1C", "up" => "[1A", "down" => "[1B",
"linestart" => "[9D", "topleft" => "[H", "bottomright" => "[24;79H")
print("$ESC[2J") # clear terminal first
print("$ESC[10;10H") # move cursor to (10, 10) say
const count = [0]
for d in ["left", "right", "up", "down", "linestart", "bottomright"]
sleep(3) # three second pause for display between cursor movements
print("$ESC$(moves[d])")
print(count[1] += 1)
end
println()
println()
</syntaxhighlight>
 
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang ="scala">// version 1.1.12
 
const val ESC = "\u001B" // escape code
Line 470 ⟶ 930:
fun main(args: Array<String>) {
print("$ESC[2J") // clear terminal first
print("$ESC[10;10H") // move cursor to (10, 10) say
val aecs = arrayOf(
"[1D", // left
Line 486 ⟶ 946:
Thread.sleep(3000)
println()
}</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">#!/usr/bin/lasso9
 
local(esc = decode_base64('Gw=='))
Line 534 ⟶ 994:
// move the cursor to the bottom right corner of the screen
stdout(#esc + '[500;500H')
sleep(2000)</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Run["tput cub1"] (* one position to the left *)
Run["tput cuf1" ] (* one position to the right *)
Run["tput cuu1" ] (* up one line *)
Line 543 ⟶ 1,003:
Run["tput cr"] (* beginning of line *)
Run["tput home"] (* top left corner *)
 
WIDTH=RunThrough["tput cols", ""];
Line 549 ⟶ 1,008:
Run["tput hpa "<>WIDTH] (* end of line *)
Run["tput cup "<>HEIGHT<>" "<> WIDTH] (* bottom right corner *)</langsyntaxhighlight>
 
=={{header|Perl 6}}==
=={{header|Nim}}==
<lang perl6>shell "tput cub1"; # one position to the left
<syntaxhighlight lang="nim">import terminal
shell "tput cuf1"; # one position to the right
 
shell "tput cuu1"; # up one line
echo "Press the return key to go to next step."
shell "tput cud1"; # down one line
echo "Starting in the middle of last line."
shell "tput cr"; # beginning of line
 
shell "tput home"; # top left corner
template waitUser() =
while getch() != '\r': discard
 
let (width, height) = terminalSize()
# Start by positionning the cursor in the middle of the line.
setCursorXPos(width div 2)
waitUser()
# Move one character backward.
cursorBackward(1)
waitUser()
# Move one character forward.
cursorForward(1)
waitUser()
# Move one character up.
cursorUp(1)
waitUser()
# Move one character down.
cursorDown(1)
waitUser()
# Move at beginning of line.
setCursorXPos(0)
waitUser()
# Move at end of line.
setCursorXPos(width - 1)
waitUser()
# Move cursor to the top left corner.
setCursorPos(0, 0)
waitUser()
# Move cursor to the bottom right corner.
setCursorPos(width - 1, height - 1)
waitUser()</syntaxhighlight>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">system "tput cub1"; sleep 1; # one position to the left
system "tput cuf1"; sleep 1; # one position to the right
system "tput cuu1"; sleep 1; # up one line
system "tput cud1"; sleep 1; # down one line
system "tput cr"; sleep 1; # beginning of line
system "tput home"; sleep 1; # top left corner
 
$_ = qx[stty -a </dev/tty 2>&1];
my ($rows,$cols) = +m/'(\d+) rows; ' <(\d+)> col/;
$rows--; $cols--;
my $cols = +m/'columns ' <(\d+)>/;
 
shellsystem "tput hpacup $rows $cols"; # endbottom ofright linecorner
sleep 1;</syntaxhighlight>
shell "tput cup $rows $cols"; # bottom right corner</lang>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Cursor_movement.exw
-- ================================
--
-- These may vary by platform/hardware... (this program is ideal for sorting such things out)
--</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">HOME</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">327</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">END</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">335</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">UP</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">328</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">DOWN</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">336</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">LEFT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">331</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">RIGHT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">333</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">PGUP</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">329</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (goto top left)</span>
<span style="color: #000000;">PGDN</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">337</span> <span style="color: #000080;font-style:italic;">-- (goto bottom right)</span>
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">maxl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">maxc</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">video_config</span><span style="color: #0000FF;">()[</span><span style="color: #004600;">VC_SCRNLINES</span><span style="color: #0000FF;">..</span><span style="color: #000000;">VC_SCRNCOLS</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">move_cursor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dy</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_position</span><span style="color: #0000FF;">(),{</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">maxl</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">c</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">maxc</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">move_to</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ny</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">nx</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_position</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ny</span><span style="color: #0000FF;">!=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ny</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">nx</span><span style="color: #0000FF;">!=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nx</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">showkey</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_position</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">maxc</span><span style="color: #0000FF;">-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">key</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">#1B</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- escape quits</span>
<span style="color: #000000;">showkey</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">HOME</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nx</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- home</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">END</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nx</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">maxc</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- end</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">UP</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_cursor</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- up</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">DOWN</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_cursor</span><span style="color: #0000FF;">(+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- down</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">LEFT</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_cursor</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- left</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">RIGHT</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_cursor</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- right</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">PGUP</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- page_up</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">=</span><span style="color: #000000;">PGDN</span> <span style="color: #008080;">then</span> <span style="color: #000000;">move_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">maxl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">maxc</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- page_down</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(call 'tput "cub1") # one position to the left
(call 'tput "cuf1") # one position to the right
(call 'tput "cuu1") # up one line
Line 573 ⟶ 1,129:
(call 'tput "hpa" (sys "COLUMNS")) # end of the line
(call 'tput "home") # top left corner
(call 'tput "cup" (sys "LINES") (sys "COLUMNS")) # bottom right corner</langsyntaxhighlight>
 
 
=={{header|Python}}==
{{libheader|curses}}
<langsyntaxhighlight Pythonlang="python">import curses
 
scr = curses.initscr()
Line 620 ⟶ 1,175:
y,x = scr.getmaxyx()
curses.move(y,x)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require (planet neil/charterm:3:0))
Line 648 ⟶ 1,203:
(when continue?
(loop (on-key (charterm-read-key))))))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>shell "tput cub1"; # one position to the left
shell "tput cuf1"; # one position to the right
shell "tput cuu1"; # up one line
shell "tput cud1"; # down one line
shell "tput cr"; # beginning of line
shell "tput home"; # top left corner
 
$_ = qx[stty -a </dev/tty 2>&1];
my $rows = +m/'rows ' <(\d+)>/;
my $cols = +m/'columns ' <(\d+)>/;
 
shell "tput hpa $cols"; # end of line
shell "tput cup $rows $cols"; # bottom right corner</syntaxhighlight>
 
=={{header|REXX}}==
{{works with|PC/REXX}}
{{works with|Personal REXX}}
 
This version ''only'' works with PC/REXX or Personal REXX.
This version &nbsp; ''only'' &nbsp; works with PC/REXX or Personal REXX.
<lang rexx>/*REXX pgm demonstrates how to achieve movement of the terminal cursor. */
<syntaxhighlight lang="rexx">/*REXX pgm demonstrates how to achieve movement of the terminal cursor. */
 
parse value scrsize() with sd sw /*find the display screen size. */
Line 677 ⟶ 1,248:
call cursor sd,sw /*move cursor to bot right corner*/
 
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
 
=={{header|Ruby}}==
{{Works with|Ubuntu|22.04}}
<syntaxhighlight lang="ruby">require 'io/console'
 
def c (method, *args) # to avoid repeating sleeps
STDOUT.send(method, *args)
sleep 1
end
 
x, y = STDOUT.winsize
c(:clear_screen)
c(:cursor_right, 1)
c(:cursor_down, 1)
c(:cursor_left, 1)
c(:cursor_up, 1)
c(:goto_column, y)
c(:goto_column, 0)
c(:goto, 0, y)
c(:goto, x, 0)
</syntaxhighlight>
=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="scala">object CursorMovement extends App {
val ESC = "\u001B" // escape code
 
print(s"$ESC[2J$ESC[10;10H") // clear terminal first, move cursor to (10, 10) say
val aecs = Seq(
"[1D", // left
"[1C", // right
"[1A", // up
"[1B", // down
"[9D", // line start
"[H", // top left
"[24;79H" // bottom right - assuming 80 x 24 terminal
)
for (aec <- aecs) {
Thread.sleep(3000) // three second display between cursor movements
print(s"$ESC$aec")
}
 
}</syntaxhighlight>
 
=={{header|Tcl}}==
{{trans|UNIX Shell}}
<langsyntaxhighlight lang="tcl"># Simplification wrapper for when we're actually affecting the terminal
proc tput args {
exec tput {*}$args >@stdout <@stdin
Line 698 ⟶ 1,311:
 
tput hpa $width; # end of line
tput cpu $height $width; # bottom right corner</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
<langsyntaxhighlight lang="bash">tput cub1 # one position to the left
tput cuf1 # one position to the right
tput cuu1 # up one line
Line 714 ⟶ 1,327:
 
tput hpa $WIDTH # end of line
tput cup $HEIGHT $WIDTH # bottom right corner</langsyntaxhighlight>
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">import "timer" for Timer
import "io" for Stdout
 
System.write("\e[2J") // clear terminal
System.write("\e[12;40H") // move to (12, 40)
Stdout.flush()
Timer.sleep(2000)
System.write("\e[D") // move left
Stdout.flush()
Timer.sleep(2000)
System.write("\e[C") // move right
Stdout.flush()
Timer.sleep(2000)
System.write("\e[A") // move up
Stdout.flush()
Timer.sleep(2000)
System.write("\e[B") // move down
Stdout.flush()
Timer.sleep(2000)
System.write("\e[G") // move to beginning of line
Stdout.flush()
Timer.sleep(2000)
System.write("\e[79C") // move to end of line (assuming 80 column terminal)
Stdout.flush()
Timer.sleep(2000)
System.write("\e[1;1H") // move to top left corner
Stdout.flush()
Timer.sleep(2000)
System.write("\e[24;80H") // move to bottom right corner (assuming 80 x 24 terminal)
Stdout.flush()
Timer.sleep(2000)
System.write("\e[1;1H") // home cursor again before quitting</syntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int I, X, Y, W, H;
[Cursor(10, 13); \set cursor to arbitrary location on screen
Line 741 ⟶ 1,388:
Cursor(W-1, H-1); \move to bottom right corner
I:= ChIn(1);
]</langsyntaxhighlight>
 
Moving the cursor position beyond the terminal boundaries simply makes
Line 749 ⟶ 1,396:
{{trans|Go}}
zkl doesn't know anything about terminals but can print Ansi terminal codes:
<langsyntaxhighlight lang="zkl">print("\e[2J\e[6;3H"); // clear screen, move to an initial position
Atomic.sleep(1); // pause to let cursor blink
print("\e[D"); // left
Line 760 ⟶ 1,407:
Atomic.sleep(1);
print("\e[;H"); // top left
Atomic.sleep(1);</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
 
<langsyntaxhighlight lang="zxbasic">10 PRINT CHR$(8);:REM cursor one position left
20 PRINT CHR$(9);:REM cursor one position right
30 GO SUB 500: REM get cursor position
Line 790 ⟶ 1,437:
610 POKE 23688,33-cc
620 POKE 23689,24-cr
630 RETURN</langsyntaxhighlight>
9,476

edits