Terminal control/Cursor positioning: Difference between revisions
(→{{header|Python}}: whitespace) |
(Add AutoHotkey) |
||
Line 3: | Line 3: | ||
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. |
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. |
||
[[Terminal Control::task| ]] |
[[Terminal Control::task| ]] |
||
=={{header|AutoHotkey}}== |
|||
{{works with|AutoHotkey_L}} |
|||
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> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
Revision as of 01:42, 19 December 2011
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.
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>
BASIC
Applesoft BASIC
<lang Applesoft BASIC> 10 VTAB 6: HTAB 3
20 PRINT "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(3,6);"Hello"</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
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
Euphoria
<lang Euphoria>position(6,3) puts(1,"Hello")</lang>
Forth
<lang forth>2 5 at-xy ." Hello"</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>
Pascal
<lang Pascal> program cursor_pos; uses crt; begin
gotoxy(6,3); write('Hello');
end. </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.
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>
Retro
<lang Retro>with console'
- hello 3 6 at-xy "Hello" puts ;</lang>
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>
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>