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

From Rosetta Code
Content added Content deleted
(Added Kotlin)
Line 90: Line 90:
println("You entered '$s'")
println("You entered '$s'")
}</lang>
}</lang>

=={{header|Phix}}==
<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

clear_screen() -- clear the console
string s = getInput(3, 5, 8)
position(23,1)
printf(1,"You entered '%s'\n",{s})</lang>

Revision as of 05:41, 5 November 2018

Terminal control/Restricted width positional input/With wrapping is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Create a routine to obtain data entry using a specific place on the terminal screen. Data entry fields should be restricted to a specific length, and the cursor should not move beyond the input length.

The routine should accept parameters for row number, column number and input length, and should obtain a string value entered by the user.

For the purpose of this task, obtain input from the user, showing data entry at row 3, column 5, with input width restricted to a maximum of 8 characters.

Note: In this task input wrapping is allowed. If the length of input exceeds the maximum width, the left hand side of the field should disappear, allowing additional input to be obtained. However, the cursor must not move beyond the designated input length.

It is permissible to use navigation keys to see input field length.

(See Terminal control/Restricted width positional input/No wrapping for similar input routine with no wrapping).

For a similar task using a graphical user interface, see Graphical User Interface/Restricted width positional input/With wrapping.

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. <lang scala>// Kotlin Native v0.5

import kotlinx.cinterop.* import platform.windows.* import platform.posix.*

val ascii = 32..126 val conOut = GetStdHandle(STD_OUTPUT_HANDLE)!!

fun setCursor(p: COORD) = SetConsoleCursorPosition(conOut, p.readValue())

fun getInput(row: Short, col: Short, width: Int): String {

   require(row in 0..20 && col in 0..(79 - width) && width in 1..78) { "Invalid parameter(s)" }
   val coord = nativeHeap.alloc<COORD>().apply { X = col; Y = row }
   setCursor(coord)
   val sb = StringBuilder(width)
   var wlen = 0                                          // length of text in editing window
   var full = false
   loop@ while (true) {
       val ch = _getwch()                                // gets next character, no echo
       when (ch.toInt()) {
           3, 13 -> break@loop                           // break on Ctrl-C or enter key
           8 -> {                                        // mimic backspace
               if (wlen > 0) {
                   print("\b \b")
                   sb.length--
                   wlen--
               }
               if (sb.length > wlen) {
                   coord.apply { X = col; Y = row }
                   setCursor(coord)
                   print(sb.toString().takeLast(width))
                   wlen = width
               }
           }
           0, 224 -> _getwch()                           // consume extra character
           in ascii -> {                                 // echo ascii character
               sb.append(ch.toChar())
               if (!full) {
                   _putwch(ch)
                   wlen++
               }
               else if (sb.length > wlen) {
                   coord.apply { X = col; Y = row }
                   setCursor(coord)
                   print(sb.toString().takeLast(width))
               }
           }
           else -> {}                                    // igore other characters
       }
       full = wlen == width                              // wlen can't exceed width
   }
   nativeHeap.free(coord)
   return sb.toString()

}

fun main(args: Array<String>) {

   system("cls")  // clear the console
   val s = getInput(2, 4, 8)  // Windows console row/col numbering starts at 0
   memScoped {
       val coord = alloc<COORD>().apply { X = 0 ; Y = 22 }
       setCursor(coord)
   }
   println("You entered '$s'")

}</lang>

Phix

<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

clear_screen() -- clear the console string s = getInput(3, 5, 8) position(23,1) printf(1,"You entered '%s'\n",{s})</lang>