Petr Schreiber
10-07-2012, 16:28
Sometimes we are (read "I am") lazy to write long words, and the laziness can affect even console type scripts. So I tried to implement something similar to Google "whisperer".
How does it work
I created simple Prompt procedure, which takes as input:
prompt for user
array full off possible suggestions
The function returns the string once user hits the Return (Enter) key.
To be able to "guess" what user wants to type, I need to check what user does character by character - this is possible thanks to Console_Waitkey (Console_Read and Console_ReadLine return after press of Enter, no sooner).
Once I detect the user pressed down arrow, I check if there is an suggestion active, and if yes, I fill it for the (lazy :)) user.
How to test
To test the procedure, I prepaired little example. The suggestions are stored in array named ... suggestions. They possible words are (feel free to modify):
the
thin
turtle
terrific
thinbasic
When you start the program, just type the letter "t", and you will see it will immediately offer you the word "terrific" (as it is the first match in alphabetical order), as you type other letters, you get the other suggestions or none (if there is no match).
Feel free to play with it, modify, use as you need. The possible uses could be help with typing directory name or other cases, when input can be partially predicted. You just need to fill array of suggestions and pass it to the function, the rest should be done automagically by the "engine".
Example
'
' Smart Prompt Test
'
' version 1.0
' Petr Schreiber 2012
'
Uses "console"
String suggestions(5) = "the", "thin", "turtle", "terrific", "thinbasic"
String sRetVal = Prompt("Enter the keyword: ", suggestions)
PrintL "You wrote " + sRetVal
PrintL
PrintL "Press any key to quit"
WaitKey
Function Prompt(sQuestion As String, suggestions() As String ) As String
' -- Let's keep the suggestions sorted for faster search
Array Sort suggestions()
' -- Print the question first
Print sQuestion
' -- Set contrast color for user reply
Console_SetTextAttribute(%CONSOLE_FOREGROUND_BLUE | %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY)
String sRetVal
String sChar, sSuggestion
Long xPos, yPos, j, found, offset
' -- Keep reading till user presses Return (aka Enter)
While sChar <> "RETURN"
' -- Store cursor position for later
xPos = Console_GetCursorX
yPos = Console_GetCursorY
' -- Read new characer
sChar = Grab$(Console_WaitKey(), "[", "]")
' -- If it is normal, just write it to terminal
If Len(sChar) = 1 Then
sRetVal += sChar
Print sChar
' -- If it is down key, user wants to fill in the suggestion
ElseIf sChar = "DOWN" Then
If Len(sSuggestion) Then
Console_SetCursorPosition(Len(sQuestion)+1, ypos+1)
Print sSuggestion
Console_SetCursorPosition(Len(sQuestion)+Len(sSuggestion)+1, ypos+1)
sRetVal = sSuggestion
End If
' -- Special handling for deleting
ElseIf sChar = "DELETE" Then
If xPos > Len(sQuestion) Then
Console_PrintAt(" ", xPos,ypos+1)
Console_SetCursorPosition(xpos, ypos)
sRetVal = LEFT$(sRetVal, Len(sRetVal)-1)
End If
End If
' -- Here we do the dynamic search (very suboptimal :)) for suggestions
' -- Only if user already entered something
If Len(sRetVal) Then
found = FALSE
' -- We search whole passed array
For j = LBound(suggestions) To UBound(suggestions)
' -- If the entered thing matches pattern of suggestion, it could be it!
If IsLike(suggestions(j), sRetVal+"*" , FALSE) Then
' -- Store suggestion for later use and print it on the screen
sSuggestion = suggestions(j)
offset = Len(sQuestion) + Len(sSuggestion)
Console_PrintAt($SPC(Len(sQuestion))+sSuggestion + $SPC(80-offset), 1,ypos+2)
found = TRUE
Exit For
End If
Next
' -- If nothing was found, then erase the suggestion line
If found = FALSE Then
Console_PrintAt($SPC(80), 1,ypos+2)
sSuggestion = ""
End If
Else
Console_PrintAt($SPC(80), 1,ypos+2)
sSuggestion = ""
End If
Wend
' -- Clean up after the suggester, if somehing was left on the screen to not waste one line
Console_PrintAt($SPC(80), 1,ypos+2)
' -- Force continuing on next line after question
PrintL
' -- Reset color back to normal
Console_SetTextAttribute(%CONSOLE_FOREGROUND_BLUE | %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_RED )
' -- Return the value
Return sRetVal
End Function
Petr
How does it work
I created simple Prompt procedure, which takes as input:
prompt for user
array full off possible suggestions
The function returns the string once user hits the Return (Enter) key.
To be able to "guess" what user wants to type, I need to check what user does character by character - this is possible thanks to Console_Waitkey (Console_Read and Console_ReadLine return after press of Enter, no sooner).
Once I detect the user pressed down arrow, I check if there is an suggestion active, and if yes, I fill it for the (lazy :)) user.
How to test
To test the procedure, I prepaired little example. The suggestions are stored in array named ... suggestions. They possible words are (feel free to modify):
the
thin
turtle
terrific
thinbasic
When you start the program, just type the letter "t", and you will see it will immediately offer you the word "terrific" (as it is the first match in alphabetical order), as you type other letters, you get the other suggestions or none (if there is no match).
Feel free to play with it, modify, use as you need. The possible uses could be help with typing directory name or other cases, when input can be partially predicted. You just need to fill array of suggestions and pass it to the function, the rest should be done automagically by the "engine".
Example
'
' Smart Prompt Test
'
' version 1.0
' Petr Schreiber 2012
'
Uses "console"
String suggestions(5) = "the", "thin", "turtle", "terrific", "thinbasic"
String sRetVal = Prompt("Enter the keyword: ", suggestions)
PrintL "You wrote " + sRetVal
PrintL
PrintL "Press any key to quit"
WaitKey
Function Prompt(sQuestion As String, suggestions() As String ) As String
' -- Let's keep the suggestions sorted for faster search
Array Sort suggestions()
' -- Print the question first
Print sQuestion
' -- Set contrast color for user reply
Console_SetTextAttribute(%CONSOLE_FOREGROUND_BLUE | %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY)
String sRetVal
String sChar, sSuggestion
Long xPos, yPos, j, found, offset
' -- Keep reading till user presses Return (aka Enter)
While sChar <> "RETURN"
' -- Store cursor position for later
xPos = Console_GetCursorX
yPos = Console_GetCursorY
' -- Read new characer
sChar = Grab$(Console_WaitKey(), "[", "]")
' -- If it is normal, just write it to terminal
If Len(sChar) = 1 Then
sRetVal += sChar
Print sChar
' -- If it is down key, user wants to fill in the suggestion
ElseIf sChar = "DOWN" Then
If Len(sSuggestion) Then
Console_SetCursorPosition(Len(sQuestion)+1, ypos+1)
Print sSuggestion
Console_SetCursorPosition(Len(sQuestion)+Len(sSuggestion)+1, ypos+1)
sRetVal = sSuggestion
End If
' -- Special handling for deleting
ElseIf sChar = "DELETE" Then
If xPos > Len(sQuestion) Then
Console_PrintAt(" ", xPos,ypos+1)
Console_SetCursorPosition(xpos, ypos)
sRetVal = LEFT$(sRetVal, Len(sRetVal)-1)
End If
End If
' -- Here we do the dynamic search (very suboptimal :)) for suggestions
' -- Only if user already entered something
If Len(sRetVal) Then
found = FALSE
' -- We search whole passed array
For j = LBound(suggestions) To UBound(suggestions)
' -- If the entered thing matches pattern of suggestion, it could be it!
If IsLike(suggestions(j), sRetVal+"*" , FALSE) Then
' -- Store suggestion for later use and print it on the screen
sSuggestion = suggestions(j)
offset = Len(sQuestion) + Len(sSuggestion)
Console_PrintAt($SPC(Len(sQuestion))+sSuggestion + $SPC(80-offset), 1,ypos+2)
found = TRUE
Exit For
End If
Next
' -- If nothing was found, then erase the suggestion line
If found = FALSE Then
Console_PrintAt($SPC(80), 1,ypos+2)
sSuggestion = ""
End If
Else
Console_PrintAt($SPC(80), 1,ypos+2)
sSuggestion = ""
End If
Wend
' -- Clean up after the suggester, if somehing was left on the screen to not waste one line
Console_PrintAt($SPC(80), 1,ypos+2)
' -- Force continuing on next line after question
PrintL
' -- Reset color back to normal
Console_SetTextAttribute(%CONSOLE_FOREGROUND_BLUE | %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_RED )
' -- Return the value
Return sRetVal
End Function
Petr