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

Line 228:
println("You entered '$s'")
}</lang>
 
=={{header|Nim}}==
{{trans|Julia}}
Rather than using directly escape codes, we use the standard module <code>terminal</code> which works on Posix systems and on Windows. Unfortunately, there is no function to clear from cursor to end of line, so we had to provide one, but it works only on Posix systems.
 
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).
 
<lang Nim>import strformat, terminal
 
proc eraseLineEnd() = stdout.write("\e[K")
 
proc inputXYUpto(row, col, cmax: int; width = cmax): string =
while result.len < cmax and not ((let c = getch(); c) in ['\xff', '\f', '\r']):
setCursorPos(row, col)
eraseLineEnd()
if c in ['\b', '\x7f'] and result.len > 0:
result.setLen(result.len - 1)
else:
result.add c
stdout.write result[(if result.len > width: result.len - width else: 0)..result.high]
 
eraseScreen()
setCursorPos(3, 5)
let s = inputXYUpto(3, 5, 80, 8)
echo &"\n\n\nResult: You entered <<{s}>>"</lang>
 
=={{header|Phix}}==
Anonymous user