KeymapExample.cpp

Go to the documentation of this file.
00001 
00005 //
00006 // Date:      11 April  2006
00007 //            07 May    2006
00008 //
00009 // Copyright (c) Sergey Satskiy 2005 - 2006
00010 //               <sergesatsky@yahoo.com>
00011 //
00012 // Permission to copy, use, modify, sell and distribute this software 
00013 // is granted provided this copyright notice appears in all copies. 
00014 // This software is provided "as is" without express or implied
00015 // warranty, and with no claim as to its suitability for any purpose.
00016 //
00017 
00018 
00019 #include <iostream>
00020 #include <vector>
00021 #include <string>
00022 #include <map>
00023 #include <list>
00024 using namespace std;
00025 
00026     // The wrapper is in a single header file.
00027 #include "SReadline.h"
00028 using namespace swift;
00029 
00030 
00031     // User input prompt
00032 const string        Prompt( "Please input your command> " );
00033 
00034 
00035 
00036 
00037     // First parameter is a file name to save / load the
00038     // commands history. The second is a max number of stored commands
00039     // Both parameters could be ommited. In this case the history size
00040     // will be 64 and no save/restore operations will be performed.
00041 SReadline           Reader( "/tmp/.testhist", 32 );
00042 
00043     // Two keymaps to switch between them
00044 SKeymap             Keymap1( emacs_standard_keymap );   // A usual keymap
00045 SKeymap             Keymap2( false );                   // Printables are NOT bound
00046 
00047 
00048 
00049 
00050 int  PrintInfo( int  Count, int  Key )
00051 {
00052     cout << "Keymaps usage example v.1.0" << endl
00053          << "Press Ctrl-K to get back to the interactive mode." << endl;
00054     return 0;
00055 }
00056 
00057 int  SwitchToKeymap2( int  Count, int  Key )
00058 {
00059     Reader.SetKeymap( Keymap2 );
00060     cout << endl
00061          << "Entered to non-interactive mode. Use Ctrl-I for information." << endl;
00062     return 0;
00063 }
00064  
00065 int  SwitchToKeymap1( int  Count, int  Key )
00066 {
00067     Reader.SetKeymap( Keymap1 );
00068     cout << "Entering to interactive mode." << endl
00069          << "Press Ctrl-K to switch to the non-interactive mode." << endl;
00070 
00071         // Restore the command line as it was
00072     cout << Prompt << rl_line_buffer;
00073     return 0;
00074 }
00075  
00076 
00077 
00078 int  main( int  argc, char **  argv )
00079 { 
00080         // Prepare the keymap2
00081     Keymap2.Bind( 'a', PrintInfo );
00082     Keymap2.Bind( CTRL( 'I' ), PrintInfo );
00083     Keymap2.Bind( CTRL( 'K' ), SwitchToKeymap1 );
00084 
00085         // Bind a switch to keymap1
00086     Keymap1.Bind( CTRL( 'K' ), SwitchToKeymap2 );
00087 
00088 
00089         // Start with the usual keymap
00090     Reader.SetKeymap( Keymap1 );
00091 
00092 
00093     cout << "Use CTRL+K for switching keymaps" << endl;
00094     
00095         // Now we can ask user for a line
00096     string      UserInput;
00097     bool        EndOfInput( false );
00098 
00099     for ( ; ; )
00100     {
00101             // The last parameter could be ommited
00102         UserInput = Reader.GetLine( Prompt, EndOfInput );
00103         if ( EndOfInput )
00104         {
00105             cout << "End of the session. Exiting." << endl;
00106             break;
00107         }
00108         cout << "User input: '" << UserInput << "'." << endl;
00109         cout << "Press Ctrl+D for gracefull exit" << endl;
00110     }
00111         
00112     return 0;
00113 }
00114 

Generated on Mon May 8 21:58:07 2006 for Keymaps and SReadline wrapper usage example by  doxygen 1.4.5