Skip to content

Commit

Permalink
#15 flag renamed to --shell-equivalents, lists 3 shell jobs in total
Browse files Browse the repository at this point in the history
  • Loading branch information
epogrebnyak committed Mar 3, 2024
1 parent 73b02b9 commit eba115a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 35 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ Just a simple utility to explore `PATH` environment variable on both Windows and

## Workflow

`justpath` shows your `PATH` environment variable line by line with numbering, comments and highlighing
and helps detecting invalid or duplicate directories on your `PATH`.
`justpath` shows your `PATH` environment variable line by line with numbering, comments and highlighing and helps detecting invalid or duplicate directories on your `PATH`.

You can also create a modified version of `PATH` string and use it to set `PATH` variable in your shell startup script or through an environment manager.
Note that `justpath` itself cannot change your shell `PATH`.
Expand Down Expand Up @@ -91,6 +90,12 @@ Are there any duplicate directories in `PATH`?
justpath --duplicates
```

Same, but resolving symbolic links:

```
justpath --duplicates --follow-symlinks
```

What is the `PATH` without invalid paths and duplicates?

```console
Expand All @@ -109,6 +114,12 @@ A clean `PATH` string in OS-native format:
justpath --correct --string
```

One-line alternatives for `justpath` commands where they exist:

```console
justpath --shell-equivalents
```

## Useful cases

### 1. Filter directory names
Expand Down Expand Up @@ -303,8 +314,8 @@ Few good links about CLI applications in general:
### Linux scripting

On Linux you can run `echo $PATH | tr ";" "\n"` to view your path line by line and
combine it with `grep`, `sort`, `uniq` and `wc -l` for the same effect
as most `justpath` commands.
combine it with `grep`, `sort`, `uniq` and `wc -l` for the similar effect
as `justpath` commands.

The benefit of a script is that you do not need to install any extra dependency.
The drawback is that not everyone is good at writing bash scripts.
Expand All @@ -313,8 +324,7 @@ Scripting would also be a bit more problematic on Windows.
Check out the discussion at [Hacker News](https://news.ycombinator.com/item?id=39493363)
about bash and zsh scripts and `justpath` scenarios.

> [!TIP] > `--shell-equivalent` flag provides a reference about one line commands for several shells.
> Try `justpath --raw --shell-equivalent` or `justpath --shell-equivalent`.
> [!TIP] > `--shell-equivalents` flag provides a reference about one line commands for several shells that do similar jobs as `justpath` itself. Try `justpath --shell-equivalents`.
### Other utilities

Expand Down
60 changes: 60 additions & 0 deletions justpath/oneliners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from dataclasses import dataclass


@dataclass
class OneLiner:
job: str
justpath_args: str
bash: str = ""
cmd: str = ""
ps: str = ""
python: str = ""

@property
def justpath(self):
return "justpath " + self.justpath_args

@property
def python_command(self):
return f'python -c "{self.python}"'

def print(self):
print(self.job)
if self.bash:
print("bash (Linux):\n ", self.bash)
if self.cmd:
print("cmd.exe (Windows):\n ", self.cmd)
if self.ps:
print("poweshell (Windows):\n ", self.ps)
if self.python:
print("python (any OS):\n ", self.python_command)
print("justpath (any OS):\n ", self.justpath)


RAW = OneLiner(
job="Print PATH as is",
justpath_args="--raw",
bash="echo $PATH",
cmd="echo %PATH%",
ps="echo $Env:PATH",
python="import os; print(os.environ['PATH']",
)

BY_LINE = OneLiner(
job="Print PATH by line (possibly with line numbers)",
justpath_args="",
bash='echo $PATH | tr ":" "\\n" | nl',
ps='$env:PATH.split(";")',
python="import os; print(os.environ['PATH'].replace(os.pathsep, '\\n'))",
)

DUPLICATES = OneLiner(
job="Show duplicate directories",
justpath_args="--duplicates # preserves order from PATH",
bash='echo $PATH | tr ":" "\\n" | sort | uniq -d # does not preserve order from PATH',
)

def print_alternatives():
for job in [RAW, DUPLICATES, BY_LINE]:
job.print()
print()
31 changes: 5 additions & 26 deletions justpath/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from colorama import Fore, Style
from typer import Option, Typer

from justpath.oneliners import print_alternatives


class PathVar(UserDict[int, Path]):
@classmethod
Expand Down Expand Up @@ -128,34 +130,11 @@ def show(
color: option("Use color to highlight errors.") = True, # type: ignore
string: option("Print a single string suitable as PATH content.") = False, # type: ignore
json: option("Format output as JSON.") = False, # type: ignore
shell_equivalent: option("Provide bash or cmd shell command.") = False, # type: ignore
shell_equivalents: option("Print useful commands for bash, cmd and Powershell.") = False, # type: ignore
):
"""Show directories from PATH."""
if shell_equivalent:
any_flag = any(
[
raw,
count,
sort,
invalid,
purge_invalid,
duplicates,
purge_duplicates,
includes,
excludes,
]
)
if raw:
print("bash:\n echo $PATH")
print("cmd.exe:\n echo %PATH%")
print("poweshell:\n echo $Env:PATH")
print("python:\n python -c \"import os; print(os.environ['PATH'])\"")
if not any_flag:
print('bash:\n echo $PATH | tr ":" "\\n"')
print('poweshell:\n $env:PATH.split(";")')
print(
"python:\n python -c \"import os; print(os.environ['PATH'].replace(os.pathsep, '\\n'))\""
)
if shell_equivalents:
print_alternatives()
sys.exit(0)
if raw:
show_raw()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "justpath"
version = "0.0.15"
version = "0.0.16"
description = "Explore PATH environment variable on Windows and Linux."
authors = ["Evgeny Pogrebnyak <[email protected]>"]
license = "GNU"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_one_liners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from justpath.oneliners import RAW, BY_LINE, DUPLICATES


def test_oneliners():
RAW.print()
BY_LINE.print()
DUPLICATES.print()
4 changes: 2 additions & 2 deletions tests/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

commands = [
["--help"],
["--shell-equivalents"],
["--raw"],
["--raw", "--shell-equivalent"],
["--shell-equivalent"],
["--count"],
["--bare"],
["--sort", "--includes", "mingw", "--excludes", "tools"],
["--invalid"],
["--purge-invalid"],
["--duplicates"],
["--duplicates", "--follow-symlinks"],
["--purge-duplicates"],
["--correct"],
["--correct", "--follow-symlinks"],
Expand Down

0 comments on commit eba115a

Please sign in to comment.