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

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Wren}}: Removed unused function.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 6 users not shown)
Line 17:
==={{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 34:
 
;; call the routine
(field-input-no-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 = ""
Do
Do: KBD = Inkey: Loop Until KBD <> ""
If KBD = Chr(8) Then
cadena = Left(cadena, Len(cadena) - 1)
Print !"\b ";
Else
If Len(cadena) < ancho Then cadena &= KBD
End If
Locate fila, columna : Print cadena;
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 42 ⟶ 64:
<br>
This uses _getch() rather than _getwch() as only ASCII is supported.
<langsyntaxhighlight lang="go">package main
 
/*
Line 105 ⟶ 127:
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. Note the code below is identical to the code in [[Terminal_control/Restricted_width_positional_input/No_wrapping]] but with no width argument in the input call.
<langsyntaxhighlight lang="julia">getch() = UInt8(ccall(:_getch, Cint, ()))
cls() = print("\33[2J")
reposition(row, col) = print("\u001b[$row;$(col)H")
Line 133 ⟶ 155:
s = input_y_x_upto(3, 5, 8)
println("\n\n\nResult: You entered <<$s>>")
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
Line 142 ⟶ 164:
 
It would also be possible to allow for printable Unicode characters (>= 160) to be entered by adding an extra clause to the 'when' statement. However, for this to work properly, you will need to be using a suitable code page (65001, say) and a suitable font (Lucida Console, say).
<langsyntaxhighlight lang="scala">// Kotlin Native v0.5
 
import kotlinx.cinterop.*
Line 183 ⟶ 205:
}
println("You entered '$s'")
}</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 189 ⟶ 211:
As in the Julia version we translated, the code is identical to the code in [[Terminal_control/Restricted_width_positional_input/No_wrapping]] but with no width argument in the input call. See this version for some general comments about the code.
 
<langsyntaxhighlight Nimlang="nim">import strformat, terminal
 
proc eraseLineEnd() = stdout.write("\e[K")
Line 206 ⟶ 228:
setCursorPos(3, 5)
let s = inputXYUpto(3, 5, 8)
echo &"\n\n\nResult: You entered <<{s}>>"</langsyntaxhighlight>
 
=={{header|PhixPerl}}==
Works on ArchLinux in an xterm.
<lang Phix>function getInput(integer row, col, width)
<syntaxhighlight lang="perl">#!/usr/bin/perl
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
elsif ch>=' ' and ch<='~' then
if length(s)<=width then
puts(1,ch)
s &= ch
end if
end if
end while
return s
end function
 
use strict; # https://rosettacode.org/wiki/Terminal_control/Restricted_width_positional_input/No_wrapping
clear_screen() -- clear the console
use warnings;
string s = getInput(3, 5, 8)
use List::Util qw( min max );
position(23,1)
$| = 1;
printf(1,"You entered '%s'\n",{s})</lang>
 
sub label { printf "\e[%d;%dH%s", @_; }
 
sub getinput
{
my ($row, $column, $length, $prompt) = @_;
defined $prompt and length $prompt < $column - 1 and
label( $row, $column - length $prompt, $prompt);
my $at = "\e[%d;%dH%-$length.${length}s\e[%d;%dH";
my $input = '';
my $pos = 0;
use Term::ReadKey;
eval
{
ReadMode 'cbreak';
local $_;
my $more = 1;
while( $more )
{
printf $at, $row, $column, $input, $row, $column + $pos;
0 < sysread *STDIN, $_, 1024 or last;
print "\e[10;1H\e[J"; use Data::Dump 'dd'; dd $_;
for( /\e[O\[][0-9;]*[A-~]|./gs )
{
/^[\n\r\e]\z/ ? do { $more = 0 } :
/^[ -~]\z/ ? do {
substr $input, $pos++, 0, $_; $input &= "\xff" x $length;
$pos = min $pos, $length } :
$_ eq "\b" ? do { $pos = max $pos - 1, 0; substr $input, $pos, 1, '' } :
$_ eq "\e[D" ? do { $pos = max 0, $pos - 1 } :
$_ eq "\e[C" ? do { $pos = min length $input, $pos + 1 } :
$_ eq "\e[H" ? do { $pos = 0 } :
$_ eq "\e[F" ? do { $pos = length $input } :
$_ eq "\e[3~" ? do { length $input and substr $input, $pos, 1, ''; } :
0;
}
}
};
ReadMode 'restore';
return $input;
}
 
print "\e[H\e[J";
#label(2, 5, "Enter input below");
my $text = getinput( 3, 25, 8, "test string: " );
print "\n\nyour input <$text>\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, backspace)</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;">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: #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: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">ch</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 242 ⟶ 322:
All printable character keys 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 304 ⟶ 384:
@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}}==
(This REXX program 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 324 ⟶ 404:
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 371 ⟶ 451:
 
var res = input.call(3, 5, 8)
System.print(res)</langsyntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc GetData(Col, Row, Width, Str);
int Col, Row, Width; char Str;
int I, C;
string 0;
[Cursor(Col, Row);
I:= 0;
loop [C:= ChIn(1); \Ctrl+C aborts
if C = $0D \CR\ then
[Str(I):= 0; \terminate\ quit];
if C = $08 \BS\ then
[if I > 0 then
[I:= I-1;
ChOut(0, C); \echo backspace
ChOut(0, ^ ); \erase character
ChOut(0, C); \echo backspace
];
];
if I<Width & C>=$20 & C<=$7E then
[ChOut(0, C); \echo character
Str(I):= C;
I:= I+1;
];
];
];
 
char Str(8+1);
[ChOut(0, $0C \FF\); \clear screen
GetData(5, 3, 8, Str);
Text(0, "
You entered: ");
Text(0, Str);
]</syntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Restricted_width_positional_input/No_wrapping
// by Galileo, 04/2022
 
clear screen
 
sub getInput$(r, c, long)
local text$, c$
c = c - 1
r = r - 1
print at(c, r);
do
c$ = inkey$
if c$ = "enter" break
if c$ = "backspace" then
text$ = left$(text$, len(text$) - 1)
print "\b ";
else
if len(text$) < long text$ = text$ + c$
end if
print at(c, r) text$;
loop
return text$
end sub
 
text$ = getInput$(3, 5, 8)
print at(1, 23) "You entered: ", text$</syntaxhighlight>
9,476

edits