Terminal control/Positional read: Difference between revisions

m
(Terminal control/Positional read In FreeBASIC)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 4:
=={{header|Action!}}==
Method 1: terminal reading
<langsyntaxhighlight Actionlang="action!">proc Main()
byte CHARS, cursorinh=$2F0
 
Line 16:
position(2,4) printf("Character at column 2 row 2 was %C",CHARS)
 
return</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
{{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>
<langsyntaxhighlight AHKlang="ahk">DllCall( "AllocConsole" ) ; create a console if not launched from one
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
Loop 10
Line 57:
return out
return 0
}</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight ApplesoftBasiclang="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))</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> PRINT TAB(2,5) "Here"
char$ = GET$(2,5)
PRINT ''"Character at column 3 row 6 was " char$</langsyntaxhighlight>
{{works with|all BBC BASICs}} that support calling OSBYTE
<langsyntaxhighlight lang="bbcbasic"> PRINT TAB(2,5) "Here"
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
PRINT ''"Character at column 3 row 6 was CHR$(";char%;")"</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
The top Left corner Is at position 0,0
<langsyntaxhighlight 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
'after the end of Printed text - may be picked up instead of the untouched character in its place.
Line 94:
Dim As Integer char_ascii_value = Screen(6,3)
Locate 6, 14 : Print "Character at column 3, row 6 = "; Chr(char_ascii_value)
Sleep</langsyntaxhighlight>
 
 
==={{header|Locomotive Basic}}===
<langsyntaxhighlight lang="locobasic">10 LOCATE 3,6
20 a$=COPYCHR$(#0)</langsyntaxhighlight>
 
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:
The top left corner is (1, 1).
 
<langsyntaxhighlight lang="qbasic">c$ = CHR$(SCREEN(6, 3))</langsyntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
<langsyntaxhighlight lang="basic"> 10 REM The top left corner is at position 0,0
20 REM So we subtract one from the coordinates
30 LET c$ = SCREEN$(5,2)</langsyntaxhighlight>
 
 
Line 118:
 
{{libheader|Win32}}
<langsyntaxhighlight lang="c">#include <windows.h>
#include <wchar.h>
 
Line 150:
wprintf(L"Character at (3, 6) had been '%lc'\n", c);
return 0;
}</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
==={{header|ncurses}}===
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
<langsyntaxhighlight lang="lisp">(defun positional-read ()
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
;; print random characters in a 10x20 grid
Line 172:
(format scr "extracted char: ~A" char))
(refresh scr)
(get-char scr)))</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
/*
Line 203:
}
fmt.Printf("The character at column 3, row 6 is '%c'\n", c)
}</langsyntaxhighlight>
 
{{out}}
Line 212:
=={{header|Julia}}==
{{trans|Raku}}
<langsyntaxhighlight lang="julia">using LibNCurses
 
randtxt(n) = foldl(*, rand(split("1234567890abcdefghijklmnopqrstuvwxyz", ""), n))
Line 226:
ch = LibNCurses.winch(row, col)
LibNCurses.mvwaddstr(col, 52, "The character at ($row, $col) is $ch.") )
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
This is based on the C entry and works on Windows 10:
<langsyntaxhighlight lang="scala">// Kotlin Native version 0.3
 
import kotlinx.cinterop.*
Line 254:
else println("Something went wrong!")
}
}</langsyntaxhighlight>
 
{{out}}
Line 262:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 316:
move 14 1
refresh
endwin</langsyntaxhighlight>
 
{{out}}<pre>
Line 337:
{{libheader|nim-ncurses}}
This Nim version is inspired by Raku and Julia versions.
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strutils
import ncurses
 
Line 357:
discard getch()
 
endwin()</langsyntaxhighlight>
 
{{out}}
Line 375:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight Perllang="perl"># 20200917 added Perl programming solution
 
use strict;
Line 401:
$win->getch;
 
endwin;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Positional_read.exw
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: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 430:
=={{header|PowerShell}}==
This gets the character at position (3, 6) of the ''buffer'', not necessarily of the screen.
<langsyntaxhighlight lang="powershell">
$coord = [System.Management.Automation.Host.Coordinates]::new(3, 6)
$rect = [System.Management.Automation.Host.Rectangle]::new($coord, $coord)
$char = $Host.UI.RawUI.GetBufferContents($rect).Character
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import curses
from random import randint
 
Line 458:
 
curses.endwin()
</syntaxhighlight>
</lang>
 
{{out}}
Line 475:
=={{header|Racket}}==
Works in a CMD box on Windows:
<langsyntaxhighlight lang="racket">
#lang racket
(require ffi/unsafe ffi/unsafe/define)
Line 486:
(and (ReadConsoleOutputCharacterA (GetStdHandle -11) b 1 #x50002)
(printf "The character at 3x6 is <~a>\n" b))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Python}}
<syntaxhighlight lang="raku" perl6line>use NCurses;
 
# Reference:
Line 526:
delwin($win) if $win;
endwin;
}</langsyntaxhighlight>
 
{{out}}
Line 559:
{{works with|PC/REXX}}
{{works with|Personal REXX}}
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates reading a character from (at) at specific screen location. */
row = 6 /*point to a particular row on screen*/
col = 3 /* " " " " column " " */
Line 567:
 
other = scrRead(40, 3, 1) /*same thing, but for row forty. */
/*stick a fork in it, we're all done. */</langsyntaxhighlight><br><br>
 
=={{header|TXR}}==
<langsyntaxhighlight lang="txrlisp">;;; Type definitions and constants
 
(typedef BOOL (enum BOOL FALSE TRUE))
Line 641:
(unless (plusp [nread 0])
(error "ReadConsoleOutputCharacter read zero characters"))
(format t "character is ~s\n" [chars 0])))</langsyntaxhighlight>
 
Notes:
Line 651:
{{libheader|ncurses}}
An embedded program so we can ask the C host to communicate with ncurses for us.
<syntaxhighlight lang="wren">/* Terminal_control_Positional_read.wren */
<lang ecmascript>/* terminal_control_positional_read.wren */
 
import "random" for Random
Line 708:
// clean-up
win.delwin()
Ncurses.endwin()</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc Terminal_control_Positional_read.c -o Terminal_control_Positional_read -lncurses -lwren -lm */
<lang c>#include <stdio.h>
 
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 838 ⟶ 840:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "terminal_control_positional_readTerminal_control_Positional_read.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 854 ⟶ 856:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 874 ⟶ 876:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\stdlib;
int C;
[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
C:= CallInt($10, $0800, 0) & $00FF; \mask off attribute, leaving the character
]</langsyntaxhighlight>
 
{{omit from|ACL2}}
9,476

edits