Thread: Handling/Sowing history

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Handling/Sowing history

    I want to make a program that should show the history of commands like shells do using curves.

    My problem is that i don't know how to clear and re-write another command in the same position without print one more prompt.

    ex :
    myshell > command1
    By pressing the up-curve i want to do
    myshell > command2
    And not print and the last command like
    myshell > command1
    myshell > command2
    In reality i want to clear and write without clearing the whole screen.

    Is this easy or better use something else ?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This sort of terminal manupulation is non-portable. You could try to use the GNU readline library, which is meant for the exact task. Or you could roll something yourself using ncurses. The least preferable option would be hard coded terminal escapes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    It's for linux.
    Yes i know it's hard, probably i'll let it go for now.
    I suppose that i would use something like gotoxy in order to re-write command.

    Thanks

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ch4 View Post
    It's for linux.
    Yes i know it's hard, probably i'll let it go for now.
    I suppose that i would use something like gotoxy in order to re-write command.

    Thanks
    GNU doesn't mean Linux. GNU software is intended to be as widely portable as possible. Here's a Windows version of readline.

    Readline for Windows
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ch4 View Post
    It's for linux.
    Well, you are in luck since the shell does this using the readline() library.

    However, I've added a line history to a GUI before. It's fairly simple; you just have a function with a static array to hold the history and a static count as a current place holder. Of course, the GUI provides some features here -- have a look into ncurses(), it is simpler than a GUI API but you can get pretty nice stuff out of it. Certainly a line history will be easy; I *think* there might even be functions built in...

    Tab completion, that would be hard.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    Tab completion, that would be hard.
    GNU readline has tab-completion built in. You just need to provide a callback which takes a prefix and expands it into a list of possible options. readline takes care of the rest.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst readline and ncurses are both excellent solutions to the problem, there is a simpler solution (with some restrictions, however):
    Just print a carriage return ('\r'), and the cursor moves to the beginning of the line. Once that has happened, you can just re-print the line with the new content, and sufficient spaces plus backspaces to delete any remnants of the old line (if the old line was LONGER than the new one). So the output would look something like this:
    Code:
    prompt> command 1
    \r
    prompt> cmd 2       \b\b\b\b\b
    This is fairly portable, but it will not work EVERYWHERE.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to find information about C/C++ history
    By JOCAAN in forum Tech Board
    Replies: 1
    Last Post: 12-29-2007, 10:34 AM
  2. best way to search through 'history'
    By anykey in forum C++ Programming
    Replies: 2
    Last Post: 08-16-2005, 04:31 PM
  3. History of the Napkin
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2004, 01:41 AM
  4. I need help in history!
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 02-11-2003, 09:18 AM
  5. will not erase this history for internet explorer
    By HelpPLease123 in forum Tech Board
    Replies: 8
    Last Post: 11-02-2002, 01:18 AM