Terminal control/Restricted width positional input/With wrapping: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 4 users not shown)
Line 23:
==={{header|ncurses}}===
To interface ncurses from Lisp, the ''[https://www.cliki.net/croatoan croatoan]'' library is used.
<langsyntaxhighlight lang="lisp">;; Load the library from the quicklisp repository
(ql:quickload "croatoan")
(in-package :croatoan)
Line 40:
 
;; call the routine
(field-input-with-wrapping 2 4 8)</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Function getInput(fila As Integer, columna As Integer, ancho As Integer) As String
Locate fila, columna, 0
Dim As String KBD, cadena = ""
Dim As Integer ini
Do
Do: KBD = Inkey: Loop Until KBD <> ""
If KBD = Chr(8) Then 'backspace
cadena = Left(cadena, Len(cadena) - 1)
Print !"\b ";
Else
cadena &= KBD
End If
ini = Iif(Len(cadena) > ancho, Len(cadena) - ancho + 1, 1)
Locate fila, columna : Print Mid(cadena, ini, ancho);
Loop Until KBD = Chr(13)
Return cadena
End Function
 
Dim As String s = getInput(3, 5, 8)
Locate 23,1 : Print "You entered: "; s
Sleep</syntaxhighlight>
 
 
=={{header|Go}}==
Line 47 ⟶ 75:
<br>
This uses _getch() rather than _getwch() as only ASCII is supported.
<langsyntaxhighlight lang="go">package main
 
/*
Line 127 ⟶ 155:
setCursor(coord)
fmt.Printf("You entered '%s'\n", s)
}</langsyntaxhighlight>
 
 
=={{header|Julia}}==
Requires an ANSI compatible terminal and a system C library implementing _getch() for unbuffered keyboard input.
<langsyntaxhighlight lang="julia">getch() = UInt8(ccall(:_getch, Cint, ()))
cls() = print("\33[2J")
reposition(row, col) = print("\u001b[$row;$(col)H")
Line 156 ⟶ 184:
s = input_y_x_upto(3, 5, 80, 8)
println("\n\n\nResult: You entered <<$s>>")
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{works with|Windows 10}}
This follows a similar approach to the Kotlin entry for the [[Terminal control/Restricted width positional input/No wrapping]] task except, of course, that the code now allows for wrapping.
<langsyntaxhighlight lang="scala">// Kotlin Native v0.5
 
import kotlinx.cinterop.*
Line 229 ⟶ 257:
}
println("You entered '$s'")
}</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 237 ⟶ 265:
We had also to check for backspace character and for character <code>'\x7f'</code> as the backspace key gives this last value (but Control+H gives <code>'\x08'</code> i.e. ASCII backspace character).
 
<langsyntaxhighlight Nimlang="nim">import strformat, terminal
 
proc eraseLineEnd() = stdout.write("\e[K")
Line 254 ⟶ 282:
setCursorPos(3, 5)
let s = inputXYUpto(3, 5, 80, 8)
echo &"\n\n\nResult: You entered <<{s}>>"</langsyntaxhighlight>
 
=={{header|PhixPerl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
<lang Phix>function getInput(integer row, col, width)
position(row,col)
string s = ""
while 1 do
integer ch = wait_key()
if ch='\r' then exit end if
if ch='\b' then
if length(s)>0 then
puts(1,"\b \b")
s = s[1..$-1]
end if
if length(s)>=width then
position(row,col)
puts(1,s[-width..-1])
end if
elsif ch>=' ' and ch<='~' then
s &= ch
if length(s)<=width then
puts(1,ch)
else
position(row,col)
puts(1,s[-width..-1])
end if
end if
end while
return s
end function
 
use strict; # https://rosettacode.org/wiki/Terminal_control/Restricted_width_positional_input/With_wrapping
clear_screen() -- clear the console
use warnings;
string s = getInput(3, 5, 8)
use Term::ReadKey;
position(23,1)
 
printf(1,"You entered '%s'\n",{s})</lang>
sub input
{
my ($row, $column, $length) = @_;
my ($input, $done, $start) = ( '', 0,
"\e[$row;${column}H" . ' ' x $length . "\e[$row;${column}H");
local $| = 1;
ReadMode 'raw';
until( $done )
{
print $start, substr $input, -$length;
local $_ = ReadKey 0;
if( tr/ -~// ) { $input .= $_ } # add char
elsif( tr/\cu// ) { $input = '' } # clear all
elsif( tr/\b\x7f// ) { chop $input } # delete last char
elsif( tr/\n\r\e\cc// ) { $done++ } # guess!
}
ReadMode 'restore';
return $input;
}
 
print "\e[H\e[Jinput at row 3 column 5 length 8\n";
my $in = input( 3, 5, 8 );
print "\n\n\ninput is $in\n\n";</syntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (position, wait_key)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">getInput</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">,</span><span style="color: #000000;">col</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'\r'</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'\b'</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\b \b"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">width</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">,</span><span style="color: #000000;">col</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">width</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'~'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ch</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)<=</span><span style="color: #000000;">width</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">,</span><span style="color: #000000;">col</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">width</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">clear_screen</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- clear the console</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">getInput</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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;">"You entered '%s'\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
 
=={{header|Raku}}==
Line 297 ⟶ 359:
All printable character keys (except Tab) work, as does backspace and enter. Ctrl-c to exit. All other keys / key-combos are ignored.
 
<syntaxhighlight lang="raku" perl6line>use Term::termios;
 
constant $saved = Term::termios.new(fd => 1).getattr;
Line 363 ⟶ 425:
@screen = "\e[41m{' ' x $cols}\e[0m" xx $rows;
print "\e[H\e[J{@screen.join: "\n"}\e[$row;{$column}H$str\e[$row;{$column + $pointer}H";
}</langsyntaxhighlight>
 
=={{header|REXX}}==
(Only works with: &nbsp; REXX/Personal)
<langsyntaxhighlight lang="rexx">/*REXX pgm reads text from the terminal screen from a certain row, column, and length.*/
parse arg row col len . /*obtain optional arguments from the CL*/
if row=='' | row=="," then row= 3 /*Not specified? Then use the default.*/
Line 383 ⟶ 445:
say 'data read from terminal row ' row " col " col ' length ' len " is:"
say $
exit 0 /*stick a fork in it, we're all done. */</langsyntaxhighlight>
 
=={{header|Wren}}==
Due to a bug the ''Stdin.readByte()'' method can currently process only the first byte of a multi-byte character. The others are skipped.
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin, Stdout
 
var textAtPos = Fn.new { |text, r, c|
Line 432 ⟶ 494:
 
var res = input.call(3, 5, 8)
System.print(res)</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Restricted_width_positional_input/With_wrapping
// by Galileo, 04/2022
 
Line 465 ⟶ 527:
text$ = getInput$(3, 5, 8)
print at(1, 23) "You entered: ", text$</langsyntaxhighlight>
9,482

edits