How to resolve the algorithm Terminal control/Cursor movement step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Terminal control/Cursor movement step by step in the Common Lisp programming language

Table of Contents

Problem Statement

Demonstrate how to achieve movement of the terminal cursor:

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).

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Terminal control/Cursor movement step by step in the Common Lisp programming language

Source code in the common programming language

(defun cursor-movement ()
  (with-screen (scr :input-blocking t :input-echoing nil :cursor-visible t)
    ;; display the screen and wait for a keypress
    (refresh scr) (get-char scr)

    (move-direction scr :right) (refresh scr) (get-char scr)
    (move-direction scr :left) (refresh scr) (get-char scr)
    (move-direction scr :down) (refresh scr) (get-char scr)
    (move-direction scr :up) (refresh scr) (get-char scr)
    ;; end of line
    (move scr (car (cursor-position scr)) (1- (width scr))) (refresh scr) (get-char scr)
    ;; beginning of line
    (move scr (car (cursor-position scr)) 0) (refresh scr) (get-char scr)
    ;; bottom right corner
    (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)))


  

You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Ring programming language
You may also check:How to resolve the algorithm Function frequency step by step in the Racket programming language
You may also check:How to resolve the algorithm Send email step by step in the Java programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Lua programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Scratch programming language