Terminal control/Cursor movement: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Removed extra curly brace)
No edit summary
Line 120: Line 120:
(call 'tput "home") # top left corner
(call 'tput "home") # top left corner
(call 'tput "cup" (sys "LINES") (sys "COLUMNS")) # bottom right corner</lang>
(call 'tput "cup" (sys "LINES") (sys "COLUMNS")) # bottom right corner</lang>


=={{header|Python}}==
{{libheader|curses}}
<lang Python>import curses

scr = curses.initscr()
# Demonstrate how to move the cursor one position to the left
def move_left():
y,x = curses.getyx()
curses.move(y,x-1)
# Demonstrate how to move the cursor one position to the right
def move_right():
y,x = curses.getyx()
curses.move(y,x+1)
# Demonstrate how to move the cursor up one line (without affecting its horizontal position)
def move_up():
y,x = curses.getyx()
curses.move(y-1,x)
# Demonstrate how to move the cursor down one line (without affecting its horizontal position)
def move_down():
y,x = curses.getyx()
curses.move(y+1,x)

# Demonstrate how to move the cursor to the beginning of the line
def move_line_home()
y,x = curses.getyx()
curses.move(y,0)

# Demonstrate how to move the cursor to the end of the line
def move_line_end()
y,x = curses.getyx()
maxy,maxx = scr.getmaxyx()
curses.move(y,maxx)

# Demonstrate how to move the cursor to the top left corner of the screen
def move_page_home():
curses.move(0,0)
# Demonstrate how to move the cursor to the bottom right corner of the screen
def move_page_end():
y,x = scr.getmaxyx()
curses.move(y,x)
</lang>


=={{header|Racket}}==
=={{header|Racket}}==

Revision as of 21:21, 11 August 2013

Task
Terminal control/Cursor movement
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to demonstrate how to achieve movement of the terminal cursor:

  • Demonstrate how to move the cursor one position to the left
  • Demonstrate how to move the cursor one position to the right
  • Demonstrate how to move the cursor up one line (without affecting its horizontal position)
  • Demonstrate how to move the cursor down one line (without affecting its horizontal position)
  • Demonstrate how to move the cursor to the beginning of the line
  • Demonstrate how to move the cursor to the end of the line
  • Demonstrate how to move the cursor to the top left corner of the screen
  • Demonstrate how to move the cursor to the bottom right corner of the screen

For the purpose of this task, it is not permitted to overwrite any characters or attributes on any part of the screen (so outputting a space is not a suitable solution to achieve a movement to the right).

Handling of out of bounds locomotion

This task has no specific requirements to trap or correct cursor movement beyond the terminal boundaries, so the implementer should decide what behaviour 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.

BASIC

Works with: GW-BASIC
Works with: QBasic

<lang qbasic>10 'move left 20 LOCATE , POS(0) - 1 30 'move right 40 LOCATE , POS(0) + 1 50 'move up 60 LOCATE CSRLIN - 1 70 'move down 80 LOCATE CSRLIN + 1 900 'beginning of line 100 LOCATE , 1 110 'end of line; requires previous knowledge of screen width -- typically 80 120 LOCATE , 80 130 'top left corner 140 LOCATE 1, 1 150 'bottom right corner; requires knowledge of screen dimensions (80x25 here) 160 LOCATE 25, 80</lang>

BBC BASIC

<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
     VDU 10   : REM Move down one line
     VDU 13   : REM Move to the beginning of the line
     VDU 30   : REM Move to the top left corner
     VDU 23,16,16;0;0;0; : REM Disable scrolling
     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</lang>

C#

Works with: Mono version 1.2
Works with: Visual C# version 2003

<lang csharp>static void Main(string[] args) {

   //There will be a 3 second pause between each cursor movement.
   Console.Write("\n\n\n\n     Cursor is here -->   ");
   System.Threading.Thread.Sleep(3000);
   Console.CursorLeft = Console.CursorLeft - 1; //Console.CursorLeft += -1 is an alternative.
   System.Threading.Thread.Sleep(3000);
   Console.CursorLeft = Console.CursorLeft + 1;
   System.Threading.Thread.Sleep(3000);
   Console.CursorTop = Console.CursorTop - 1;
   System.Threading.Thread.Sleep(3000);
   Console.CursorTop = Console.CursorTop + 1;
   System.Threading.Thread.Sleep(3000);
   Console.CursorLeft = 0; //Move the cursor far left.
   System.Threading.Thread.Sleep(3000);
   Console.CursorLeft = Console.BufferWidth - 1;
   /* BufferWidth represents the number of characters wide the console area is.
       * The exact value may vary on different systems.
       * As the cursor position is a 0 based index we must subtract 1 from buffer width or we move the cursor out of bounds.
       * In some cases WindowWidth may be preferable (however in this demonstration window and buffer should be the same).
       */ 
   System.Threading.Thread.Sleep(3000);
   Console.SetCursorPosition(0,0); //I have used an alternative method for moving the cursor here which I feel is cleaner for the task at hand.
   System.Threading.Thread.Sleep(3000);
   Console.SetCursorPosition(Console.BufferWidth-1, Console.WindowHeight-1); //Buffer height is usually longer than the window so window has been used instead.
   System.Threading.Thread.Sleep(3000);

} </lang>

Mathematica

<lang Mathematica>Run["tput cub1"] (* one position to the left *) Run["tput cuf1" ] (* one position to the right *) Run["tput cuu1" ] (* up one line *) Run["tput cud1"] (* down one line *) Run["tput cr"] (* beginning of line *) Run["tput home"] (* top left corner *)


WIDTH=RunThrough["tput cols", ""]; HEIGHT=RunThrough["tput lines", ""];

Run["tput hpa "<>WIDTH] (* end of line *) Run["tput cup "<>HEIGHT<>" "<> WIDTH] (* bottom right corner *)</lang>

Perl 6

<lang perl6>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</lang>

PicoLisp

<lang PicoLisp>(call 'tput "cub1") # one position to the left (call 'tput "cuf1") # one position to the right (call 'tput "cuu1") # up one line (call 'tput "cud1") # down one line (call 'tput "cr") # beginning of the line (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</lang>


Python

Library: curses

<lang Python>import curses

scr = curses.initscr()

  1. Demonstrate how to move the cursor one position to the left

def move_left(): y,x = curses.getyx() curses.move(y,x-1)

  1. Demonstrate how to move the cursor one position to the right

def move_right(): y,x = curses.getyx() curses.move(y,x+1)

  1. Demonstrate how to move the cursor up one line (without affecting its horizontal position)

def move_up(): y,x = curses.getyx() curses.move(y-1,x)

  1. Demonstrate how to move the cursor down one line (without affecting its horizontal position)

def move_down(): y,x = curses.getyx() curses.move(y+1,x)

  1. Demonstrate how to move the cursor to the beginning of the line

def move_line_home() y,x = curses.getyx() curses.move(y,0)

  1. Demonstrate how to move the cursor to the end of the line

def move_line_end() y,x = curses.getyx() maxy,maxx = scr.getmaxyx() curses.move(y,maxx)

  1. Demonstrate how to move the cursor to the top left corner of the screen

def move_page_home(): curses.move(0,0)

  1. Demonstrate how to move the cursor to the bottom right corner of the screen

def move_page_end(): y,x = scr.getmaxyx() curses.move(y,x) </lang>

Racket

<lang racket>

  1. lang racket

(require (planet neil/charterm:3:0)) (define x 0) (define y 0)

(define (on-key k)

 (match k
   ['down   (move  0 -1)]
   ['up     (move  0 +1)]
   ['right  (move +1  0)]
   ['left   (move -1  0)]
   [else    #f]))

(define (move dx dy)

 (set! x (+ x dx))
 (set! y (+ y dy))
 (charterm-cursor x y))

(with-charterm

(charterm-clear-screen)
(charterm-cursor 0 0)
(let loop ([continue? #t])
  (when continue?
    (loop (on-key (charterm-read-key))))))

</lang>


REXX

Works with: PC/REXX
Works with: Personal REXX

This version only works with PC/REXX or Personal REXX. <lang rexx>/*REXX pgm demonstrates how to achieve movement of the terminal cursor. */

parse value scrsize() with sd sw /*find the display screen size. */ parse value cursor() with row col /*find where the cursor is now. */

colL=col-1; if colL==0 then colL=sw /*prepare to move cursor to left.*/ call cursor row,colL /*move cursor to the left (wrap).*/

colR=col+1; if colR>sw then colL=1 /*prepare to move cursor to right*/ call cursor row,colR /*move cursor to the right (wrap)*/

rowU=row-1; if rowU==0 then rowU=sd /*prepare to move cursor up. */ call cursor rowU,col /*move cursor up (with wrap). */

rowD=row+1; if rowD>sd then rowD=1 /*prepare to move cursor down. */ call cursor rowD,col /*move cursor down (with wrap). */

call cursor row,1 /*move cursor to beginning of row*/ call cursor row,sw /*move cursor to end of row*/ call cursor 1,1 /*move cursor to top left corner.*/ call cursor sd,sw /*move cursor to bot right corner*/

                                      /*stick a fork in it, we're done.*/</lang>

Tcl

Translation of: UNIX Shell

<lang tcl># Simplification wrapper for when we're actually affecting the terminal proc tput args {

   exec tput {*}$args >@stdout <@stdin

}

tput cub1; # one position to the left tput cuf1; # one position to the right tput cuu1; # up one line tput cud1; # down one line tput cr; # beginning of line tput home; # top left corner

  1. For line ends and bottom, we need to determine size of terminal

set width [exec tput cols] set height [exec tput lines]

tput hpa $width; # end of line tput cpu $height $width; # bottom right corner</lang>

UNIX Shell

<lang bash>tput cub1 # one position to the left tput cuf1 # one position to the right tput cuu1 # up one line tput cud1 # down one line tput cr # beginning of line tput home # top left corner

  1. For line ends and bottom, we need to determine size
  2. of terminal

WIDTH=`tput cols` HEIGHT=`tput lines`

tput hpa $WIDTH # end of line tput cup $HEIGHT $WIDTH # bottom right corner</lang>

XPL0

<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations int I, X, Y, W, H; [Cursor(10, 13); \set cursor to arbitrary location on screen I:= ChIn(1); \wait for keystroke (no echo to display) ChOut(0, $08\BS\); \backspace moves one position right I:= ChIn(1); X:= Peek($40, $50); \get cursor location from BIOS data Y:= Peek($40, $51); Cursor(X+1, Y); \move one position right I:= ChIn(1); Cursor(X+1, Y-1); \move up one line I:= ChIn(1); ChOut(0, $0A\LF\); \line feed moves down one line I:= ChIn(1); ChOut(0, $0D\CR\); \carriage return moves to beginning of current line I:= ChIn(1); W:= Peek($40, $4A); \get width of display (standard = 80; mine = 94) Cursor(W-1, Y); \move to end of current line I:= ChIn(1); Cursor(0, 0); \move to top left corner I:= ChIn(1); H:= Peek($40, $84) + 1; \get height of display (standard = 25; mine = 50) Cursor(W-1, H-1); \move to bottom right corner I:= ChIn(1); ]</lang>

Moving the cursor position beyond the terminal boundaries simply makes the flashing cursor disappear. This is sometimes useful.

ZX Spectrum Basic

<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 40 IF cr>0 THEN LET cr=cr-1: GO SUB 550: REM cursor up one line 50 IF cr<22 THEN LET cr=cr+1: GO SUB 550: REM cursor down one line 60 POKE 23688,33: REM cursor to beginning of the line 70 POKE 23688,0: REM cursor to end of line 80 POKE 23688,33:POKE 23689,24: REM cursor to top left 90 REM bottom two rows are reserved for input and errors 100 REM so we reserve those lines here 110 POKE 23688,0: POKE 23689,2: REM bottom right

499 STOP: REM do not overrun into subroutines

500 REM get cursor position 510 LET cc=33-PEEK 23688:REM current column 520 LET cr=24-PEEK 23689:REM current row 530 RETURN

550 REM set cursor position 560 PRINT AT cr,cc; 570 RETURN

600 REM alternative set cursor position 610 POKE 23688,33-cc 620 POKE 23689,24-cr 630 RETURN</lang>