Unix Programming - ncurses + history + readline

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2007 > ncurses + history + readline





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author ncurses + history + readline
lnatz

2007-02-08, 7:20 pm

Hi,
I am writing a shell(project). I would like to use readline, ncurses
and/or history to search history. I want to write a function that
would recognize the KEY_UP and KEY_DOWN,and search the history. I want
the upand down key to act the same as it would in a unix terminal. If
anyone could help me, I would greatly appreciate it.

Thank you in advance,
Natalie

Chris F.A. Johnson

2007-02-08, 7:20 pm

On 2007-02-08, lnatz wrote:
> Hi,
> I am writing a shell(project). I would like to use readline, ncurses
> and/or history to search history. I want to write a function that
> would recognize the KEY_UP and KEY_DOWN,and search the history. I want
> the upand down key to act the same as it would in a unix terminal. If
> anyone could help me, I would greatly appreciate it.


Please do not multipost. As this is a better group for the question
than comp.unix.shell, I'll post the answer here as well:

The readline library has everything you need and is very easy to
use (man readline).

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Thomas Dickey

2007-02-09, 7:20 am

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:

> The readline library has everything you need and is very easy to
> use (man readline).


It depends. I don't find anything in it that I need, or would choose to use.

ymmv.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Ido.Yehieli@gmail.com

2007-02-09, 7:19 pm

On Feb 9, 12:32 am, "lnatz" <nmrabinov...@gmail.com> wrote:
> I am writing a shell(project). I would like to use readline, ncurses
> and/or history to search history. I want to write a function that
> would recognize the KEY_UP and KEY_DOWN,and search the history.


history > file
vi file

ido.

Chris F.A. Johnson

2007-02-09, 7:19 pm

On 2007-02-09, Thomas Dickey wrote:
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
>
> It depends. I don't find anything in it that I need, or would choose to use.


It has everything the OP asked for.

> ymmv.


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Thomas Dickey

2007-02-10, 1:16 pm

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> On 2007-02-09, Thomas Dickey wrote:
[vbcol=seagreen]
> It has everything the OP asked for.


that hasn't been proven.

(but given your preference for hardcoded approaches to coding
it's unlikely that you'll provide any contrary details ;-)

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Chris F.A. Johnson

2007-02-10, 1:16 pm

On 2007-02-10, Thomas Dickey wrote:
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
>
> that hasn't been proven.


Given what the OP asked for (quoted below), what does it not
provide?

[vbcol=seagreen]
> (but given your preference for hardcoded approaches to coding


If you looked at my code, you would know that is not true.

I assume that you are referring to my use of "ANSI" screen codes in
shell scripts. If so, can you suggest a more portable way, in shell
scripts, than to use the closest thing there is to a standard for
terminals?

> it's unlikely that you'll provide any contrary details ;-)


Non-sequitur alert.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Thomas Dickey

2007-02-10, 1:16 pm

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:

[vbcol=seagreen]
> If you looked at my code, you would know that is not true.


oh. I've read your shell scripts (and they definitely illustrate my point).
What other code would you like us to read?

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Chris F.A. Johnson

2007-02-10, 7:18 pm

On 2007-02-10, Thomas Dickey wrote:
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
>
>
> oh. I've read your shell scripts (and they definitely illustrate my point).
> What other code would you like us to read?


Can you point to an example?

If you use screen codes as an example, please answer the seconf
part of my post (which you snipped).


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Thomas Dickey

2007-02-11, 1:30 am

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> On 2007-02-10, Thomas Dickey wrote:
[vbcol=seagreen]
> Can you point to an example?


> If you use screen codes as an example, please answer the seconf
> part of my post (which you snipped).


Screen codes aside, your scripts tend to have too much embedded data -
not enough abstraction to provide good reuse - but then that's _partly_
a limitation of the use of scripts. At the moment I'm looking at the
pop3-funcs-sh (date-funcs-sh would do for an example too ;-)

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Chris F.A. Johnson

2007-02-11, 1:30 am

On 2007-02-11, Thomas Dickey wrote:
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
>
>
> Screen codes aside, your scripts tend to have too much embedded data -
> not enough abstraction to provide good reuse


Examples?

> - but then that's _partly_ a limitation of the use of scripts. At
> the moment I'm looking at the pop3-funcs-sh (date-funcs-sh would do
> for an example too ;-)


Can you be more specific.

If there's something you can point to, then I will have to decide
whether it could be done better or not. If it can, I will.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Thomas Dickey

2007-02-13, 1:30 am

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> Can you be more specific.


> If there's something you can point to, then I will have to decide
> whether it could be done better or not. If it can, I will.


Offhand, things that are startling to read (and detract from maintainability).
Here's a chunk from pop3-funcs (note the misspelling ;-):

## Header labels begin with a label, followed by a colon.
## They usually only have initial capital only, but sometimes they are all
## caps, or other combinations. The variables store patterns that will match
## any combinastion of upper and lower case.
local subject='[Ss][Uu][Bb][Jj][Ee][Cc][Tt]:'
local from='[Ff][Rr][Oo][Mm]:'
local reply='[Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]:'
local msgID='[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]:'
local sender='[Ss][Ee][Nn][Dd][Ee][Rr]:'
local date='[Dd][Aa][Tt][Ee]:'
local to='[Tt][Oo]:'
local cc='[Cc][Cc]:'

....and the variables are used each in one place - in a case statement.
Separating the _control_ and _data_, one would for instance simply
uppercase the text to match and make the patterns simple to read.

Things like showheader() are startling to read, but granted due to
the limited capability of the scripting language, there's not much
that can be made to repair it...

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Chris F.A. Johnson

2007-02-21, 1:18 pm

On 2007-02-13, Thomas Dickey wrote:
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
>
> Offhand, things that are startling to read (and detract from maintainability).
> Here's a chunk from pop3-funcs (note the misspelling ;-):


What misspelling?

After sending my last post, I thought I should have added a
suggestion to ignore pop3-funcs; it is a dog's breakfast due to the
conditions under which it was written. It was a hasty response to
the SWEN virus a few years ago.

When I was writing my book, it got cleaned up a little, but the
publisher was more interested in meeting deadlines than in putting
out a good book (and the book'srequirements added other problems;
see below). A while after submitting the chapter, I almost
suggested deleting it altogether, but it was already in the
production process. I have had good comments about the chapter, so
perhaps it's not as bad as all that.

> ## Header labels begin with a label, followed by a colon.
> ## They usually only have initial capital only, but sometimes they are all
> ## caps, or other combinations. The variables store patterns that will match
> ## any combinastion of upper and lower case.
> local subject='[Ss][Uu][Bb][Jj][Ee][Cc][Tt]:'
> local from='[Ff][Rr][Oo][Mm]:'
> local reply='[Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]:'
> local msgID='[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]:'
> local sender='[Ss][Ee][Nn][Dd][Ee][Rr]:'
> local date='[Dd][Aa][Tt][Ee]:'
> local to='[Tt][Oo]:'
> local cc='[Cc][Cc]:'
>
> ...and the variables are used each in one place - in a case statement.
> Separating the _control_ and _data_, one would for instance simply
> uppercase the text to match and make the patterns simple to read.


Indeed. There were two reasons for doing it like this. One was to
separate this [moderately] ugly mess from the case statement; the
other, to keep the lines short enough to be within the guidelines
for the book.

Why I didn't convert the lines to uppercase first is a separate
issue: it would have required an external command. In my current
version, which still downloads all my mail, I use a dynamically
loadable builtin, ucase (see my article at
<http://www.unixreview.com/documents...06a/ur0606a.htm> ),
to do exactly what you suggest.

> Things like showheader() are startling to read, but granted due to
> the limited capability of the scripting language, there's not much
> that can be made to repair it...


Shell sripting is not nearly as limited as you appear to think.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Thomas Dickey

2007-02-21, 7:16 pm

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:[vbcol=seagreen]
> What misspelling?
^^^^^^^^^^^^
--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com