Thread: How to implement tab completion behaviour found in terminals?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question How to implement tab completion behaviour found in terminals?

    Hello all!

    I'm doing a terminal app and wanted to add the feature of completing the word when the user hits tab. Like in the terminals for folder names and such.

    The tiny problem is I have no idea of how to do that. For example what method to read from the user?
    Code:
    #include <iostream> 
    #include <string> 
    using namespace std; 
     
    int main () { 
      string mystr;
      cin >> mystr; //needs an enter and 
      getline (cin, mystr); //also
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you should read the FAQ and find out what "raw" terminal options are available to you.
    That is, you get every key as it is pressed.

    Standard C++ is line buffered, so you only get whole lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    I hope this wont annoy you but I don't understand your idea. And/or maybe I haven't explained mine very well either.

    You're saying to read the FAQ of my terminal and then see what raw options are available. Then I would use the method to run orders in the terminal from my program. Is this it?

    Because what I wanted is to have a C++ program to run from the command line that is able to complete commands every time the user hits the tab button.
    I thought that this would not be so difficult but maybe it is?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    How to get individual keys typed from the keyboard, without using buffered input, is operating system dependent. Windows has API calls, Linux/Unix has the termio system.

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    I see. So a program with that option would never be very portable pitty.

    Thanks for the reply, I'll see what I should use for Mac.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    wow, at first glance seems pretty awsome!

    I'll dig into it.

    Thanks!

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    I'm sorry for the noobness but I can't figure out how to use it.
    It's a library, so it's supposed to provide functions, right? Sort of like an API?

    Another issue is that, as far as I can understand, Mac users are supposed to use the 4.1 version (instead of 6.1). Before I figured this out I downloaded and installed the 6.1, now I' trying to do the same for 4.1 but get stuck here Fink - Package Database - Package readline (Comfortable terminal input library)

    Do you have experience using this?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by kotoko View Post
    Another issue is that, as far as I can understand, Mac users are supposed to use the 4.1 version (instead of 6.1).
    Not what I read; but, I have no idea how to use the library or whatever ReadLine is.

    MacOS X users may obtain MacOS X packages for readline-4.3 from Fink, and readline-6.0 from DarwinPorts or MacPorts.
    Last edited by stahta01; 11-05-2010 at 03:04 PM.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Perhaps learning the language should be your focus instead of trying to implement this or that with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM