Terminal control/Positional read: Difference between revisions

Content added Content deleted
(Terminal control/Positional read In FreeBASIC)
m (syntax highlighting fixup automation)
Line 4: Line 4:
=={{header|Action!}}==
=={{header|Action!}}==
Method 1: terminal reading
Method 1: terminal reading
<lang Action!>proc Main()
<syntaxhighlight lang="action!">proc Main()
byte CHARS, cursorinh=$2F0
byte CHARS, cursorinh=$2F0


Line 16: Line 16:
position(2,4) printf("Character at column 2 row 2 was %C",CHARS)
position(2,4) printf("Character at column 2 row 2 was %C",CHARS)


return</lang>
return</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
{{works with|AutoHotkey_L}}
<p>AutoHotkey is not built for the command line, so we need call the WinAPI directly.</p><p>For fun, this writes random characters to the command window so that it has something to retrieve. </p>
<p>AutoHotkey is not built for the command line, so we need call the WinAPI directly.</p><p>For fun, this writes random characters to the command window so that it has something to retrieve. </p>
<lang AHK>DllCall( "AllocConsole" ) ; create a console if not launched from one
<syntaxhighlight lang="ahk">DllCall( "AllocConsole" ) ; create a console if not launched from one
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
Loop 10
Loop 10
Line 57: Line 57:
return out
return out
return 0
return 0
}</lang>
}</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<lang ApplesoftBasic> 10 DEF FN C(H) = SCRN( H - 1,(V - 1) * 2) + SCRN( H - 1,(V - 1) * 2 + 1) * 16
<syntaxhighlight lang="applesoftbasic"> 10 DEF FN C(H) = SCRN( H - 1,(V - 1) * 2) + SCRN( H - 1,(V - 1) * 2 + 1) * 16
20 LET V = 6:C$ = CHR$ ( FN C(3))</lang>
20 LET V = 6:C$ = CHR$ ( FN C(3))</syntaxhighlight>


==={{header|BBC BASIC}}===
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> PRINT TAB(2,5) "Here"
<syntaxhighlight lang="bbcbasic"> PRINT TAB(2,5) "Here"
char$ = GET$(2,5)
char$ = GET$(2,5)
PRINT ''"Character at column 3 row 6 was " char$</lang>
PRINT ''"Character at column 3 row 6 was " char$</syntaxhighlight>
{{works with|all BBC BASICs}} that support calling OSBYTE
{{works with|all BBC BASICs}} that support calling OSBYTE
<lang bbcbasic> PRINT TAB(2,5) "Here"
<syntaxhighlight lang="bbcbasic"> PRINT TAB(2,5) "Here"
PRINT TAB(2,5); : REM Position cursor over character to read
PRINT TAB(2,5); : REM Position cursor over character to read
A%=&87:char%=((USR&FFF4)AND&FF00)DIV256 : REM Ask operating system to read character
A%=&87:char%=((USR&FFF4)AND&FF00)DIV256 : REM Ask operating system to read character
PRINT ''"Character at column 3 row 6 was CHR$(";char%;")"</lang>
PRINT ''"Character at column 3 row 6 was CHR$(";char%;")"</syntaxhighlight>


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
The top Left corner Is at position 0,0
The top Left corner Is at position 0,0
<lang freebasic>'Works on Windows. On Linux, the value returned can differ from the character shown on the console.
<syntaxhighlight lang="freebasic">'Works on Windows. On Linux, the value returned can differ from the character shown on the console.
'For example, unprintable control codes - such as the LF character (10) that implicitly occurs
'For example, unprintable control codes - such as the LF character (10) that implicitly occurs
'after the end of Printed text - may be picked up instead of the untouched character in its place.
'after the end of Printed text - may be picked up instead of the untouched character in its place.
Line 94: Line 94:
Dim As Integer char_ascii_value = Screen(6,3)
Dim As Integer char_ascii_value = Screen(6,3)
Locate 6, 14 : Print "Character at column 3, row 6 = "; Chr(char_ascii_value)
Locate 6, 14 : Print "Character at column 3, row 6 = "; Chr(char_ascii_value)
Sleep</lang>
Sleep</syntaxhighlight>




==={{header|Locomotive Basic}}===
==={{header|Locomotive Basic}}===
<lang locobasic>10 LOCATE 3,6
<syntaxhighlight lang="locobasic">10 LOCATE 3,6
20 a$=COPYCHR$(#0)</lang>
20 a$=COPYCHR$(#0)</syntaxhighlight>


Amstrad CPC screen memory only stores pixels but no character information (as opposed to e.g. the C64), so the firmware routine (TXT_UNWRITE) called by BASIC works by trying to find a match between screen pixels and the shape of a currently defined character. If the character table or screen pixels in the area of the character are changed between writing and reading, COPYCHR$ will therefore fail.
Amstrad CPC screen memory only stores pixels but no character information (as opposed to e.g. the C64), so the firmware routine (TXT_UNWRITE) called by BASIC works by trying to find a match between screen pixels and the shape of a currently defined character. If the character table or screen pixels in the area of the character are changed between writing and reading, COPYCHR$ will therefore fail.
Line 106: Line 106:
The top left corner is (1, 1).
The top left corner is (1, 1).


<lang qbasic>c$ = CHR$(SCREEN(6, 3))</lang>
<syntaxhighlight lang="qbasic">c$ = CHR$(SCREEN(6, 3))</syntaxhighlight>


==={{header|ZX Spectrum Basic}}===
==={{header|ZX Spectrum Basic}}===
<lang basic> 10 REM The top left corner is at position 0,0
<syntaxhighlight lang="basic"> 10 REM The top left corner is at position 0,0
20 REM So we subtract one from the coordinates
20 REM So we subtract one from the coordinates
30 LET c$ = SCREEN$(5,2)</lang>
30 LET c$ = SCREEN$(5,2)</syntaxhighlight>




Line 118: Line 118:


{{libheader|Win32}}
{{libheader|Win32}}
<lang c>#include <windows.h>
<syntaxhighlight lang="c">#include <windows.h>
#include <wchar.h>
#include <wchar.h>


Line 150: Line 150:
wprintf(L"Character at (3, 6) had been '%lc'\n", c);
wprintf(L"Character at (3, 6) had been '%lc'\n", c);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
==={{header|ncurses}}===
==={{header|ncurses}}===
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
<lang lisp>(defun positional-read ()
<syntaxhighlight lang="lisp">(defun positional-read ()
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
;; print random characters in a 10x20 grid
;; print random characters in a 10x20 grid
Line 172: Line 172:
(format scr "extracted char: ~A" char))
(format scr "extracted char: ~A" char))
(refresh scr)
(refresh scr)
(get-char scr)))</lang>
(get-char scr)))</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


/*
/*
Line 203: Line 203:
}
}
fmt.Printf("The character at column 3, row 6 is '%c'\n", c)
fmt.Printf("The character at column 3, row 6 is '%c'\n", c)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 212: Line 212:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Raku}}
{{trans|Raku}}
<lang julia>using LibNCurses
<syntaxhighlight lang="julia">using LibNCurses


randtxt(n) = foldl(*, rand(split("1234567890abcdefghijklmnopqrstuvwxyz", ""), n))
randtxt(n) = foldl(*, rand(split("1234567890abcdefghijklmnopqrstuvwxyz", ""), n))
Line 226: Line 226:
ch = LibNCurses.winch(row, col)
ch = LibNCurses.winch(row, col)
LibNCurses.mvwaddstr(col, 52, "The character at ($row, $col) is $ch.") )
LibNCurses.mvwaddstr(col, 52, "The character at ($row, $col) is $ch.") )
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
This is based on the C entry and works on Windows 10:
This is based on the C entry and works on Windows 10:
<lang scala>// Kotlin Native version 0.3
<syntaxhighlight lang="scala">// Kotlin Native version 0.3


import kotlinx.cinterop.*
import kotlinx.cinterop.*
Line 254: Line 254:
else println("Something went wrong!")
else println("Something went wrong!")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 262: Line 262:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang="ksh">
#!/bin/ksh
#!/bin/ksh


Line 316: Line 316:
move 14 1
move 14 1
refresh
refresh
endwin</lang>
endwin</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 337: Line 337:
{{libheader|nim-ncurses}}
{{libheader|nim-ncurses}}
This Nim version is inspired by Raku and Julia versions.
This Nim version is inspired by Raku and Julia versions.
<lang Nim>import random, sequtils, strutils
<syntaxhighlight lang="nim">import random, sequtils, strutils
import ncurses
import ncurses


Line 357: Line 357:
discard getch()
discard getch()


endwin()</lang>
endwin()</syntaxhighlight>


{{out}}
{{out}}
Line 375: Line 375:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang Perl># 20200917 added Perl programming solution
<syntaxhighlight lang="perl"># 20200917 added Perl programming solution


use strict;
use strict;
Line 401: Line 401:
$win->getch;
$win->getch;


endwin;</lang>
endwin;</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Positional_read.exw
-- demo\rosetta\Positional_read.exw
Line 415: Line 415:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n\n=&gt;%c"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n\n=&gt;%c"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 430: Line 430:
=={{header|PowerShell}}==
=={{header|PowerShell}}==
This gets the character at position (3, 6) of the ''buffer'', not necessarily of the screen.
This gets the character at position (3, 6) of the ''buffer'', not necessarily of the screen.
<lang powershell>
<syntaxhighlight lang="powershell">
$coord = [System.Management.Automation.Host.Coordinates]::new(3, 6)
$coord = [System.Management.Automation.Host.Coordinates]::new(3, 6)
$rect = [System.Management.Automation.Host.Rectangle]::new($coord, $coord)
$rect = [System.Management.Automation.Host.Rectangle]::new($coord, $coord)
$char = $Host.UI.RawUI.GetBufferContents($rect).Character
$char = $Host.UI.RawUI.GetBufferContents($rect).Character
</syntaxhighlight>
</lang>


=={{header|Python}}==
=={{header|Python}}==
<lang python>import curses
<syntaxhighlight lang="python">import curses
from random import randint
from random import randint


Line 458: Line 458:


curses.endwin()
curses.endwin()
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 475: Line 475:
=={{header|Racket}}==
=={{header|Racket}}==
Works in a CMD box on Windows:
Works in a CMD box on Windows:
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(require ffi/unsafe ffi/unsafe/define)
(require ffi/unsafe ffi/unsafe/define)
Line 486: Line 486:
(and (ReadConsoleOutputCharacterA (GetStdHandle -11) b 1 #x50002)
(and (ReadConsoleOutputCharacterA (GetStdHandle -11) b 1 #x50002)
(printf "The character at 3x6 is <~a>\n" b))
(printf "The character at 3x6 is <~a>\n" b))
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
{{trans|Python}}
{{trans|Python}}
<lang perl6>use NCurses;
<syntaxhighlight lang="raku" line>use NCurses;


# Reference:
# Reference:
Line 526: Line 526:
delwin($win) if $win;
delwin($win) if $win;
endwin;
endwin;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 559: Line 559:
{{works with|PC/REXX}}
{{works with|PC/REXX}}
{{works with|Personal REXX}}
{{works with|Personal REXX}}
<lang rexx>/*REXX program demonstrates reading a character from (at) at specific screen location. */
<syntaxhighlight lang="rexx">/*REXX program demonstrates reading a character from (at) at specific screen location. */
row = 6 /*point to a particular row on screen*/
row = 6 /*point to a particular row on screen*/
col = 3 /* " " " " column " " */
col = 3 /* " " " " column " " */
Line 567: Line 567:


other = scrRead(40, 3, 1) /*same thing, but for row forty. */
other = scrRead(40, 3, 1) /*same thing, but for row forty. */
/*stick a fork in it, we're all done. */</lang><br><br>
/*stick a fork in it, we're all done. */</syntaxhighlight><br><br>


=={{header|TXR}}==
=={{header|TXR}}==
<lang txrlisp>;;; Type definitions and constants
<syntaxhighlight lang="txrlisp">;;; Type definitions and constants


(typedef BOOL (enum BOOL FALSE TRUE))
(typedef BOOL (enum BOOL FALSE TRUE))
Line 641: Line 641:
(unless (plusp [nread 0])
(unless (plusp [nread 0])
(error "ReadConsoleOutputCharacter read zero characters"))
(error "ReadConsoleOutputCharacter read zero characters"))
(format t "character is ~s\n" [chars 0])))</lang>
(format t "character is ~s\n" [chars 0])))</syntaxhighlight>


Notes:
Notes:
Line 651: Line 651:
{{libheader|ncurses}}
{{libheader|ncurses}}
An embedded program so we can ask the C host to communicate with ncurses for us.
An embedded program so we can ask the C host to communicate with ncurses for us.
<lang ecmascript>/* terminal_control_positional_read.wren */
<syntaxhighlight lang="ecmascript">/* terminal_control_positional_read.wren */


import "random" for Random
import "random" for Random
Line 708: Line 708:
// clean-up
// clean-up
win.delwin()
win.delwin()
Ncurses.endwin()</lang>
Ncurses.endwin()</syntaxhighlight>
<br>
<br>
We now embed this in the following C program, compile and run it.
We now embed this in the following C program, compile and run it.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 854: Line 854:
free(script);
free(script);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 874: Line 874:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\stdlib;
<syntaxhighlight lang="xpl0">include c:\cxpl\stdlib;
int C;
int C;
[Cursor(3, 6); \move cursor to column 3, row 6 (top left = 0,0)
[Cursor(3, 6); \move cursor to column 3, row 6 (top left = 0,0)
\Call BIOS interrupt routine to read character (& attribute) at cursor position
\Call BIOS interrupt routine to read character (& attribute) at cursor position
C:= CallInt($10, $0800, 0) & $00FF; \mask off attribute, leaving the character
C:= CallInt($10, $0800, 0) & $00FF; \mask off attribute, leaving the character
]</lang>
]</syntaxhighlight>


{{omit from|ACL2}}
{{omit from|ACL2}}