• mini-project: cdlog.

    From Kaz Kylheku@21:1/5 to All on Thu Jan 25 22:26:01 2024
    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: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Janis Papanagnou on Fri Jan 26 00:34:57 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kaz Kylheku on Fri Jan 26 00:31:35 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Janis Papanagnou on Fri Jan 26 00:03:05 2024
    On 2024-01-25, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
    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.

    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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julieta Shem@21:1/5 to Kaz Kylheku on Thu Jan 25 21:14:37 2024
    Kaz Kylheku <433-929-6894@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Kaz Kylheku on Fri Jan 26 00:53:19 2024
    On 2024-01-26, Kaz Kylheku <433-929-6894@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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kaz Kylheku on Fri Jan 26 02:34:33 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Kaz Kylheku on Thu Feb 8 21:21:24 2024
    On 2024-01-25, Kaz Kylheku <433-929-6894@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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kaz Kylheku on Fri Feb 9 00:37:39 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Janis Papanagnou on Fri Feb 9 00:17:51 2024
    On 2024-02-08, Janis Papanagnou <janis_papanagnou+ng@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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Kaz Kylheku on Fri Feb 9 01:01:26 2024
    On 2024-02-09, Kaz Kylheku <433-929-6894@kylheku.com> wrote:
    On 2024-02-08, Janis Papanagnou <janis_papanagnou+ng@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.)

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kaz Kylheku on Fri Feb 9 08:31:51 2024
    On 09.02.2024 01:17, Kaz Kylheku wrote:
    On 2024-02-08, Janis Papanagnou <janis_papanagnou+ng@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Kaz Kylheku on Fri Feb 23 22:04:20 2024
    On 2024-01-25, Kaz Kylheku <433-929-6894@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/

    - 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

    cdlog now has these features:

    - session recovery:
    - on startup, a new session is always allocated (lowest available #)
    - session recovery is now unobtrusive. On startup, an informational
    message is produced advising of the existence of prior sessions,
    and the use of the "cdr" (cdlog recover) command.
    - user can switch to an existing session, or clone the session's
    content into the current one.

    - mcd: change to a FIFO entry by menu

    - mcs: swap with a FIFO entry by menu

    - LRU mode: (enabled by cdlog_lru variable):
    - when you cd to a directory that is already in the FIFO, that FIFO
    entry is deleted, so effectively a rotation takes place: e.g

    PWD: /foo/bar -> cd /def -> /def

    FIFO: /abc /foo/bar
    /def /abc
    /ghi /ghi

    previous current dir /foo/bar is pushed into FIFO, but the
    matching /def is removed, so the FIFO acts as a LRU cache.
    The oldest entry is only evicted when there is a miss.

    - cd aliases:
    - using cdalias X Y you can define aliases you can use with "cd".
    this is a nice alternative to using CDPATH.

    - tab completion
    - custom tab completion for "cd" completes the above cd aliases,
    and also paths, and directories in CDPATH.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)