Results 1 to 5 of 5

Thread: Prompt > Simple console demo of word whisperer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Prompt > Simple console demo of word whisperer

    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
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Petr Schreiber; 11-07-2012 at 09:26.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. MetaCircles - simple entity demo
    By Petr Schreiber in forum TBGL module by Petr Schreiber
    Replies: 5
    Last Post: 25-07-2011, 00:15
  2. Windows 7 cool tip for command prompt
    By kryton9 in forum Shout Box Area
    Replies: 3
    Last Post: 06-07-2010, 05:27
  3. Question About ThinBASIC Command Prompt
    By Mark Pruitt in forum thinBasic General
    Replies: 3
    Last Post: 07-06-2005, 01:07

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •