Terminal control/Cursor positioning: Difference between revisions
(Add BaCon) |
(Added Wren) |
||
Line 638:
Text(0, "Hello"); \upper-left corner is coordinate 0, 0
]</lang>
=={{header|Wren}}==
<lang ecmascript>System.write("\x1b[2J") // clear the terminal
System.print("\x1b[6;3HHello") // move to (6, 3) and print 'Hello'</lang>
=={{header|zkl}}==
|
Revision as of 12:57, 17 April 2020
You are encouraged to solve this task according to the task description, using any language you may know.
Move the cursor to column 3, row 6 and display the word "Hello", so that the letter H is in column 3 on row 6.
Ada
<lang Ada>with Ada.Text_IO;
procedure Cursor_Pos is
begin
Ada.Text_IO.Set_Line(6); Ada.Text_IO.Set_Col(3); Ada.Text_IO.Put("Hello");
end Cursor_Pos;</lang>
ARM Assembly
<lang ARM Assembly>
/* ARM assembly Raspberry PI */ /* program cursorPos.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"
szMessMovePos: .asciz "\033[6;3HHello\n"
szCarriageReturn: .asciz "\n"
szClear1: .byte 0x1B
.byte 'c' @ other console clear .byte 0
/* UnInitialized data */ .bss
/* code section */ .text .global main main:
ldr r0,iAdrszMessStartPgm @ display start message bl affichageMess ldr r0,iAdrszClear1 bl affichageMess ldr r0,iAdrszMessMovePos 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 iAdrszCarriageReturn: .int szCarriageReturn iAdrszClear1: .int szClear1 iAdrszMessMovePos: .int szMessMovePos
/******************************************************************/ /* 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
</lang>
AutoHotkey
Remember that AHK is not built for the console, so we must call the WinAPI directly. <lang AHK>DllCall( "AllocConsole" ) ; create a console if not launched from one hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
DllCall("SetConsoleCursorPosition", UPtr, hConsole, UInt, (6 << 16) | 3) WriteConsole(hConsole, "Hello")
MsgBox
WriteConsole(hConsole, text){ VarSetCapacity(out, 16) If DllCall( "WriteConsole", UPtr, hConsole, Str, text, UInt, StrLen(text) , UPtrP, out, uint, 0 ) return out return 0 }</lang>
Axe
Since the rows and columns are zero-indexed, we must subtract 1 from both. <lang axe>Output(2,5,"HELLO")</lang>
BaCon
<lang freebasic>' Cursor positioning, requires ANSI compliant terminal
GOTOXY 3,6
PRINT "Hello"</lang>
The X Y in GOTOXY
is Column Row order.
BASIC
Applesoft BASIC
<lang Applesoft BASIC> 10 VTAB 6: HTAB 3
20 PRINT "HELLO"</lang>
IS-BASIC
<lang IS-BASIC>100 PRINT AT 6,3:"Hello"</lang>
Locomotive Basic
<lang locobasic> 10 LOCATE 3,6
20 PRINT "Hello"</lang>
ZX Spectrum Basic
<lang zxbasic> 10 REM The top left corner is at position 0,0
20 REM So we subtract one from the coordinates 30 PRINT AT 5,2 "Hello"</lang>
BBC BASIC
<lang bbcbasic>PRINT TAB(2,5);"Hello"</lang>
Commodore BASIC
<lang basic> 100 print chr$(19) :rem change to lowercase set
110 print chr$(14) :rem go to position 1,1 120 print:print:print:print 130 print tab(2) "Hello" </lang>
Befunge
Assuming a terminal with support for ANSI escape sequences. <lang befunge>0"olleHH3;6["39*>:#,_$@</lang>
Blast
<lang blast># This will display a message at a specific position on the terminal screen .begin cursor 6,3 display "Hello!" return
- This is the end of the script</lang>
C/C++
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x:<lang c>#include <stdio.h> int main() { printf("\033[6;3HHello\n"); return 0; }</lang>
The C version of the minesweeper game uses curses. Minesweeper_game#C
On Windows, using console API: <lang c>#include <windows.h>
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos = {3, 6}; SetConsoleCursorPosition(hConsole, pos); WriteConsole(hConsole, "Hello", 5, NULL, NULL); return 0;
}</lang>
C#
<lang csharp>static void Main(string[] args) {
Console.SetCursorPosition(3, 6); Console.Write("Hello");
}</lang>
COBOL
<lang cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. cursor-positioning.
PROCEDURE DIVISION. DISPLAY "Hello" AT LINE 6, COL 3
GOBACK .</lang>
Common Lisp
ncurses
To interface the ncurses C library from Lisp, the croatoan library is used. <lang lisp>(defun cursor-positioning ()
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil) (move scr 5 2) (princ "Hello" scr) (refresh scr) ;; wait for keypress (get-char scr)))</lang>
D
ANSI escape sequences allow you to move the cursor anywhere on the screen. See more at: Bash Prompt HowTo - Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement
Position the Cursor: \033[<L>;<C>H or \033[<L>;<C>f puts the cursor at line L and column C.
<lang D> import std.stdio;
void main() {
writef("\033[6;3fHello");
} </lang>
Output:
0123456789 1 2 3 4 5 6 Hello 9 8 9
Elena
ELENA 4.x : <lang elena>public program() {
console.setCursorPosition(3,6).write("Hello")
}</lang>
Euphoria
<lang Euphoria>position(6,3) puts(1,"Hello")</lang>
F#
<lang fsharp>open System
Console.SetCursorPosition(3, 6) Console.Write("Hello")</lang>
Forth
<lang forth>2 5 at-xy ." Hello"</lang>
Fortran
Intel Fortran on Windows
<lang fortran>program textposition
use kernel32 implicit none integer(HANDLE) :: hConsole integer(BOOL) :: q hConsole = GetStdHandle(STD_OUTPUT_HANDLE) q = SetConsoleCursorPosition(hConsole, T_COORD(3, 6)) q = WriteConsole(hConsole, loc("Hello"), 5, NULL, NULL)
end program</lang>
Go
External command
<lang go>package main
import (
"bytes" "fmt" "os" "os/exec"
)
func main() {
cmd := exec.Command("tput", "-S") cmd.Stdin = bytes.NewBufferString("clear\ncup 5 2") cmd.Stdout = os.Stdout cmd.Run() fmt.Println("Hello")
}</lang>
ANSI escape codes
<lang go>package main
import "fmt"
func main() {
fmt.Println("\033[2J\033[6;3HHello")
}</lang>
Ncurses
<lang go>package main
import (
"log"
gc "code.google.com/p/goncurses"
)
func main() {
s, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() s.Move(5, 2) s.Println("Hello") s.GetChar()
}</lang>
Icon and Unicon
If the OS has older termcap files, CUP is included with link ansi <lang unicon>procedure main()
writes(CUP(6,3), "Hello")
end
procedure CUP(i,j)
writes("\^[[",i,";",j,"H") return
end</lang>
J
Using terminal positioning verbs of Terminal_control/Coloured_text#J <lang J>'Hello',~move 6 3</lang>
Julia
<lang julia>const ESC = "\u001B"
gotoANSI(x, y) = print("$ESC[$(y);$(x)H")
gotoANSI(3, 6) println("Hello") </lang>
Kotlin
<lang scala>// version 1.1.2
fun main(args: Array<String>) {
print("\u001Bc") // clear screen first println("\u001B[6;3HHello")
}</lang>
Lasso
<lang Lasso>local(esc = decode_base64('Gw=='))
stdout( #esc + '[6;3HHello')</lang>
Liberty BASIC
<lang lb>locate 3, 6 print "Hello"
</lang>
Logo
<lang logo>setcursor [2 5] type "Hello</lang> You can also draw positioned text on the turtle graphics window. <lang logo>setpos [20 50] setxy 20 30 ; alternate way to set position label "Hello</lang>
Mathematica
<lang Mathematica>Run["tput cup 6 3"] Print["Hello"]</lang>
Nim
<lang nim>import terminal setCursorPos(3, 6) echo "Hello"</lang>
NS-HUBASIC
<lang NS-HUBASIC>10 LOCATE 3,6 20 PRINT "HELLO"</lang>
OCaml
Using the library ANSITerminal:
<lang ocaml>#load "unix.cma"
- directory "+ANSITerminal"
- load "ANSITerminal.cma"
module Trm = ANSITerminal
let () =
Trm.erase Trm.Screen; Trm.set_cursor 3 6; Trm.print_string [] "Hello";
- </lang>
Pascal
<lang Pascal> program cursor_pos; uses crt; begin
gotoxy(6,3); write('Hello');
end. </lang>
Perl
Using the Term::Cap module: <lang perl> use Term::Cap;
my $t = Term::Cap->Tgetent; print $t->Tgoto("cm", 2, 5); # 0-based print "Hello"; </lang>
Phix
<lang Phix>position(6,3) puts(1,"Hello")</lang>
PHP
<lang PHP> echo "\033[".$x.",".$y."H"; // Position line $y and column $x. echo "\033[".$n."A"; // Up $n lines. echo "\033[".$n."B"; // Down $n lines. echo "\033[".$n."C"; // Forward $n columns. echo "\033[".$n."D"; // Backward $n columns. echo "\033[2J"; // Clear the screen, move to (0,0). </lang>
PicoLisp
<lang PicoLisp>(call 'tput "cup" 6 3) (prin "Hello")</lang>
PowerShell
The following will only work in the PowerShell console host. Most notably it will not work in the PowerShell ISE.
<lang powershell>$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 2,5
$Host.UI.Write('Hello')</lang>
Alternatively, in any PowerShell host that uses the Windows console, one can directly use the .NET Console
class:
<lang powershell>[Console]::SetCursorPosition(2,5)
[Console]::Write('Hello')</lang>
PureBasic
<lang PureBasic>EnableGraphicalConsole(#True) ConsoleLocate(3,6) Print("Hello")</lang>
Python
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x:<lang Python>print("\033[6;3HHello")</lang> On Windows it needs to import and init the colorama module first.
ANSI sequences are not recognized in Windows console, here is a program using Windows API:
<lang python>from ctypes import *
STD_OUTPUT_HANDLE = -11
class COORD(Structure):
pass
COORD._fields_ = [("X", c_short), ("Y", c_short)]
def print_at(r, c, s):
h = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) windll.kernel32.SetConsoleCursorPosition(h, COORD(c, r)) c = s.encode("windows-1252") windll.kernel32.WriteConsoleA(h, c_char_p(c), len(c), None, None)
print_at(6, 3, "Hello")</lang>
Racket
<lang racket>
- lang racket
(require (planet neil/charterm:3:0)) (with-charterm
(charterm-clear-screen) (charterm-cursor 3 6) (displayln "Hello World"))
</lang>
Raku
(formerly Perl 6) Assuming an ANSI terminal: <lang perl6>print "\e[6;3H"; print 'Hello';</lang>
Retro
<lang Retro>with console'
- hello 3 6 at-xy "Hello" puts ;</lang>
REXX
The REXX language doesn't have any cursor or screen management tools, but some REXX interpreters have added the functionality via different methods.
<lang rexx>/*REXX program demonstrates cursor position and writing of text to same.*/
call cursor 3,6 /*move the cursor to row 3, col 6*/ say 'Hello' /*write the text at that location*/
call scrwrite 30,50,'Hello.' /*another method. */
call scrwrite 40,60,'Hello.',,,14 /*another ... in yellow.*/</lang>
Ring
<lang ring>
- Project : Terminal control/Cursor positioning
for n = 1 to 5
see nl
next see " Hello" </lang> Output:
Hello
Ruby
<lang ruby>require 'curses'
Curses.init_screen begin
Curses.setpos(6, 3) # column 6, row 3 Curses.addstr("Hello")
Curses.getch # Wait until user presses some key.
ensure
Curses.close_screen
end</lang>
Scala
<lang scala>object Main extends App {
print("\u001Bc") // clear screen first println("\u001B[6;3HHello")
}</lang>
Seed7
The function setPos is portable and positions the cursor on the console window. SetPos is based on terminfo respectively the Windows console API.
<lang seed7>$ include "seed7_05.s7i";
include "console.s7i";
const proc: main is func
local var text: console is STD_NULL; begin console := open(CONSOLE); setPos(console, 6, 3); write(console, "Hello"); # Terminal windows often restore the previous # content, when a program is terminated. Therefore # the program waits until Return/Enter is pressed. readln; end func;</lang>
Tcl
<lang tcl>exec tput cup 5 2 >/dev/tty puts "Hello"</lang>
UNIX Shell
<lang sh># The tput utility numbers from zero, so we have subtracted 1 from row and column
- number to obtain correct positioning.
tput cup 5 2</lang>
Whitespace
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x (see below): <lang whitespace>
</lang>
This solution was generated from the following pseudo-Assembly. <lang asm>push "Hello" ;The characters are pushed onto the stack in reverse order push "[6;3H" push 27 ;ESC
push 11 ;Number of characters to print call 0 ;Calls print-string function exit
0:
dup jumpz 1 ;Return if counter is zero exch prtc ;Swap counter with the next character and print it push 1 sub ;Subtract one from counter jump 0 ;Loop back to print next character
1:
pop ret ;Pop counter and return</lang>
XPL0
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
[Cursor(2, 5); \3rd column, 6th row Text(0, "Hello"); \upper-left corner is coordinate 0, 0 ]</lang>
Wren
<lang ecmascript>System.write("\x1b[2J") // clear the terminal System.print("\x1b[6;3HHello") // move to (6, 3) and print 'Hello'</lang>
zkl
Using ANSI escape sequence, where ESC[y;xH moves curser to row y, col x: <lang zkl>print("\e[6;3H" "Hello");</lang>
- Programming Tasks
- Terminal control
- Ada
- ARM Assembly
- AutoHotkey
- Axe
- BaCon
- BASIC
- Applesoft BASIC
- IS-BASIC
- Locomotive Basic
- ZX Spectrum Basic
- BBC BASIC
- Commodore BASIC
- Befunge
- Blast
- C
- C++
- C sharp
- COBOL
- Common Lisp
- Ncurses
- D
- Elena
- Euphoria
- F Sharp
- Forth
- Fortran
- Go
- Curses
- Icon
- Unicon
- J
- Julia
- Kotlin
- Lasso
- Liberty BASIC
- Logo
- Mathematica
- Nim
- NS-HUBASIC
- OCaml
- Pascal
- Perl
- Phix
- PHP
- PicoLisp
- PowerShell
- PureBasic
- Python
- Racket
- Raku
- Retro
- REXX
- Ring
- Ruby
- Scala
- Seed7
- Tcl
- UNIX Shell
- Whitespace
- XPL0
- Wren
- Zkl
- ACL2/Omit
- GUISS/Omit
- Maxima/Omit
- PARI/GP/Omit
- Scratch/Omit