Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mini-project: cdlog.

93 views
Skip to first unread message

Kaz Kylheku

unread,
Jan 25, 2024, 5:26:07 PMJan 25
to
You know how Bash has that directory stack feature: pushd, popd, dirs.

I used that for a couple of decades on and off but never fully warmed
fup to it.

Recently I made an alternative system.

https://www.kylheku.com/cgit/cdlog/about/

- The cd alias always pushes the current directory, into a
9 element FIFO.

- FIFO is available as $c1, $c2, ... $c9.

- First four entries also as $x, $y, $z and $w: two character
referencing.

- pd command pops back from FIFO: changes to $c1 and removes it.

- cs command switches (or rotates through explicitly specified elems):
- cs 2 # exchange: current <--> c2
- cs 2 4 # rotate: current <--- c2 <--- c4
# `--------------^

- cl lists first four FIFO entries

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazi...@mstdn.ca

Janis Papanagnou

unread,
Jan 25, 2024, 6:31:41 PMJan 25
to
On 25.01.2024 23:26, Kaz Kylheku wrote:
> You know how Bash has that directory stack feature: pushd, popd, dirs.

(Probably the ones that were inherited from Csh.)

>
> I used that for a couple of decades on and off but never fully warmed
> fup to it.

Understandably.

>
> Recently I made an alternative system.
>
> https://www.kylheku.com/cgit/cdlog/about/
>
> - The cd alias always pushes the current directory, into a
> 9 element FIFO.

A FIFO, not a stack?

>
> - FIFO is available as $c1, $c2, ... $c9.
>
> - First four entries also as $x, $y, $z and $w: two character
> referencing.
>
> - pd command pops back from FIFO: changes to $c1 and removes it.
>
> - cs command switches (or rotates through explicitly specified elems):
> - cs 2 # exchange: current <--> c2
> - cs 2 4 # rotate: current <--- c2 <--- c4
> # `--------------^
>
> - cl lists first four FIFO entries

The Kornshell book provides two versions for such additional 'cd'
functionality (as ksh code). One of it supports also a menu based
'mcd' (that works on the built up directory stack). I think it's
a very useful function. (Maybe a sensible extension for your lib?)
(A slight problem for some folks may be that it uses the same name
as "mcd(1) - change MSDOS directory".)

BTW, I noticed that the functions are provided by my Linux systems,
the 'mcd' can be found in file /usr/share/ksh/functions/dirs (so
no need to copy/paste the source from the book.).

Janis

Janis Papanagnou

unread,
Jan 25, 2024, 6:35:03 PMJan 25
to
On 26.01.2024 00:31, Janis Papanagnou wrote:
> On 25.01.2024 23:26, Kaz Kylheku wrote:
>> You know how Bash has that directory stack feature: pushd, popd, dirs.
>
> (Probably the ones that were inherited from Csh.)
>
>>[...]
>
> The Kornshell book provides two versions for such additional 'cd'
> functionality (as ksh code). [...]

That formulation may have been misleading. I meant not your logic but
the Csh one mentioned above, and one with the 'mcd' menu extension.

Janis

Kaz Kylheku

unread,
Jan 25, 2024, 7:03:11 PMJan 25
to
A simple mcd could just print out the directories (either the most
recent 4 or all 9), and let the user pick by number. Then, when
the selection is made, jump back to the start of the first line of the
output, and "clear to end of screen" using the right ANSI escape.

We don't need a highlighted bar that can be moved with arrow keys
or mouse protcool support or anything.

Julieta Shem

unread,
Jan 25, 2024, 7:14:42 PMJan 25
to
Kaz Kylheku <433-92...@kylheku.com> writes:

> You know how Bash has that directory stack feature: pushd, popd, dirs.
>
> I used that for a couple of decades on and off but never fully warmed
> fup to it.
>
> Recently I made an alternative system.
>
> https://www.kylheku.com/cgit/cdlog/about/

That's nice. The shell I use the most is ESHELL. I couldn't take
advantage of your cdlog. ESHELL has a cd- feature. I never stopped to
understand it. I inferred that cd = means list the directories. Then
cd-<integer> sets the shell to that directory.

%cd =
0: c:/whatever/somewhere0
1: c:/whatever/somewhere1
2: c:/whatever/somewhere2
[...]
29: c:/whatever/somewhere29

%cd -29

%pwd
c:/whatever/somewhere29

Kaz Kylheku

unread,
Jan 25, 2024, 7:53:24 PMJan 25
to
On 2024-01-26, Kaz Kylheku <433-92...@kylheku.com> wrote:
> A simple mcd could just print out the directories (either the most
> recent 4 or all 9), and let the user pick by number. Then, when
> the selection is made, jump back to the start of the first line of the
> output, and "clear to end of screen" using the right ANSI escape.
>
> We don't need a highlighted bar that can be moved with arrow keys
> or mouse protcool support or anything.

I shipped the feature. mcs lets you swap the current directory with
one from the menu. mcd will change to it, and push the current into
the FIFO.

Janis Papanagnou

unread,
Jan 25, 2024, 8:34:38 PMJan 25
to
On 26.01.2024 01:03, Kaz Kylheku wrote:
> [cd menu]
>
> We don't need a highlighted bar that can be moved with arrow keys
> or mouse protcool support or anything.

I didn't expect (nor wanted) such fancy stuff. :-)

Janis

Kaz Kylheku

unread,
Feb 8, 2024, 4:21:30 PMFeb 8
to
On 2024-01-25, Kaz Kylheku <433-92...@kylheku.com> wrote:
> You know how Bash has that directory stack feature: pushd, popd, dirs.
>
> I used that for a couple of decades on and off but never fully warmed
> fup to it.
>
> Recently I made an alternative system.
>
> https://www.kylheku.com/cgit/cdlog/about/

I've extended cdlog here and there.

- It has the menu selection we talked about, in the form of
mcd and mcs commands. When you umake a menu selection, the
menu is erased from the screen and the cursor retraces.

- pd (pop directory) can take a number to pop from that point
rather than the top (delete that entry and change to it).

- cll (cd log long) lists all nine entries, unlike cl.

- peristence: whenever the state changes, the current working
directory and the 9 element FIFO are saved to ~/.cdlog.dirs.
On startup, this state is recovered.

Janis Papanagnou

unread,
Feb 8, 2024, 6:37:45 PMFeb 8
to
On 08.02.2024 22:21, Kaz Kylheku wrote:
>
> - peristence: whenever the state changes, the current working
> directory and the 9 element FIFO are saved to ~/.cdlog.dirs.
> On startup, this state is recovered.

Is it possible to have or define individual .cdlog.dirs files?

(I'm asking because I'm using shell functions to persist the
current working directory _for each terminal instance_, so that
on reopening of any terminal I am again in the same tty specific
working directory context.)

Janis

Kaz Kylheku

unread,
Feb 8, 2024, 7:17:57 PMFeb 8
to
On 2024-02-08, Janis Papanagnou <janis_pap...@hotmail.com> wrote:
> On 08.02.2024 22:21, Kaz Kylheku wrote:
>>
>> - peristence: whenever the state changes, the current working
>> directory and the 9 element FIFO are saved to ~/.cdlog.dirs.
>> On startup, this state is recovered.
>
> Is it possible to have or define individual .cdlog.dirs files?

Well, yes; just a good set of requirement have to be identified
for that.

> (I'm asking because I'm using shell functions to persist the
> current working directory _for each terminal instance_, so that
> on reopening of any terminal I am again in the same tty specific
> working directory context.)

If you just have one session and bail out, and log in on a different
tty, then what?

One way is to have recovery workflow. Show the user what saved contexts
exist, including the date when they were saved, and let them pick the
one to use.

These contexts could be in some LRU list, so they don't proliferate.
If there are eight, the oldest one is deleted.

Kaz Kylheku

unread,
Feb 8, 2024, 8:01:31 PMFeb 8
to
On 2024-02-09, Kaz Kylheku <433-92...@kylheku.com> wrote:
> On 2024-02-08, Janis Papanagnou <janis_pap...@hotmail.com> wrote:
>> On 08.02.2024 22:21, Kaz Kylheku wrote:
>>>
>>> - peristence: whenever the state changes, the current working
>>> directory and the 9 element FIFO are saved to ~/.cdlog.dirs.
>>> On startup, this state is recovered.
>>
>> Is it possible to have or define individual .cdlog.dirs files?
>
> Well, yes; just a good set of requirement have to be identified
> for that.
>
>> (I'm asking because I'm using shell functions to persist the
>> current working directory _for each terminal instance_, so that
>> on reopening of any terminal I am again in the same tty specific
>> working directory context.)
>
> If you just have one session and bail out, and log in on a different
> tty, then what?
>
> One way is to have recovery workflow. Show the user what saved contexts
> exist, including the date when they were saved, and let them pick the
> one to use.

I have it prototyped:

Example of choosing a context:

$ bash
These cdlog contexts exist:
[1]: Thu Feb 8 16:49:41 PST 2024
/home/kaz
/home/kaz/cdlog
[2]: Thu Feb 8 16:56:58 PST 2024
/home/kaz/junk
/home/kaz/cdlog
Make a selection or press Enter for new context: 2
$ pwd
/home/kaz/junk
$ cl
1: /home/kaz/cdlog
2:
3:
4:

We picked 2. And so it changd to the /home/kaz/junk directory (first
line of the file), and stuffed the rest of the file into the cdlog FIFO.

Example of creating a new context:

$ bash
These cdlog contexts exist:
[1]: Thu Feb 8 16:49:41 PST 2024
/home/kaz
/home/kaz/cdlog
[2]: Thu Feb 8 16:57:17 PST 2024
/home/kaz/junk
/home/kaz/cdlog
Make a selection or press Enter for new context:
$ echo $cdlog_dirs
/home/kaz/.cdlog.3.dirs
$ cl
1:
2:
3:
4:
$

The $cdlog_dirs variable shows us it is assigned the 3 slot,
namely ~/.cdlog.3.dirs.

When the slots fill up, the initialization code will find the oldest one
and nuke it, then use that number. (Code not yet tested.)

Janis Papanagnou

unread,
Feb 9, 2024, 2:31:58 AMFeb 9
to
On 09.02.2024 01:17, Kaz Kylheku wrote:
> On 2024-02-08, Janis Papanagnou <janis_pap...@hotmail.com> wrote:
>> On 08.02.2024 22:21, Kaz Kylheku wrote:
>>>
>>> - peristence: whenever the state changes, the current working
>>> directory and the 9 element FIFO are saved to ~/.cdlog.dirs.
>>> On startup, this state is recovered.
>>
>> Is it possible to have or define individual .cdlog.dirs files?
>
> Well, yes; just a good set of requirement have to be identified
> for that.

Yes, there's various behaviors that people might expects. Below
are mine described. (It's coupled to the tty device number, as
I've similarly set up my shell history files to depend on the tty
number. - Other folks may certainly have differing requirements.)

>
>> (I'm asking because I'm using shell functions to persist the
>> current working directory _for each terminal instance_, so that
>> on reopening of any terminal I am again in the same tty specific
>> working directory context.)
>
> If you just have one session and bail out, and log in on a different
> tty, then what?

In my case I memorize the cd context for each shell with own tty.
That means a new shell instance in the same tty will initially
operate on the same directory. If I close a shell terminal and
then open a new one it gets the same tty device number assigned
and thus the same (previous) working directory for that shell.
I couple the dot-file it with the terminal tty info from tty(1)
On a 'cd' I do pwd > "${HOME}/.sh_pwdstat_${TTYNR}" that gets
reloaded when a new shell is initialized. (There's a rare hangup
case I've observed that a shell plus its window gets closed but
the tty is still connected to the closed (zombie?) shell; here I
have to identify and kill the invisible shell process explicitly
so that the device number can be reassigned). But normally the
previous context is available (as desired by me) in the reopened
shell.

>
> One way is to have recovery workflow. Show the user what saved contexts
> exist, including the date when they were saved, and let them pick the
> one to use.

This is a grade of flexibility that is (for me) unnecessary (it
may be even a burden to see a menu). I'm not (not yet) using a
cd stack. But if so I'd certainly also couple it (like history
and the last working cd path) also to the tty; I'd probably just
extend my .sh_pwdstat_${TTYNR} to carry the stack of paths like
you've done in your .cdlog.dirs file.

>
> These contexts could be in some LRU list, so they don't proliferate.
> If there are eight, the oldest one is deleted.
>

Janis

0 new messages