You are not logged in.

#1 2012-04-24 22:57:22

n0stradamus
Member
Registered: 2010-11-08
Posts: 94
Website

[SOLVED] C++ | Editing content on the command line

Hi!

It's me again with another episode of my stupid programming problems big_smile
Consider some string in memory that you want the user to be able to edit.
Let's say this string contains the word 'trayn' and you want the user to correct the obvious spelling mistake.
Program execution should look something like this, where ^ marks the cursor position:

Edit misspelled word here: trayn
                                ^

So I want the user to edit what we already have, so he doesn't need to re-enter the whole string.
Do I need an extra library for this or can I code that functionality without?

Looking forward to your answers!

Last edited by n0stradamus (2012-04-25 20:36:22)

Offline

#2 2012-04-24 23:52:34

bsilbaugh
Member
From: Maryland, USA
Registered: 2011-11-15
Posts: 141

Re: [SOLVED] C++ | Editing content on the command line

I don't think there is a simple solution to your problem that only uses primitive C/C++ constructs (if that's what you're using). This looks like a good place to use the ncurses library.

An alternative approach would be to print out a list of closely matching words from your dictionary, and then have the user enter the index of the correct word from the list; this is how the designers of the vim spell checker solved the problem.

EDIT: Fixed minor typo.

Last edited by bsilbaugh (2012-04-25 00:10:48)


- Good judgement comes from experience; experience comes from bad judgement. -- Mark Twain
- There's a remedy for everything but death. -- The wise fool, Sancho Panza
- The purpose of a system is what it does. -- Anthony Stafford Beer

Offline

#3 2012-04-25 00:16:58

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,564
Website

Re: [SOLVED] C++ | Editing content on the command line

It should be possible with readline though I don't know the right invocation.

It is possible in bash with "read", which I believe is just a front-end for readline.

#!/bin/bash
read -e -i "Default Value" -p "Prompt> " STRING && echo $STRING

Edit: Check out rl_insert_text(char *) with readline()

Edit2:

#include <stdio.h>
#include <readline/readline.h>

int defaultHook(void) {
	return rl_insert_text("Default Value");
}

int main() {
	char *line;
	rl_startup_hook = (Function*) defaultHook;
	line = readline("Prompt> ");
	printf("%s\n",line);
	return 0;
}

Edit 3: Whoa, this is too many edits.  Anyhow, this is just C, not C++.  As I haven't touched the latter in years, I'll leave it to you to figure out if it needs to be modified for C++.

Last edited by Trilby (2012-04-25 00:40:09)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2012-04-25 09:59:05

n0stradamus
Member
Registered: 2010-11-08
Posts: 94
Website

Re: [SOLVED] C++ | Editing content on the command line

Thanks, I didn't know the readline library was that easy to use.
I'll definitely look into it, when it comes down to implementing what I was wondering about.

@bsilbaugh: I thought ncurses needed your whole terminal to operate on? Could you point me to an example where you only use one line?
                     That could be interesting, because readline requires ncurses and I already have one big dependency smile

Offline

#5 2012-04-25 11:29:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,564
Website

Re: [SOLVED] C++ | Editing content on the command line

Hmm, I didn't realize ncurses was a dependency of readline.  Where do you expect this program to be used?  Readline is pretty standard on desktop *nix systems.  It seems though that the above example wouldn't work on OSX as easily.

You could do this just with ncurses, but to me it seems a bit like reinventing the wheel.  There is no need to use the init_scr function (or whatever the exact name is) with ncurses.  You can use just the input and cursor control functions like getch() and write your own loop to handle keypresses.  I did this once, and it took a good bit of work testing the current cursor position against the line length to handle insertions differently than appending characters at the end.  I fumbled for a while to get a version that worked really well, then I discovered readline and never looked back.

Perhaps I am missing something in ncurses that would handle this for you, but as I understand it, this is precisely what readline was made for.

Last edited by Trilby (2012-04-25 11:29:44)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2012-04-25 13:32:13

bsilbaugh
Member
From: Maryland, USA
Registered: 2011-11-15
Posts: 141

Re: [SOLVED] C++ | Editing content on the command line

You're right, generally speaking the ncurses interface consumes your whole terminal.

If you're wanting a simple command line interface, then I think Trilby's suggestion to use readline is a a good one. I'm kicking myself for not thinking of that.


- Good judgement comes from experience; experience comes from bad judgement. -- Mark Twain
- There's a remedy for everything but death. -- The wise fool, Sancho Panza
- The purpose of a system is what it does. -- Anthony Stafford Beer

Offline

#7 2012-04-25 20:35:58

n0stradamus
Member
Registered: 2010-11-08
Posts: 94
Website

Re: [SOLVED] C++ | Editing content on the command line

Ok, it was foolish of me to consider ncurses as an alternative to readline because of dependencies.
bash requires readline and I think pretty much every system has bash as the default shell smile

Thanks for pointing me to readline, I'll try it out now!

Offline

Board footer

Powered by FluxBB