myshell-py 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. myshell_py-0.2.0/.gitignore +31 -0
  2. myshell_py-0.2.0/PKG-INFO +195 -0
  3. myshell_py-0.2.0/README.md +179 -0
  4. myshell_py-0.2.0/myshell/__init__.py +1 -0
  5. myshell_py-0.2.0/myshell/__main__.py +5 -0
  6. myshell_py-0.2.0/myshell/aliases.py +22 -0
  7. myshell_py-0.2.0/myshell/arith.py +273 -0
  8. myshell_py-0.2.0/myshell/builtins.py +490 -0
  9. myshell_py-0.2.0/myshell/cli.py +155 -0
  10. myshell_py-0.2.0/myshell/compat.py +14 -0
  11. myshell_py-0.2.0/myshell/completion.py +137 -0
  12. myshell_py-0.2.0/myshell/coreutils.py +837 -0
  13. myshell_py-0.2.0/myshell/editor.py +218 -0
  14. myshell_py-0.2.0/myshell/errors.py +41 -0
  15. myshell_py-0.2.0/myshell/executor.py +752 -0
  16. myshell_py-0.2.0/myshell/expand.py +704 -0
  17. myshell_py-0.2.0/myshell/guide.py +609 -0
  18. myshell_py-0.2.0/myshell/heredoc.py +26 -0
  19. myshell_py-0.2.0/myshell/highlight.py +227 -0
  20. myshell_py-0.2.0/myshell/history.py +123 -0
  21. myshell_py-0.2.0/myshell/jobs.py +254 -0
  22. myshell_py-0.2.0/myshell/lexer.py +246 -0
  23. myshell_py-0.2.0/myshell/more_builtins.py +544 -0
  24. myshell_py-0.2.0/myshell/parser.py +437 -0
  25. myshell_py-0.2.0/myshell/paths.py +18 -0
  26. myshell_py-0.2.0/myshell/progress.py +98 -0
  27. myshell_py-0.2.0/myshell/prompt.py +197 -0
  28. myshell_py-0.2.0/myshell/quiz.py +148 -0
  29. myshell_py-0.2.0/myshell/state.py +107 -0
  30. myshell_py-0.2.0/pyproject.toml +31 -0
  31. myshell_py-0.2.0/pytest.ini +3 -0
  32. myshell_py-0.2.0/requirements.txt +16 -0
  33. myshell_py-0.2.0/shell.py +11 -0
  34. myshell_py-0.2.0/src/myshell/__init__.py +2 -0
  35. myshell_py-0.2.0/tests/conftest.py +169 -0
  36. myshell_py-0.2.0/tests/test_aliases.py +56 -0
  37. myshell_py-0.2.0/tests/test_arith.py +101 -0
  38. myshell_py-0.2.0/tests/test_builtins.py +260 -0
  39. myshell_py-0.2.0/tests/test_cli.py +93 -0
  40. myshell_py-0.2.0/tests/test_completion.py +169 -0
  41. myshell_py-0.2.0/tests/test_coreutils.py +269 -0
  42. myshell_py-0.2.0/tests/test_editor.py +211 -0
  43. myshell_py-0.2.0/tests/test_expand.py +250 -0
  44. myshell_py-0.2.0/tests/test_expansion_e2e.py +147 -0
  45. myshell_py-0.2.0/tests/test_guide.py +283 -0
  46. myshell_py-0.2.0/tests/test_heredoc.py +137 -0
  47. myshell_py-0.2.0/tests/test_highlight.py +137 -0
  48. myshell_py-0.2.0/tests/test_history.py +93 -0
  49. myshell_py-0.2.0/tests/test_history_fallback.py +204 -0
  50. myshell_py-0.2.0/tests/test_if.py +170 -0
  51. myshell_py-0.2.0/tests/test_interactive.py +300 -0
  52. myshell_py-0.2.0/tests/test_jobs.py +116 -0
  53. myshell_py-0.2.0/tests/test_language.py +283 -0
  54. myshell_py-0.2.0/tests/test_lexer.py +185 -0
  55. myshell_py-0.2.0/tests/test_loops.py +153 -0
  56. myshell_py-0.2.0/tests/test_more_builtins.py +285 -0
  57. myshell_py-0.2.0/tests/test_parser.py +122 -0
  58. myshell_py-0.2.0/tests/test_pipelines.py +110 -0
  59. myshell_py-0.2.0/tests/test_prompt.py +139 -0
  60. myshell_py-0.2.0/tests/test_quiz_progress.py +202 -0
  61. myshell_py-0.2.0/tests/test_redirects.py +192 -0
@@ -0,0 +1,31 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ .eggs/
6
+ build/
7
+ dist/
8
+
9
+ # Environments
10
+ .venv/
11
+ venv/
12
+ env/
13
+
14
+ # Test and lint caches
15
+ .pytest_cache/
16
+ .coverage
17
+ htmlcov/
18
+ .ruff_cache/
19
+ .mypy_cache/
20
+
21
+ # Editors and systems
22
+ .idea/
23
+ .vscode/
24
+ *.swp
25
+ .DS_Store
26
+ Thumbs.db
27
+
28
+ # Shell state files that may land in the project folder
29
+ .custom_shell_history
30
+ .myshellrc
31
+ .myshell_progress.json
@@ -0,0 +1,195 @@
1
+ Metadata-Version: 2.5
2
+ Name: myshell-py
3
+ Version: 0.2.0
4
+ Summary: A Unix-style shell written in Python, with a built-in guide.
5
+ License: MIT
6
+ Keywords: education,shell,terminal
7
+ Classifier: Environment :: Console
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Topic :: System :: Shells
10
+ Requires-Python: >=3.9
11
+ Requires-Dist: prompt-toolkit>=3.0.36
12
+ Provides-Extra: dev
13
+ Requires-Dist: pyflakes>=3.0; extra == 'dev'
14
+ Requires-Dist: pytest>=7.0; extra == 'dev'
15
+ Description-Content-Type: text/markdown
16
+
17
+ # myshell
18
+
19
+ A Unix-style shell written in Python. It runs on Linux and macOS with full job control. It also runs on Windows through a simpler fallback.
20
+
21
+ ## Requirements
22
+
23
+ - Python 3.9 or newer. Tested on 3.9 and 3.13 on Linux, macOS and Windows (see `.github/workflows/ci.yml`).
24
+ - `prompt_toolkit` is optional. With it you get Ctrl+R history search, grey suggestions and syntax colors. Without it the shell uses plain `input()` and readline.
25
+ - pytest and pyflakes for development: `pip install -e ".[dev]"`
26
+
27
+ ## Install and run
28
+
29
+ uvx myshell run without installing (after the first release)
30
+ uv tool install myshell install it
31
+ pip install myshell
32
+
33
+ From a clone:
34
+
35
+ python shell.py interactive shell
36
+ python shell.py -c "echo hi" run one command string
37
+ python shell.py script.sh a b run a script. $1 is a, $2 is b
38
+ python -m myshell same thing, as a package
39
+ uv run myshell uses pyproject.toml
40
+
41
+ `myshell -c 'cmd' NAME ARG...` sets `$0` to NAME and `$1...` to the ARGs.
42
+
43
+ ## Run the tests
44
+
45
+ pip install -e ".[dev]"
46
+ python -m pytest
47
+ python -m pyflakes myshell tests
48
+
49
+ With uv: `uv run --extra dev pytest`.
50
+
51
+ ## Release
52
+
53
+ uv build writes dist/*.whl and dist/*.tar.gz
54
+ uv publish needs a PyPI token or trusted publishing
55
+
56
+ Pushing a tag such as `v0.2.0` runs `.github/workflows/release.yml`, which builds and publishes with trusted publishing. Add the repository as a trusted publisher on pypi.org first (project `myshell`, workflow `release.yml`, environment `pypi`). Bump `version` in `pyproject.toml` before each release. If the name `myshell` is taken when you publish, change `name` in `pyproject.toml`. The command stays `myshell`.
57
+
58
+ ## Features
59
+
60
+ Commands and operators
61
+
62
+ - Pipes: `a | b`
63
+ - Lists: `a; b`, `a && b`, `a || b`
64
+ - Background jobs: `a &`
65
+ - Comments with `#`
66
+
67
+ Quoting and expansion
68
+
69
+ - Single quotes, double quotes and backslash escapes
70
+ - Variables: `$X`, `${X}`, `$?`, `$$`, and `$0 $1 ... $# $@ $*`
71
+ - Parameter expansion: `${X:-default}` `${X:=v}` `${X:?msg}` `${X:+alt}` `${#X}` `${X%.txt}` `${X%%p}` `${X#p}` `${X##p}` `${X/old/new}` `${X//old/new}` `${X:off:len}` `${X^^}` `${X,,}`
72
+ - Brace expansion: `file.{txt,md}`, `{1..5}`, `{a..e}`, `{01..10}`, `{1..10..2}`
73
+ - Command substitution: `$(cmd)` and backticks
74
+ - Arithmetic: `$((1 + 2 * x))`, including `+=`, `?:`, hex and octal
75
+ - Word splitting, globbing (`*`, `?`, `[ab]`) and tilde (`~`)
76
+ - Assignments: `NAME=value`, and `NAME=value cmd` for one command
77
+
78
+ Redirection
79
+
80
+ - `< > >> 2> 2>> &> &>>`
81
+ - `2>&1` and `1>&2`, applied left to right
82
+ - Here-strings: `<<<`
83
+ - Here-documents: `<<EOF` and `<<-EOF`. A quoted delimiter such as `<<'EOF'` turns expansion off.
84
+
85
+ Control flow
86
+
87
+ - `if`, `elif`, `else`, `fi`
88
+ - `while`, `until`, `for NAME in WORDS`, with `do` and `done`. A bare `for x; do` loops over `"$@"`.
89
+ - `case WORD in a|b) ...;; *.txt) ...;; *) ...;; esac`
90
+ - Functions: `name() { ...; }` or `function name { ...; }`, with `$1`, `$@`, `$#`, `return` and `local`
91
+ - Groups: `{ a; b; }`
92
+ - `break N` and `continue N`
93
+ - Blocks work on one line or across many lines
94
+
95
+ Builtins
96
+
97
+ - `echo` (with `-n -e -E`), `pwd cd exit alias unalias type which export unset source .`
98
+ - `read` (`-r -p -d -n`), `printf` (`-v`), `set` (`-e -u -x`, `-o`, `--`), `shift local return`
99
+ - `env` (`-i -u NAME=value`), `exec`, `trap` (EXIT, INT, TERM, HUP, USR1, USR2)
100
+ - `true false :`
101
+ - `jobs fg bg wait`
102
+ - `test` and `[ ]`: file, string and number checks, and `!`
103
+ - `history`: `N`, `-c`, `-d N`, `-w [file]`, `-r [file]`
104
+ - `guide`: lessons, cheat sheet and tips
105
+
106
+ Commands that work on every system
107
+
108
+ - `ls cat head tr sleep mkdir rm cp mv touch wc grep` are built in. A real program of the same name on your PATH wins, so on Linux and macOS you still get GNU or BSD tools. Where there is none (plain Windows), the built-in one runs. Set `MYSHELL_PORTABLE=1` to always use the built-in ones.
109
+ - They also work in pipes and `$(...)` without fork. This is what makes Pip's lessons run on Windows.
110
+
111
+ Interactive use
112
+
113
+ - Job control: Ctrl+C, Ctrl+Z, `fg`, `bg`
114
+ - Tab completion for commands, paths and function names
115
+ - With `prompt_toolkit`: Ctrl+R reverse search, grey suggestion from history (Right arrow or End takes it), colors (green known command, red unknown, yellow strings, cyan variables). `MYSHELL_EDITOR=basic` turns it off.
116
+ - After a command that takes longer than 2 seconds the shell prints `took 3.2s`. Set `SLOW_COMMAND_SECS` to change the limit, or to `off`.
117
+ - Traps: `trap 'cmd' EXIT` runs when the shell ends
118
+ - History saved in `~/.custom_shell_history`, limited to 1000 entries
119
+ - Startup file `~/.myshellrc`
120
+ - Prompt set with `PS1`. Continuation lines use `PS2`.
121
+
122
+ ## Pip, the guide
123
+
124
+ Pip is a small original character who lives in the shell.
125
+
126
+ ,---. .--------------------------------------.
127
+
128
+ ( ^ ^ ) | Hi, I am Pip, your shell guide. |
129
+ \ v / '--------------------------------------'
130
+ (_@_)>
131
+
132
+ - Greets you at startup with a tip. Only on a real terminal.
133
+ - `guide` opens a menu of 13 lessons, with `[x]` next to the ones you finished. Each step shows a command. Press Enter to run it, `s` to skip, `q` to stop.
134
+ - `guide TOPIC` jumps to one lesson. Names and numbers both work, for example `guide pipes` or `guide 2`.
135
+ - `guide cheat` prints a cheat sheet. `guide tip` prints one tip. `guide list` lists the topics.
136
+ - `guide quiz [TOPIC] [N]` asks N multiple-choice questions (default 5). `guide progress` shows finished lessons and best scores. `guide progress reset` clears them. Progress lives in `~/.myshell_progress.json`.
137
+ - Explains mistakes. A mistyped command gets "Did you mean ...?". An unclosed quote, `$(` or block gets a plain explanation.
138
+ - `guide off` hides Pip for the session. Put `GUIDE=off` in `~/.myshellrc` to hide Pip always.
139
+ - Scripts, `-c` and piped input never show Pip's greeting or hints.
140
+
141
+ ## Prompt escapes
142
+
143
+ | Escape | Meaning |
144
+ | ------------------------ | ------------------------------------------------------------ |
145
+ | `\w` | current directory, `~` for home |
146
+ | `\W` | last part of the directory |
147
+ | `\u` `\h` | user name, host name |
148
+ | `\$` | `$`, or `#` for root |
149
+ | `\j` | running jobs |
150
+ | `\?` | exit status of the last command |
151
+ | `\n` `\e` `\\` | newline, escape, backslash |
152
+ | `\t` `\T` `\A` `\@` `\d` | time (24-hour, 12-hour, HH:MM, am/pm), date |
153
+ | `\g` | git branch of the current folder, empty outside a repository |
154
+ | `\[` `\]` | wrap colors so line editing counts width correctly |
155
+
156
+ The default prompt adds the branch in brackets inside a git repository.
157
+
158
+ Example `~/.myshellrc`:
159
+
160
+ PS1='\u@\h \W \$ '
161
+ PS2='more> '
162
+ alias ll='ls -l'
163
+
164
+ ## Layout
165
+
166
+ | File | Job |
167
+ | -------------------------- | ------------------------------------------------------------ |
168
+ | `cli.py` | entry point, interactive loop |
169
+ | `lexer.py` | text to tokens |
170
+ | `parser.py` | tokens to a tree |
171
+ | `expand.py` | variables, substitution, math, globbing |
172
+ | `arith.py` | the `$((...))` evaluator |
173
+ | `heredoc.py` | here-document bodies |
174
+ | `executor.py` | runs the tree with fork and pipes |
175
+ | `jobs.py` | process groups and job control |
176
+ | `builtins.py` | core builtin commands, lookup of builtins |
177
+ | `more_builtins.py` | read, printf, set, env, which, trap, exec and friends |
178
+ | `coreutils.py` | ls, cat, head, tr, sleep, mkdir, rm, cp, mv, touch, wc, grep |
179
+ | `editor.py` `highlight.py` | optional prompt_toolkit line editor and syntax colors |
180
+ | `progress.py` `quiz.py` | Pip's progress file and quiz questions |
181
+ | `completion.py` | tab completion |
182
+ | `prompt.py` | prompt escapes |
183
+ | `guide.py` | Pip: lessons, hints, greeting |
184
+ | `state.py` | variables, aliases, history, status |
185
+ | `compat.py` | platform checks |
186
+
187
+ ## Limits
188
+
189
+ - Only file descriptors 0, 1 and 2 are redirected.
190
+ - Here-documents do not work inside multi-line `if`, loop and function blocks.
191
+ - Pipes, redirects and `&` after `fi`, `done`, `esac` or `}` are errors. So `cmd | while read x; do ...; done` does not work yet.
192
+ - Function bodies must be `{ ...; }` groups. There are no arrays, `eval`, `select` or `[[ ]]`.
193
+ - `test` has no `-a`, `-o` or parentheses.
194
+ - On Windows there is no job control: `&` runs in the foreground and `fg`, `bg` and `wait` are unavailable. Pipelines that include built-in commands run one stage after the other, with each stage's output held in memory.
195
+ - Signal traps run at the next command boundary, not in the middle of a command.
@@ -0,0 +1,179 @@
1
+ # myshell
2
+
3
+ A Unix-style shell written in Python. It runs on Linux and macOS with full job control. It also runs on Windows through a simpler fallback.
4
+
5
+ ## Requirements
6
+
7
+ - Python 3.9 or newer. Tested on 3.9 and 3.13 on Linux, macOS and Windows (see `.github/workflows/ci.yml`).
8
+ - `prompt_toolkit` is optional. With it you get Ctrl+R history search, grey suggestions and syntax colors. Without it the shell uses plain `input()` and readline.
9
+ - pytest and pyflakes for development: `pip install -e ".[dev]"`
10
+
11
+ ## Install and run
12
+
13
+ uvx myshell run without installing (after the first release)
14
+ uv tool install myshell install it
15
+ pip install myshell
16
+
17
+ From a clone:
18
+
19
+ python shell.py interactive shell
20
+ python shell.py -c "echo hi" run one command string
21
+ python shell.py script.sh a b run a script. $1 is a, $2 is b
22
+ python -m myshell same thing, as a package
23
+ uv run myshell uses pyproject.toml
24
+
25
+ `myshell -c 'cmd' NAME ARG...` sets `$0` to NAME and `$1...` to the ARGs.
26
+
27
+ ## Run the tests
28
+
29
+ pip install -e ".[dev]"
30
+ python -m pytest
31
+ python -m pyflakes myshell tests
32
+
33
+ With uv: `uv run --extra dev pytest`.
34
+
35
+ ## Release
36
+
37
+ uv build writes dist/*.whl and dist/*.tar.gz
38
+ uv publish needs a PyPI token or trusted publishing
39
+
40
+ Pushing a tag such as `v0.2.0` runs `.github/workflows/release.yml`, which builds and publishes with trusted publishing. Add the repository as a trusted publisher on pypi.org first (project `myshell`, workflow `release.yml`, environment `pypi`). Bump `version` in `pyproject.toml` before each release. If the name `myshell` is taken when you publish, change `name` in `pyproject.toml`. The command stays `myshell`.
41
+
42
+ ## Features
43
+
44
+ Commands and operators
45
+
46
+ - Pipes: `a | b`
47
+ - Lists: `a; b`, `a && b`, `a || b`
48
+ - Background jobs: `a &`
49
+ - Comments with `#`
50
+
51
+ Quoting and expansion
52
+
53
+ - Single quotes, double quotes and backslash escapes
54
+ - Variables: `$X`, `${X}`, `$?`, `$$`, and `$0 $1 ... $# $@ $*`
55
+ - Parameter expansion: `${X:-default}` `${X:=v}` `${X:?msg}` `${X:+alt}` `${#X}` `${X%.txt}` `${X%%p}` `${X#p}` `${X##p}` `${X/old/new}` `${X//old/new}` `${X:off:len}` `${X^^}` `${X,,}`
56
+ - Brace expansion: `file.{txt,md}`, `{1..5}`, `{a..e}`, `{01..10}`, `{1..10..2}`
57
+ - Command substitution: `$(cmd)` and backticks
58
+ - Arithmetic: `$((1 + 2 * x))`, including `+=`, `?:`, hex and octal
59
+ - Word splitting, globbing (`*`, `?`, `[ab]`) and tilde (`~`)
60
+ - Assignments: `NAME=value`, and `NAME=value cmd` for one command
61
+
62
+ Redirection
63
+
64
+ - `< > >> 2> 2>> &> &>>`
65
+ - `2>&1` and `1>&2`, applied left to right
66
+ - Here-strings: `<<<`
67
+ - Here-documents: `<<EOF` and `<<-EOF`. A quoted delimiter such as `<<'EOF'` turns expansion off.
68
+
69
+ Control flow
70
+
71
+ - `if`, `elif`, `else`, `fi`
72
+ - `while`, `until`, `for NAME in WORDS`, with `do` and `done`. A bare `for x; do` loops over `"$@"`.
73
+ - `case WORD in a|b) ...;; *.txt) ...;; *) ...;; esac`
74
+ - Functions: `name() { ...; }` or `function name { ...; }`, with `$1`, `$@`, `$#`, `return` and `local`
75
+ - Groups: `{ a; b; }`
76
+ - `break N` and `continue N`
77
+ - Blocks work on one line or across many lines
78
+
79
+ Builtins
80
+
81
+ - `echo` (with `-n -e -E`), `pwd cd exit alias unalias type which export unset source .`
82
+ - `read` (`-r -p -d -n`), `printf` (`-v`), `set` (`-e -u -x`, `-o`, `--`), `shift local return`
83
+ - `env` (`-i -u NAME=value`), `exec`, `trap` (EXIT, INT, TERM, HUP, USR1, USR2)
84
+ - `true false :`
85
+ - `jobs fg bg wait`
86
+ - `test` and `[ ]`: file, string and number checks, and `!`
87
+ - `history`: `N`, `-c`, `-d N`, `-w [file]`, `-r [file]`
88
+ - `guide`: lessons, cheat sheet and tips
89
+
90
+ Commands that work on every system
91
+
92
+ - `ls cat head tr sleep mkdir rm cp mv touch wc grep` are built in. A real program of the same name on your PATH wins, so on Linux and macOS you still get GNU or BSD tools. Where there is none (plain Windows), the built-in one runs. Set `MYSHELL_PORTABLE=1` to always use the built-in ones.
93
+ - They also work in pipes and `$(...)` without fork. This is what makes Pip's lessons run on Windows.
94
+
95
+ Interactive use
96
+
97
+ - Job control: Ctrl+C, Ctrl+Z, `fg`, `bg`
98
+ - Tab completion for commands, paths and function names
99
+ - With `prompt_toolkit`: Ctrl+R reverse search, grey suggestion from history (Right arrow or End takes it), colors (green known command, red unknown, yellow strings, cyan variables). `MYSHELL_EDITOR=basic` turns it off.
100
+ - After a command that takes longer than 2 seconds the shell prints `took 3.2s`. Set `SLOW_COMMAND_SECS` to change the limit, or to `off`.
101
+ - Traps: `trap 'cmd' EXIT` runs when the shell ends
102
+ - History saved in `~/.custom_shell_history`, limited to 1000 entries
103
+ - Startup file `~/.myshellrc`
104
+ - Prompt set with `PS1`. Continuation lines use `PS2`.
105
+
106
+ ## Pip, the guide
107
+
108
+ Pip is a small original character who lives in the shell.
109
+
110
+ ,---. .--------------------------------------.
111
+
112
+ ( ^ ^ ) | Hi, I am Pip, your shell guide. |
113
+ \ v / '--------------------------------------'
114
+ (_@_)>
115
+
116
+ - Greets you at startup with a tip. Only on a real terminal.
117
+ - `guide` opens a menu of 13 lessons, with `[x]` next to the ones you finished. Each step shows a command. Press Enter to run it, `s` to skip, `q` to stop.
118
+ - `guide TOPIC` jumps to one lesson. Names and numbers both work, for example `guide pipes` or `guide 2`.
119
+ - `guide cheat` prints a cheat sheet. `guide tip` prints one tip. `guide list` lists the topics.
120
+ - `guide quiz [TOPIC] [N]` asks N multiple-choice questions (default 5). `guide progress` shows finished lessons and best scores. `guide progress reset` clears them. Progress lives in `~/.myshell_progress.json`.
121
+ - Explains mistakes. A mistyped command gets "Did you mean ...?". An unclosed quote, `$(` or block gets a plain explanation.
122
+ - `guide off` hides Pip for the session. Put `GUIDE=off` in `~/.myshellrc` to hide Pip always.
123
+ - Scripts, `-c` and piped input never show Pip's greeting or hints.
124
+
125
+ ## Prompt escapes
126
+
127
+ | Escape | Meaning |
128
+ | ------------------------ | ------------------------------------------------------------ |
129
+ | `\w` | current directory, `~` for home |
130
+ | `\W` | last part of the directory |
131
+ | `\u` `\h` | user name, host name |
132
+ | `\$` | `$`, or `#` for root |
133
+ | `\j` | running jobs |
134
+ | `\?` | exit status of the last command |
135
+ | `\n` `\e` `\\` | newline, escape, backslash |
136
+ | `\t` `\T` `\A` `\@` `\d` | time (24-hour, 12-hour, HH:MM, am/pm), date |
137
+ | `\g` | git branch of the current folder, empty outside a repository |
138
+ | `\[` `\]` | wrap colors so line editing counts width correctly |
139
+
140
+ The default prompt adds the branch in brackets inside a git repository.
141
+
142
+ Example `~/.myshellrc`:
143
+
144
+ PS1='\u@\h \W \$ '
145
+ PS2='more> '
146
+ alias ll='ls -l'
147
+
148
+ ## Layout
149
+
150
+ | File | Job |
151
+ | -------------------------- | ------------------------------------------------------------ |
152
+ | `cli.py` | entry point, interactive loop |
153
+ | `lexer.py` | text to tokens |
154
+ | `parser.py` | tokens to a tree |
155
+ | `expand.py` | variables, substitution, math, globbing |
156
+ | `arith.py` | the `$((...))` evaluator |
157
+ | `heredoc.py` | here-document bodies |
158
+ | `executor.py` | runs the tree with fork and pipes |
159
+ | `jobs.py` | process groups and job control |
160
+ | `builtins.py` | core builtin commands, lookup of builtins |
161
+ | `more_builtins.py` | read, printf, set, env, which, trap, exec and friends |
162
+ | `coreutils.py` | ls, cat, head, tr, sleep, mkdir, rm, cp, mv, touch, wc, grep |
163
+ | `editor.py` `highlight.py` | optional prompt_toolkit line editor and syntax colors |
164
+ | `progress.py` `quiz.py` | Pip's progress file and quiz questions |
165
+ | `completion.py` | tab completion |
166
+ | `prompt.py` | prompt escapes |
167
+ | `guide.py` | Pip: lessons, hints, greeting |
168
+ | `state.py` | variables, aliases, history, status |
169
+ | `compat.py` | platform checks |
170
+
171
+ ## Limits
172
+
173
+ - Only file descriptors 0, 1 and 2 are redirected.
174
+ - Here-documents do not work inside multi-line `if`, loop and function blocks.
175
+ - Pipes, redirects and `&` after `fi`, `done`, `esac` or `}` are errors. So `cmd | while read x; do ...; done` does not work yet.
176
+ - Function bodies must be `{ ...; }` groups. There are no arrays, `eval`, `select` or `[[ ]]`.
177
+ - `test` has no `-a`, `-o` or parentheses.
178
+ - On Windows there is no job control: `&` runs in the foreground and `fg`, `bg` and `wait` are unavailable. Pipelines that include built-in commands run one stage after the other, with each stage's output held in memory.
179
+ - Signal traps run at the next command boundary, not in the middle of a command.
@@ -0,0 +1 @@
1
+ """A small Unix-style shell written in Python."""
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ sys.exit(main())
@@ -0,0 +1,22 @@
1
+ """Alias expansion on tokens, before parsing."""
2
+ from typing import Dict, List
3
+
4
+ from .lexer import COMMAND_START_AFTER, WORD, Token, lex
5
+
6
+
7
+ # Runs before parsing, so an alias value may hold pipes, redirects and operators.
8
+ def expand_aliases(tokens: List[Token], aliases: Dict[str, str],
9
+ active: frozenset = frozenset()) -> List[Token]:
10
+ result: List[Token] = []
11
+ at_start = True
12
+ for t in tokens:
13
+ name = t.text
14
+ if (at_start and t.kind == WORD and not t.quoted
15
+ and name in aliases and name not in active):
16
+ # Expand chains (a -> b -> ls). `active` blocks recursion (ls -> ls --color).
17
+ result.extend(expand_aliases(lex(aliases[name]), aliases, active | {name}))
18
+ at_start = False
19
+ continue
20
+ result.append(t)
21
+ at_start = t.kind in COMMAND_START_AFTER
22
+ return result