Terminal control/Cursor movement: Difference between revisions

From Rosetta Code
Content added Content deleted
m (wiki syntax (third attempt))
m (promoted to task)
Line 1: Line 1:
{{draft task}}
{{task}}


The task is to demonstrate how to achieve movement of the terminal cursor:
The task is to demonstrate how to achieve movement of the terminal cursor:
Line 16: Line 16:
_Handling of out of bounds locomotion_
_Handling of out of bounds locomotion_


This task has no specific requirements in terms of 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. The generation of error messages relating to an out of bounds cursor position is permitted.
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.


=={{header|BASIC}}==
=={{header|BASIC}}==

Revision as of 20:45, 19 December 2012

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>

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>

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>

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>

UNIX Shell

<lang sh>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>

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>