Terminal control/Cursor movement: Difference between revisions
Terminal control/Cursor movement (view source)
Revision as of 11:47, 13 February 2024
, 3 months ago→{{header|Wren}}: Changed to Wren S/H
(Added solution for Action!) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(5 intermediate revisions by 5 users not shown) | |||
Line 21:
=={{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 */
Line 92:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<
BYTE RTCLOK=$14
frames==+RTCLOK
Line 134:
Wait(d)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cursor_movement.png Screenshot from Atari 8-bit computer]
Line 140:
=={{header|Ada}}==
{{libheader|ANSIAda}}
<
with Ansi;
Line 176:
delay 2.000;
Put (Ansi.Restore);
end Movement;</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 267:
bx lr @ return
</syntaxhighlight>
=={{header|AutoHotkey}}==
<
hConsole:=DllCall("GetConsoleWindow","UPtr")
Stdout:=FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n")
Line 343:
bufferheight:=NumGet(&struct,2,"UShort")
return 1
}</
=={{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.
<
Output(X+1,Y)
Output(X,Y-1)
Line 354:
Output(15,Y)
Output(0,0)
Output(15,7)</
=={{header|BaCon}}==
<
' Default number of positions, if not specified, is 1.
Line 386:
' Bottom right
R = ROWS
GOTOXY C,R</
=={{header|BASIC}}==
Line 392:
{{works with|QBasic}}
<
20 LOCATE , POS(0) - 1
30 'move right
Line 407:
140 LOCATE 1, 1
150 'bottom right corner; requires knowledge of screen dimensions (80x25 here)
160 LOCATE 25, 80</
==={{header|Applesoft BASIC}}===
80-Column Text Card: Applesoft Control Codes
Line 414:
Apple II Family Identification
http://www.umich.edu/~archive/apple2/technotes/tn/misc/TN.MISC.007
<
100 DATA56,32,31,254,160,0
110 DATA176,1,136,140,13,3,96
Line 471:
430 HTAB PEEK(33)
440 GET A$
</syntaxhighlight>
=={{header|BBC BASIC}}==
<
VDU 9 : REM Move one position to the right
VDU 11 : REM Move up one line
Line 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</
=={{header|Befunge}}==
Line 522:
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">
#include<conio.h>
#include<dos.h>
Line 606:
return 0;
}
</syntaxhighlight>
=={{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 t)
;; display the screen and wait for a keypress
Line 627:
(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)))</
=={{header|C sharp|C#}}==
{{works with|Mono|1.2}}
{{works with|Visual C sharp|Visual C#|2003}}
<
{
//There will be a 3 second pause between each cursor movement.
Line 659:
System.Threading.Thread.Sleep(3000);
}
</syntaxhighlight>
=={{header|Forth}}==
Line 665:
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.
<
DECIMAL
Line 688:
( Define ANSI Forth names for these functions using our markup words)
: AT-XY ( col row -- ) SWAP <CUP> ;
: PAGE ( -- ) <ED> 1 1 <CUP> ;</
Rosetta Task
<
( move the cursor one position to the right) 1 <CUF>
( move the cursor up one line ) 1 <CUU>
Line 698:
( 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===
<
import (
Line 748 ⟶ 769:
cmd.Stdout = os.Stdout
return cmd.Run()
}</
===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.
<
import (
Line 771 ⟶ 792:
fmt.Print("\033[;H") // top left
time.Sleep(1 * time.Second)
}</
===Ncurses===
{{libheader|curses}}
<
import (
Line 833 ⟶ 854:
s.Refresh()
s.GetChar()
}</
=={{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}}
<
const moves = Dict( "left" => "[1D", "right" => "[1C", "up" => "[1A", "down" => "[1B",
"linestart" => "[9D", "topleft" => "[H", "bottomright" => "[24;79H")
Line 851 ⟶ 920:
println()
println()
</syntaxhighlight>
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<
const val ESC = "\u001B" // escape code
Line 877 ⟶ 946:
Thread.sleep(3000)
println()
}</
=={{header|Lasso}}==
<
local(esc = decode_base64('Gw=='))
Line 925 ⟶ 994:
// move the cursor to the bottom right corner of the screen
stdout(#esc + '[500;500H')
sleep(2000)</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Run["tput cuf1" ] (* one position to the right *)
Run["tput cuu1" ] (* up one line *)
Line 939 ⟶ 1,008:
Run["tput hpa "<>WIDTH] (* end of line *)
Run["tput cup "<>HEIGHT<>" "<> WIDTH] (* bottom right corner *)</
=={{header|Nim}}==
<
echo "Press the return key to go to next step."
Line 977 ⟶ 1,046:
# Move cursor to the bottom right corner.
setCursorPos(width - 1, height - 1)
waitUser()</
=={{header|Perl}}==
{{trans|Raku}}
<
system "tput cuf1"; sleep 1; # one position to the right
system "tput cuu1"; sleep 1; # up one line
Line 993 ⟶ 1,062:
system "tput cup $rows $cols"; # bottom right corner
sleep 1;</
=={{header|Phix}}==
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Cursor_movement.exw
Line 1,050 ⟶ 1,119:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</
=={{header|PicoLisp}}==
<
(call 'tput "cuf1") # one position to the right
(call 'tput "cuu1") # up one line
Line 1,060 ⟶ 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</
=={{header|Python}}==
{{libheader|curses}}
<
scr = curses.initscr()
Line 1,106 ⟶ 1,175:
y,x = scr.getmaxyx()
curses.move(y,x)
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket
(require (planet neil/charterm:3:0))
Line 1,134 ⟶ 1,203:
(when continue?
(loop (on-key (charterm-read-key))))))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
shell "tput cuf1"; # one position to the right
shell "tput cuu1"; # up one line
Line 1,150 ⟶ 1,219:
shell "tput hpa $cols"; # end of line
shell "tput cup $rows $cols"; # bottom right corner</
=={{header|REXX}}==
Line 1,157 ⟶ 1,226:
This version ''only'' works with PC/REXX or Personal REXX.
<
parse value scrsize() with sd sw /*find the display screen size. */
Line 1,179 ⟶ 1,248:
call cursor sd,sw /*move cursor to bot right corner*/
/*stick a fork in it, we're done.*/</
=={{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}}
<
val ESC = "\u001B" // escape code
Line 1,201 ⟶ 1,290:
}
}</
=={{header|Tcl}}==
{{trans|UNIX Shell}}
<
proc tput args {
exec tput {*}$args >@stdout <@stdin
Line 1,222 ⟶ 1,311:
tput hpa $width; # end of line
tput cpu $height $width; # bottom right corner</
=={{header|UNIX Shell}}==
<
tput cuf1 # one position to the right
tput cuu1 # up one line
Line 1,238 ⟶ 1,327:
tput hpa $WIDTH # end of line
tput cup $HEIGHT $WIDTH # bottom right corner</
=={{header|Wren}}==
<
import "io" for Stdout
Line 1,272 ⟶ 1,361:
Stdout.flush()
Timer.sleep(2000)
System.write("\e[1;1H") // home cursor again before quitting</
=={{header|XPL0}}==
<
int I, X, Y, W, H;
[Cursor(10, 13); \set cursor to arbitrary location on screen
Line 1,299 ⟶ 1,388:
Cursor(W-1, H-1); \move to bottom right corner
I:= ChIn(1);
]</
Moving the cursor position beyond the terminal boundaries simply makes
Line 1,307 ⟶ 1,396:
{{trans|Go}}
zkl doesn't know anything about terminals but can print Ansi terminal codes:
<
Atomic.sleep(1); // pause to let cursor blink
print("\e[D"); // left
Line 1,318 ⟶ 1,407:
Atomic.sleep(1);
print("\e[;H"); // top left
Atomic.sleep(1);</
=={{header|ZX Spectrum Basic}}==
<
20 PRINT CHR$(9);:REM cursor one position right
30 GO SUB 500: REM get cursor position
Line 1,348 ⟶ 1,437:
610 POKE 23688,33-cc
620 POKE 23689,24-cr
630 RETURN</
|