J - command line directory traversal tool

  • 11 Replies
  • 414 Views
*

bugmagnet

  • Master
  • ****
  • 252
  • Software Engineer, Propelis P/L
J - command line directory traversal tool
« on: February 16, 2017, 13:48:30 »
j.8th is a command-line navigation tool wrapping the functionality of PUSHD and POPD, adding shortcuts and optional commands to run once the change of directory has been achieved.

j.8th is executed within the context of j.cmd. Shortcuts are stored in "j.json"  which is stored in "%USERPROFILE%\.j\". Both the folder and the file are created if they do not exist. Shortcuts are words which resolve to paths in the directory. j.8th provides tools for adding and listing shortcuts.

j.cmd at its simplest is
   @echo off
   8th c:\bin\j.8th "%~f0" %*

(Currently where j.8th is stored is hardwired. That will change on my version eventually. Maybe sooner on yours.)

The result of any given run of j.8th adds one or two lines to j.cmd.

The following are examples of command lines:

   C:\> j -add root "%CD%"
   Creates an entry in the j.json file, mapping "root" to "C:\" (using the %CD% substitution.)
   
   C:\> j root Users Bruce Downloads
   Changes the directory to C:\Users\Bruce\Downloads (using PUSHD)
   
   C:\Users\Bruce\Downloads> j
   Changes back to the previous directory (using POPD)
   
   C:\> j -l
   Lists the contents of j.json as
      symbol -> path
      
   C:\> j -a home "%USERPROFILE%"
   Creates an entry in j.json, mapping "home" to "C:\Users\Bruce"
   
   C:\> j home Downloads -i dir /w
   Changes to C:\Users\Bruce\Downloads and then issues the "dir /w" command. "-i" means 'inline' in the sense that "dir/w" is appended to j.cmd
   
   C:\Users\Bruce\Downloads> j home Destkop # Machines -s mstsc Azure.rdp
   Changes to C:\Users\Bruce\Desktop\#\Machines and then issues "mstsc Azure.rdp" using "START".
   
   C:\Users\Bruce\Desktop\#\Machines> j -h
   Displays the help string:
      j [ [ -add | -a ] sym val | [ -delete | -d ] sym | -list | [ -help | -h ] | [ -jsondir path ] | [ -cmddir path ] ] | sym [subdir ...] [ -s shell_cmd | -i inline_cmd ] | <empty>

Reason for doing this:
   I started working with computers in 1977. Back then when there was only a command line (and sometimes not even that.) Despite the nice glossy front ends available nowadays I still live mostly on a command-line: fish (or bash) in Linux; CMD (or occasionally PowerShell) in Windows.
   I had written a purely batch version of J but wanted some more functionality. 8th provided the tools for that functionality.

*

bugmagnet

  • Master
  • ****
  • 252
  • Software Engineer, Propelis P/L
Re: J - command line directory traversal tool
« Reply #1 on: February 16, 2017, 13:49:29 »
P.S. Constructive critiques requested

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: J - command line directory traversal tool
« Reply #2 on: February 16, 2017, 15:27:48 »
Sounds cool!  I don't use Windows though, so my input wouldn't be too useful...

*

d.k

  • Apprentice
  • **
  • 32
Re: J - command line directory traversal tool
« Reply #3 on: February 16, 2017, 18:18:35 »
Nice and handy!  :D

*

RichAMead

  • beta
  • Guru
  • *****
  • 593
  • "We all live in a big black hole. No, really."
Re: J - command line directory traversal tool
« Reply #4 on: February 17, 2017, 03:44:57 »
Next is to make it cross-platform (and platform aware)    :P

*

bugmagnet

  • Master
  • ****
  • 252
  • Software Engineer, Propelis P/L
Re: J - command line directory traversal tool
« Reply #5 on: February 17, 2017, 14:29:54 »
I have looked into that. The biggest issue is whereas CMD lets you change the current directory, and the change sticks after the CMD script ends, bash doesn't. Rather everything runs in a subprocess with the calling process's directory unchanged and presumably unchangeable.

Whether it's totally impossible or not I don't know but I'm not investing a lot of time and energy in it.

In terms of it being platform aware, on one level that's fairly easy as 8th has words that tell me what platform I'm running on. At another level, however, I don't think that functionality is going to help much if the cd-doesn't-stick issue can't be solved.

The bottom line is that I wanted to give something to the 8th community other than bug reports.

BTW, if you see something in my code that is too verbose, please let me know.
« Last Edit: February 17, 2017, 14:38:20 by bugmagnet »

*

bugmagnet

  • Master
  • ****
  • 252
  • Software Engineer, Propelis P/L
Re: J - command line directory traversal tool
« Reply #6 on: February 17, 2017, 15:26:47 »
https://bash.cyberciti.biz/guide/Source_command looks like a possible path of enquiry

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: J - command line directory traversal tool
« Reply #7 on: February 18, 2017, 16:20:13 »
Yeah, not only current directory, but any environment variable changes made by a process don't translate up to the parent process, under POSIX kinds of OSes (Linux and macOS for us).  So that particular functionality of Windows (or is it disfunction?  I would argue the latter... ) cannot be replicated in a sane manner under POSIX.

*

bugmagnet

  • Master
  • ****
  • 252
  • Software Engineer, Propelis P/L
Re: J - command line directory traversal tool
« Reply #8 on: February 19, 2017, 04:35:37 »
From the POSIX side, Windows is dysfunctional and vice versa. It boils down to a design decision, the consequences of which we have to deal with daily.

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: J - command line directory traversal tool
« Reply #9 on: February 19, 2017, 05:10:25 »
The 'design decision' on the Windows side was the very bad one to continue to support legacy (MS-DOS) in the CLI.  Why does Windows need CR-LF?  Why does it have CLI tools which are *similar* to *nix ones, but sufficiently different to make switching between systems a pain?

MS was never (!) interested in cross-platform interoperability, despite having a POSIX subsystem in NT since the beginning (it was never very functional).  I worked there, once upon a time; I've seen a lot of cruft.  Finally they're claiming to have a working POSIX subsystem... only what, 20 years after NT released?

Now they've got the "PowerShell", rather than porting bash or something.  So if you want to do xplatform you still have to learn multiple toolchains.  Feh.

*

bugmagnet

  • Master
  • ****
  • 252
  • Software Engineer, Propelis P/L
Re: J - command line directory traversal tool
« Reply #10 on: February 20, 2017, 07:12:30 »
Ok, it appears that the changing of a parent's cwd is technically possible from within the child process. See

http://unix.stackexchange.com/questions/281994/changing-the-current-working-directory-of-a-certain-process which refers to https://rudd-o.com/linux-and-free-software/the-shell-challenge-changing-another-process-working-directory

I have had a brief fiddle in the Bash environment that Windows has recently started making available.
Code: [Select]
#!/bin/bash

cwd="$1" # target working directory

echo $BASHPID
echo $PPID

pid=$PPID

# now let's command the GNU debugger
gdb -q <<EOF
  attach $pid
  echo "$cwd"
  call chdir("$cwd")
  detach
  quit
EOF
It didn't work too well. I may try in "real" Linux if I feel sufficient motivated.

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: J - command line directory traversal tool
« Reply #11 on: February 20, 2017, 07:33:42 »
Huh.