How to resolve the algorithm Terminal control/Positional read step by step in the TXR programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Terminal control/Positional read step by step in the TXR programming language
Table of Contents
Problem Statement
Determine the character displayed on the screen at column 3, row 6 and store that character in a variable. Note that it is permissible to utilize system or language provided methods or system provided facilities, system maintained records or available buffers or system maintained display records to achieve this task, rather than query the terminal directly, if those methods are more usual for the system type or language.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Terminal control/Positional read step by step in the TXR programming language
Source code in the txr programming language
;;; Type definitions and constants
(typedef BOOL (enum BOOL FALSE TRUE))
(typedef HANDLE cptr)
(typedef WCHAR wchar)
(typedef DWORD uint32)
(typedef WORD uint16)
(typedef SHORT short)
(typedef COORD
(struct COORD
(X SHORT)
(Y SHORT)))
(typedef SMALL_RECT
(struct SMALL_RECT
(Left SHORT)
(Top SHORT)
(Right SHORT)
(Bottom SHORT)))
(typedef CONSOLE_SCREEN_BUFFER_INFO
(struct CONSOLE_SCREEN_BUFFER_INFO
(dwSize COORD)
(dwCursorPosition COORD)
(wAttributes WORD)
(srWindow SMALL_RECT)
(dwMaximumWindowSize COORD)))
;;; Various constants
(defvarl STD_INPUT_HANDLE (- #x100000000 10))
(defvarl STD_OUTPUT_HANDLE (- #x100000000 11))
(defvarl STD_ERROR_HANDLE (- #x100000000 12))
(defvarl NULL cptr-null)
(defvarl INVALID_HANDLE_VALUE (cptr-int -1))
;;; Foreign Function Bindings
(with-dyn-lib "kernel32.dll"
(deffi GetStdHandle "GetStdHandle" HANDLE (DWORD))
(deffi GetConsoleScreenBufferInfo "GetConsoleScreenBufferInfo"
BOOL (HANDLE (ptr-out CONSOLE_SCREEN_BUFFER_INFO)))
(deffi ReadConsoleOutputCharacter "ReadConsoleOutputCharacterW"
BOOL (HANDLE (ptr-out (array 1 WCHAR))
DWORD COORD (ptr-out (array 1 DWORD)))))
;;; Now the character at <2, 5> -- column 3, row 6.
(let ((console-handle (GetStdHandle STD_OUTPUT_HANDLE)))
(when (equal console-handle INVALID_HANDLE_VALUE)
(error "couldn't get console handle"))
(let* ((cinfo (new CONSOLE_SCREEN_BUFFER_INFO))
(getinfo-ok (GetConsoleScreenBufferInfo console-handle cinfo))
(coord (if getinfo-ok
^#S(COORD X ,(+ 2 cinfo.srWindow.Left)
Y ,(+ 5 cinfo.srWindow.Top))
#S(COORD X 0 Y 0)))
(chars (vector 1))
(nread (vector 1))
(read-ok (ReadConsoleOutputCharacter console-handle chars
1 coord nread)))
(when (eq getinfo-ok 'FALSE)
(error "GetConsoleScreenBufferInfo failed"))
(prinl cinfo)
(when (eq read-ok 'FALSE)
(error "ReadConsoleOutputCharacter failed"))
(unless (plusp [nread 0])
(error "ReadConsoleOutputCharacter read zero characters"))
(format t "character is ~s\n" [chars 0])))
You may also check:How to resolve the algorithm Conjugate transpose step by step in the C programming language
You may also check:How to resolve the algorithm Rep-string step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the OCaml programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the SETL programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Rust programming language