Compare commits
45 Commits
a43f8a6dde
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ef1d92a108 | |||
| 898002812e | |||
| 627489905e | |||
| 42647ffda8 | |||
| 1c42a36810 | |||
| 4b07990fc5 | |||
| 939888e579 | |||
| d83cda616b | |||
| 6f44759deb | |||
| 392506d414 | |||
| c4ba89f83a | |||
| 22113cbdfd | |||
| 406fd57b5a | |||
| 4a82f29231 | |||
| 7c8a418f96 | |||
| 782a3820da | |||
| b0325b6bbb | |||
| bce6cd10f7 | |||
| ac8940f936 | |||
| 3705986169 | |||
| db3f1b5108 | |||
| 958dad9942 | |||
| ec9795c781 | |||
| bcc4a87e10 | |||
| 067b4f6184 | |||
| 7e2763bb80 | |||
| 99b9440ee1 | |||
| f6a3935bcb | |||
| 1f9d59b546 | |||
| 101d8434f8 | |||
| 9bd9c66b92 | |||
| 5173e1908d | |||
| 1f9c96f24e | |||
| 09024728f6 | |||
| 307386ffc6 | |||
| 13227943c6 | |||
| a46ebe6868 | |||
| 5500b081fe | |||
| f279159873 | |||
| bb3db444db | |||
| 85e5fc2875 | |||
| ac429cbdf4 | |||
| a464540fbe | |||
| 07d7015dd4 | |||
| 490ce4571d |
21
Cargo.lock
generated
21
Cargo.lock
generated
@@ -224,6 +224,17 @@ version = "2.3.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fnmatch-regex"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0f319c7da34eac5f0b8c7220a4afb2e1ddde0c24ae87c7435a8e36dcd62a43a3"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"itertools",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "foldhash"
|
name = "foldhash"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
@@ -562,12 +573,13 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shed"
|
name = "shed"
|
||||||
version = "0.5.0"
|
version = "0.6.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ariadne",
|
"ariadne",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"clap",
|
"clap",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
"fnmatch-regex",
|
||||||
"glob",
|
"glob",
|
||||||
"itertools",
|
"itertools",
|
||||||
"log",
|
"log",
|
||||||
@@ -577,6 +589,7 @@ dependencies = [
|
|||||||
"regex",
|
"regex",
|
||||||
"scopeguard",
|
"scopeguard",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"smallvec",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
@@ -584,6 +597,12 @@ dependencies = [
|
|||||||
"yansi",
|
"yansi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smallvec"
|
||||||
|
version = "1.15.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
name = "shed"
|
name = "shed"
|
||||||
description = "A linux shell written in rust"
|
description = "A linux shell written in rust"
|
||||||
publish = false
|
publish = false
|
||||||
version = "0.5.0"
|
version = "0.6.2"
|
||||||
|
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
@@ -14,6 +14,7 @@ ariadne = "0.6.0"
|
|||||||
bitflags = "2.8.0"
|
bitflags = "2.8.0"
|
||||||
clap = { version = "4.5.38", features = ["derive"] }
|
clap = { version = "4.5.38", features = ["derive"] }
|
||||||
env_logger = "0.11.9"
|
env_logger = "0.11.9"
|
||||||
|
fnmatch-regex = "0.3.0"
|
||||||
glob = "0.3.2"
|
glob = "0.3.2"
|
||||||
itertools = "0.14.0"
|
itertools = "0.14.0"
|
||||||
log = "0.4.29"
|
log = "0.4.29"
|
||||||
@@ -35,6 +36,8 @@ rand = "0.10.0"
|
|||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
scopeguard = "1.2.0"
|
scopeguard = "1.2.0"
|
||||||
serde_json = "1.0.149"
|
serde_json = "1.0.149"
|
||||||
|
smallvec = { version = "1.15.1", features = ["write"] }
|
||||||
|
tempfile = "3.24.0"
|
||||||
unicode-segmentation = "1.12.0"
|
unicode-segmentation = "1.12.0"
|
||||||
unicode-width = "0.2.0"
|
unicode-width = "0.2.0"
|
||||||
vte = "0.15"
|
vte = "0.15"
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
# shed
|
# shed
|
||||||
|
|
||||||
A Linux shell written in Rust. The name is a nod to the original Unix utilities `sh` and `ed`. It's a shell with a heavy emphasis on smooth line editing.
|
A Linux shell written in Rust. The name is a nod to the original Unix utilities `sh` and `ed`. It's a shell with a heavy emphasis on smooth line editing and general interactive UX improvements over existing shells.
|
||||||
|
|
||||||
|
<sub>btw if you don't use `vim` this probably isn't your shell</sub>
|
||||||
|
|
||||||
<img width="506" height="407" alt="shed" src="https://github.com/user-attachments/assets/3945f663-a361-4418-bf20-0c4eaa2a36d2" />
|
<img width="506" height="407" alt="shed" src="https://github.com/user-attachments/assets/3945f663-a361-4418-bf20-0c4eaa2a36d2" />
|
||||||
|
|
||||||
@@ -8,7 +10,7 @@ A Linux shell written in Rust. The name is a nod to the original Unix utilities
|
|||||||
|
|
||||||
### Line Editor
|
### Line Editor
|
||||||
|
|
||||||
`shed` includes a built-in `vim` emulator as its line editor, written from scratch. It aims to provide a more precise vim-like editing experience at the shell prompt.
|
`shed` includes a built-in `vim` emulator as its line editor, written from scratch. It aims to provide a more precise vim-like editing experience at the shell prompt than conventional `vi` mode implementations.
|
||||||
|
|
||||||
- **Normal mode** - motions (`w`, `b`, `e`, `f`, `t`, `%`, `0`, `$`, etc.), verbs (`d`, `c`, `y`, `p`, `r`, `x`, `~`, etc.), text objects (`iw`, `aw`, `i"`, `a{`, `is`, etc.), registers, `.` repeat, `;`/`,` repeat, and counts
|
- **Normal mode** - motions (`w`, `b`, `e`, `f`, `t`, `%`, `0`, `$`, etc.), verbs (`d`, `c`, `y`, `p`, `r`, `x`, `~`, etc.), text objects (`iw`, `aw`, `i"`, `a{`, `is`, etc.), registers, `.` repeat, `;`/`,` repeat, and counts
|
||||||
- **Insert mode** - insert, append, replace, with Ctrl+W word deletion and undo/redo
|
- **Insert mode** - insert, append, replace, with Ctrl+W word deletion and undo/redo
|
||||||
@@ -40,6 +42,8 @@ gitbranch() { git branch --show-current 2>/dev/null; }
|
|||||||
export PS1='\u@\h \W \@gitbranch \$ '
|
export PS1='\u@\h \W \@gitbranch \$ '
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If `shed` receives `SIGUSR1` while in interactive mode, it will refresh and redraw the prompt. This can be used to create asynchronous, dynamic prompt content.
|
||||||
|
|
||||||
Additionally, `echo` now has a `-p` flag that expands prompt escape sequences, similar to how the `-e` flag expands conventional escape sequences.
|
Additionally, `echo` now has a `-p` flag that expands prompt escape sequences, similar to how the `-e` flag expands conventional escape sequences.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
76
doc/arith.txt
Normal file
76
doc/arith.txt
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
*arith* *arithmetic* *arithmetic-expansion*
|
||||||
|
|
||||||
|
#ARITHMETIC EXPANSION#
|
||||||
|
|
||||||
|
Arithmetic expansion evaluates a mathematical expression and substitutes
|
||||||
|
the result. The expression is subject to parameter expansion and command
|
||||||
|
substitution before evaluation.
|
||||||
|
|
||||||
|
`$((expression))`
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo $((2 + 3))` # prints: 5
|
||||||
|
`x=$((width * height))`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Operators *arith-operators*
|
||||||
|
|
||||||
|
The following operators are supported, listed from highest to lowest
|
||||||
|
precedence:
|
||||||
|
|
||||||
|
`( )` *arith-parens*
|
||||||
|
|
||||||
|
Grouping. Override default precedence.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo $(( (2+3) * 4 ))` # prints: 20
|
||||||
|
|
||||||
|
`*` `/` `%` *arith-muldivmod*
|
||||||
|
|
||||||
|
Multiplication, division, and modulo (remainder).
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo $((10 / 3))` # prints: 3
|
||||||
|
`echo $((10 % 3))` # prints: 1
|
||||||
|
|
||||||
|
`+` `-` *arith-addsub*
|
||||||
|
|
||||||
|
Addition and subtraction.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo $((10 - 3 + 1))` # prints: 8
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Variables in Expressions *arith-variables*
|
||||||
|
|
||||||
|
Variables can be referenced by name inside arithmetic expressions.
|
||||||
|
They are expanded and converted to numbers.
|
||||||
|
|
||||||
|
`x=10`
|
||||||
|
`echo $(($x + 5))` # prints: 15
|
||||||
|
`echo $((x + 5))` # also works
|
||||||
|
|
||||||
|
If a variable is unset or not a valid number, an error is reported.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Nesting *arith-nesting*
|
||||||
|
|
||||||
|
Arithmetic expressions can be nested with parentheses to any depth:
|
||||||
|
|
||||||
|
`echo $(( (1+2) * (3+4) ))` # prints: 21
|
||||||
|
|
||||||
|
Arithmetic expansion can also appear inside other expansions:
|
||||||
|
|
||||||
|
`echo "Total: $((price * qty))"`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Whitespace *arith-whitespace*
|
||||||
|
|
||||||
|
Whitespace inside `$((...))` is ignored and can be used freely for
|
||||||
|
readability:
|
||||||
|
|
||||||
|
`echo $((2+3))` # prints: 5
|
||||||
|
`echo $(( 2 + 3 ))` # same result
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |param| |redirect| |glob|
|
||||||
210
doc/autocmd.txt
Normal file
210
doc/autocmd.txt
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
*autocmd* *autocmds* *hooks*
|
||||||
|
|
||||||
|
#AUTOCMDS#
|
||||||
|
|
||||||
|
Autocmds (automatic commands) execute shell commands in response to
|
||||||
|
specific events. They provide a hook system for customizing shell
|
||||||
|
behavior without modifying the shell itself.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Registration *autocmd-register*
|
||||||
|
|
||||||
|
`autocmd [OPTIONS] {kind} {command}`
|
||||||
|
|
||||||
|
Register {command} to run whenever the event {kind} fires.
|
||||||
|
|
||||||
|
Options:
|
||||||
|
|
||||||
|
`-p {pattern}` *autocmd-pattern*
|
||||||
|
|
||||||
|
Only run this autocmd when {pattern} (a regex) matches the
|
||||||
|
event's context string. The context varies by event type:
|
||||||
|
for |autocmd-pre-cmd| it is the command being executed, for
|
||||||
|
|autocmd-post-change-dir| it is the target directory, etc.
|
||||||
|
|
||||||
|
`-c` *autocmd-clear*
|
||||||
|
|
||||||
|
Clear all autocmds of the specified {kind}. No command is
|
||||||
|
needed.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
`autocmd post-cmd 'echo "exit: $?"'`
|
||||||
|
`autocmd -p '^git' pre-cmd 'echo running git...'`
|
||||||
|
`autocmd -c pre-cmd`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Event Kinds *autocmd-kinds*
|
||||||
|
|
||||||
|
2.1 Command Execution *autocmd-cmd-events*
|
||||||
|
|
||||||
|
`pre-cmd` *autocmd-pre-cmd*
|
||||||
|
|
||||||
|
Fires before a command is executed. The command string is
|
||||||
|
available for pattern matching.
|
||||||
|
|
||||||
|
`post-cmd` *autocmd-post-cmd*
|
||||||
|
|
||||||
|
Fires after a command finishes. The command string is available
|
||||||
|
for pattern matching.
|
||||||
|
|
||||||
|
2.2 Directory Changes *autocmd-dir-events*
|
||||||
|
|
||||||
|
`pre-change-dir` *autocmd-pre-change-dir*
|
||||||
|
|
||||||
|
Fires before `cd` changes the working directory. The target
|
||||||
|
directory is available for pattern matching.
|
||||||
|
|
||||||
|
Special variables:
|
||||||
|
`$_NEW_DIR` the directory being changed to
|
||||||
|
`$_OLD_DIR` the current directory (before the change)
|
||||||
|
|
||||||
|
`post-change-dir` *autocmd-post-change-dir*
|
||||||
|
|
||||||
|
Fires after a successful directory change.
|
||||||
|
|
||||||
|
Special variables:
|
||||||
|
`$_NEW_DIR` the new working directory
|
||||||
|
`$_OLD_DIR` the previous directory
|
||||||
|
|
||||||
|
2.3 Job Events *autocmd-job-events*
|
||||||
|
|
||||||
|
`on-job-finish` *autocmd-on-job-finish*
|
||||||
|
|
||||||
|
Fires when a background job completes. The job's command string
|
||||||
|
is available for pattern matching.
|
||||||
|
|
||||||
|
2.4 Prompt Events *autocmd-prompt-events*
|
||||||
|
|
||||||
|
`pre-prompt` *autocmd-pre-prompt*
|
||||||
|
|
||||||
|
Fires before the prompt is rendered. Useful for updating prompt
|
||||||
|
state.
|
||||||
|
|
||||||
|
`post-prompt` *autocmd-post-prompt*
|
||||||
|
|
||||||
|
Fires after the prompt is rendered.
|
||||||
|
|
||||||
|
2.5 Mode Change Events *autocmd-mode-events*
|
||||||
|
|
||||||
|
`pre-mode-change` *autocmd-pre-mode-change*
|
||||||
|
|
||||||
|
Fires before the vi editing mode changes. The `$SHED_VI_MODE`
|
||||||
|
variable still holds the old mode.
|
||||||
|
|
||||||
|
`post-mode-change` *autocmd-post-mode-change*
|
||||||
|
|
||||||
|
Fires after the vi editing mode changes. `$SHED_VI_MODE` reflects
|
||||||
|
the new mode.
|
||||||
|
|
||||||
|
2.6 History Events *autocmd-hist-events*
|
||||||
|
|
||||||
|
`on-history-open` *autocmd-on-history-open*
|
||||||
|
|
||||||
|
Fires when the fuzzy history search window opens.
|
||||||
|
|
||||||
|
Special variables:
|
||||||
|
`$_ENTRIES` array of all history entries
|
||||||
|
`$_NUM_ENTRIES` count of all entries
|
||||||
|
`$_MATCHES` array of currently matching entries
|
||||||
|
`$_NUM_MATCHES` count of matching entries
|
||||||
|
`$_SEARCH_STR` the current search string
|
||||||
|
|
||||||
|
`on-history-close` *autocmd-on-history-close*
|
||||||
|
|
||||||
|
Fires when the history search is dismissed without selecting.
|
||||||
|
|
||||||
|
`on-history-select` *autocmd-on-history-select*
|
||||||
|
|
||||||
|
Fires when a history entry is selected. The entry text is
|
||||||
|
available for pattern matching.
|
||||||
|
|
||||||
|
Special variables:
|
||||||
|
`$_HIST_ENTRY` the selected history entry
|
||||||
|
|
||||||
|
2.7 Completion Events *autocmd-comp-events*
|
||||||
|
|
||||||
|
`on-completion-start` *autocmd-on-completion-start*
|
||||||
|
|
||||||
|
Fires when the completion menu becomes visible.
|
||||||
|
|
||||||
|
Special variables:
|
||||||
|
`$_MATCHES` array of completion candidates
|
||||||
|
`$_NUM_MATCHES` count of candidates
|
||||||
|
`$_SEARCH_STR` the token being completed
|
||||||
|
|
||||||
|
`on-completion-cancel` *autocmd-on-completion-cancel*
|
||||||
|
|
||||||
|
Fires when the completion menu is dismissed without selecting.
|
||||||
|
|
||||||
|
`on-completion-select` *autocmd-on-completion-select*
|
||||||
|
|
||||||
|
Fires when a completion candidate is accepted. The candidate
|
||||||
|
is available for pattern matching.
|
||||||
|
|
||||||
|
Special variables:
|
||||||
|
`$_COMP_CANDIDATE` the selected completion candidate
|
||||||
|
|
||||||
|
2.8 Screensaver Events *autocmd-screensaver-events*
|
||||||
|
|
||||||
|
`on-screensaver-exec` *autocmd-on-screensaver-exec*
|
||||||
|
|
||||||
|
Fires when the screensaver activates after the idle timeout.
|
||||||
|
The screensaver command (see `shopt prompt.screensaver_cmd`) is
|
||||||
|
available for pattern matching.
|
||||||
|
|
||||||
|
`on-screensaver-return` *autocmd-on-screensaver-return*
|
||||||
|
|
||||||
|
Fires when the shell returns from the screensaver command.
|
||||||
|
The screensaver command is available for pattern matching.
|
||||||
|
|
||||||
|
2.9 Exit Event *autocmd-exit-event*
|
||||||
|
|
||||||
|
`on-exit` *autocmd-on-exit*
|
||||||
|
|
||||||
|
Fires when the shell is about to exit.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Behavior *autocmd-behavior*
|
||||||
|
|
||||||
|
- Multiple autocmds can be registered for the same event kind. They
|
||||||
|
execute in registration order.
|
||||||
|
|
||||||
|
- If an autocmd command fails, the error is printed but subsequent
|
||||||
|
autocmds for the same event still run.
|
||||||
|
|
||||||
|
- Autocmds do not affect the shell's exit status (`$?`). The exit
|
||||||
|
status is saved before autocmd execution and restored afterward.
|
||||||
|
|
||||||
|
- Pattern matching uses Rust regex syntax. If an autocmd has no
|
||||||
|
pattern, it always fires for its event kind.
|
||||||
|
|
||||||
|
- Special variables (e.g. `$_NEW_DIR`) are only available within the
|
||||||
|
scope of the autocmd execution. They are not set globally.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Examples *autocmd-examples*
|
||||||
|
|
||||||
|
Notify on directory change:
|
||||||
|
|
||||||
|
`autocmd post-change-dir 'echo "moved to $_NEW_DIR"'`
|
||||||
|
|
||||||
|
Run a linter only on git commands:
|
||||||
|
|
||||||
|
`autocmd -p '^git commit' post-cmd 'lint-check'`
|
||||||
|
|
||||||
|
Refresh prompt on mode change (for mode indicator):
|
||||||
|
|
||||||
|
`autocmd post-mode-change 'kill -USR1 $$'`
|
||||||
|
(SIGUSR1 can be used to remotely refresh the prompt. See |prompt|)
|
||||||
|
|
||||||
|
Log completed jobs:
|
||||||
|
|
||||||
|
`autocmd on-job-finish 'echo "job done" >> /tmp/jobs.log'`
|
||||||
|
|
||||||
|
Clean up on exit:
|
||||||
|
|
||||||
|
`autocmd on-exit 'rm -f /tmp/my-shell-*.tmp'`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |keybinds| |prompt| |ex|
|
||||||
109
doc/ex.txt
Normal file
109
doc/ex.txt
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
*ex* *ex-mode* *ex-commands* *colon-commands*
|
||||||
|
|
||||||
|
#EX MODE#
|
||||||
|
|
||||||
|
Ex mode provides colon commands for operations that go beyond single-key
|
||||||
|
normal mode actions. Enter ex mode by pressing `:` in normal mode.
|
||||||
|
|
||||||
|
The command line supports full editing via insert mode, and has its own
|
||||||
|
command history navigable with `Up` and `Down`.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Shell Commands *ex-shell*
|
||||||
|
|
||||||
|
`:!{cmd}` *ex-bang*
|
||||||
|
|
||||||
|
Execute {cmd} in the shell. The following special variables are
|
||||||
|
set during execution and can be read or modified:
|
||||||
|
|
||||||
|
`$_BUFFER` the current editor buffer contents
|
||||||
|
`$_CURSOR` the cursor position (flat byte index)
|
||||||
|
`$_ANCHOR` the visual selection anchor position
|
||||||
|
|
||||||
|
If the command modifies these variables, the editor state is
|
||||||
|
updated accordingly. This allows ex commands to programmatically
|
||||||
|
edit the buffer.
|
||||||
|
|
||||||
|
If the command sets `$_KEYS`, the value is fed back into the
|
||||||
|
editor as a key sequence.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`:!echo "$_BUFFER" | tr a-z A-Z > /tmp/out`
|
||||||
|
`:!_BUFFER=$(echo "$_BUFFER" | sort)`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. File Operations *ex-file*
|
||||||
|
|
||||||
|
`:r {file}` *ex-read*
|
||||||
|
|
||||||
|
Read the contents of {file} and insert them into the buffer at
|
||||||
|
the cursor position.
|
||||||
|
|
||||||
|
`:r !{cmd}` *ex-read-cmd*
|
||||||
|
|
||||||
|
Execute {cmd} and insert its output into the buffer.
|
||||||
|
|
||||||
|
`:w {file}` *ex-write*
|
||||||
|
|
||||||
|
Write the buffer contents to {file}. Creates the file if it does
|
||||||
|
not exist, or truncates it if it does.
|
||||||
|
|
||||||
|
`:w >> {file}` *ex-write-append*
|
||||||
|
|
||||||
|
Append the buffer contents to {file}.
|
||||||
|
|
||||||
|
`:w !{cmd}` *ex-write-cmd*
|
||||||
|
|
||||||
|
Pipe the buffer contents to {cmd} as stdin.
|
||||||
|
|
||||||
|
`:e {file}` *ex-edit*
|
||||||
|
|
||||||
|
Open {file} in the editor defined by `$EDITOR`. Requires the
|
||||||
|
`EDITOR` environment variable to be set.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`:e ~/.config/shed/shedrc`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Buffer Operations *ex-buffer*
|
||||||
|
|
||||||
|
`:d` *ex-delete*
|
||||||
|
|
||||||
|
Delete the entire buffer.
|
||||||
|
|
||||||
|
`:y` *ex-yank*
|
||||||
|
|
||||||
|
Yank the entire buffer into the default register.
|
||||||
|
|
||||||
|
`:pu` *ex-put*
|
||||||
|
|
||||||
|
Put (paste) from the default register after the cursor.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Other Commands *ex-other*
|
||||||
|
|
||||||
|
`:q` *ex-quit*
|
||||||
|
|
||||||
|
Quit the editor / exit the shell.
|
||||||
|
|
||||||
|
`:help {topic}` *ex-help*
|
||||||
|
|
||||||
|
Display help for {topic}. Runs the `help` builtin.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
5. Path Expansion *ex-paths*
|
||||||
|
|
||||||
|
File paths in ex commands are subject to variable expansion. You can
|
||||||
|
use environment variables in paths:
|
||||||
|
|
||||||
|
`:e $HOME/.config/shed/shedrc`
|
||||||
|
`:w ${TMPDIR}/output.txt`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
6. Ex Command History *ex-history*
|
||||||
|
|
||||||
|
Ex mode maintains its own command history, separate from the main
|
||||||
|
shell history. Navigate with `Up` and `Down` while in ex mode.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |keybinds| |autocmd| |prompt|
|
||||||
155
doc/glob.txt
Normal file
155
doc/glob.txt
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
*glob* *globbing* *pathname-expansion* *filename-expansion*
|
||||||
|
|
||||||
|
#PATHNAME EXPANSION#
|
||||||
|
|
||||||
|
After word splitting, the shell scans each word for the characters `*`,
|
||||||
|
`?`, and `[`. If any appear (and are not quoted), the word is treated as a
|
||||||
|
pattern and replaced with an alphabetically sorted list of matching file
|
||||||
|
names. If no files match, the pattern is left unchanged.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Wildcards *glob-wildcards*
|
||||||
|
|
||||||
|
`*` *glob-star*
|
||||||
|
|
||||||
|
Matches any string of zero or more characters, except that it does
|
||||||
|
not match a leading `.` (see |glob-dotglob|) or a `/`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo *.txt` # all .txt files
|
||||||
|
`ls src/*.rs` # all .rs files in src/
|
||||||
|
|
||||||
|
`?` *glob-question*
|
||||||
|
|
||||||
|
Matches exactly one character, with the same restrictions as `*`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`ls file?.txt` # file1.txt, fileA.txt, etc.
|
||||||
|
|
||||||
|
`[...]` *glob-bracket*
|
||||||
|
|
||||||
|
Matches any one of the enclosed characters. A range can be specified
|
||||||
|
with a hyphen.
|
||||||
|
|
||||||
|
`[abc]` matches `a`, `b`, or `c`
|
||||||
|
`[a-z]` matches any lowercase letter
|
||||||
|
`[0-9]` matches any digit
|
||||||
|
`[A-Za-z]` matches any letter
|
||||||
|
|
||||||
|
`[!...]` `[^...]` *glob-bracket-negate*
|
||||||
|
|
||||||
|
Matches any character NOT in the set.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`ls [!.]*.txt` # .txt files not starting with dot
|
||||||
|
`echo file[^0-9].txt` # files without a digit
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Hidden Files *glob-dotglob*
|
||||||
|
|
||||||
|
By default, patterns do not match files whose names begin with `.`
|
||||||
|
(hidden files). A leading dot must be matched explicitly:
|
||||||
|
|
||||||
|
`echo .*` # only hidden files
|
||||||
|
`echo .* *` # hidden and non-hidden files
|
||||||
|
|
||||||
|
The `dotglob` shell option changes this behavior:
|
||||||
|
|
||||||
|
`shopt core.dotglob true`
|
||||||
|
|
||||||
|
When enabled, `*` and `?` will also match files starting with `.`.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Brace Expansion *brace* *brace-expansion*
|
||||||
|
|
||||||
|
Brace expansion is performed before globbing and generates multiple
|
||||||
|
words from a single pattern. It is not a POSIX feature.
|
||||||
|
|
||||||
|
`{a,b,c}` *brace-list*
|
||||||
|
|
||||||
|
Comma-separated list. Each item becomes a separate word.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo {a,b,c}` # prints: a b c
|
||||||
|
`echo file.{txt,log}` # prints: file.txt file.log
|
||||||
|
`mkdir -p src/{bin,lib}`
|
||||||
|
|
||||||
|
`{N..M}` *brace-range*
|
||||||
|
|
||||||
|
Numeric or character range.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo {1..5}` # prints: 1 2 3 4 5
|
||||||
|
`echo {a..f}` # prints: a b c d e f
|
||||||
|
`echo {5..1}` # prints: 5 4 3 2 1
|
||||||
|
|
||||||
|
`{N..M..S}` *brace-range-step*
|
||||||
|
|
||||||
|
Numeric range with step {S}.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo {0..10..2}` # prints: 0 2 4 6 8 10
|
||||||
|
`echo {1..20..5}` # prints: 1 6 11 16
|
||||||
|
|
||||||
|
`{01..10}` *brace-range-pad*
|
||||||
|
|
||||||
|
Zero-padded ranges. If either endpoint has leading zeros, all
|
||||||
|
generated values are padded to the same width.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo {01..05}` # prints: 01 02 03 04 05
|
||||||
|
`echo {001..3}` # prints: 001 002 003
|
||||||
|
|
||||||
|
Brace expansion can be nested and combined with other expansions:
|
||||||
|
|
||||||
|
`echo {a,b{1..3},c}` # prints: a b1 b2 b3 c
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Quoting and Escaping *glob-quoting*
|
||||||
|
|
||||||
|
Glob characters lose their special meaning when quoted:
|
||||||
|
|
||||||
|
`echo "*"` # prints literal *
|
||||||
|
`echo '*.txt'` # prints literal *.txt
|
||||||
|
`echo \*` # prints literal *
|
||||||
|
|
||||||
|
This is important when passing patterns to commands like `find` or
|
||||||
|
`grep` where you want the command (not the shell) to interpret the
|
||||||
|
pattern.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
5. Tilde Expansion *tilde* *tilde-expansion*
|
||||||
|
|
||||||
|
Tilde expansion is performed before pathname expansion.
|
||||||
|
|
||||||
|
`~` *tilde-home*
|
||||||
|
|
||||||
|
Expands to the value of `$HOME`.
|
||||||
|
|
||||||
|
`~/path` *tilde-home-path*
|
||||||
|
|
||||||
|
Expands `~` to `$HOME`, then appends the path.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`cd ~/projects`
|
||||||
|
`ls ~/.config`
|
||||||
|
|
||||||
|
`~user` *tilde-user*
|
||||||
|
|
||||||
|
Expands to the home directory of {user}.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`ls ~root` # /root
|
||||||
|
`cat ~nobody/.profile`
|
||||||
|
|
||||||
|
`~uid` *tilde-uid*
|
||||||
|
|
||||||
|
Expands to the home directory of the user with numeric uid {uid}.
|
||||||
|
This is a shed-specific extension.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo ~0` # /root (uid 0)
|
||||||
|
`echo ~1000` # first normal user's home
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |param| |redirect| |arith|
|
||||||
44
doc/help.txt
Normal file
44
doc/help.txt
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
*help*
|
||||||
|
|
||||||
|
#SHED HELP#
|
||||||
|
|
||||||
|
Shed is an experimental UNIX command interpreter (shell) usable as both an interactive login shell and as a shell script command preprocessor. Shed combines the functionality of `bash` with the UX and customizability of `vim`. Shed seeks to improve the interactive shell experience by providing:
|
||||||
|
* Programmable, dynamic prompts
|
||||||
|
* A hackable line editor similar to `zsh`'s `zle`
|
||||||
|
* Custom tab completion
|
||||||
|
* An event hook system
|
||||||
|
* Fuzzy history search/tab completion
|
||||||
|
* And many more features
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Available Topics *help-topics*
|
||||||
|
|
||||||
|
`help arith` Arithmetic expansion: `$(( ))` syntax |arith|
|
||||||
|
`help autocmd` Autocmd hooks for shell events |autocmd|
|
||||||
|
`help ex` Ex mode (colon) commands |ex|
|
||||||
|
`help glob` Pathname expansion and wildcards |glob|
|
||||||
|
`help keybinds` Vi mode keys, motions, text objects |keybinds|
|
||||||
|
`help param` Parameter expansion: `${}` operators |param|
|
||||||
|
`help prompt` Prompt configuration, PS1, PSR, echo -p |prompt|
|
||||||
|
`help redirect` Redirection, pipes, heredocs |redirect|
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Navigating Help *help-nav*
|
||||||
|
|
||||||
|
`help {topic}` Open a help page by name. Prefix matches work,
|
||||||
|
so `help key` opens |keybinds|.
|
||||||
|
|
||||||
|
`help {tag}` Jump directly to a tagged section. Tags are the
|
||||||
|
words marked with `*asterisks*` in the help files.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
`help text-objects` jumps to |text-objects|
|
||||||
|
`help prompt-escapes` jumps to |prompt-escapes|
|
||||||
|
`help heredoc` jumps to |heredoc|
|
||||||
|
|
||||||
|
Cross-references like |prompt| are clickable tags. Search for them
|
||||||
|
with `help` followed by the reference name.
|
||||||
|
|
||||||
|
Inside the pager, use `/` to search and `q` to quit.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
461
doc/keybinds.txt
Normal file
461
doc/keybinds.txt
Normal file
@@ -0,0 +1,461 @@
|
|||||||
|
*keybinds* *keys* *vi-mode* *keybindings*
|
||||||
|
|
||||||
|
#VI MODE KEYBINDINGS#
|
||||||
|
|
||||||
|
The line editor uses a vi-style modal editing system with six modes:
|
||||||
|
Normal, Insert, Visual, Replace, Ex (command), and Verbatim. The default
|
||||||
|
mode on startup is Insert.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Normal Mode *normal-mode*
|
||||||
|
|
||||||
|
Normal mode is for navigating and manipulating text. Press `Esc` from
|
||||||
|
any other mode to return here.
|
||||||
|
|
||||||
|
1.1 Movement *normal-movement*
|
||||||
|
|
||||||
|
`h` `l` *key-h* *key-l*
|
||||||
|
|
||||||
|
Move left / right by one character.
|
||||||
|
|
||||||
|
`j` `k` *key-j* *key-k*
|
||||||
|
|
||||||
|
Move down / up by one line.
|
||||||
|
|
||||||
|
`w` `W` *key-w* *key-W*
|
||||||
|
|
||||||
|
Move to the start of the next word. `w` stops at punctuation
|
||||||
|
boundaries, `W` only stops at whitespace.
|
||||||
|
|
||||||
|
`b` `B` *key-b* *key-B*
|
||||||
|
|
||||||
|
Move to the start of the previous word.
|
||||||
|
|
||||||
|
`e` `E` *key-e* *key-E*
|
||||||
|
|
||||||
|
Move to the end of the next word.
|
||||||
|
|
||||||
|
`ge` `gE` *key-ge* *key-gE*
|
||||||
|
|
||||||
|
Move to the end of the previous word.
|
||||||
|
|
||||||
|
`0` *key-0*
|
||||||
|
|
||||||
|
Move to the start of the line.
|
||||||
|
|
||||||
|
`^` *key-caret*
|
||||||
|
|
||||||
|
Move to the first non-whitespace character on the line.
|
||||||
|
|
||||||
|
`$` *key-dollar*
|
||||||
|
|
||||||
|
Move to the end of the line.
|
||||||
|
|
||||||
|
`g_` *key-g_*
|
||||||
|
|
||||||
|
Move to the last non-whitespace character on the line.
|
||||||
|
|
||||||
|
`gg` *key-gg*
|
||||||
|
|
||||||
|
Move to the first line of the buffer.
|
||||||
|
|
||||||
|
`G` *key-G*
|
||||||
|
|
||||||
|
Move to the last line of the buffer.
|
||||||
|
|
||||||
|
`|` *key-bar*
|
||||||
|
|
||||||
|
Move to a specific column. `10|` moves to column 10.
|
||||||
|
|
||||||
|
`%` *key-percent*
|
||||||
|
|
||||||
|
Jump to the matching bracket: `()`, `[]`, `{}`.
|
||||||
|
|
||||||
|
`](` `[(` *key-paren-nav*
|
||||||
|
|
||||||
|
Jump to the next / previous unmatched parenthesis.
|
||||||
|
|
||||||
|
`]}` `[{` *key-brace-nav*
|
||||||
|
|
||||||
|
Jump to the next / previous unmatched brace.
|
||||||
|
|
||||||
|
1.2 Character Search *char-search*
|
||||||
|
|
||||||
|
`f{char}` *key-f*
|
||||||
|
|
||||||
|
Move forward to the next occurrence of {char} on the current line.
|
||||||
|
|
||||||
|
`F{char}` *key-F*
|
||||||
|
|
||||||
|
Move backward to the previous occurrence of {char}.
|
||||||
|
|
||||||
|
`t{char}` *key-t*
|
||||||
|
|
||||||
|
Move forward to just before {char}.
|
||||||
|
|
||||||
|
`T{char}` *key-T*
|
||||||
|
|
||||||
|
Move backward to just after {char}.
|
||||||
|
|
||||||
|
`;` *key-semicolon*
|
||||||
|
|
||||||
|
Repeat the last `f`, `F`, `t`, or `T` in the same direction.
|
||||||
|
|
||||||
|
`,` *key-comma*
|
||||||
|
|
||||||
|
Repeat the last `f`, `F`, `t`, or `T` in the reverse direction.
|
||||||
|
|
||||||
|
1.3 Scrolling *scrolling*
|
||||||
|
|
||||||
|
`Ctrl+D` *key-ctrl-d*
|
||||||
|
|
||||||
|
In normal mode, scroll down half a screen. See |viewport|.
|
||||||
|
In insert mode, `Ctrl+D` clears the buffer if there is any content, and exits the shell if there is not.
|
||||||
|
|
||||||
|
`Ctrl+U` *key-ctrl-u*
|
||||||
|
|
||||||
|
Scroll up half a screen.
|
||||||
|
|
||||||
|
`Ctrl+G` *key-ctrl-g*
|
||||||
|
|
||||||
|
Print current cursor position (line, column, total lines).
|
||||||
|
|
||||||
|
1.4 Operators *operators*
|
||||||
|
|
||||||
|
Operators take a {motion} or |text-object| to define the range they
|
||||||
|
act on. Double an operator to act on the whole line (e.g. `dd`, `>>`,
|
||||||
|
`==`).
|
||||||
|
|
||||||
|
`d{motion}` *key-d*
|
||||||
|
|
||||||
|
Delete the text covered by {motion}.
|
||||||
|
|
||||||
|
`dd` delete the whole line
|
||||||
|
`D` delete to end of line (same as `d$`)
|
||||||
|
`x` delete character under cursor (same as `dl`)
|
||||||
|
`X` delete character before cursor (same as `dh`)
|
||||||
|
|
||||||
|
`c{motion}` *key-c*
|
||||||
|
|
||||||
|
Delete the text covered by {motion} and enter insert mode.
|
||||||
|
|
||||||
|
`cc` change the whole line
|
||||||
|
`C` change to end of line (same as `c$`)
|
||||||
|
`s` change the character under cursor (same as `cl`)
|
||||||
|
`S` change the whole line (same as `cc`)
|
||||||
|
|
||||||
|
`y{motion}` *key-y*
|
||||||
|
|
||||||
|
Yank (copy) the text covered by {motion} into a register.
|
||||||
|
|
||||||
|
`yy` yank the whole line
|
||||||
|
`Y` yank the whole line
|
||||||
|
|
||||||
|
`>{motion}` *key-indent*
|
||||||
|
|
||||||
|
Indent the lines covered by {motion}.
|
||||||
|
|
||||||
|
`>>` indent the current line
|
||||||
|
|
||||||
|
`<{motion}` *key-dedent*
|
||||||
|
|
||||||
|
Dedent the lines covered by {motion}.
|
||||||
|
|
||||||
|
`<<` dedent the current line
|
||||||
|
|
||||||
|
`={motion}` *key-equalize*
|
||||||
|
|
||||||
|
Auto-indent the lines covered by {motion}. Uses the minimum
|
||||||
|
nesting depth of each line's start and end to determine the
|
||||||
|
correct indentation level.
|
||||||
|
|
||||||
|
`==` equalize the current line
|
||||||
|
|
||||||
|
`g~{motion}` *key-toggle-case*
|
||||||
|
|
||||||
|
Toggle the case of the text covered by {motion}.
|
||||||
|
|
||||||
|
`gu{motion}` *key-gu*
|
||||||
|
|
||||||
|
Convert the text covered by {motion} to lowercase.
|
||||||
|
|
||||||
|
`gU{motion}` *key-gU*
|
||||||
|
|
||||||
|
Convert the text covered by {motion} to uppercase.
|
||||||
|
|
||||||
|
1.5 Single-Key Actions *normal-actions*
|
||||||
|
|
||||||
|
`p` *key-p*
|
||||||
|
|
||||||
|
Paste from the register after the cursor.
|
||||||
|
|
||||||
|
`P` *key-P*
|
||||||
|
|
||||||
|
Paste from the register before the cursor.
|
||||||
|
|
||||||
|
`r{char}` *key-r*
|
||||||
|
|
||||||
|
Replace the character under the cursor with {char}. With a count,
|
||||||
|
replaces that many characters.
|
||||||
|
|
||||||
|
`~` *key-tilde*
|
||||||
|
|
||||||
|
Toggle the case of the character under the cursor and advance.
|
||||||
|
Accepts a count.
|
||||||
|
|
||||||
|
`J` *key-J*
|
||||||
|
|
||||||
|
Join the current line with the next line.
|
||||||
|
|
||||||
|
`u` *key-u*
|
||||||
|
|
||||||
|
Undo the last change.
|
||||||
|
|
||||||
|
`Ctrl+R` *key-ctrl-r*
|
||||||
|
|
||||||
|
Redo the last undone change.
|
||||||
|
|
||||||
|
`.` *key-dot*
|
||||||
|
|
||||||
|
Repeat the last editing command.
|
||||||
|
|
||||||
|
`Ctrl+A` *key-ctrl-a*
|
||||||
|
|
||||||
|
Increment the number under the cursor. Recognizes decimal,
|
||||||
|
hexadecimal (`0x`), binary (`0b`), and octal (`0o`) formats.
|
||||||
|
Preserves leading zeros and prefix.
|
||||||
|
|
||||||
|
`Ctrl+X` *key-ctrl-x*
|
||||||
|
|
||||||
|
Decrement the number under the cursor.
|
||||||
|
|
||||||
|
1.6 Entering Other Modes *mode-entry*
|
||||||
|
|
||||||
|
`i` insert before cursor *key-i*
|
||||||
|
`a` insert after cursor *key-a*
|
||||||
|
`I` insert at first non-whitespace *key-I*
|
||||||
|
`A` insert at end of line *key-A*
|
||||||
|
`o` open a new line below *key-o*
|
||||||
|
`O` open a new line above *key-O*
|
||||||
|
`R` enter replace mode *key-R*
|
||||||
|
`v` enter visual mode (character-wise) *key-v*
|
||||||
|
`V` enter visual mode (line-wise) *key-V-visual*
|
||||||
|
`gv` reselect last visual region *key-gv*
|
||||||
|
`:` enter ex mode *key-colon*
|
||||||
|
|
||||||
|
1.7 Registers *registers*
|
||||||
|
|
||||||
|
`"{reg}` *key-register*
|
||||||
|
|
||||||
|
Use register {reg} for the next delete, yank, or put. Registers
|
||||||
|
`a`-`z` store text; uppercase `A`-`Z` appends to the corresponding
|
||||||
|
lowercase register.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Insert Mode *insert-mode*
|
||||||
|
|
||||||
|
Insert mode is for typing text. Characters are inserted at the cursor.
|
||||||
|
|
||||||
|
`Esc` return to normal mode
|
||||||
|
`Backspace` delete character before cursor
|
||||||
|
`Ctrl+H` same as Backspace
|
||||||
|
`Ctrl+W` delete word before cursor
|
||||||
|
`Delete` delete character under cursor
|
||||||
|
`Tab` trigger completion (see |completion|)
|
||||||
|
`Shift+Tab` trigger completion (reverse direction)
|
||||||
|
`Ctrl+R` open history search (see |history-search|)
|
||||||
|
`Ctrl+V` enter verbatim mode (insert literal key sequence)
|
||||||
|
`Enter` submit line or insert newline if input is incomplete
|
||||||
|
|
||||||
|
Arrow keys, Home, and End work as expected for navigation.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Visual Mode *visual-mode*
|
||||||
|
|
||||||
|
Visual mode selects a region of text. Enter with `v` (character-wise)
|
||||||
|
or `V` (line-wise) from normal mode.
|
||||||
|
|
||||||
|
All normal-mode motions work to extend the selection. Operators act
|
||||||
|
on the selected region without needing a motion:
|
||||||
|
|
||||||
|
`d` `x` delete selection
|
||||||
|
`c` `s` `S` change selection (delete and enter insert mode)
|
||||||
|
`y` yank selection
|
||||||
|
`p` `P` paste, replacing selection
|
||||||
|
`>` `<` indent / dedent selection
|
||||||
|
`=` equalize selection
|
||||||
|
`~` toggle case of selection
|
||||||
|
`u` lowercase selection
|
||||||
|
`U` uppercase selection
|
||||||
|
`r{char}` replace every character in selection with {char}
|
||||||
|
`J` join selected lines
|
||||||
|
`o` `O` swap cursor and selection anchor
|
||||||
|
|
||||||
|
Press `Esc` to return to normal mode without acting.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Replace Mode *replace-mode*
|
||||||
|
|
||||||
|
Replace mode overwrites existing characters as you type. Enter with
|
||||||
|
`R` from normal mode.
|
||||||
|
|
||||||
|
`Esc` return to normal mode
|
||||||
|
`Backspace` undo the last replacement
|
||||||
|
|
||||||
|
All other keys replace the character under the cursor and advance.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
5. Ex Mode *ex-mode-keys*
|
||||||
|
|
||||||
|
Ex mode accepts colon commands. Enter with `:` from normal mode.
|
||||||
|
See |ex| for available commands.
|
||||||
|
|
||||||
|
`Enter` execute the command
|
||||||
|
`Esc` cancel and return to normal mode
|
||||||
|
`Ctrl+C` clear the command line
|
||||||
|
`Up` `Down` navigate ex command history
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
6. Text Objects *text-objects*
|
||||||
|
|
||||||
|
Text objects define a range of text based on structure. They are used
|
||||||
|
with operators: `d`, `c`, `y`, etc. Each has an "inner" (`i`) and
|
||||||
|
"around" (`a`) variant.
|
||||||
|
|
||||||
|
`iw` `aw` *obj-word*
|
||||||
|
|
||||||
|
Word (punctuation-delimited). `aw` includes trailing whitespace.
|
||||||
|
|
||||||
|
`iW` `aW` *obj-WORD*
|
||||||
|
|
||||||
|
WORD (whitespace-delimited). `aW` includes trailing whitespace.
|
||||||
|
|
||||||
|
`i"` `a"` *obj-dquote*
|
||||||
|
|
||||||
|
Double-quoted string.
|
||||||
|
|
||||||
|
`i'` `a'` *obj-squote*
|
||||||
|
|
||||||
|
Single-quoted string.
|
||||||
|
|
||||||
|
`` i` `` `` a` `` *obj-backtick*
|
||||||
|
|
||||||
|
Backtick-quoted string.
|
||||||
|
|
||||||
|
`i)` `a)` `ib` `ab` *obj-paren*
|
||||||
|
|
||||||
|
Parenthesized block.
|
||||||
|
|
||||||
|
`i]` `a]` *obj-bracket*
|
||||||
|
|
||||||
|
Square-bracketed block.
|
||||||
|
|
||||||
|
`i}` `a}` `iB` `aB` *obj-brace*
|
||||||
|
|
||||||
|
Brace-delimited block.
|
||||||
|
|
||||||
|
`i<` `a<` *obj-angle*
|
||||||
|
|
||||||
|
Angle-bracketed block.
|
||||||
|
|
||||||
|
`it` `at` *obj-tag*
|
||||||
|
|
||||||
|
XML/HTML tag block.
|
||||||
|
|
||||||
|
`is` `as` *obj-sentence*
|
||||||
|
|
||||||
|
Sentence.
|
||||||
|
|
||||||
|
`ip` `ap` *obj-paragraph*
|
||||||
|
|
||||||
|
Paragraph (separated by blank lines).
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
7. Counts *counts*
|
||||||
|
|
||||||
|
Most motions, operators, and actions accept a numeric count prefix:
|
||||||
|
|
||||||
|
`3j` move down 3 lines
|
||||||
|
`2dw` delete 2 words
|
||||||
|
`5>>` indent 5 lines
|
||||||
|
`10l` move 10 characters right
|
||||||
|
|
||||||
|
When both the operator and the motion have counts, they are
|
||||||
|
multiplied: `2d3w` deletes 6 words.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
8. Viewport *viewport*
|
||||||
|
|
||||||
|
When the buffer is taller than the terminal, the editor displays a
|
||||||
|
scrolling viewport. The viewport follows the cursor and respects the
|
||||||
|
`line.scroll_offset` shopt (minimum lines visible above/below the cursor).
|
||||||
|
|
||||||
|
`Ctrl+D` scroll down half a screen
|
||||||
|
`Ctrl+U` scroll up half a screen
|
||||||
|
`Ctrl+G` display current position in the buffer
|
||||||
|
|
||||||
|
Line numbers in the left margin reflect actual buffer positions, not
|
||||||
|
viewport-relative indices.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
9. User-Defined Keymaps *keymaps* *keymap*
|
||||||
|
|
||||||
|
Custom key bindings are created with the `keymap` command:
|
||||||
|
|
||||||
|
`keymap [flags] {keys} {action}`
|
||||||
|
|
||||||
|
Flags select the mode(s) the binding applies to:
|
||||||
|
|
||||||
|
`-n` normal mode
|
||||||
|
`-i` insert mode
|
||||||
|
`-v` visual mode
|
||||||
|
`-x` ex mode
|
||||||
|
`-o` operator-pending mode
|
||||||
|
`-r` replace mode
|
||||||
|
|
||||||
|
Keys and actions use angle-bracket notation for special keys:
|
||||||
|
|
||||||
|
`<CR>` Enter `<Esc>` Escape
|
||||||
|
`<Tab>` Tab `<BS>` Backspace
|
||||||
|
`<Del>` Delete `<Space>` Space
|
||||||
|
`<Up>` `<Down>` `<Left>` `<Right>` Arrow keys
|
||||||
|
`<Home>` `<End>` Home / End
|
||||||
|
`<F1>` - `<F12>` Function keys
|
||||||
|
`<CMD>` Enter ex mode
|
||||||
|
`<Leader>` Leader key (set via `shopt prompt.leader`)
|
||||||
|
|
||||||
|
Modifier prefixes:
|
||||||
|
|
||||||
|
`C-` Control `A-` `M-` Alt / Meta
|
||||||
|
`S-` Shift
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
`keymap -n <Leader>w ':w<CR>'` Leader+w writes to file
|
||||||
|
`keymap -i jk '<Esc>'` jk exits insert mode
|
||||||
|
`keymap -n <C-n> ':!mycmd<CR>'` Ctrl+n runs a shell command
|
||||||
|
|
||||||
|
To remove a binding:
|
||||||
|
|
||||||
|
`keymap --remove {keys}`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
10. Completion *completion*
|
||||||
|
|
||||||
|
`Tab` start or cycle through completion candidates (forward)
|
||||||
|
`Shift+Tab` cycle backward
|
||||||
|
|
||||||
|
When the completion menu is visible, `Tab` and arrow keys navigate
|
||||||
|
candidates. `Enter` accepts the selected candidate. `Esc` dismisses
|
||||||
|
the menu.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
11. History Search *history-search*
|
||||||
|
|
||||||
|
`Ctrl+R` open fuzzy history search (from insert or ex mode)
|
||||||
|
|
||||||
|
Type to filter history entries. `Enter` accepts the selected entry.
|
||||||
|
`Esc` dismisses the search.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |ex| |autocmd| |prompt|
|
||||||
197
doc/param.txt
Normal file
197
doc/param.txt
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
*param* *parameter-expansion* *param-expansion*
|
||||||
|
|
||||||
|
#PARAMETER EXPANSION#
|
||||||
|
|
||||||
|
The shell provides several forms of parameter expansion for working with
|
||||||
|
variables. In each form, {word} is subject to tilde expansion, parameter
|
||||||
|
expansion, command substitution, and arithmetic expansion.
|
||||||
|
|
||||||
|
If {parameter} is unset or null, the behavior depends on the operator used.
|
||||||
|
"Unset" means the variable has never been assigned. "Null" means the variable
|
||||||
|
is set but its value is the empty string.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Basic Forms *param-basic*
|
||||||
|
|
||||||
|
`$var` Value of {var}
|
||||||
|
`${var}` Same, with explicit braces (needed for `${var}foo`)
|
||||||
|
|
||||||
|
Braces are required when {var} is followed by characters that could be part
|
||||||
|
of the name, or when using any of the operators below.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Default Values *param-default*
|
||||||
|
|
||||||
|
`${var:-word}` *param-default-val*
|
||||||
|
|
||||||
|
Use default value. If {var} is unset or null, expand to {word}.
|
||||||
|
Otherwise, expand to the value of {var}.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`name=${1:-world}`
|
||||||
|
`echo "hello $name"` # prints "hello world" if \$1 is unset
|
||||||
|
|
||||||
|
`${var-word}` *param-default-nonnull*
|
||||||
|
|
||||||
|
Like `:-` but only substitutes {word} if {var} is completely unset,
|
||||||
|
not if it is null.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Assign Defaults *param-assign*
|
||||||
|
|
||||||
|
`${var:=word}` *param-assign-val*
|
||||||
|
|
||||||
|
Assign default value. If {var} is unset or null, assign {word} to
|
||||||
|
{var} and then expand to the new value.
|
||||||
|
|
||||||
|
Note: This cannot be used with positional parameters or special
|
||||||
|
parameters.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo ${cache:=/tmp/cache}` # sets and uses \$cache
|
||||||
|
|
||||||
|
`${var=word}` *param-assign-nonnull*
|
||||||
|
|
||||||
|
Like `:=` but only assigns if {var} is completely unset.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Error on Unset *param-error*
|
||||||
|
|
||||||
|
`${var:?word}` *param-error-val*
|
||||||
|
|
||||||
|
Display error. If {var} is unset or null, print {word} to stderr
|
||||||
|
and exit (in a non-interactive shell). If {word} is omitted, a
|
||||||
|
default message is printed.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`input=${1:?usage: myscript \<filename\>}`
|
||||||
|
|
||||||
|
`${var?word}` *param-error-nonnull*
|
||||||
|
|
||||||
|
Like `:?` but only errors if {var} is completely unset.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
5. Alternate Value *param-alt*
|
||||||
|
|
||||||
|
`${var:+word}` *param-alt-val*
|
||||||
|
|
||||||
|
Use alternate value. If {var} is unset or null, expand to nothing.
|
||||||
|
Otherwise, expand to {word}.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo ${verbose:+--verbose}` # flag only if \$verbose is set
|
||||||
|
|
||||||
|
`${var+word}` *param-alt-nonnull*
|
||||||
|
|
||||||
|
Like `:+` but substitutes {word} only if {var} is set (even if null).
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
6. String Length *param-length*
|
||||||
|
|
||||||
|
`${#var}` *param-strlen*
|
||||||
|
|
||||||
|
Expands to the length of the value of {var} in characters.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`str="hello"`
|
||||||
|
`echo ${#str}` # prints 5
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
7. Substring Removal *param-substring*
|
||||||
|
|
||||||
|
`${var#pattern}` *param-trim-short-left*
|
||||||
|
|
||||||
|
Remove shortest matching prefix. Removes the shortest match of
|
||||||
|
{pattern} from the beginning of the value of {var}.
|
||||||
|
|
||||||
|
`${var##pattern}` *param-trim-long-left*
|
||||||
|
|
||||||
|
Remove longest matching prefix.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`path="/home/user/file.txt"`
|
||||||
|
`echo ${path##*/}` # prints "file.txt"
|
||||||
|
|
||||||
|
`${var%pattern}` *param-trim-short-right*
|
||||||
|
|
||||||
|
Remove shortest matching suffix. Removes the shortest match of
|
||||||
|
{pattern} from the end of the value of {var}.
|
||||||
|
|
||||||
|
`${var%%pattern}` *param-trim-long-right*
|
||||||
|
|
||||||
|
Remove longest matching suffix.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`file="archive.tar.gz"`
|
||||||
|
`echo ${file%%.*}` # prints "archive"
|
||||||
|
`echo ${file%.*}` # prints "archive.tar"
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
8. Search and Replace *param-replace*
|
||||||
|
|
||||||
|
`${var/pattern/replacement}` *param-replace-first*
|
||||||
|
|
||||||
|
Replace first match. Replaces the first occurrence of {pattern}
|
||||||
|
in the value of {var} with {replacement}.
|
||||||
|
|
||||||
|
`${var//pattern/replacement}` *param-replace-all*
|
||||||
|
|
||||||
|
Replace all matches.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`str="hello world"`
|
||||||
|
`echo ${str/o/0}` # prints "hell0 world"
|
||||||
|
`echo ${str//o/0}` # prints "hell0 w0rld"
|
||||||
|
|
||||||
|
`${var/#pattern/replacement}` *param-replace-prefix*
|
||||||
|
|
||||||
|
Replace if matching at the beginning.
|
||||||
|
|
||||||
|
`${var/%pattern/replacement}` *param-replace-suffix*
|
||||||
|
|
||||||
|
Replace if matching at the end.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
9. Case Modification *param-case*
|
||||||
|
|
||||||
|
`${var^}` *param-upper-first*
|
||||||
|
|
||||||
|
Uppercase the first character of {var}.
|
||||||
|
|
||||||
|
`${var^^}` *param-upper-all*
|
||||||
|
|
||||||
|
Uppercase all characters.
|
||||||
|
|
||||||
|
`${var,}` *param-lower-first*
|
||||||
|
|
||||||
|
Lowercase the first character of {var}.
|
||||||
|
|
||||||
|
`${var,,}` *param-lower-all*
|
||||||
|
|
||||||
|
Lowercase all characters.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`name="john doe"`
|
||||||
|
`echo ${name^}` # prints "John doe"
|
||||||
|
`echo ${name^^}` # prints "JOHN DOE"
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
10. Substrings *param-slice*
|
||||||
|
|
||||||
|
`${var:offset}` *param-slice-from*
|
||||||
|
|
||||||
|
Substring starting at {offset} (0-indexed).
|
||||||
|
|
||||||
|
`${var:offset:length}` *param-slice-range*
|
||||||
|
|
||||||
|
Substring of {length} characters starting at {offset}.
|
||||||
|
|
||||||
|
Negative offsets count from the end (note the space before the minus
|
||||||
|
to distinguish from `:-`):
|
||||||
|
|
||||||
|
`str="hello world"`
|
||||||
|
`echo ${str: -5}` # prints "world"
|
||||||
|
`echo ${str:0:5}` # prints "hello"
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |redirect| |glob| |arith|
|
||||||
229
doc/prompt.txt
Normal file
229
doc/prompt.txt
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
*prompt* *ps1* *psr* *prompt-expansion*
|
||||||
|
|
||||||
|
#PROMPT#
|
||||||
|
|
||||||
|
The shell displays a configurable prompt before each command. Two prompt
|
||||||
|
strings are available: PS1 (the main prompt) and PSR (an optional
|
||||||
|
right-aligned prompt).
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Setting the Prompt *prompt-set*
|
||||||
|
|
||||||
|
The prompt is controlled by the `PS1` and `PSR` environment variables.
|
||||||
|
Set them in your shell configuration:
|
||||||
|
|
||||||
|
`PS1='\u@\h:\W\$ '`
|
||||||
|
`PSR='$SHED_VI_MODE'`
|
||||||
|
|
||||||
|
Prompts are re-expanded before each display, so command substitutions
|
||||||
|
and functions are evaluated every time.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Escape Sequences *prompt-escapes*
|
||||||
|
|
||||||
|
The following backslash escapes are recognized in PS1 and PSR:
|
||||||
|
|
||||||
|
`\u` *prompt-user*
|
||||||
|
|
||||||
|
The current username (from $USER).
|
||||||
|
|
||||||
|
`\h` *prompt-host*
|
||||||
|
|
||||||
|
The hostname (from $HOST).
|
||||||
|
|
||||||
|
`\w` *prompt-pwd*
|
||||||
|
|
||||||
|
The current working directory, with $HOME replaced by `~`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`/home/user/projects` -> `~/projects`
|
||||||
|
|
||||||
|
`\W` *prompt-pwd-short*
|
||||||
|
|
||||||
|
Truncated working directory. Shows only the last N path segments,
|
||||||
|
where N is controlled by `shopt prompt.trunc_prompt_path`
|
||||||
|
(default: 4).
|
||||||
|
|
||||||
|
Example (with trunc_prompt_path=2):
|
||||||
|
`/home/user/projects/myapp` -> `projects/myapp`
|
||||||
|
|
||||||
|
`\s` *prompt-shell*
|
||||||
|
|
||||||
|
The shell name: `shed`.
|
||||||
|
|
||||||
|
`\$` *prompt-symbol*
|
||||||
|
|
||||||
|
`#` if the effective UID is 0 (root), `$` otherwise.
|
||||||
|
|
||||||
|
`\j` *prompt-jobs*
|
||||||
|
|
||||||
|
The number of background jobs currently managed by the shell.
|
||||||
|
|
||||||
|
`\t` *prompt-runtime-ms*
|
||||||
|
|
||||||
|
The runtime of the last command in milliseconds.
|
||||||
|
|
||||||
|
`\T` *prompt-runtime-fmt*
|
||||||
|
|
||||||
|
The runtime of the last command in human-readable format
|
||||||
|
(e.g. `1m 23s 456ms`).
|
||||||
|
|
||||||
|
`\n` *prompt-newline*
|
||||||
|
|
||||||
|
A literal newline. Use this to create multi-line prompts.
|
||||||
|
|
||||||
|
`\r` *prompt-return*
|
||||||
|
|
||||||
|
A literal carriage return.
|
||||||
|
|
||||||
|
`\\` *prompt-backslash*
|
||||||
|
|
||||||
|
A literal backslash.
|
||||||
|
|
||||||
|
`\e[...` *prompt-ansi*
|
||||||
|
|
||||||
|
An ANSI escape sequence. The sequence starts with `\e[` and ends
|
||||||
|
at the first letter character. Used for colors and formatting.
|
||||||
|
|
||||||
|
Common codes:
|
||||||
|
`\e[0m` reset all attributes
|
||||||
|
`\e[1m` bold
|
||||||
|
`\e[3m` italic
|
||||||
|
`\e[4m` underline
|
||||||
|
`\e[31m` red foreground
|
||||||
|
`\e[32m` green foreground
|
||||||
|
`\e[33m` yellow foreground
|
||||||
|
`\e[34m` blue foreground
|
||||||
|
`\e[35m` magenta foreground
|
||||||
|
`\e[36m` cyan foreground
|
||||||
|
`\e[1;32m` bold green
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`PS1='\e[1;32m\u\e[0m@\e[34m\h\e[0m:\w\$ '`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Functions in Prompts *prompt-functions*
|
||||||
|
|
||||||
|
`\@{funcname}` *prompt-func*
|
||||||
|
`\@funcname`
|
||||||
|
|
||||||
|
Call a shell function and insert its output. The function must
|
||||||
|
be defined before the prompt is expanded. If the function does
|
||||||
|
not exist, the literal sequence is displayed.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`git_branch() { git branch --show-current 2>/dev/null; }`
|
||||||
|
`PS1='\u@\h (\@git_branch)\$ '`
|
||||||
|
|
||||||
|
This allows dynamic prompt content that updates on every command.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. Right Prompt (PSR) *prompt-right* *psr*
|
||||||
|
|
||||||
|
The PSR variable defines an optional right-aligned prompt displayed
|
||||||
|
on the last line of PS1. It supports the same escape sequences as PS1.
|
||||||
|
|
||||||
|
PSR is only displayed when it fits: if the combined width of the
|
||||||
|
input line and PSR exceeds the terminal width, PSR is hidden.
|
||||||
|
|
||||||
|
PSR is restricted to a single line. If it contains newlines, only the
|
||||||
|
first line is used.
|
||||||
|
|
||||||
|
`PSR='$SHED_VI_MODE'`
|
||||||
|
`PSR='\T'` # show last command runtime on the right
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
5. Multi-Line Prompts *prompt-multiline*
|
||||||
|
|
||||||
|
PS1 may span multiple lines using `\n`. The editor tracks line
|
||||||
|
positions for proper cursor movement and redrawing.
|
||||||
|
|
||||||
|
`PS1='\e[1m\u@\h\e[0m\n\W\$ '`
|
||||||
|
|
||||||
|
This displays the username and hostname on the first line, and the
|
||||||
|
working directory and prompt symbol on the second.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
6. echo -p *echo-prompt*
|
||||||
|
|
||||||
|
The `echo` builtin accepts a `-p` flag that enables prompt-style
|
||||||
|
expansion on its arguments. All prompt escape sequences listed above
|
||||||
|
are recognized.
|
||||||
|
|
||||||
|
`echo -p '\u'` # prints the current username
|
||||||
|
`echo -p '\W'` # prints the truncated working directory
|
||||||
|
`echo -p '\e[31mred\e[0m'` # prints "red" in red
|
||||||
|
|
||||||
|
The `-p` flag can be combined with `-n` (no trailing newline) and
|
||||||
|
`-e` (interpret escape sequences like `\n` and `\t`). When both `-e`
|
||||||
|
and `-p` are used, prompt expansion runs first, then escape sequence
|
||||||
|
interpretation.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
7. Prompt Options (shopt) *prompt-options*
|
||||||
|
|
||||||
|
The following options under `shopt prompt.*` affect prompt behavior:
|
||||||
|
|
||||||
|
`prompt.trunc_prompt_path` *opt-trunc-path*
|
||||||
|
|
||||||
|
Maximum number of path segments shown by `\W`. Default: 4.
|
||||||
|
|
||||||
|
`prompt.highlight` *opt-highlight*
|
||||||
|
|
||||||
|
Enable syntax highlighting in the input line. Default: true.
|
||||||
|
|
||||||
|
`prompt.auto_indent` *opt-auto-indent*
|
||||||
|
|
||||||
|
Automatically indent new lines to match the current nesting
|
||||||
|
depth. Default: true.
|
||||||
|
|
||||||
|
`prompt.linebreak_on_incomplete` *opt-linebreak*
|
||||||
|
|
||||||
|
Insert a newline when Enter is pressed on an incomplete command
|
||||||
|
(e.g. unclosed quotes or pipes). Default: true.
|
||||||
|
|
||||||
|
`prompt.line_numbers` *opt-line-numbers*
|
||||||
|
|
||||||
|
Display line numbers in the left margin for multi-line buffers.
|
||||||
|
Default: true.
|
||||||
|
|
||||||
|
`prompt.leader` *opt-leader*
|
||||||
|
|
||||||
|
The leader key sequence for |keymaps|. Default: `" "` (space).
|
||||||
|
|
||||||
|
`prompt.comp_limit` *opt-comp-limit*
|
||||||
|
|
||||||
|
Maximum number of completion candidates to display. Default: 100.
|
||||||
|
|
||||||
|
`prompt.completion_ignore_case` *opt-comp-case*
|
||||||
|
|
||||||
|
Case-insensitive tab completion. Default: false.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
8. Special Variables *prompt-variables*
|
||||||
|
|
||||||
|
`SHED_VI_MODE` *var-vi-mode*
|
||||||
|
|
||||||
|
Set automatically before each prompt to the current vi mode name:
|
||||||
|
`NORMAL`, `INSERT`, `VISUAL`, `COMMAND`, `REPLACE`, `SEARCH`, or
|
||||||
|
`COMPLETE`. Useful in PSR or prompt functions.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`PSR='$SHED_VI_MODE'`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
9. Remote Refresh (SIGUSR1) *prompt-sigusr1*
|
||||||
|
|
||||||
|
Sending `SIGUSR1` to the shell process causes it to re-expand and
|
||||||
|
redraw the prompt. This is useful for updating the prompt from
|
||||||
|
external processes (e.g. a background script that detects a state
|
||||||
|
change).
|
||||||
|
|
||||||
|
`kill -USR1 $$`
|
||||||
|
|
||||||
|
Combined with prompt functions (see |prompt-func|), this allows the
|
||||||
|
prompt to reflect changes that happen outside the shell's normal
|
||||||
|
command cycle.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |keybinds| |autocmd| |ex|
|
||||||
181
doc/redirect.txt
Normal file
181
doc/redirect.txt
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
*redirect* *redirection* *redir*
|
||||||
|
|
||||||
|
#REDIRECTION#
|
||||||
|
|
||||||
|
Redirections allow you to control where a command reads its input from and
|
||||||
|
where it sends its output. A redirection applies to a specific file
|
||||||
|
descriptor; if no descriptor number is given, output redirections default
|
||||||
|
to stdout (fd 1) and input redirections default to stdin (fd 0).
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
1. Output Redirection *redir-output*
|
||||||
|
|
||||||
|
`command > file` *redir-out*
|
||||||
|
|
||||||
|
Redirect stdout to {file}, creating it if it does not exist or
|
||||||
|
truncating it if it does.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo hello > out.txt`
|
||||||
|
`ls 2> errors.txt` # redirect stderr
|
||||||
|
|
||||||
|
`command >| file` *redir-out-force*
|
||||||
|
|
||||||
|
Like `>` but overrides the {noclobber} option. If {noclobber} is set,
|
||||||
|
`>` will refuse to overwrite an existing file; `>|` forces the
|
||||||
|
overwrite.
|
||||||
|
|
||||||
|
`command >> file` *redir-append*
|
||||||
|
|
||||||
|
Append stdout to {file}, creating it if it does not exist.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`echo line >> log.txt`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
2. Input Redirection *redir-input*
|
||||||
|
|
||||||
|
`command < file` *redir-in*
|
||||||
|
|
||||||
|
Redirect {file} to stdin.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`sort < unsorted.txt`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
3. Read-Write Redirection *redir-readwrite*
|
||||||
|
|
||||||
|
`command <> file` *redir-rw*
|
||||||
|
|
||||||
|
Open {file} for both reading and writing on the specified file
|
||||||
|
descriptor (default fd 0). The file is created if it does not exist
|
||||||
|
but is not truncated.
|
||||||
|
|
||||||
|
Useful with the `seek` builtin for random-access file operations.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`exec 3<> data.bin`
|
||||||
|
`seek 3 0 set` # seek to beginning
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
4. File Descriptor Duplication *redir-dup*
|
||||||
|
|
||||||
|
`command N>&M` *redir-dup-out*
|
||||||
|
|
||||||
|
Duplicate output file descriptor {M} onto {N}. After this, writing
|
||||||
|
to fd {N} goes to the same place as fd {M}.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`command > out.txt 2>&1` # stderr goes where stdout goes
|
||||||
|
|
||||||
|
`command N<&M` *redir-dup-in*
|
||||||
|
|
||||||
|
Duplicate input file descriptor {M} onto {N}.
|
||||||
|
|
||||||
|
`command N>&-` *redir-close-out*
|
||||||
|
`command N<&-` *redir-close-in*
|
||||||
|
|
||||||
|
Close file descriptor {N}.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`exec 3>&-` # close fd 3
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
5. Pipelines *redir-pipe*
|
||||||
|
|
||||||
|
`command1 | command2` *pipe*
|
||||||
|
|
||||||
|
Connect stdout of {command1} to stdin of {command2}. Both commands
|
||||||
|
run concurrently.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`cat file.txt | grep pattern | sort`
|
||||||
|
|
||||||
|
`command1 |& command2` *pipe-and*
|
||||||
|
|
||||||
|
Connect both stdout and stderr of {command1} to stdin of {command2}.
|
||||||
|
Equivalent to `command1 2>&1 | command2`.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
6. Here Documents *heredoc*
|
||||||
|
|
||||||
|
`command << DELIM` *redir-heredoc*
|
||||||
|
|
||||||
|
Read input from the script body until a line containing only {DELIM}
|
||||||
|
is found. The text between is fed to stdin of {command}.
|
||||||
|
|
||||||
|
Parameter expansion, command substitution, and arithmetic expansion
|
||||||
|
are performed in the body unless the delimiter is quoted.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`cat << EOF`
|
||||||
|
`Hello $USER`
|
||||||
|
`EOF`
|
||||||
|
|
||||||
|
`command << 'DELIM'` *redir-heredoc-literal*
|
||||||
|
|
||||||
|
Quoting the delimiter (single or double quotes) suppresses all
|
||||||
|
expansion in the heredoc body. The text is passed literally.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`cat << 'EOF'`
|
||||||
|
`This $variable is not expanded`
|
||||||
|
`EOF`
|
||||||
|
|
||||||
|
`command <<- DELIM` *redir-heredoc-indent*
|
||||||
|
|
||||||
|
Like `<<` but strips leading tab characters from each line of the
|
||||||
|
body and from the closing delimiter. This allows heredocs to be
|
||||||
|
indented for readability without affecting the content.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`if true; then`
|
||||||
|
` cat <<- EOF`
|
||||||
|
` indented content`
|
||||||
|
` EOF`
|
||||||
|
`fi`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
7. Here Strings *herestring*
|
||||||
|
|
||||||
|
`command <<< word` *redir-herestring*
|
||||||
|
|
||||||
|
Feed {word} as a single string to stdin of {command}, with a
|
||||||
|
trailing newline appended. {word} is subject to the usual expansions.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
`read first rest <<< "hello world"`
|
||||||
|
`bc <<< "2 + 2"`
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
8. File Descriptor Numbers *redir-fd*
|
||||||
|
|
||||||
|
Any redirection operator can be prefixed with a file descriptor number:
|
||||||
|
|
||||||
|
`2> file` redirect stderr to file
|
||||||
|
`3< file` open file on fd 3
|
||||||
|
`4>> file` append to file on fd 4
|
||||||
|
`5<> file` open file read-write on fd 5
|
||||||
|
|
||||||
|
Standard file descriptors:
|
||||||
|
|
||||||
|
0 stdin
|
||||||
|
1 stdout
|
||||||
|
2 stderr
|
||||||
|
|
||||||
|
File descriptors 3 and above are available for general use with `exec`.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
9. Combining Redirections *redir-combine*
|
||||||
|
|
||||||
|
Multiple redirections can appear on a single command, processed left
|
||||||
|
to right:
|
||||||
|
|
||||||
|
`command > out.txt 2>&1` # stdout to file, stderr to same file
|
||||||
|
`command 2>&1 > out.txt` # different! stderr to terminal,
|
||||||
|
# stdout to file
|
||||||
|
|
||||||
|
Order matters: each redirection is applied in sequence.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
See also: |param| |glob| |arith|
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
{
|
{
|
||||||
packages.default = pkgs.rustPlatform.buildRustPackage {
|
packages.default = pkgs.rustPlatform.buildRustPackage {
|
||||||
pname = "shed";
|
pname = "shed";
|
||||||
version = "0.5.0";
|
version = "0.6.2";
|
||||||
|
|
||||||
src = self;
|
src = self;
|
||||||
|
|
||||||
|
|||||||
@@ -2,325 +2,14 @@
|
|||||||
|
|
||||||
let
|
let
|
||||||
cfg = config.programs.shed;
|
cfg = config.programs.shed;
|
||||||
boolToString = b:
|
|
||||||
if b then "true" else "false";
|
|
||||||
|
|
||||||
mkAutoCmd = cfg:
|
|
||||||
lib.concatLines (map (hook: "autocmd ${hook} ${lib.optionalString (cfg.pattern != null) "-p \"${cfg.pattern}\""} '${cfg.command}'") cfg.hooks);
|
|
||||||
|
|
||||||
|
|
||||||
mkFunctionDef = name: body:
|
|
||||||
let
|
|
||||||
indented = "\t" + lib.concatStringsSep "\n\t" (lib.splitString "\n" body);
|
|
||||||
in
|
|
||||||
''
|
|
||||||
${name}() {
|
|
||||||
${indented}
|
|
||||||
}'';
|
|
||||||
|
|
||||||
mkKeymapCmd = cfg: let
|
|
||||||
flags = "-${lib.concatStrings cfg.modes}";
|
|
||||||
keys = "'${cfg.keys}'";
|
|
||||||
action = "'${cfg.command}'";
|
|
||||||
in
|
|
||||||
"keymap ${flags} ${keys} ${action}";
|
|
||||||
|
|
||||||
|
|
||||||
mkCompleteCmd = name: cfg: let
|
|
||||||
flags = lib.concatStrings [
|
|
||||||
(lib.optionalString cfg.files " -f")
|
|
||||||
(lib.optionalString cfg.dirs " -d")
|
|
||||||
(lib.optionalString cfg.commands " -c")
|
|
||||||
(lib.optionalString cfg.variables " -v")
|
|
||||||
(lib.optionalString cfg.users " -u")
|
|
||||||
(lib.optionalString cfg.jobs " -j")
|
|
||||||
(lib.optionalString cfg.aliases " -a")
|
|
||||||
(lib.optionalString cfg.signals " -S")
|
|
||||||
(lib.optionalString cfg.noSpace " -n")
|
|
||||||
(lib.optionalString (cfg.function != null) " -F ${cfg.function}")
|
|
||||||
(lib.optionalString (cfg.fallback != "no") " -o ${cfg.fallback}")
|
|
||||||
(lib.optionalString (cfg.wordList != []) " -W '${lib.concatStringsSep " " cfg.wordList}'")
|
|
||||||
|
|
||||||
];
|
|
||||||
in "complete${flags} ${name}";
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.programs.shed = {
|
options.programs.shed = import ./shed_opts.nix { inherit pkgs lib; };
|
||||||
enable = lib.mkEnableOption "shed shell";
|
|
||||||
|
|
||||||
package = lib.mkOption {
|
|
||||||
type = lib.types.package;
|
|
||||||
default = pkgs.shed;
|
|
||||||
description = "The shed package to use";
|
|
||||||
};
|
|
||||||
|
|
||||||
aliases = lib.mkOption {
|
|
||||||
type = lib.types.attrsOf lib.types.str;
|
|
||||||
default = {};
|
|
||||||
description = "Aliases to set when shed starts";
|
|
||||||
};
|
|
||||||
|
|
||||||
functions = lib.mkOption {
|
|
||||||
type = lib.types.attrsOf lib.types.str;
|
|
||||||
default = {};
|
|
||||||
description = "Shell functions to set when shed starts";
|
|
||||||
};
|
|
||||||
|
|
||||||
autocmds = lib.mkOption {
|
|
||||||
type = lib.types.listOf (lib.types.submodule {
|
|
||||||
options = {
|
|
||||||
hooks = lib.mkOption {
|
|
||||||
type = lib.types.addCheck (lib.types.listOf (lib.types.enum [
|
|
||||||
"pre-cmd"
|
|
||||||
"post-cmd"
|
|
||||||
"pre-change-dir"
|
|
||||||
"post-change-dir"
|
|
||||||
"on-job-finish"
|
|
||||||
"pre-prompt"
|
|
||||||
"post-prompt"
|
|
||||||
"pre-mode-change"
|
|
||||||
"post-mode-change"
|
|
||||||
"on-exit"
|
|
||||||
"on-history-open"
|
|
||||||
"on-history-close"
|
|
||||||
"on-history-select"
|
|
||||||
"on-completion-start"
|
|
||||||
"on-completion-cancel"
|
|
||||||
"on-completion-select"
|
|
||||||
])) (list: list != []);
|
|
||||||
description = "The events that trigger this autocmd";
|
|
||||||
};
|
|
||||||
pattern = lib.mkOption {
|
|
||||||
type = lib.types.nullOr lib.types.str;
|
|
||||||
default = null;
|
|
||||||
description = "A regex pattern to use in the hook to determine whether it runs or not. What it's compared to differs by hook, for instance 'pre-change-dir' compares it to the new directory, pre-cmd compares it to the command, etc";
|
|
||||||
};
|
|
||||||
command = lib.mkOption {
|
|
||||||
type = lib.types.addCheck lib.types.str (cmd: cmd != "");
|
|
||||||
description = "The shell command to execute when the hook is triggered and the pattern (if provided) matches";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
});
|
|
||||||
default = [];
|
|
||||||
description = "Custom autocmds to set when shed starts";
|
|
||||||
};
|
|
||||||
|
|
||||||
keymaps = lib.mkOption {
|
|
||||||
type = lib.types.listOf (lib.types.submodule {
|
|
||||||
options = {
|
|
||||||
modes = lib.mkOption {
|
|
||||||
type = lib.types.listOf (lib.types.enum [ "n" "i" "x" "v" "o" "r" ]);
|
|
||||||
default = [];
|
|
||||||
description = "The editing modes this keymap can be used in";
|
|
||||||
};
|
|
||||||
keys = lib.mkOption {
|
|
||||||
type = lib.types.str;
|
|
||||||
default = "";
|
|
||||||
description = "The sequence of keys that trigger this keymap";
|
|
||||||
};
|
|
||||||
command = lib.mkOption {
|
|
||||||
type = lib.types.str;
|
|
||||||
default = "";
|
|
||||||
description = "The sequence of characters to send to the line editor when the keymap is triggered.";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
});
|
|
||||||
default = [];
|
|
||||||
description = "Custom keymaps to set when shed starts";
|
|
||||||
};
|
|
||||||
|
|
||||||
extraCompletion = lib.mkOption {
|
|
||||||
type = lib.types.attrsOf (lib.types.submodule {
|
|
||||||
options = {
|
|
||||||
files = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete file names in the current directory";
|
|
||||||
};
|
|
||||||
dirs = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete directory names in the current directory";
|
|
||||||
};
|
|
||||||
commands = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete executable commands in the PATH";
|
|
||||||
};
|
|
||||||
variables = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete variable names";
|
|
||||||
};
|
|
||||||
users = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete user names from /etc/passwd";
|
|
||||||
};
|
|
||||||
jobs = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete job names or pids from the current shell session";
|
|
||||||
};
|
|
||||||
aliases = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete alias names defined in the current shell session";
|
|
||||||
};
|
|
||||||
signals = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Complete signal names for commands like kill";
|
|
||||||
};
|
|
||||||
wordList = lib.mkOption {
|
|
||||||
type = lib.types.listOf lib.types.str;
|
|
||||||
default = [];
|
|
||||||
description = "Complete from a custom list of words";
|
|
||||||
};
|
|
||||||
function = lib.mkOption {
|
|
||||||
type = lib.types.nullOr lib.types.str;
|
|
||||||
default = null;
|
|
||||||
description = "Complete using a custom shell function (should be defined in extraCompletionPreConfig)";
|
|
||||||
};
|
|
||||||
noSpace = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Don't append a space after completion";
|
|
||||||
};
|
|
||||||
fallback = lib.mkOption {
|
|
||||||
type = lib.types.enum [ "no" "default" "dirnames" ];
|
|
||||||
default = "no";
|
|
||||||
description = "Fallback behavior when no matches are found: 'no' means no fallback, 'default' means fall back to the default shell completion behavior, and 'directories' means fall back to completing directory names";
|
|
||||||
};
|
|
||||||
|
|
||||||
};
|
|
||||||
});
|
|
||||||
default = {};
|
|
||||||
description = "Additional completion scripts to source when shed starts (e.g. for custom tools or functions)";
|
|
||||||
};
|
|
||||||
|
|
||||||
environmentVars = lib.mkOption {
|
|
||||||
type = lib.types.attrsOf lib.types.str;
|
|
||||||
default = {};
|
|
||||||
description = "Environment variables to set when shed starts";
|
|
||||||
};
|
|
||||||
|
|
||||||
settings = {
|
|
||||||
dotGlob = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Whether to include hidden files in glob patterns";
|
|
||||||
};
|
|
||||||
autocd = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Whether to automatically change into directories when they are entered as commands";
|
|
||||||
};
|
|
||||||
historyIgnoresDupes = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Whether to ignore duplicate entries in the command history";
|
|
||||||
};
|
|
||||||
maxHistoryEntries = lib.mkOption {
|
|
||||||
type = lib.types.int;
|
|
||||||
default = 10000;
|
|
||||||
description = "The maximum number of entries to keep in the command history";
|
|
||||||
};
|
|
||||||
interactiveComments = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = true;
|
|
||||||
description = "Whether to allow comments in interactive mode";
|
|
||||||
};
|
|
||||||
autoHistory = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = true;
|
|
||||||
description = "Whether to automatically add commands to the history as they are executed";
|
|
||||||
};
|
|
||||||
bellEnabled = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = true;
|
|
||||||
description = "Whether to allow shed to ring the terminal bell on certain events (e.g. command completion, errors, etc.)";
|
|
||||||
};
|
|
||||||
maxRecurseDepth = lib.mkOption {
|
|
||||||
type = lib.types.int;
|
|
||||||
default = 1000;
|
|
||||||
description = "The maximum depth to allow when recursively executing shell functions";
|
|
||||||
};
|
|
||||||
|
|
||||||
leaderKey = lib.mkOption {
|
|
||||||
type = lib.types.str;
|
|
||||||
default = "\\\\";
|
|
||||||
description = "The leader key to use for custom keymaps (e.g. if set to '\\\\', then a keymap with keys='x' would be triggered by '\\x')";
|
|
||||||
};
|
|
||||||
promptPathSegments = lib.mkOption {
|
|
||||||
type = lib.types.int;
|
|
||||||
default = 4;
|
|
||||||
description = "The maximum number of path segments to show in the prompt";
|
|
||||||
};
|
|
||||||
completionLimit = lib.mkOption {
|
|
||||||
type = lib.types.int;
|
|
||||||
default = 1000;
|
|
||||||
description = "The maximum number of completion candidates to show before truncating the list";
|
|
||||||
};
|
|
||||||
syntaxHighlighting = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = true;
|
|
||||||
description = "Whether to enable syntax highlighting in the shell";
|
|
||||||
};
|
|
||||||
linebreakOnIncomplete = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = true;
|
|
||||||
description = "Whether to automatically insert a newline when the input is incomplete";
|
|
||||||
};
|
|
||||||
extraPostConfig = lib.mkOption {
|
|
||||||
type = lib.types.str;
|
|
||||||
default = "";
|
|
||||||
description = "Additional configuration to append to the shed configuration file";
|
|
||||||
};
|
|
||||||
extraPreConfig = lib.mkOption {
|
|
||||||
type = lib.types.str;
|
|
||||||
default = "";
|
|
||||||
description = "Additional configuration to prepend to the shed configuration file";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
config =
|
config =
|
||||||
let
|
|
||||||
completeLines = lib.concatLines (lib.mapAttrsToList mkCompleteCmd cfg.extraCompletion);
|
|
||||||
keymapLines = lib.concatLines (map mkKeymapCmd cfg.keymaps);
|
|
||||||
functionLines = lib.concatLines (lib.mapAttrsToList mkFunctionDef cfg.functions);
|
|
||||||
autocmdLines = lib.concatLines (map mkAutoCmd cfg.autocmds);
|
|
||||||
in
|
|
||||||
lib.mkIf cfg.enable {
|
lib.mkIf cfg.enable {
|
||||||
home.packages = [ cfg.package ];
|
home.packages = [ cfg.package ];
|
||||||
|
|
||||||
home.file.".shedrc".text = lib.concatLines [
|
home.file.".shedrc".text = import ./render_rc.nix lib cfg;
|
||||||
cfg.settings.extraPreConfig
|
|
||||||
(lib.concatLines (lib.mapAttrsToList (name: value: "export ${name}=\"${value}\"") cfg.environmentVars))
|
|
||||||
(lib.concatLines (lib.mapAttrsToList (name: value: "alias ${name}=\"${value}\"") cfg.aliases))
|
|
||||||
(lib.concatLines [
|
|
||||||
"shopt core.dotglob=${boolToString cfg.settings.dotGlob}"
|
|
||||||
"shopt core.autocd=${boolToString cfg.settings.autocd}"
|
|
||||||
"shopt core.hist_ignore_dupes=${boolToString cfg.settings.historyIgnoresDupes}"
|
|
||||||
"shopt core.max_hist=${toString cfg.settings.maxHistoryEntries}"
|
|
||||||
"shopt core.interactive_comments=${boolToString cfg.settings.interactiveComments}"
|
|
||||||
"shopt core.auto_hist=${boolToString cfg.settings.autoHistory}"
|
|
||||||
"shopt core.bell_enabled=${boolToString cfg.settings.bellEnabled}"
|
|
||||||
"shopt core.max_recurse_depth=${toString cfg.settings.maxRecurseDepth}"
|
|
||||||
|
|
||||||
"shopt prompt.leader='${cfg.settings.leaderKey}'"
|
|
||||||
"shopt prompt.trunc_prompt_path=${toString cfg.settings.promptPathSegments}"
|
|
||||||
"shopt prompt.comp_limit=${toString cfg.settings.completionLimit}"
|
|
||||||
"shopt prompt.highlight=${boolToString cfg.settings.syntaxHighlighting}"
|
|
||||||
"shopt prompt.linebreak_on_incomplete=${boolToString cfg.settings.linebreakOnIncomplete}"
|
|
||||||
functionLines
|
|
||||||
completeLines
|
|
||||||
keymapLines
|
|
||||||
autocmdLines
|
|
||||||
])
|
|
||||||
cfg.settings.extraPostConfig
|
|
||||||
];
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,18 +4,11 @@ let
|
|||||||
cfg = config.programs.shed;
|
cfg = config.programs.shed;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.programs.shed = {
|
options.programs.shed = import ./shed_opts.nix { inherit pkgs lib; };
|
||||||
enable = lib.mkEnableOption "shed shell";
|
|
||||||
|
|
||||||
package = lib.mkOption {
|
|
||||||
type = lib.types.package;
|
|
||||||
default = pkgs.shed;
|
|
||||||
description = "The shed package to use";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
environment.systemPackages = [ cfg.package ];
|
environment.systemPackages = [ cfg.package ];
|
||||||
environment.shells = [ cfg.package ];
|
environment.shells = [ cfg.package ];
|
||||||
|
environment.etc."shed/shedrc".text = import ./render_rc.nix lib cfg;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
86
nix/render_rc.nix
Normal file
86
nix/render_rc.nix
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
lib: cfg:
|
||||||
|
|
||||||
|
let
|
||||||
|
boolToString = b:
|
||||||
|
if b then "true" else "false";
|
||||||
|
|
||||||
|
mkAutoCmd = cfg:
|
||||||
|
lib.concatLines (map (hook: "autocmd ${hook} ${lib.optionalString (cfg.pattern != null) "-p \"${cfg.pattern}\""} '${cfg.command}'") cfg.hooks);
|
||||||
|
|
||||||
|
|
||||||
|
mkFunctionDef = name: body:
|
||||||
|
let
|
||||||
|
indented = "\t" + lib.concatStringsSep "\n\t" (lib.splitString "\n" body);
|
||||||
|
in
|
||||||
|
''
|
||||||
|
${name}() {
|
||||||
|
${indented}
|
||||||
|
}'';
|
||||||
|
|
||||||
|
mkKeymapCmd = cfg: let
|
||||||
|
flags = "-${lib.concatStrings cfg.modes}";
|
||||||
|
keys = "'${cfg.keys}'";
|
||||||
|
action = "'${cfg.command}'";
|
||||||
|
in
|
||||||
|
"keymap ${flags} ${keys} ${action}";
|
||||||
|
|
||||||
|
|
||||||
|
mkCompleteCmd = name: cfg: let
|
||||||
|
flags = lib.concatStrings [
|
||||||
|
(lib.optionalString cfg.files " -f")
|
||||||
|
(lib.optionalString cfg.dirs " -d")
|
||||||
|
(lib.optionalString cfg.commands " -c")
|
||||||
|
(lib.optionalString cfg.variables " -v")
|
||||||
|
(lib.optionalString cfg.users " -u")
|
||||||
|
(lib.optionalString cfg.jobs " -j")
|
||||||
|
(lib.optionalString cfg.aliases " -a")
|
||||||
|
(lib.optionalString cfg.signals " -S")
|
||||||
|
(lib.optionalString cfg.noSpace " -n")
|
||||||
|
(lib.optionalString (cfg.function != null) " -F ${cfg.function}")
|
||||||
|
(lib.optionalString (cfg.fallback != "no") " -o ${cfg.fallback}")
|
||||||
|
(lib.optionalString (cfg.wordList != []) " -W '${lib.concatStringsSep " " cfg.wordList}'")
|
||||||
|
|
||||||
|
];
|
||||||
|
in "complete${flags} ${name}";
|
||||||
|
|
||||||
|
completeLines = lib.concatLines (lib.mapAttrsToList mkCompleteCmd cfg.extraCompletion);
|
||||||
|
keymapLines = lib.concatLines (map mkKeymapCmd cfg.keymaps);
|
||||||
|
functionLines = lib.concatLines (lib.mapAttrsToList mkFunctionDef cfg.functions);
|
||||||
|
autocmdLines = lib.concatLines (map mkAutoCmd cfg.autocmds);
|
||||||
|
in
|
||||||
|
lib.concatLines [
|
||||||
|
cfg.extraPreConfig
|
||||||
|
(lib.concatLines (lib.mapAttrsToList (name: value: "export ${name}=\"${value}\"") cfg.environmentVars))
|
||||||
|
(lib.concatLines (lib.mapAttrsToList (name: value: "alias ${name}=\"${value}\"") cfg.aliases))
|
||||||
|
(lib.concatLines [
|
||||||
|
"shopt line.viewport_height=${toString cfg.shopts.line.viewport_height}"
|
||||||
|
"shopt line.scroll_offset=${toString cfg.shopts.line.scroll_offset}"
|
||||||
|
|
||||||
|
"shopt core.dotglob=${boolToString cfg.shopts.core.dotglob}"
|
||||||
|
"shopt core.autocd=${boolToString cfg.shopts.core.autocd}"
|
||||||
|
"shopt core.hist_ignore_dupes=${boolToString cfg.shopts.core.hist_ignore_dupes}"
|
||||||
|
"shopt core.max_hist=${toString cfg.shopts.core.max_hist}"
|
||||||
|
"shopt core.interactive_comments=${boolToString cfg.shopts.core.interactive_comments}"
|
||||||
|
"shopt core.auto_hist=${boolToString cfg.shopts.core.auto_hist}"
|
||||||
|
"shopt core.bell_enabled=${boolToString cfg.shopts.core.bell_enabled}"
|
||||||
|
"shopt core.max_recurse_depth=${toString cfg.shopts.core.max_recurse_depth}"
|
||||||
|
"shopt core.xpg_echo=${boolToString cfg.shopts.core.xpg_echo}"
|
||||||
|
"shopt core.noclobber=${boolToString cfg.shopts.core.noclobber}"
|
||||||
|
|
||||||
|
"shopt prompt.leader='${cfg.shopts.prompt.leader}'"
|
||||||
|
"shopt prompt.trunc_prompt_path=${toString cfg.shopts.prompt.trunc_prompt_path}"
|
||||||
|
"shopt prompt.comp_limit=${toString cfg.shopts.prompt.comp_limit}"
|
||||||
|
"shopt prompt.highlight=${boolToString cfg.shopts.prompt.highlight}"
|
||||||
|
"shopt prompt.linebreak_on_incomplete=${boolToString cfg.shopts.prompt.linebreak_on_incomplete}"
|
||||||
|
"shopt prompt.line_numbers=${boolToString cfg.shopts.prompt.line_numbers}"
|
||||||
|
"shopt prompt.screensaver_idle_time=${toString cfg.shopts.prompt.screensaver_idle_time}"
|
||||||
|
"shopt prompt.screensaver_cmd='${cfg.shopts.prompt.screensaver_cmd}'"
|
||||||
|
"shopt prompt.completion_ignore_case=${boolToString cfg.shopts.prompt.completion_ignore_case}"
|
||||||
|
"shopt prompt.auto_indent=${boolToString cfg.shopts.prompt.auto_indent}"
|
||||||
|
functionLines
|
||||||
|
completeLines
|
||||||
|
keymapLines
|
||||||
|
autocmdLines
|
||||||
|
])
|
||||||
|
cfg.extraPostConfig
|
||||||
|
]
|
||||||
316
nix/shed_opts.nix
Normal file
316
nix/shed_opts.nix
Normal file
@@ -0,0 +1,316 @@
|
|||||||
|
{ pkgs, lib }:
|
||||||
|
|
||||||
|
{
|
||||||
|
enable = lib.mkEnableOption "shed shell";
|
||||||
|
|
||||||
|
package = lib.mkOption {
|
||||||
|
type = lib.types.package;
|
||||||
|
default = pkgs.shed;
|
||||||
|
description = "The shed package to use";
|
||||||
|
};
|
||||||
|
|
||||||
|
aliases = lib.mkOption {
|
||||||
|
type = lib.types.attrsOf lib.types.str;
|
||||||
|
default = {};
|
||||||
|
description = "Aliases to set when shed starts";
|
||||||
|
};
|
||||||
|
|
||||||
|
functions = lib.mkOption {
|
||||||
|
type = lib.types.attrsOf lib.types.str;
|
||||||
|
default = {};
|
||||||
|
description = "Shell functions to set when shed starts";
|
||||||
|
};
|
||||||
|
|
||||||
|
autocmds = lib.mkOption {
|
||||||
|
type = lib.types.listOf (lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
hooks = lib.mkOption {
|
||||||
|
type = lib.types.addCheck (lib.types.listOf (lib.types.enum [
|
||||||
|
"pre-cmd"
|
||||||
|
"post-cmd"
|
||||||
|
"pre-change-dir"
|
||||||
|
"post-change-dir"
|
||||||
|
"on-job-finish"
|
||||||
|
"pre-prompt"
|
||||||
|
"post-prompt"
|
||||||
|
"pre-mode-change"
|
||||||
|
"post-mode-change"
|
||||||
|
"on-exit"
|
||||||
|
"on-history-open"
|
||||||
|
"on-history-close"
|
||||||
|
"on-history-select"
|
||||||
|
"on-completion-start"
|
||||||
|
"on-completion-cancel"
|
||||||
|
"on-completion-select"
|
||||||
|
])) (list: list != []);
|
||||||
|
description = "The events that trigger this autocmd";
|
||||||
|
};
|
||||||
|
pattern = lib.mkOption {
|
||||||
|
type = lib.types.nullOr lib.types.str;
|
||||||
|
default = null;
|
||||||
|
description = "A regex pattern to use in the hook to determine whether it runs or not. What it's compared to differs by hook, for instance 'pre-change-dir' compares it to the new directory, pre-cmd compares it to the command, etc";
|
||||||
|
};
|
||||||
|
command = lib.mkOption {
|
||||||
|
type = lib.types.addCheck lib.types.str (cmd: cmd != "");
|
||||||
|
description = "The shell command to execute when the hook is triggered and the pattern (if provided) matches";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
default = [];
|
||||||
|
description = "Custom autocmds to set when shed starts";
|
||||||
|
};
|
||||||
|
|
||||||
|
keymaps = lib.mkOption {
|
||||||
|
type = lib.types.listOf (lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
modes = lib.mkOption {
|
||||||
|
type = lib.types.listOf (lib.types.enum [ "n" "i" "x" "v" "o" "r" ]);
|
||||||
|
default = [];
|
||||||
|
description = "The editing modes this keymap can be used in";
|
||||||
|
};
|
||||||
|
keys = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "";
|
||||||
|
description = "The sequence of keys that trigger this keymap";
|
||||||
|
};
|
||||||
|
command = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "";
|
||||||
|
description = "The sequence of characters to send to the line editor when the keymap is triggered.";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
});
|
||||||
|
default = [];
|
||||||
|
description = "Custom keymaps to set when shed starts";
|
||||||
|
};
|
||||||
|
|
||||||
|
extraCompletion = lib.mkOption {
|
||||||
|
type = lib.types.attrsOf (lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
files = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete file names in the current directory";
|
||||||
|
};
|
||||||
|
dirs = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete directory names in the current directory";
|
||||||
|
};
|
||||||
|
commands = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete executable commands in the PATH";
|
||||||
|
};
|
||||||
|
variables = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete variable names";
|
||||||
|
};
|
||||||
|
users = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete user names from /etc/passwd";
|
||||||
|
};
|
||||||
|
jobs = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete job names or pids from the current shell session";
|
||||||
|
};
|
||||||
|
aliases = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete alias names defined in the current shell session";
|
||||||
|
};
|
||||||
|
signals = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Complete signal names for commands like kill";
|
||||||
|
};
|
||||||
|
wordList = lib.mkOption {
|
||||||
|
type = lib.types.listOf lib.types.str;
|
||||||
|
default = [];
|
||||||
|
description = "Complete from a custom list of words";
|
||||||
|
};
|
||||||
|
function = lib.mkOption {
|
||||||
|
type = lib.types.nullOr lib.types.str;
|
||||||
|
default = null;
|
||||||
|
description = "Complete using a custom shell function (should be defined in extraCompletionPreConfig)";
|
||||||
|
};
|
||||||
|
noSpace = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Don't append a space after completion";
|
||||||
|
};
|
||||||
|
fallback = lib.mkOption {
|
||||||
|
type = lib.types.enum [ "no" "default" "dirnames" ];
|
||||||
|
default = "no";
|
||||||
|
description = "Fallback behavior when no matches are found: 'no' means no fallback, 'default' means fall back to the default shell completion behavior, and 'directories' means fall back to completing directory names";
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
});
|
||||||
|
default = {};
|
||||||
|
description = "Additional completion scripts to source when shed starts (e.g. for custom tools or functions)";
|
||||||
|
};
|
||||||
|
|
||||||
|
environmentVars = lib.mkOption {
|
||||||
|
type = lib.types.attrsOf lib.types.str;
|
||||||
|
default = {};
|
||||||
|
description = "Environment variables to set when shed starts";
|
||||||
|
};
|
||||||
|
|
||||||
|
shopts = lib.mkOption {
|
||||||
|
type = lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
line = lib.mkOption {
|
||||||
|
type = lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
viewport_height = lib.mkOption {
|
||||||
|
type = lib.types.either lib.types.int lib.types.str;
|
||||||
|
default = "50%";
|
||||||
|
description = "Maximum viewport height for the line editor buffer";
|
||||||
|
};
|
||||||
|
scroll_offset = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
|
default = 1;
|
||||||
|
description = "The minimum number of lines to keep visible above and below the cursor when scrolling (i.e. the 'scrolloff' option in vim)";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
default = {};
|
||||||
|
description = "Settings related to the line editor (i.e. the 'shopt line.*' options)";
|
||||||
|
};
|
||||||
|
core = lib.mkOption {
|
||||||
|
type = lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
dotglob = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to include hidden files in glob patterns";
|
||||||
|
};
|
||||||
|
autocd = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to automatically change into directories when they are entered as commands";
|
||||||
|
};
|
||||||
|
hist_ignore_dupes = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to ignore duplicate entries in the command history";
|
||||||
|
};
|
||||||
|
max_hist = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
|
default = 10000;
|
||||||
|
description = "The maximum number of entries to keep in the command history";
|
||||||
|
};
|
||||||
|
interactive_comments = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
description = "Whether to allow comments in interactive mode";
|
||||||
|
};
|
||||||
|
auto_hist = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
description = "Whether to automatically add commands to the history as they are executed";
|
||||||
|
};
|
||||||
|
bell_enabled = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
description = "Whether to allow shed to ring the terminal bell on certain events (e.g. command completion, errors, etc.)";
|
||||||
|
};
|
||||||
|
max_recurse_depth = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
|
default = 1000;
|
||||||
|
description = "The maximum depth to allow when recursively executing shell functions";
|
||||||
|
};
|
||||||
|
xpg_echo = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to have the 'echo' builtin expand escape sequences like \\n and \\t (if false, it will print them verbatim)";
|
||||||
|
};
|
||||||
|
noclobber = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to prevent redirection from overwriting existing files by default (i.e. behave as if 'set -o noclobber' is always in effect)";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
default = {};
|
||||||
|
description = "Core settings (i.e. the 'shopt core.*' options)";
|
||||||
|
};
|
||||||
|
prompt = lib.mkOption {
|
||||||
|
type = lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
leader = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "\\\\";
|
||||||
|
description = "The leader key to use for custom keymaps (e.g. if set to '\\\\', then a keymap with keys='x' would be triggered by '\\x')";
|
||||||
|
};
|
||||||
|
trunc_prompt_path = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
|
default = 4;
|
||||||
|
description = "The maximum number of path segments to show in the prompt";
|
||||||
|
};
|
||||||
|
comp_limit = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
|
default = 1000;
|
||||||
|
description = "The maximum number of completion candidates to show before truncating the list";
|
||||||
|
};
|
||||||
|
highlight = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
description = "Whether to enable syntax highlighting in the shell";
|
||||||
|
};
|
||||||
|
linebreak_on_incomplete = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
description = "Whether to automatically insert a newline when the input is incomplete";
|
||||||
|
};
|
||||||
|
line_numbers = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to show line numbers in the prompt";
|
||||||
|
};
|
||||||
|
screensaver_cmd = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "";
|
||||||
|
description = "A shell command to execute after a period of inactivity (i.e. a custom screensaver)";
|
||||||
|
};
|
||||||
|
screensaver_idle_time = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
|
default = 0;
|
||||||
|
description = "The amount of inactivity time in seconds before the screensaver command is executed";
|
||||||
|
};
|
||||||
|
completion_ignore_case = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to ignore case when completing commands and file names";
|
||||||
|
};
|
||||||
|
auto_indent = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
description = "Whether to automatically indent new lines based on the previous line";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
default = {};
|
||||||
|
description = "Settings related to the prompt (i.e. the 'shopt prompt.*' options)";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
default = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
extraPostConfig = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "";
|
||||||
|
description = "Additional configuration to append to the shed configuration file";
|
||||||
|
};
|
||||||
|
extraPreConfig = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "";
|
||||||
|
description = "Additional configuration to prepend to the shed configuration file";
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -38,7 +38,6 @@ pub fn alias(node: Node) -> ShResult<()> {
|
|||||||
write(stdout, alias_output.as_bytes())?; // Write it
|
write(stdout, alias_output.as_bytes())?; // Write it
|
||||||
} else {
|
} else {
|
||||||
for (arg, span) in argv {
|
for (arg, span) in argv {
|
||||||
|
|
||||||
let Some((name, body)) = arg.split_once('=') else {
|
let Some((name, body)) = arg.split_once('=') else {
|
||||||
let Some(alias) = read_logic(|l| l.get_alias(&arg)) else {
|
let Some(alias) = read_logic(|l| l.get_alias(&arg)) else {
|
||||||
return Err(ShErr::at(
|
return Err(ShErr::at(
|
||||||
@@ -59,7 +58,10 @@ pub fn alias(node: Node) -> ShResult<()> {
|
|||||||
return Err(ShErr::at(
|
return Err(ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span,
|
span,
|
||||||
format!("alias: Cannot assign alias to reserved name '{}'", name.fg(next_color())),
|
format!(
|
||||||
|
"alias: Cannot assign alias to reserved name '{}'",
|
||||||
|
name.fg(next_color())
|
||||||
|
),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
write_logic(|l| l.insert_alias(name, body, span.clone()));
|
write_logic(|l| l.insert_alias(name, body, span.clone()));
|
||||||
|
|||||||
@@ -229,9 +229,9 @@ pub fn get_arr_op_opts(opts: Vec<Opt>) -> ShResult<ArrOpOpts> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::collections::VecDeque;
|
use crate::state::{self, VarFlags, VarKind, read_vars, write_vars};
|
||||||
use crate::state::{self, read_vars, write_vars, VarFlags, VarKind};
|
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
fn set_arr(name: &str, elems: &[&str]) {
|
fn set_arr(name: &str, elems: &[&str]) {
|
||||||
let arr = VecDeque::from_iter(elems.iter().map(|s| s.to_string()));
|
let arr = VecDeque::from_iter(elems.iter().map(|s| s.to_string()));
|
||||||
|
|||||||
@@ -159,7 +159,10 @@ mod tests {
|
|||||||
test_input("autocmd post-cmd 'echo post'").unwrap();
|
test_input("autocmd post-cmd 'echo post'").unwrap();
|
||||||
|
|
||||||
assert_eq!(read_logic(|l| l.get_autocmds(AutoCmdKind::PreCmd)).len(), 1);
|
assert_eq!(read_logic(|l| l.get_autocmds(AutoCmdKind::PreCmd)).len(), 1);
|
||||||
assert_eq!(read_logic(|l| l.get_autocmds(AutoCmdKind::PostCmd)).len(), 1);
|
assert_eq!(
|
||||||
|
read_logic(|l| l.get_autocmds(AutoCmdKind::PostCmd)).len(),
|
||||||
|
1
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===================== Pattern =====================
|
// ===================== Pattern =====================
|
||||||
@@ -205,7 +208,10 @@ mod tests {
|
|||||||
|
|
||||||
test_input("autocmd -c pre-cmd").unwrap();
|
test_input("autocmd -c pre-cmd").unwrap();
|
||||||
assert_eq!(read_logic(|l| l.get_autocmds(AutoCmdKind::PreCmd)).len(), 0);
|
assert_eq!(read_logic(|l| l.get_autocmds(AutoCmdKind::PreCmd)).len(), 0);
|
||||||
assert_eq!(read_logic(|l| l.get_autocmds(AutoCmdKind::PostCmd)).len(), 1);
|
assert_eq!(
|
||||||
|
read_logic(|l| l.get_autocmds(AutoCmdKind::PostCmd)).len(),
|
||||||
|
1
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -245,11 +251,21 @@ mod tests {
|
|||||||
fn all_kinds_parse() {
|
fn all_kinds_parse() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
let kinds = [
|
let kinds = [
|
||||||
"pre-cmd", "post-cmd", "pre-change-dir", "post-change-dir",
|
"pre-cmd",
|
||||||
"on-job-finish", "pre-prompt", "post-prompt",
|
"post-cmd",
|
||||||
"pre-mode-change", "post-mode-change",
|
"pre-change-dir",
|
||||||
"on-history-open", "on-history-close", "on-history-select",
|
"post-change-dir",
|
||||||
"on-completion-start", "on-completion-cancel", "on-completion-select",
|
"on-job-finish",
|
||||||
|
"pre-prompt",
|
||||||
|
"post-prompt",
|
||||||
|
"pre-mode-change",
|
||||||
|
"post-mode-change",
|
||||||
|
"on-history-open",
|
||||||
|
"on-history-close",
|
||||||
|
"on-history-select",
|
||||||
|
"on-completion-start",
|
||||||
|
"on-completion-cancel",
|
||||||
|
"on-completion-select",
|
||||||
"on-exit",
|
"on-exit",
|
||||||
];
|
];
|
||||||
for kind in kinds {
|
for kind in kinds {
|
||||||
|
|||||||
@@ -99,7 +99,10 @@ pub mod tests {
|
|||||||
let new_dir = env::current_dir().unwrap();
|
let new_dir = env::current_dir().unwrap();
|
||||||
assert_ne!(old_dir, new_dir);
|
assert_ne!(old_dir, new_dir);
|
||||||
|
|
||||||
assert_eq!(new_dir.display().to_string(), temp_dir.path().display().to_string());
|
assert_eq!(
|
||||||
|
new_dir.display().to_string(),
|
||||||
|
temp_dir.path().display().to_string()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -111,7 +114,10 @@ pub mod tests {
|
|||||||
test_input("cd").unwrap();
|
test_input("cd").unwrap();
|
||||||
|
|
||||||
let cwd = env::current_dir().unwrap();
|
let cwd = env::current_dir().unwrap();
|
||||||
assert_eq!(cwd.display().to_string(), temp_dir.path().display().to_string());
|
assert_eq!(
|
||||||
|
cwd.display().to_string(),
|
||||||
|
temp_dir.path().display().to_string()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -316,10 +316,10 @@ pub fn get_comp_opts(opts: Vec<Opt>) -> ShResult<CompOpts> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::state::{self, VarFlags, VarKind, read_meta, write_vars};
|
||||||
|
use crate::testutil::{TestGuard, test_input};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
use crate::state::{self, read_meta, write_vars, VarFlags, VarKind};
|
|
||||||
use crate::testutil::{TestGuard, test_input};
|
|
||||||
|
|
||||||
// ===================== complete: Registration =====================
|
// ===================== complete: Registration =====================
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ use crate::{
|
|||||||
|
|
||||||
pub fn truncate_home_path(path: String) -> String {
|
pub fn truncate_home_path(path: String) -> String {
|
||||||
if let Ok(home) = env::var("HOME")
|
if let Ok(home) = env::var("HOME")
|
||||||
&& path.starts_with(&home) {
|
&& path.starts_with(&home)
|
||||||
|
{
|
||||||
let new = path.strip_prefix(&home).unwrap();
|
let new = path.strip_prefix(&home).unwrap();
|
||||||
return format!("~{new}");
|
return format!("~{new}");
|
||||||
}
|
}
|
||||||
@@ -376,8 +377,7 @@ pub fn dirs(node: Node) -> ShResult<()> {
|
|||||||
.map(|d| d.to_string_lossy().to_string());
|
.map(|d| d.to_string_lossy().to_string());
|
||||||
|
|
||||||
if abbreviate_home {
|
if abbreviate_home {
|
||||||
stack.map(truncate_home_path)
|
stack.map(truncate_home_path).collect()
|
||||||
.collect()
|
|
||||||
} else {
|
} else {
|
||||||
stack.collect()
|
stack.collect()
|
||||||
}
|
}
|
||||||
@@ -428,9 +428,12 @@ pub fn dirs(node: Node) -> ShResult<()> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
|
use crate::{
|
||||||
|
state::{self, read_meta},
|
||||||
|
testutil::{TestGuard, test_input},
|
||||||
|
};
|
||||||
|
use pretty_assertions::{assert_eq, assert_ne};
|
||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
use crate::{state::{self, read_meta}, testutil::{TestGuard, test_input}};
|
|
||||||
use pretty_assertions::{assert_ne,assert_eq};
|
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -580,8 +583,14 @@ use tempfile::TempDir;
|
|||||||
let out = g.read_output();
|
let out = g.read_output();
|
||||||
let lines: Vec<&str> = out.split('\n').filter(|l| !l.is_empty()).collect();
|
let lines: Vec<&str> = out.split('\n').filter(|l| !l.is_empty()).collect();
|
||||||
assert_eq!(lines.len(), 2);
|
assert_eq!(lines.len(), 2);
|
||||||
assert_eq!(lines[0], super::truncate_home_path(path.to_string_lossy().to_string()));
|
assert_eq!(
|
||||||
assert_eq!(lines[1], super::truncate_home_path(original.to_string_lossy().to_string()));
|
lines[0],
|
||||||
|
super::truncate_home_path(path.to_string_lossy().to_string())
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
lines[1],
|
||||||
|
super::truncate_home_path(original.to_string_lossy().to_string())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ pub fn echo(node: Node) -> ShResult<()> {
|
|||||||
let output_channel = borrow_fd(STDOUT_FILENO);
|
let output_channel = borrow_fd(STDOUT_FILENO);
|
||||||
let xpg_echo = read_shopts(|o| o.core.xpg_echo); // If true, echo expands escape sequences by default, and -E opts out
|
let xpg_echo = read_shopts(|o| o.core.xpg_echo); // If true, echo expands escape sequences by default, and -E opts out
|
||||||
|
|
||||||
let use_escape = (xpg_echo && !flags.contains(EchoFlags::NO_ESCAPE)) || flags.contains(EchoFlags::USE_ESCAPE);
|
let use_escape =
|
||||||
|
(xpg_echo && !flags.contains(EchoFlags::NO_ESCAPE)) || flags.contains(EchoFlags::USE_ESCAPE);
|
||||||
|
|
||||||
let mut echo_output = prepare_echo_args(
|
let mut echo_output = prepare_echo_args(
|
||||||
argv
|
argv
|
||||||
@@ -308,11 +309,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn prepare_multiple_args() {
|
fn prepare_multiple_args() {
|
||||||
let result = prepare_echo_args(
|
let result = prepare_echo_args(vec!["hello".into(), "world".into()], false, false).unwrap();
|
||||||
vec!["hello".into(), "world".into()],
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
).unwrap();
|
|
||||||
assert_eq!(result, vec!["hello", "world"]);
|
assert_eq!(result, vec!["hello", "world"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ pub fn eval(node: Node) -> ShResult<()> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::state::{self, read_vars, write_vars, VarFlags, VarKind};
|
use crate::state::{self, VarFlags, VarKind, read_vars, write_vars};
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
|
||||||
// ===================== Basic =====================
|
// ===================== Basic =====================
|
||||||
@@ -80,7 +80,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn eval_expands_variable() {
|
fn eval_expands_variable() {
|
||||||
let guard = TestGuard::new();
|
let guard = TestGuard::new();
|
||||||
write_vars(|v| v.set_var("CMD", VarKind::Str("echo evaluated".into()), VarFlags::NONE)).unwrap();
|
write_vars(|v| v.set_var("CMD", VarKind::Str("echo evaluated".into()), VarFlags::NONE))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
test_input("eval $CMD").unwrap();
|
test_input("eval $CMD").unwrap();
|
||||||
let out = guard.read_output();
|
let out = guard.read_output();
|
||||||
|
|||||||
@@ -62,7 +62,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn exec_nonexistent_command_fails() {
|
fn exec_nonexistent_command_fails() {
|
||||||
let _g = TestGuard::new();
|
let _g = TestGuard::new();
|
||||||
let result = test_input("exec _____________no_such_______command_xyz_____________hopefully______this_doesnt______exist_____somewhere_in___your______PATH__________________");
|
let result = test_input(
|
||||||
|
"exec _____________no_such_______command_xyz_____________hopefully______this_doesnt______exist_____somewhere_in___your______PATH__________________",
|
||||||
|
);
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
306
src/builtin/help.rs
Normal file
306
src/builtin/help.rs
Normal file
@@ -0,0 +1,306 @@
|
|||||||
|
use std::{env, io::Write, path::Path};
|
||||||
|
|
||||||
|
use ariadne::Span as ASpan;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
libsh::{
|
||||||
|
error::{ShErr, ShErrKind, ShResult},
|
||||||
|
guards::RawModeGuard,
|
||||||
|
},
|
||||||
|
parse::{
|
||||||
|
NdRule, Node,
|
||||||
|
execute::{exec_input, prepare_argv},
|
||||||
|
lex::{QuoteState, Span},
|
||||||
|
},
|
||||||
|
readline::{complete::ScoredCandidate, markers},
|
||||||
|
state,
|
||||||
|
};
|
||||||
|
|
||||||
|
const TAG_SEQ: &str = "\x1b[1;33m"; // bold yellow — searchable tags
|
||||||
|
const REF_SEQ: &str = "\x1b[4;36m"; // underline cyan — cross-references
|
||||||
|
const RESET_SEQ: &str = "\x1b[0m";
|
||||||
|
const HEADER_SEQ: &str = "\x1b[1;35m"; // bold magenta — section headers
|
||||||
|
const CODE_SEQ: &str = "\x1b[32m"; // green — inline code
|
||||||
|
const KEYWORD_2_SEQ: &str = "\x1b[1;32m"; // bold green — {keyword}
|
||||||
|
const KEYWORD_3_SEQ: &str = "\x1b[3;37m"; // italic white — [optional]
|
||||||
|
|
||||||
|
pub fn help(node: Node) -> ShResult<()> {
|
||||||
|
let NdRule::Command {
|
||||||
|
assignments: _,
|
||||||
|
argv,
|
||||||
|
} = node.class
|
||||||
|
else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut argv = prepare_argv(argv)?.into_iter().peekable();
|
||||||
|
let help = argv.next().unwrap(); // drop 'help'
|
||||||
|
|
||||||
|
// Join all of the word-split arguments into a single string
|
||||||
|
// Preserve the span too
|
||||||
|
let (topic, span) = if argv.peek().is_none() {
|
||||||
|
("help.txt".to_string(), help.1)
|
||||||
|
} else {
|
||||||
|
argv.fold((String::new(), Span::default()), |mut acc, arg| {
|
||||||
|
if acc.1 == Span::default() {
|
||||||
|
acc.1 = arg.1.clone();
|
||||||
|
} else {
|
||||||
|
let new_end = arg.1.end();
|
||||||
|
let start = acc.1.start();
|
||||||
|
acc.1.set_range(start..new_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
if acc.0.is_empty() {
|
||||||
|
acc.0 = arg.0;
|
||||||
|
} else {
|
||||||
|
acc.0 = acc.0 + &format!(" {}", arg.0);
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let hpath = env::var("SHED_HPATH").unwrap_or_default();
|
||||||
|
|
||||||
|
// search for prefixes of help doc filenames
|
||||||
|
for path in hpath.split(':') {
|
||||||
|
let dir = Path::new(path);
|
||||||
|
let Ok(entries) = dir.read_dir() else { continue };
|
||||||
|
for entry in entries {
|
||||||
|
let Ok(entry) = entry else { continue };
|
||||||
|
let path = entry.path();
|
||||||
|
if !path.is_file() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let stem = path.file_stem().unwrap().to_string_lossy();
|
||||||
|
if stem.starts_with(&topic) {
|
||||||
|
let Ok(contents) = std::fs::read_to_string(&path) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let unescaped = unescape_help(&contents);
|
||||||
|
let expanded = expand_help(&unescaped);
|
||||||
|
open_help(&expanded, None, Some(stem.into_owned()))?;
|
||||||
|
state::set_status(0);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// didn't find a filename match, its probably a tag search
|
||||||
|
for path in hpath.split(':') {
|
||||||
|
let path = Path::new(path);
|
||||||
|
if let Ok(entries) = path.read_dir() {
|
||||||
|
for entry in entries {
|
||||||
|
let Ok(entry) = entry else { continue };
|
||||||
|
let path = entry.path();
|
||||||
|
let filename = path.file_stem().unwrap().to_string_lossy().to_string();
|
||||||
|
|
||||||
|
if !path.is_file() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(contents) = std::fs::read_to_string(&path) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let unescaped = unescape_help(&contents);
|
||||||
|
let expanded = expand_help(&unescaped);
|
||||||
|
let tags = read_tags(&expanded);
|
||||||
|
|
||||||
|
for (_tag, _line) in &tags {}
|
||||||
|
|
||||||
|
if let Some((_matched_tag, line)) = get_best_match(&topic, &tags) {
|
||||||
|
open_help(&expanded, Some(line), Some(filename))?;
|
||||||
|
state::set_status(0);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state::set_status(1);
|
||||||
|
Err(ShErr::at(
|
||||||
|
ShErrKind::NotFound,
|
||||||
|
span,
|
||||||
|
"No relevant help page found for this topic",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open_help(content: &str, line: Option<usize>, file_name: Option<String>) -> ShResult<()> {
|
||||||
|
let pager = env::var("SHED_HPAGER").unwrap_or(env::var("PAGER").unwrap_or("less -R".into()));
|
||||||
|
let line_arg = line.map(|ln| format!("+{ln}")).unwrap_or_default();
|
||||||
|
let prompt_arg = file_name
|
||||||
|
.map(|name| format!("-Ps'{name}'"))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let mut tmp = tempfile::NamedTempFile::new()?;
|
||||||
|
let tmp_path = tmp.path().to_string_lossy().to_string();
|
||||||
|
tmp.write_all(content.as_bytes())?;
|
||||||
|
tmp.flush()?;
|
||||||
|
|
||||||
|
RawModeGuard::with_cooked_mode(|| {
|
||||||
|
exec_input(
|
||||||
|
format!("{pager} {line_arg} {prompt_arg} {tmp_path}"),
|
||||||
|
None,
|
||||||
|
true,
|
||||||
|
Some("help".into()),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_best_match(topic: &str, tags: &[(String, usize)]) -> Option<(String, usize)> {
|
||||||
|
let mut candidates: Vec<_> = tags
|
||||||
|
.iter()
|
||||||
|
.map(|(tag, line)| (ScoredCandidate::new(tag.to_string()), *line))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for (cand, _) in candidates.iter_mut() {
|
||||||
|
cand.fuzzy_score(topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
candidates.retain(|(c, _)| c.score.unwrap_or(i32::MIN) > i32::MIN);
|
||||||
|
candidates.sort_by_key(|(c, _)| c.score.unwrap_or(i32::MIN));
|
||||||
|
|
||||||
|
candidates
|
||||||
|
.first()
|
||||||
|
.map(|(c, line)| (c.content.clone(), *line))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_tags(raw: &str) -> Vec<(String, usize)> {
|
||||||
|
let mut tags = vec![];
|
||||||
|
|
||||||
|
for (line_num, line) in raw.lines().enumerate() {
|
||||||
|
let mut rest = line;
|
||||||
|
|
||||||
|
while let Some(pos) = rest.find(TAG_SEQ) {
|
||||||
|
let after_seq = &rest[pos + TAG_SEQ.len()..];
|
||||||
|
if let Some(end) = after_seq.find(RESET_SEQ) {
|
||||||
|
let tag = &after_seq[..end];
|
||||||
|
tags.push((tag.to_string(), line_num + 1));
|
||||||
|
rest = &after_seq[end + RESET_SEQ.len()..];
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tags
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn expand_help(raw: &str) -> String {
|
||||||
|
let mut result = String::new();
|
||||||
|
let mut chars = raw.chars();
|
||||||
|
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
match ch {
|
||||||
|
markers::RESET => result.push_str(RESET_SEQ),
|
||||||
|
markers::TAG => result.push_str(TAG_SEQ),
|
||||||
|
markers::REFERENCE => result.push_str(REF_SEQ),
|
||||||
|
markers::HEADER => result.push_str(HEADER_SEQ),
|
||||||
|
markers::CODE => result.push_str(CODE_SEQ),
|
||||||
|
markers::KEYWORD_2 => result.push_str(KEYWORD_2_SEQ),
|
||||||
|
markers::KEYWORD_3 => result.push_str(KEYWORD_3_SEQ),
|
||||||
|
_ => result.push(ch),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unescape_help(raw: &str) -> String {
|
||||||
|
let mut result = String::new();
|
||||||
|
let mut chars = raw.chars().peekable();
|
||||||
|
let mut qt_state = QuoteState::default();
|
||||||
|
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
match ch {
|
||||||
|
'\\' => {
|
||||||
|
if let Some(next_ch) = chars.next() {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'\n' => {
|
||||||
|
result.push(ch);
|
||||||
|
qt_state = QuoteState::default();
|
||||||
|
}
|
||||||
|
'"' => {
|
||||||
|
result.push(ch);
|
||||||
|
qt_state.toggle_double();
|
||||||
|
}
|
||||||
|
'\'' => {
|
||||||
|
result.push(ch);
|
||||||
|
qt_state.toggle_single();
|
||||||
|
}
|
||||||
|
_ if qt_state.in_quote() || chars.peek().is_none_or(|ch| ch.is_whitespace()) => {
|
||||||
|
result.push(ch);
|
||||||
|
}
|
||||||
|
'*' => {
|
||||||
|
result.push(markers::TAG);
|
||||||
|
while let Some(next_ch) = chars.next() {
|
||||||
|
if next_ch == '*' {
|
||||||
|
result.push(markers::RESET);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'|' => {
|
||||||
|
result.push(markers::REFERENCE);
|
||||||
|
while let Some(next_ch) = chars.next() {
|
||||||
|
if next_ch == '|' {
|
||||||
|
result.push(markers::RESET);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'#' => {
|
||||||
|
result.push(markers::HEADER);
|
||||||
|
while let Some(next_ch) = chars.next() {
|
||||||
|
if next_ch == '#' {
|
||||||
|
result.push(markers::RESET);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'`' => {
|
||||||
|
result.push(markers::CODE);
|
||||||
|
while let Some(next_ch) = chars.next() {
|
||||||
|
if next_ch == '`' {
|
||||||
|
result.push(markers::RESET);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'{' => {
|
||||||
|
result.push(markers::KEYWORD_2);
|
||||||
|
while let Some(next_ch) = chars.next() {
|
||||||
|
if next_ch == '}' {
|
||||||
|
result.push(markers::RESET);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'[' => {
|
||||||
|
result.push(markers::KEYWORD_3);
|
||||||
|
while let Some(next_ch) = chars.next() {
|
||||||
|
if next_ch == ']' {
|
||||||
|
result.push(markers::RESET);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => result.push(ch),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
@@ -180,8 +180,8 @@ pub fn keymap(node: Node) -> ShResult<()> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::getopt::Opt;
|
|
||||||
use crate::expand::expand_keymap;
|
use crate::expand::expand_keymap;
|
||||||
|
use crate::getopt::Opt;
|
||||||
use crate::state::{self, read_logic};
|
use crate::state::{self, read_logic};
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
|
||||||
@@ -217,7 +217,8 @@ mod tests {
|
|||||||
let opts = KeyMapOpts::from_opts(&[
|
let opts = KeyMapOpts::from_opts(&[
|
||||||
Opt::Short('n'),
|
Opt::Short('n'),
|
||||||
Opt::LongWithArg("remove".into(), "jk".into()),
|
Opt::LongWithArg("remove".into(), "jk".into()),
|
||||||
]).unwrap();
|
])
|
||||||
|
.unwrap();
|
||||||
assert_eq!(opts.remove, Some("jk".into()));
|
assert_eq!(opts.remove, Some("jk".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,10 +274,7 @@ mod tests {
|
|||||||
let _g = TestGuard::new();
|
let _g = TestGuard::new();
|
||||||
test_input("keymap -n jk '<ESC>'").unwrap();
|
test_input("keymap -n jk '<ESC>'").unwrap();
|
||||||
|
|
||||||
let maps = read_logic(|l| l.keymaps_filtered(
|
let maps = read_logic(|l| l.keymaps_filtered(KeyMapFlags::NORMAL, &expand_keymap("jk")));
|
||||||
KeyMapFlags::NORMAL,
|
|
||||||
&expand_keymap("jk"),
|
|
||||||
));
|
|
||||||
assert!(!maps.is_empty());
|
assert!(!maps.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,10 +283,7 @@ mod tests {
|
|||||||
let _g = TestGuard::new();
|
let _g = TestGuard::new();
|
||||||
test_input("keymap -i jk '<ESC>'").unwrap();
|
test_input("keymap -i jk '<ESC>'").unwrap();
|
||||||
|
|
||||||
let maps = read_logic(|l| l.keymaps_filtered(
|
let maps = read_logic(|l| l.keymaps_filtered(KeyMapFlags::INSERT, &expand_keymap("jk")));
|
||||||
KeyMapFlags::INSERT,
|
|
||||||
&expand_keymap("jk"),
|
|
||||||
));
|
|
||||||
assert!(!maps.is_empty());
|
assert!(!maps.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,10 +293,7 @@ mod tests {
|
|||||||
test_input("keymap -n jk '<ESC>'").unwrap();
|
test_input("keymap -n jk '<ESC>'").unwrap();
|
||||||
test_input("keymap -n jk 'dd'").unwrap();
|
test_input("keymap -n jk 'dd'").unwrap();
|
||||||
|
|
||||||
let maps = read_logic(|l| l.keymaps_filtered(
|
let maps = read_logic(|l| l.keymaps_filtered(KeyMapFlags::NORMAL, &expand_keymap("jk")));
|
||||||
KeyMapFlags::NORMAL,
|
|
||||||
&expand_keymap("jk"),
|
|
||||||
));
|
|
||||||
assert_eq!(maps.len(), 1);
|
assert_eq!(maps.len(), 1);
|
||||||
assert_eq!(maps[0].action, "dd");
|
assert_eq!(maps[0].action, "dd");
|
||||||
}
|
}
|
||||||
@@ -312,10 +304,7 @@ mod tests {
|
|||||||
test_input("keymap -n jk '<ESC>'").unwrap();
|
test_input("keymap -n jk '<ESC>'").unwrap();
|
||||||
test_input("keymap -n --remove jk").unwrap();
|
test_input("keymap -n --remove jk").unwrap();
|
||||||
|
|
||||||
let maps = read_logic(|l| l.keymaps_filtered(
|
let maps = read_logic(|l| l.keymaps_filtered(KeyMapFlags::NORMAL, &expand_keymap("jk")));
|
||||||
KeyMapFlags::NORMAL,
|
|
||||||
&expand_keymap("jk"),
|
|
||||||
));
|
|
||||||
assert!(maps.is_empty());
|
assert!(maps.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -389,7 +389,7 @@ pub fn get_map_opts(opts: Vec<Opt>) -> MapOpts {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{MapNode, MapFlags, get_map_opts};
|
use super::{MapFlags, MapNode, get_map_opts};
|
||||||
use crate::getopt::Opt;
|
use crate::getopt::Opt;
|
||||||
use crate::state::{self, read_vars};
|
use crate::state::{self, read_vars};
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
@@ -433,10 +433,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn mapnode_remove_nested() {
|
fn mapnode_remove_nested() {
|
||||||
let mut root = MapNode::default();
|
let mut root = MapNode::default();
|
||||||
root.set(
|
root.set(&["a".into(), "b".into()], MapNode::StaticLeaf("val".into()));
|
||||||
&["a".into(), "b".into()],
|
|
||||||
MapNode::StaticLeaf("val".into()),
|
|
||||||
);
|
|
||||||
root.remove(&["a".into(), "b".into()]);
|
root.remove(&["a".into(), "b".into()]);
|
||||||
assert!(root.get(&["a".into(), "b".into()]).is_none());
|
assert!(root.get(&["a".into(), "b".into()]).is_none());
|
||||||
// Parent branch should still exist
|
// Parent branch should still exist
|
||||||
|
|||||||
@@ -11,26 +11,28 @@ pub mod eval;
|
|||||||
pub mod exec;
|
pub mod exec;
|
||||||
pub mod flowctl;
|
pub mod flowctl;
|
||||||
pub mod getopts;
|
pub mod getopts;
|
||||||
|
pub mod help;
|
||||||
pub mod intro;
|
pub mod intro;
|
||||||
pub mod jobctl;
|
pub mod jobctl;
|
||||||
pub mod keymap;
|
pub mod keymap;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod pwd;
|
pub mod pwd;
|
||||||
pub mod read;
|
pub mod read;
|
||||||
|
pub mod resource;
|
||||||
|
pub mod seek;
|
||||||
pub mod shift;
|
pub mod shift;
|
||||||
pub mod shopt;
|
pub mod shopt;
|
||||||
pub mod source;
|
pub mod source;
|
||||||
pub mod test; // [[ ]] thing
|
pub mod test; // [[ ]] thing
|
||||||
pub mod trap;
|
pub mod trap;
|
||||||
pub mod varcmds;
|
pub mod varcmds;
|
||||||
pub mod resource;
|
|
||||||
|
|
||||||
pub const BUILTINS: [&str; 49] = [
|
pub const BUILTINS: [&str; 51] = [
|
||||||
"echo", "cd", "read", "export", "local", "pwd", "source", ".", "shift", "jobs", "fg", "bg", "disown",
|
"echo", "cd", "read", "export", "local", "pwd", "source", ".", "shift", "jobs", "fg", "bg",
|
||||||
"alias", "unalias", "return", "break", "continue", "exit", "shopt", "builtin",
|
"disown", "alias", "unalias", "return", "break", "continue", "exit", "shopt", "builtin",
|
||||||
"command", "trap", "pushd", "popd", "dirs", "exec", "eval", "true", "false", ":", "readonly",
|
"command", "trap", "pushd", "popd", "dirs", "exec", "eval", "true", "false", ":", "readonly",
|
||||||
"unset", "complete", "compgen", "map", "pop", "fpop", "push", "fpush", "rotate", "wait", "type",
|
"unset", "complete", "compgen", "map", "pop", "fpop", "push", "fpush", "rotate", "wait", "type",
|
||||||
"getopts", "keymap", "read_key", "autocmd", "ulimit", "umask"
|
"getopts", "keymap", "read_key", "autocmd", "ulimit", "umask", "seek", "help",
|
||||||
];
|
];
|
||||||
|
|
||||||
pub fn true_builtin() -> ShResult<()> {
|
pub fn true_builtin() -> ShResult<()> {
|
||||||
@@ -50,7 +52,10 @@ pub fn noop_builtin() -> ShResult<()> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use crate::{state, testutil::{TestGuard, test_input}};
|
use crate::{
|
||||||
|
state,
|
||||||
|
testutil::{TestGuard, test_input},
|
||||||
|
};
|
||||||
|
|
||||||
// You can never be too sure!!!!!!
|
// You can never be too sure!!!!!!
|
||||||
|
|
||||||
|
|||||||
@@ -27,10 +27,10 @@ pub fn pwd(node: Node) -> ShResult<()> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::env;
|
|
||||||
use tempfile::TempDir;
|
|
||||||
use crate::state;
|
use crate::state;
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
use std::env;
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pwd_prints_cwd() {
|
fn pwd_prints_cwd() {
|
||||||
|
|||||||
@@ -367,7 +367,7 @@ pub fn get_read_key_opts(opts: Vec<Opt>) -> ShResult<ReadKeyOpts> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::state::{self, read_vars, write_vars, VarFlags, VarKind};
|
use crate::state::{self, VarFlags, VarKind, read_vars, write_vars};
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
|
||||||
// ===================== Basic read into REPLY =====================
|
// ===================== Basic read into REPLY =====================
|
||||||
|
|||||||
@@ -1,8 +1,19 @@
|
|||||||
use ariadne::Fmt;
|
use ariadne::Fmt;
|
||||||
use nix::{libc::STDOUT_FILENO, sys::{resource::{Resource, getrlimit, setrlimit}, stat::{Mode, umask}}, unistd::write};
|
use nix::{
|
||||||
|
libc::STDOUT_FILENO,
|
||||||
|
sys::{
|
||||||
|
resource::{Resource, getrlimit, setrlimit},
|
||||||
|
stat::{Mode, umask},
|
||||||
|
},
|
||||||
|
unistd::write,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
getopt::{Opt, OptSpec, get_opts_from_tokens_strict}, libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt, next_color}, parse::{NdRule, Node}, procio::borrow_fd, state::{self}
|
getopt::{Opt, OptSpec, get_opts_from_tokens_strict},
|
||||||
|
libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt, next_color},
|
||||||
|
parse::{NdRule, Node},
|
||||||
|
procio::borrow_fd,
|
||||||
|
state::{self},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn ulimit_opt_spec() -> [OptSpec; 5] {
|
fn ulimit_opt_spec() -> [OptSpec; 5] {
|
||||||
@@ -26,7 +37,7 @@ fn ulimit_opt_spec() -> [OptSpec;5] {
|
|||||||
OptSpec {
|
OptSpec {
|
||||||
opt: Opt::Short('v'), // virtual memory
|
opt: Opt::Short('v'), // virtual memory
|
||||||
takes_arg: true,
|
takes_arg: true,
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,39 +61,51 @@ fn get_ulimit_opts(opt: &[Opt]) -> ShResult<UlimitOpts> {
|
|||||||
for o in opt {
|
for o in opt {
|
||||||
match o {
|
match o {
|
||||||
Opt::ShortWithArg('n', arg) => {
|
Opt::ShortWithArg('n', arg) => {
|
||||||
opts.fds = Some(arg.parse().map_err(|_| ShErr::simple(
|
opts.fds = Some(arg.parse().map_err(|_| {
|
||||||
|
ShErr::simple(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
format!("invalid argument for -n: {}", arg.fg(next_color())),
|
format!("invalid argument for -n: {}", arg.fg(next_color())),
|
||||||
))?);
|
)
|
||||||
},
|
})?);
|
||||||
|
}
|
||||||
Opt::ShortWithArg('u', arg) => {
|
Opt::ShortWithArg('u', arg) => {
|
||||||
opts.procs = Some(arg.parse().map_err(|_| ShErr::simple(
|
opts.procs = Some(arg.parse().map_err(|_| {
|
||||||
|
ShErr::simple(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
format!("invalid argument for -u: {}", arg.fg(next_color())),
|
format!("invalid argument for -u: {}", arg.fg(next_color())),
|
||||||
))?);
|
)
|
||||||
},
|
})?);
|
||||||
|
}
|
||||||
Opt::ShortWithArg('s', arg) => {
|
Opt::ShortWithArg('s', arg) => {
|
||||||
opts.stack = Some(arg.parse().map_err(|_| ShErr::simple(
|
opts.stack = Some(arg.parse().map_err(|_| {
|
||||||
|
ShErr::simple(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
format!("invalid argument for -s: {}", arg.fg(next_color())),
|
format!("invalid argument for -s: {}", arg.fg(next_color())),
|
||||||
))?);
|
)
|
||||||
},
|
})?);
|
||||||
|
}
|
||||||
Opt::ShortWithArg('c', arg) => {
|
Opt::ShortWithArg('c', arg) => {
|
||||||
opts.core = Some(arg.parse().map_err(|_| ShErr::simple(
|
opts.core = Some(arg.parse().map_err(|_| {
|
||||||
|
ShErr::simple(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
format!("invalid argument for -c: {}", arg.fg(next_color())),
|
format!("invalid argument for -c: {}", arg.fg(next_color())),
|
||||||
))?);
|
)
|
||||||
},
|
})?);
|
||||||
|
}
|
||||||
Opt::ShortWithArg('v', arg) => {
|
Opt::ShortWithArg('v', arg) => {
|
||||||
opts.vmem = Some(arg.parse().map_err(|_| ShErr::simple(
|
opts.vmem = Some(arg.parse().map_err(|_| {
|
||||||
|
ShErr::simple(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
format!("invalid argument for -v: {}", arg.fg(next_color())),
|
format!("invalid argument for -v: {}", arg.fg(next_color())),
|
||||||
))?);
|
)
|
||||||
},
|
})?);
|
||||||
o => return Err(ShErr::simple(
|
}
|
||||||
|
o => {
|
||||||
|
return Err(ShErr::simple(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
format!("invalid option: {}", o.fg(next_color())),
|
format!("invalid option: {}", o.fg(next_color())),
|
||||||
)),
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,68 +122,89 @@ pub fn ulimit(node: Node) -> ShResult<()> {
|
|||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
|
|
||||||
let (_, opts) = get_opts_from_tokens_strict(argv, &ulimit_opt_spec()).promote_err(span.clone())?;
|
let (_, opts) =
|
||||||
|
get_opts_from_tokens_strict(argv, &ulimit_opt_spec()).promote_err(span.clone())?;
|
||||||
let ulimit_opts = get_ulimit_opts(&opts).promote_err(span.clone())?;
|
let ulimit_opts = get_ulimit_opts(&opts).promote_err(span.clone())?;
|
||||||
|
|
||||||
if let Some(fds) = ulimit_opts.fds {
|
if let Some(fds) = ulimit_opts.fds {
|
||||||
let (_, hard) = getrlimit(Resource::RLIMIT_NOFILE).map_err(|e| ShErr::at(
|
let (_, hard) = getrlimit(Resource::RLIMIT_NOFILE).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to get file descriptor limit: {}", e),
|
format!("failed to get file descriptor limit: {}", e),
|
||||||
))?;
|
)
|
||||||
setrlimit(Resource::RLIMIT_NOFILE, fds, hard).map_err(|e| ShErr::at(
|
})?;
|
||||||
|
setrlimit(Resource::RLIMIT_NOFILE, fds, hard).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to set file descriptor limit: {}", e),
|
format!("failed to set file descriptor limit: {}", e),
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
if let Some(procs) = ulimit_opts.procs {
|
if let Some(procs) = ulimit_opts.procs {
|
||||||
let (_, hard) = getrlimit(Resource::RLIMIT_NPROC).map_err(|e| ShErr::at(
|
let (_, hard) = getrlimit(Resource::RLIMIT_NPROC).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to get process limit: {}", e),
|
format!("failed to get process limit: {}", e),
|
||||||
))?;
|
)
|
||||||
setrlimit(Resource::RLIMIT_NPROC, procs, hard).map_err(|e| ShErr::at(
|
})?;
|
||||||
|
setrlimit(Resource::RLIMIT_NPROC, procs, hard).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to set process limit: {}", e),
|
format!("failed to set process limit: {}", e),
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
if let Some(stack) = ulimit_opts.stack {
|
if let Some(stack) = ulimit_opts.stack {
|
||||||
let (_, hard) = getrlimit(Resource::RLIMIT_STACK).map_err(|e| ShErr::at(
|
let (_, hard) = getrlimit(Resource::RLIMIT_STACK).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to get stack size limit: {}", e),
|
format!("failed to get stack size limit: {}", e),
|
||||||
))?;
|
)
|
||||||
setrlimit(Resource::RLIMIT_STACK, stack, hard).map_err(|e| ShErr::at(
|
})?;
|
||||||
|
setrlimit(Resource::RLIMIT_STACK, stack, hard).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to set stack size limit: {}", e),
|
format!("failed to set stack size limit: {}", e),
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
if let Some(core) = ulimit_opts.core {
|
if let Some(core) = ulimit_opts.core {
|
||||||
let (_, hard) = getrlimit(Resource::RLIMIT_CORE).map_err(|e| ShErr::at(
|
let (_, hard) = getrlimit(Resource::RLIMIT_CORE).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to get core dump size limit: {}", e),
|
format!("failed to get core dump size limit: {}", e),
|
||||||
))?;
|
)
|
||||||
setrlimit(Resource::RLIMIT_CORE, core, hard).map_err(|e| ShErr::at(
|
})?;
|
||||||
|
setrlimit(Resource::RLIMIT_CORE, core, hard).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to set core dump size limit: {}", e),
|
format!("failed to set core dump size limit: {}", e),
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
if let Some(vmem) = ulimit_opts.vmem {
|
if let Some(vmem) = ulimit_opts.vmem {
|
||||||
let (_, hard) = getrlimit(Resource::RLIMIT_AS).map_err(|e| ShErr::at(
|
let (_, hard) = getrlimit(Resource::RLIMIT_AS).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to get virtual memory limit: {}", e),
|
format!("failed to get virtual memory limit: {}", e),
|
||||||
))?;
|
)
|
||||||
setrlimit(Resource::RLIMIT_AS, vmem, hard).map_err(|e| ShErr::at(
|
})?;
|
||||||
|
setrlimit(Resource::RLIMIT_AS, vmem, hard).map_err(|e| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ExecFail,
|
ShErrKind::ExecFail,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("failed to set virtual memory limit: {}", e),
|
format!("failed to set virtual memory limit: {}", e),
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
|
|
||||||
state::set_status(0);
|
state::set_status(0);
|
||||||
@@ -172,11 +216,17 @@ pub fn umask_builtin(node: Node) -> ShResult<()> {
|
|||||||
let NdRule::Command {
|
let NdRule::Command {
|
||||||
assignments: _,
|
assignments: _,
|
||||||
argv,
|
argv,
|
||||||
} = node.class else { unreachable!() };
|
} = node.class
|
||||||
|
else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
let (argv, opts) = get_opts_from_tokens_strict(
|
let (argv, opts) = get_opts_from_tokens_strict(
|
||||||
argv,
|
argv,
|
||||||
&[OptSpec { opt: Opt::Short('S'), takes_arg: false }],
|
&[OptSpec {
|
||||||
|
opt: Opt::Short('S'),
|
||||||
|
takes_arg: false,
|
||||||
|
}],
|
||||||
)?;
|
)?;
|
||||||
let argv = &argv[1..]; // skip command name
|
let argv = &argv[1..]; // skip command name
|
||||||
|
|
||||||
@@ -195,17 +245,21 @@ pub fn umask_builtin(node: Node) -> ShResult<()> {
|
|||||||
let arg = argv[0].clone();
|
let arg = argv[0].clone();
|
||||||
let raw = arg.as_str();
|
let raw = arg.as_str();
|
||||||
if raw.chars().any(|c| c.is_ascii_digit()) {
|
if raw.chars().any(|c| c.is_ascii_digit()) {
|
||||||
let mode_raw = u32::from_str_radix(raw, 8).map_err(|_| ShErr::at(
|
let mode_raw = u32::from_str_radix(raw, 8).map_err(|_| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("invalid numeric umask: {}", raw.fg(next_color())),
|
format!("invalid numeric umask: {}", raw.fg(next_color())),
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
let mode = Mode::from_bits(mode_raw).ok_or_else(|| ShErr::at(
|
let mode = Mode::from_bits(mode_raw).ok_or_else(|| {
|
||||||
|
ShErr::at(
|
||||||
ShErrKind::ParseErr,
|
ShErrKind::ParseErr,
|
||||||
span.clone(),
|
span.clone(),
|
||||||
format!("invalid umask value: {}", raw.fg(next_color())),
|
format!("invalid umask value: {}", raw.fg(next_color())),
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
umask(mode);
|
umask(mode);
|
||||||
} else {
|
} else {
|
||||||
@@ -337,7 +391,6 @@ pub fn umask_builtin(node: Node) -> ShResult<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if !opts.is_empty() {
|
} else if !opts.is_empty() {
|
||||||
let u = (old_bits >> 6) & 0o7;
|
let u = (old_bits >> 6) & 0o7;
|
||||||
let g = (old_bits >> 3) & 0o7;
|
let g = (old_bits >> 3) & 0o7;
|
||||||
@@ -345,11 +398,7 @@ pub fn umask_builtin(node: Node) -> ShResult<()> {
|
|||||||
let mut u_str = String::from("u=");
|
let mut u_str = String::from("u=");
|
||||||
let mut g_str = String::from("g=");
|
let mut g_str = String::from("g=");
|
||||||
let mut o_str = String::from("o=");
|
let mut o_str = String::from("o=");
|
||||||
let stuff = [
|
let stuff = [(u, &mut u_str), (g, &mut g_str), (o, &mut o_str)];
|
||||||
(u, &mut u_str),
|
|
||||||
(g, &mut g_str),
|
|
||||||
(o, &mut o_str),
|
|
||||||
];
|
|
||||||
for (bits, out) in stuff.into_iter() {
|
for (bits, out) in stuff.into_iter() {
|
||||||
if bits & 4 == 0 {
|
if bits & 4 == 0 {
|
||||||
out.push('r');
|
out.push('r');
|
||||||
@@ -423,7 +472,8 @@ mod tests {
|
|||||||
let opts = get_ulimit_opts(&[
|
let opts = get_ulimit_opts(&[
|
||||||
Opt::ShortWithArg('n', "256".into()),
|
Opt::ShortWithArg('n', "256".into()),
|
||||||
Opt::ShortWithArg('c', "0".into()),
|
Opt::ShortWithArg('c', "0".into()),
|
||||||
]).unwrap();
|
])
|
||||||
|
.unwrap();
|
||||||
assert_eq!(opts.fds, Some(256));
|
assert_eq!(opts.fds, Some(256));
|
||||||
assert_eq!(opts.core, Some(0));
|
assert_eq!(opts.core, Some(0));
|
||||||
assert!(opts.procs.is_none());
|
assert!(opts.procs.is_none());
|
||||||
|
|||||||
263
src/builtin/seek.rs
Normal file
263
src/builtin/seek.rs
Normal file
@@ -0,0 +1,263 @@
|
|||||||
|
use nix::{
|
||||||
|
libc::STDOUT_FILENO,
|
||||||
|
unistd::{Whence, lseek, write},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
getopt::{Opt, OptSpec, get_opts_from_tokens},
|
||||||
|
libsh::error::{ShErr, ShErrKind, ShResult},
|
||||||
|
parse::{NdRule, Node, execute::prepare_argv},
|
||||||
|
procio::borrow_fd,
|
||||||
|
state,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const LSEEK_OPTS: [OptSpec; 2] = [
|
||||||
|
OptSpec {
|
||||||
|
opt: Opt::Short('c'),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
|
OptSpec {
|
||||||
|
opt: Opt::Short('e'),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
pub struct LseekOpts {
|
||||||
|
cursor_rel: bool,
|
||||||
|
end_rel: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seek(node: Node) -> ShResult<()> {
|
||||||
|
let NdRule::Command {
|
||||||
|
assignments: _,
|
||||||
|
argv,
|
||||||
|
} = node.class
|
||||||
|
else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
let (argv, opts) = get_opts_from_tokens(argv, &LSEEK_OPTS)?;
|
||||||
|
let lseek_opts = get_lseek_opts(opts)?;
|
||||||
|
let mut argv = prepare_argv(argv)?.into_iter();
|
||||||
|
argv.next(); // drop 'seek'
|
||||||
|
|
||||||
|
let Some(fd) = argv.next() else {
|
||||||
|
return Err(ShErr::simple(
|
||||||
|
ShErrKind::ExecFail,
|
||||||
|
"lseek: Missing required argument 'fd'",
|
||||||
|
));
|
||||||
|
};
|
||||||
|
let Ok(fd) = fd.0.parse::<u32>() else {
|
||||||
|
return Err(
|
||||||
|
ShErr::at(ShErrKind::ExecFail, fd.1, "Invalid file descriptor")
|
||||||
|
.with_note("file descriptors are integers"),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(offset) = argv.next() else {
|
||||||
|
return Err(ShErr::simple(
|
||||||
|
ShErrKind::ExecFail,
|
||||||
|
"lseek: Missing required argument 'offset'",
|
||||||
|
));
|
||||||
|
};
|
||||||
|
let Ok(offset) = offset.0.parse::<i64>() else {
|
||||||
|
return Err(
|
||||||
|
ShErr::at(ShErrKind::ExecFail, offset.1, "Invalid offset")
|
||||||
|
.with_note("offset can be a positive or negative integer"),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
let whence = if lseek_opts.cursor_rel {
|
||||||
|
Whence::SeekCur
|
||||||
|
} else if lseek_opts.end_rel {
|
||||||
|
Whence::SeekEnd
|
||||||
|
} else {
|
||||||
|
Whence::SeekSet
|
||||||
|
};
|
||||||
|
|
||||||
|
match lseek(fd as i32, offset, whence) {
|
||||||
|
Ok(new_offset) => {
|
||||||
|
let stdout = borrow_fd(STDOUT_FILENO);
|
||||||
|
let buf = new_offset.to_string() + "\n";
|
||||||
|
write(stdout, buf.as_bytes())?;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
state::set_status(1);
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state::set_status(0);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_lseek_opts(opts: Vec<Opt>) -> ShResult<LseekOpts> {
|
||||||
|
let mut lseek_opts = LseekOpts {
|
||||||
|
cursor_rel: false,
|
||||||
|
end_rel: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
for opt in opts {
|
||||||
|
match opt {
|
||||||
|
Opt::Short('c') => lseek_opts.cursor_rel = true,
|
||||||
|
Opt::Short('e') => lseek_opts.end_rel = true,
|
||||||
|
_ => {
|
||||||
|
return Err(ShErr::simple(
|
||||||
|
ShErrKind::ExecFail,
|
||||||
|
format!("lseek: Unexpected flag '{opt}'"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(lseek_opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_set_beginning() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "hello world\n").unwrap();
|
||||||
|
let g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
test_input("seek 9 0").unwrap();
|
||||||
|
|
||||||
|
let out = g.read_output();
|
||||||
|
assert_eq!(out, "0\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_set_offset() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "hello world\n").unwrap();
|
||||||
|
let g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
test_input("seek 9 6").unwrap();
|
||||||
|
|
||||||
|
let out = g.read_output();
|
||||||
|
assert_eq!(out, "6\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_then_read() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "hello world\n").unwrap();
|
||||||
|
let g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
test_input("seek 9 6").unwrap();
|
||||||
|
// Clear the seek output
|
||||||
|
g.read_output();
|
||||||
|
|
||||||
|
test_input("read line <&9").unwrap();
|
||||||
|
let val = crate::state::read_vars(|v| v.get_var("line"));
|
||||||
|
assert_eq!(val, "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_cur_relative() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "abcdefghij\n").unwrap();
|
||||||
|
let g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
test_input("seek 9 3").unwrap();
|
||||||
|
test_input("seek -c 9 4").unwrap();
|
||||||
|
|
||||||
|
let out = g.read_output();
|
||||||
|
assert_eq!(out, "3\n7\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_end() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "hello\n").unwrap(); // 6 bytes
|
||||||
|
let g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
test_input("seek -e 9 0").unwrap();
|
||||||
|
|
||||||
|
let out = g.read_output();
|
||||||
|
assert_eq!(out, "6\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_end_negative() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "hello\n").unwrap(); // 6 bytes
|
||||||
|
let g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
test_input("seek -e 9 -2").unwrap();
|
||||||
|
|
||||||
|
let out = g.read_output();
|
||||||
|
assert_eq!(out, "4\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_write_overwrite() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "hello world\n").unwrap();
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
test_input("seek 9 6").unwrap();
|
||||||
|
test_input("echo -n 'WORLD' >&9").unwrap();
|
||||||
|
|
||||||
|
let contents = std::fs::read_to_string(&path).unwrap();
|
||||||
|
assert_eq!(contents, "hello WORLD\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_rewind_full_read() {
|
||||||
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
|
let path = dir.path().join("seek.txt");
|
||||||
|
std::fs::write(&path, "abc\n").unwrap();
|
||||||
|
let g = TestGuard::new();
|
||||||
|
|
||||||
|
test_input(format!("exec 9<> {}", path.display())).unwrap();
|
||||||
|
// Read moves cursor to EOF
|
||||||
|
test_input("read line <&9").unwrap();
|
||||||
|
// Rewind
|
||||||
|
test_input("seek 9 0").unwrap();
|
||||||
|
// Clear output from seek
|
||||||
|
g.read_output();
|
||||||
|
// Read again from beginning
|
||||||
|
test_input("read line <&9").unwrap();
|
||||||
|
|
||||||
|
let val = crate::state::read_vars(|v| v.get_var("line"));
|
||||||
|
assert_eq!(val, "abc");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_bad_fd() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
|
||||||
|
let result = test_input("seek 99 0");
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek_missing_args() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
|
||||||
|
let result = test_input("seek");
|
||||||
|
assert!(result.is_err());
|
||||||
|
|
||||||
|
let result = test_input("seek 9");
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ pub fn shopt(node: Node) -> ShResult<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (arg, span) in argv {
|
for (arg, span) in argv {
|
||||||
let Some(mut output) = write_shopts(|s| s.query(&arg)).blame(span)? else {
|
let Some(mut output) = write_shopts(|s| s.query(&arg)).promote_err(span)? else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ mod tests {
|
|||||||
assert!(out.contains("dotglob"));
|
assert!(out.contains("dotglob"));
|
||||||
assert!(out.contains("autocd"));
|
assert!(out.contains("autocd"));
|
||||||
assert!(out.contains("max_hist"));
|
assert!(out.contains("max_hist"));
|
||||||
assert!(out.contains("edit_mode"));
|
assert!(out.contains("comp_limit"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -72,7 +72,7 @@ mod tests {
|
|||||||
assert!(out.contains("dotglob"));
|
assert!(out.contains("dotglob"));
|
||||||
assert!(out.contains("autocd"));
|
assert!(out.contains("autocd"));
|
||||||
// Should not contain prompt opts
|
// Should not contain prompt opts
|
||||||
assert!(!out.contains("edit_mode"));
|
assert!(!out.contains("comp_limit"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -107,11 +107,10 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn shopt_set_edit_mode() {
|
fn shopt_set_completion_ignore_case() {
|
||||||
let _g = TestGuard::new();
|
let _g = TestGuard::new();
|
||||||
test_input("shopt prompt.edit_mode=emacs").unwrap();
|
test_input("shopt prompt.completion_ignore_case=true").unwrap();
|
||||||
let mode = read_shopts(|o| format!("{}", o.prompt.edit_mode));
|
assert!(read_shopts(|o| o.prompt.completion_ignore_case));
|
||||||
assert_eq!(mode, "emacs");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===================== Error cases =====================
|
// ===================== Error cases =====================
|
||||||
|
|||||||
@@ -46,9 +46,9 @@ pub fn source(node: Node) -> ShResult<()> {
|
|||||||
pub mod tests {
|
pub mod tests {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use tempfile::{NamedTempFile, TempDir};
|
|
||||||
use crate::state::{self, read_logic, read_vars};
|
use crate::state::{self, read_logic, read_vars};
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
use tempfile::{NamedTempFile, TempDir};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn source_simple() {
|
fn source_simple() {
|
||||||
|
|||||||
@@ -94,7 +94,10 @@ impl FromStr for TestOp {
|
|||||||
"-ge" => Ok(Self::IntGe),
|
"-ge" => Ok(Self::IntGe),
|
||||||
"-le" => Ok(Self::IntLe),
|
"-le" => Ok(Self::IntLe),
|
||||||
_ if TEST_UNARY_OPS.contains(&s) => Ok(Self::Unary(s.parse::<UnaryOp>()?)),
|
_ if TEST_UNARY_OPS.contains(&s) => Ok(Self::Unary(s.parse::<UnaryOp>()?)),
|
||||||
_ => Err(ShErr::simple(ShErrKind::SyntaxErr, format!("Invalid test operator '{}'", s))),
|
_ => Err(ShErr::simple(
|
||||||
|
ShErrKind::SyntaxErr,
|
||||||
|
format!("Invalid test operator '{}'", s),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -305,10 +308,10 @@ pub fn double_bracket_test(node: Node) -> ShResult<bool> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs;
|
|
||||||
use tempfile::{TempDir, NamedTempFile};
|
|
||||||
use crate::state;
|
use crate::state;
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
use std::fs;
|
||||||
|
use tempfile::{NamedTempFile, TempDir};
|
||||||
|
|
||||||
// ===================== Unary: file tests =====================
|
// ===================== Unary: file tests =====================
|
||||||
|
|
||||||
@@ -590,9 +593,10 @@ mod tests {
|
|||||||
fn parse_unary_ops() {
|
fn parse_unary_ops() {
|
||||||
use super::UnaryOp;
|
use super::UnaryOp;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
for op in ["-e", "-d", "-f", "-h", "-L", "-r", "-w", "-x", "-s",
|
for op in [
|
||||||
"-p", "-S", "-b", "-c", "-k", "-O", "-G", "-N", "-u",
|
"-e", "-d", "-f", "-h", "-L", "-r", "-w", "-x", "-s", "-p", "-S", "-b", "-c", "-k", "-O",
|
||||||
"-g", "-t", "-n", "-z"] {
|
"-G", "-N", "-u", "-g", "-t", "-n", "-z",
|
||||||
|
] {
|
||||||
assert!(UnaryOp::from_str(op).is_ok(), "failed to parse {op}");
|
assert!(UnaryOp::from_str(op).is_ok(), "failed to parse {op}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -171,10 +171,10 @@ pub fn trap(node: Node) -> ShResult<()> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::TrapTarget;
|
use super::TrapTarget;
|
||||||
use std::str::FromStr;
|
|
||||||
use nix::sys::signal::Signal;
|
|
||||||
use crate::state::{self, read_logic};
|
use crate::state::{self, read_logic};
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
use nix::sys::signal::Signal;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
// ===================== Pure: TrapTarget parsing =====================
|
// ===================== Pure: TrapTarget parsing =====================
|
||||||
|
|
||||||
@@ -231,7 +231,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn display_signal_roundtrip() {
|
fn display_signal_roundtrip() {
|
||||||
for name in &["INT", "QUIT", "TERM", "USR1", "USR2", "ALRM", "CHLD", "WINCH"] {
|
for name in &[
|
||||||
|
"INT", "QUIT", "TERM", "USR1", "USR2", "ALRM", "CHLD", "WINCH",
|
||||||
|
] {
|
||||||
let target = TrapTarget::from_str(name).unwrap();
|
let target = TrapTarget::from_str(name).unwrap();
|
||||||
assert_eq!(target.to_string(), *name);
|
assert_eq!(target.to_string(), *name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,8 +245,16 @@ mod tests {
|
|||||||
test_input("readonly a=1 b=2").unwrap();
|
test_input("readonly a=1 b=2").unwrap();
|
||||||
assert_eq!(read_vars(|v| v.get_var("a")), "1");
|
assert_eq!(read_vars(|v| v.get_var("a")), "1");
|
||||||
assert_eq!(read_vars(|v| v.get_var("b")), "2");
|
assert_eq!(read_vars(|v| v.get_var("b")), "2");
|
||||||
assert!(read_vars(|v| v.get_var_flags("a")).unwrap().contains(VarFlags::READONLY));
|
assert!(
|
||||||
assert!(read_vars(|v| v.get_var_flags("b")).unwrap().contains(VarFlags::READONLY));
|
read_vars(|v| v.get_var_flags("a"))
|
||||||
|
.unwrap()
|
||||||
|
.contains(VarFlags::READONLY)
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
read_vars(|v| v.get_var_flags("b"))
|
||||||
|
.unwrap()
|
||||||
|
.contains(VarFlags::READONLY)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -385,7 +393,11 @@ mod tests {
|
|||||||
let _g = TestGuard::new();
|
let _g = TestGuard::new();
|
||||||
test_input("local mylocal").unwrap();
|
test_input("local mylocal").unwrap();
|
||||||
assert_eq!(read_vars(|v| v.get_var("mylocal")), "");
|
assert_eq!(read_vars(|v| v.get_var("mylocal")), "");
|
||||||
assert!(read_vars(|v| v.get_var_flags("mylocal")).unwrap().contains(VarFlags::LOCAL));
|
assert!(
|
||||||
|
read_vars(|v| v.get_var_flags("mylocal"))
|
||||||
|
.unwrap()
|
||||||
|
.contains(VarFlags::LOCAL)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
628
src/expand.rs
628
src/expand.rs
@@ -4,6 +4,7 @@ use std::str::{Chars, FromStr};
|
|||||||
|
|
||||||
use ariadne::Fmt;
|
use ariadne::Fmt;
|
||||||
use glob::Pattern;
|
use glob::Pattern;
|
||||||
|
use nix::unistd::{Uid, User};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
use crate::libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt, next_color};
|
use crate::libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt, next_color};
|
||||||
@@ -40,18 +41,26 @@ impl Tk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Expander {
|
pub struct Expander {
|
||||||
|
flags: TkFlags,
|
||||||
raw: String,
|
raw: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Expander {
|
impl Expander {
|
||||||
pub fn new(raw: Tk) -> ShResult<Self> {
|
pub fn new(raw: Tk) -> ShResult<Self> {
|
||||||
let raw = raw.span.as_str();
|
let tk_raw = raw.span.as_str();
|
||||||
Self::from_raw(raw)
|
Self::from_raw(tk_raw, raw.flags)
|
||||||
}
|
}
|
||||||
pub fn from_raw(raw: &str) -> ShResult<Self> {
|
pub fn from_raw(raw: &str, flags: TkFlags) -> ShResult<Self> {
|
||||||
let raw = expand_braces_full(raw)?.join(" ");
|
let raw = expand_braces_full(raw)?.join(" ");
|
||||||
let unescaped = unescape_str(&raw);
|
let unescaped = if flags.contains(TkFlags::IS_HEREDOC) {
|
||||||
Ok(Self { raw: unescaped })
|
unescape_heredoc(&raw)
|
||||||
|
} else {
|
||||||
|
unescape_str(&raw)
|
||||||
|
};
|
||||||
|
Ok(Self {
|
||||||
|
raw: unescaped,
|
||||||
|
flags,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
pub fn expand(&mut self) -> ShResult<Vec<String>> {
|
pub fn expand(&mut self) -> ShResult<Vec<String>> {
|
||||||
let mut chars = self.raw.chars().peekable();
|
let mut chars = self.raw.chars().peekable();
|
||||||
@@ -75,8 +84,12 @@ impl Expander {
|
|||||||
self.raw.insert_str(0, "./");
|
self.raw.insert_str(0, "./");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.flags.contains(TkFlags::IS_HEREDOC) {
|
||||||
|
Ok(vec![self.raw.clone()])
|
||||||
|
} else {
|
||||||
Ok(self.split_words())
|
Ok(self.split_words())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
pub fn split_words(&mut self) -> Vec<String> {
|
pub fn split_words(&mut self) -> Vec<String> {
|
||||||
let mut words = vec![];
|
let mut words = vec![];
|
||||||
let mut chars = self.raw.chars();
|
let mut chars = self.raw.chars();
|
||||||
@@ -86,6 +99,11 @@ impl Expander {
|
|||||||
|
|
||||||
'outer: while let Some(ch) = chars.next() {
|
'outer: while let Some(ch) = chars.next() {
|
||||||
match ch {
|
match ch {
|
||||||
|
markers::ESCAPE => {
|
||||||
|
if let Some(next_ch) = chars.next() {
|
||||||
|
cur_word.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
markers::DUB_QUOTE | markers::SNG_QUOTE | markers::SUBSH => {
|
markers::DUB_QUOTE | markers::SNG_QUOTE | markers::SUBSH => {
|
||||||
while let Some(q_ch) = chars.next() {
|
while let Some(q_ch) = chars.next() {
|
||||||
match q_ch {
|
match q_ch {
|
||||||
@@ -456,7 +474,32 @@ pub fn expand_raw(chars: &mut Peekable<Chars<'_>>) -> ShResult<String> {
|
|||||||
while let Some(ch) = chars.next() {
|
while let Some(ch) = chars.next() {
|
||||||
match ch {
|
match ch {
|
||||||
markers::TILDE_SUB => {
|
markers::TILDE_SUB => {
|
||||||
let home = env::var("HOME").unwrap_or_default();
|
let mut username = String::new();
|
||||||
|
while chars.peek().is_some_and(|ch| *ch != '/') {
|
||||||
|
let ch = chars.next().unwrap();
|
||||||
|
username.push(ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
let home = if username.is_empty() {
|
||||||
|
// standard '~' expansion
|
||||||
|
env::var("HOME").unwrap_or_default()
|
||||||
|
} else if let Ok(result) = User::from_name(&username)
|
||||||
|
&& let Some(user) = result
|
||||||
|
{
|
||||||
|
// username expansion like '~user'
|
||||||
|
user.dir.to_string_lossy().to_string()
|
||||||
|
} else if let Ok(id) = username.parse::<u32>()
|
||||||
|
&& let Ok(result) = User::from_uid(Uid::from_raw(id))
|
||||||
|
&& let Some(user) = result
|
||||||
|
{
|
||||||
|
// uid expansion like '~1000'
|
||||||
|
// shed only feature btw B)
|
||||||
|
user.dir.to_string_lossy().to_string()
|
||||||
|
} else {
|
||||||
|
// no match, use literal
|
||||||
|
format!("~{username}")
|
||||||
|
};
|
||||||
|
|
||||||
result.push_str(&home);
|
result.push_str(&home);
|
||||||
}
|
}
|
||||||
markers::PROC_SUB_OUT => {
|
markers::PROC_SUB_OUT => {
|
||||||
@@ -634,8 +677,12 @@ pub fn expand_glob(raw: &str) -> ShResult<String> {
|
|||||||
{
|
{
|
||||||
let entry =
|
let entry =
|
||||||
entry.map_err(|_| ShErr::simple(ShErrKind::SyntaxErr, "Invalid filename found in glob"))?;
|
entry.map_err(|_| ShErr::simple(ShErrKind::SyntaxErr, "Invalid filename found in glob"))?;
|
||||||
|
let entry_raw = entry
|
||||||
|
.to_str()
|
||||||
|
.ok_or_else(|| ShErr::simple(ShErrKind::SyntaxErr, "Non-UTF8 filename found in glob"))?;
|
||||||
|
let escaped = escape_str(entry_raw, true);
|
||||||
|
|
||||||
words.push(entry.to_str().unwrap().to_string())
|
words.push(escaped)
|
||||||
}
|
}
|
||||||
Ok(words.join(" "))
|
Ok(words.join(" "))
|
||||||
}
|
}
|
||||||
@@ -973,6 +1020,11 @@ pub fn expand_cmd_sub(raw: &str) -> ShResult<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Strip ESCAPE markers from a string, leaving the characters they protect intact.
|
||||||
|
fn strip_escape_markers(s: &str) -> String {
|
||||||
|
s.replace(markers::ESCAPE, "")
|
||||||
|
}
|
||||||
|
|
||||||
/// Processes strings into intermediate representations that are more readable
|
/// Processes strings into intermediate representations that are more readable
|
||||||
/// by the program
|
/// by the program
|
||||||
///
|
///
|
||||||
@@ -989,6 +1041,7 @@ pub fn unescape_str(raw: &str) -> String {
|
|||||||
'~' if first_char => result.push(markers::TILDE_SUB),
|
'~' if first_char => result.push(markers::TILDE_SUB),
|
||||||
'\\' => {
|
'\\' => {
|
||||||
if let Some(next_ch) = chars.next() {
|
if let Some(next_ch) = chars.next() {
|
||||||
|
result.push(markers::ESCAPE);
|
||||||
result.push(next_ch)
|
result.push(next_ch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1139,6 +1192,25 @@ pub fn unescape_str(raw: &str) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
'`' => {
|
||||||
|
result.push(markers::VAR_SUB);
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
while let Some(bt_ch) = chars.next() {
|
||||||
|
match bt_ch {
|
||||||
|
'\\' => {
|
||||||
|
result.push(bt_ch);
|
||||||
|
if let Some(next_ch) = chars.next() {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'`' => {
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => result.push(bt_ch),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
'"' => {
|
'"' => {
|
||||||
result.push(markers::DUB_QUOTE);
|
result.push(markers::DUB_QUOTE);
|
||||||
break;
|
break;
|
||||||
@@ -1151,14 +1223,13 @@ pub fn unescape_str(raw: &str) -> String {
|
|||||||
result.push(markers::SNG_QUOTE);
|
result.push(markers::SNG_QUOTE);
|
||||||
while let Some(q_ch) = chars.next() {
|
while let Some(q_ch) = chars.next() {
|
||||||
match q_ch {
|
match q_ch {
|
||||||
'\\' => {
|
'\\' => match chars.peek() {
|
||||||
if chars.peek() == Some(&'\'') {
|
Some(&'\\') | Some(&'\'') => {
|
||||||
result.push('\'');
|
let ch = chars.next().unwrap();
|
||||||
chars.next();
|
result.push(ch);
|
||||||
} else {
|
|
||||||
result.push('\\');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
_ => result.push(q_ch),
|
||||||
|
},
|
||||||
'\'' => {
|
'\'' => {
|
||||||
result.push(markers::SNG_QUOTE);
|
result.push(markers::SNG_QUOTE);
|
||||||
break;
|
break;
|
||||||
@@ -1296,6 +1367,99 @@ pub fn unescape_str(raw: &str) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
'$' => {
|
||||||
|
if chars.peek() == Some(&'$') || chars.peek().is_none_or(|ch| ch.is_whitespace()) {
|
||||||
|
chars.next();
|
||||||
|
result.push('$');
|
||||||
|
} else {
|
||||||
|
result.push(markers::VAR_SUB);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'`' => {
|
||||||
|
result.push(markers::VAR_SUB);
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
while let Some(bt_ch) = chars.next() {
|
||||||
|
match bt_ch {
|
||||||
|
'\\' => {
|
||||||
|
result.push(bt_ch);
|
||||||
|
if let Some(next_ch) = chars.next() {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'`' => {
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => result.push(bt_ch),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => result.push(ch),
|
||||||
|
}
|
||||||
|
first_char = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Like unescape_str but for heredoc bodies. Only processes:
|
||||||
|
/// - $var / ${var} / $(cmd) substitution markers
|
||||||
|
/// - Backslash escapes (only before $, `, \, and newline)
|
||||||
|
///
|
||||||
|
/// Everything else (quotes, tildes, globs, process subs, etc.) is literal.
|
||||||
|
pub fn unescape_heredoc(raw: &str) -> String {
|
||||||
|
let mut chars = raw.chars().peekable();
|
||||||
|
let mut result = String::new();
|
||||||
|
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
match ch {
|
||||||
|
'\\' => {
|
||||||
|
match chars.peek() {
|
||||||
|
Some('$') | Some('`') | Some('\\') | Some('\n') => {
|
||||||
|
let next_ch = chars.next().unwrap();
|
||||||
|
if next_ch == '\n' {
|
||||||
|
// line continuation — discard both backslash and newline
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
result.push(markers::ESCAPE);
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// backslash is literal
|
||||||
|
result.push('\\');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'$' if chars.peek() == Some(&'(') => {
|
||||||
|
result.push(markers::VAR_SUB);
|
||||||
|
chars.next(); // consume '('
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
let mut paren_count = 1;
|
||||||
|
while let Some(subsh_ch) = chars.next() {
|
||||||
|
match subsh_ch {
|
||||||
|
'\\' => {
|
||||||
|
result.push(subsh_ch);
|
||||||
|
if let Some(next_ch) = chars.next() {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'(' => {
|
||||||
|
paren_count += 1;
|
||||||
|
result.push(subsh_ch);
|
||||||
|
}
|
||||||
|
')' => {
|
||||||
|
paren_count -= 1;
|
||||||
|
if paren_count == 0 {
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
result.push(subsh_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => result.push(subsh_ch),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
'$' => {
|
'$' => {
|
||||||
result.push(markers::VAR_SUB);
|
result.push(markers::VAR_SUB);
|
||||||
if chars.peek() == Some(&'$') {
|
if chars.peek() == Some(&'$') {
|
||||||
@@ -1303,9 +1467,64 @@ pub fn unescape_str(raw: &str) -> String {
|
|||||||
result.push('$');
|
result.push('$');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
'`' => {
|
||||||
|
result.push(markers::VAR_SUB);
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
while let Some(bt_ch) = chars.next() {
|
||||||
|
match bt_ch {
|
||||||
|
'\\' => {
|
||||||
|
result.push(bt_ch);
|
||||||
|
if let Some(next_ch) = chars.next() {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'`' => {
|
||||||
|
result.push(markers::SUBSH);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => result.push(bt_ch),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => result.push(ch),
|
_ => result.push(ch),
|
||||||
}
|
}
|
||||||
first_char = false;
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opposite of unescape_str - escapes a string to be executed as literal text
|
||||||
|
/// Used for completion results, and glob filename matches.
|
||||||
|
pub fn escape_str(raw: &str, use_marker: bool) -> String {
|
||||||
|
let mut result = String::new();
|
||||||
|
let mut chars = raw.chars();
|
||||||
|
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
match ch {
|
||||||
|
'\'' | '"' | '\\' | '|' | '&' | ';' | '(' | ')' | '<' | '>' | '$' | '*' | '!' | '`' | '{'
|
||||||
|
| '?' | '[' | '#' | ' ' | '\t' | '\n' => {
|
||||||
|
if use_marker {
|
||||||
|
result.push(markers::ESCAPE);
|
||||||
|
} else {
|
||||||
|
result.push('\\');
|
||||||
|
}
|
||||||
|
result.push(ch);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
'~' if result.is_empty() => {
|
||||||
|
if use_marker {
|
||||||
|
result.push(markers::ESCAPE);
|
||||||
|
} else {
|
||||||
|
result.push('\\');
|
||||||
|
}
|
||||||
|
result.push(ch);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
result.push(ch);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
@@ -1364,6 +1583,10 @@ pub fn unescape_math(raw: &str) -> String {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ParamExp {
|
pub enum ParamExp {
|
||||||
Len, // #var_name
|
Len, // #var_name
|
||||||
|
ToUpperFirst, // ^var_name
|
||||||
|
ToUpperAll, // ^^var_name
|
||||||
|
ToLowerFirst, // ,var_name
|
||||||
|
ToLowerAll, // ,,var_name
|
||||||
DefaultUnsetOrNull(String), // :-
|
DefaultUnsetOrNull(String), // :-
|
||||||
DefaultUnset(String), // -
|
DefaultUnset(String), // -
|
||||||
SetDefaultUnsetOrNull(String), // :=
|
SetDefaultUnsetOrNull(String), // :=
|
||||||
@@ -1399,6 +1622,19 @@ impl FromStr for ParamExp {
|
|||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if s == "^^" {
|
||||||
|
return Ok(ToUpperAll);
|
||||||
|
}
|
||||||
|
if s == "^" {
|
||||||
|
return Ok(ToUpperFirst);
|
||||||
|
}
|
||||||
|
if s == ",," {
|
||||||
|
return Ok(ToLowerAll);
|
||||||
|
}
|
||||||
|
if s == "," {
|
||||||
|
return Ok(ToLowerFirst);
|
||||||
|
}
|
||||||
|
|
||||||
// Handle indirect var expansion: ${!var}
|
// Handle indirect var expansion: ${!var}
|
||||||
if let Some(var) = s.strip_prefix('!') {
|
if let Some(var) = s.strip_prefix('!') {
|
||||||
if var.ends_with('*') || var.ends_with('@') {
|
if var.ends_with('*') || var.ends_with('@') {
|
||||||
@@ -1504,7 +1740,7 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
|
|
||||||
while let Some(ch) = chars.next() {
|
while let Some(ch) = chars.next() {
|
||||||
match ch {
|
match ch {
|
||||||
'!' | '#' | '%' | ':' | '-' | '+' | '=' | '/' | '?' => {
|
'!' | '#' | '%' | ':' | '-' | '+' | '^' | ',' | '=' | '/' | '?' => {
|
||||||
rest.push(ch);
|
rest.push(ch);
|
||||||
rest.push_str(&chars.collect::<String>());
|
rest.push_str(&chars.collect::<String>());
|
||||||
break;
|
break;
|
||||||
@@ -1516,6 +1752,32 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
if let Ok(expansion) = rest.parse::<ParamExp>() {
|
if let Ok(expansion) = rest.parse::<ParamExp>() {
|
||||||
match expansion {
|
match expansion {
|
||||||
ParamExp::Len => unreachable!(),
|
ParamExp::Len => unreachable!(),
|
||||||
|
ParamExp::ToUpperAll => {
|
||||||
|
let value = vars.get_var(&var_name);
|
||||||
|
Ok(value.to_uppercase())
|
||||||
|
}
|
||||||
|
ParamExp::ToUpperFirst => {
|
||||||
|
let value = vars.get_var(&var_name);
|
||||||
|
let mut chars = value.chars();
|
||||||
|
let first = chars
|
||||||
|
.next()
|
||||||
|
.map(|c| c.to_uppercase().to_string())
|
||||||
|
.unwrap_or_default();
|
||||||
|
Ok(first + chars.as_str())
|
||||||
|
}
|
||||||
|
ParamExp::ToLowerAll => {
|
||||||
|
let value = vars.get_var(&var_name);
|
||||||
|
Ok(value.to_lowercase())
|
||||||
|
}
|
||||||
|
ParamExp::ToLowerFirst => {
|
||||||
|
let value = vars.get_var(&var_name);
|
||||||
|
let mut chars = value.chars();
|
||||||
|
let first = chars
|
||||||
|
.next()
|
||||||
|
.map(|c| c.to_lowercase().to_string())
|
||||||
|
.unwrap_or_default();
|
||||||
|
Ok(first + chars.as_str())
|
||||||
|
}
|
||||||
ParamExp::DefaultUnsetOrNull(default) => {
|
ParamExp::DefaultUnsetOrNull(default) => {
|
||||||
match vars.try_get_var(&var_name).filter(|v| !v.is_empty()) {
|
match vars.try_get_var(&var_name).filter(|v| !v.is_empty()) {
|
||||||
Some(val) => Ok(val),
|
Some(val) => Ok(val),
|
||||||
@@ -1588,7 +1850,8 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
ParamExp::RemShortestPrefix(prefix) => {
|
ParamExp::RemShortestPrefix(prefix) => {
|
||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let unescaped = unescape_str(&prefix);
|
let unescaped = unescape_str(&prefix);
|
||||||
let expanded = expand_raw(&mut unescaped.chars().peekable()).unwrap_or(prefix);
|
let expanded =
|
||||||
|
strip_escape_markers(&expand_raw(&mut unescaped.chars().peekable()).unwrap_or(prefix));
|
||||||
let pattern = Pattern::new(&expanded).unwrap();
|
let pattern = Pattern::new(&expanded).unwrap();
|
||||||
for i in 0..=value.len() {
|
for i in 0..=value.len() {
|
||||||
let sliced = &value[..i];
|
let sliced = &value[..i];
|
||||||
@@ -1601,7 +1864,8 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
ParamExp::RemLongestPrefix(prefix) => {
|
ParamExp::RemLongestPrefix(prefix) => {
|
||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let unescaped = unescape_str(&prefix);
|
let unescaped = unescape_str(&prefix);
|
||||||
let expanded = expand_raw(&mut unescaped.chars().peekable()).unwrap_or(prefix);
|
let expanded =
|
||||||
|
strip_escape_markers(&expand_raw(&mut unescaped.chars().peekable()).unwrap_or(prefix));
|
||||||
let pattern = Pattern::new(&expanded).unwrap();
|
let pattern = Pattern::new(&expanded).unwrap();
|
||||||
for i in (0..=value.len()).rev() {
|
for i in (0..=value.len()).rev() {
|
||||||
let sliced = &value[..i];
|
let sliced = &value[..i];
|
||||||
@@ -1614,7 +1878,8 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
ParamExp::RemShortestSuffix(suffix) => {
|
ParamExp::RemShortestSuffix(suffix) => {
|
||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let unescaped = unescape_str(&suffix);
|
let unescaped = unescape_str(&suffix);
|
||||||
let expanded = expand_raw(&mut unescaped.chars().peekable()).unwrap_or(suffix);
|
let expanded =
|
||||||
|
strip_escape_markers(&expand_raw(&mut unescaped.chars().peekable()).unwrap_or(suffix));
|
||||||
let pattern = Pattern::new(&expanded).unwrap();
|
let pattern = Pattern::new(&expanded).unwrap();
|
||||||
for i in (0..=value.len()).rev() {
|
for i in (0..=value.len()).rev() {
|
||||||
let sliced = &value[i..];
|
let sliced = &value[i..];
|
||||||
@@ -1627,8 +1892,9 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
ParamExp::RemLongestSuffix(suffix) => {
|
ParamExp::RemLongestSuffix(suffix) => {
|
||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let unescaped = unescape_str(&suffix);
|
let unescaped = unescape_str(&suffix);
|
||||||
let expanded_suffix =
|
let expanded_suffix = strip_escape_markers(
|
||||||
expand_raw(&mut unescaped.chars().peekable()).unwrap_or(suffix.clone());
|
&expand_raw(&mut unescaped.chars().peekable()).unwrap_or(suffix.clone()),
|
||||||
|
);
|
||||||
let pattern = Pattern::new(&expanded_suffix).unwrap();
|
let pattern = Pattern::new(&expanded_suffix).unwrap();
|
||||||
for i in 0..=value.len() {
|
for i in 0..=value.len() {
|
||||||
let sliced = &value[i..];
|
let sliced = &value[i..];
|
||||||
@@ -1642,8 +1908,10 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let search = unescape_str(&search);
|
let search = unescape_str(&search);
|
||||||
let replace = unescape_str(&replace);
|
let replace = unescape_str(&replace);
|
||||||
let expanded_search = expand_raw(&mut search.chars().peekable()).unwrap_or(search);
|
let expanded_search =
|
||||||
let expanded_replace = expand_raw(&mut replace.chars().peekable()).unwrap_or(replace);
|
strip_escape_markers(&expand_raw(&mut search.chars().peekable()).unwrap_or(search));
|
||||||
|
let expanded_replace =
|
||||||
|
strip_escape_markers(&expand_raw(&mut replace.chars().peekable()).unwrap_or(replace));
|
||||||
let regex = glob_to_regex(&expanded_search, false); // unanchored pattern
|
let regex = glob_to_regex(&expanded_search, false); // unanchored pattern
|
||||||
|
|
||||||
if let Some(mat) = regex.find(&value) {
|
if let Some(mat) = regex.find(&value) {
|
||||||
@@ -1659,8 +1927,10 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let search = unescape_str(&search);
|
let search = unescape_str(&search);
|
||||||
let replace = unescape_str(&replace);
|
let replace = unescape_str(&replace);
|
||||||
let expanded_search = expand_raw(&mut search.chars().peekable()).unwrap_or(search);
|
let expanded_search =
|
||||||
let expanded_replace = expand_raw(&mut replace.chars().peekable()).unwrap_or(replace);
|
strip_escape_markers(&expand_raw(&mut search.chars().peekable()).unwrap_or(search));
|
||||||
|
let expanded_replace =
|
||||||
|
strip_escape_markers(&expand_raw(&mut replace.chars().peekable()).unwrap_or(replace));
|
||||||
let regex = glob_to_regex(&expanded_search, false);
|
let regex = glob_to_regex(&expanded_search, false);
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let mut last_match_end = 0;
|
let mut last_match_end = 0;
|
||||||
@@ -1679,8 +1949,10 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let search = unescape_str(&search);
|
let search = unescape_str(&search);
|
||||||
let replace = unescape_str(&replace);
|
let replace = unescape_str(&replace);
|
||||||
let expanded_search = expand_raw(&mut search.chars().peekable()).unwrap_or(search);
|
let expanded_search =
|
||||||
let expanded_replace = expand_raw(&mut replace.chars().peekable()).unwrap_or(replace);
|
strip_escape_markers(&expand_raw(&mut search.chars().peekable()).unwrap_or(search));
|
||||||
|
let expanded_replace =
|
||||||
|
strip_escape_markers(&expand_raw(&mut replace.chars().peekable()).unwrap_or(replace));
|
||||||
let pattern = Pattern::new(&expanded_search).unwrap();
|
let pattern = Pattern::new(&expanded_search).unwrap();
|
||||||
for i in (0..=value.len()).rev() {
|
for i in (0..=value.len()).rev() {
|
||||||
let sliced = &value[..i];
|
let sliced = &value[..i];
|
||||||
@@ -1694,8 +1966,10 @@ pub fn perform_param_expansion(raw: &str) -> ShResult<String> {
|
|||||||
let value = vars.get_var(&var_name);
|
let value = vars.get_var(&var_name);
|
||||||
let search = unescape_str(&search);
|
let search = unescape_str(&search);
|
||||||
let replace = unescape_str(&replace);
|
let replace = unescape_str(&replace);
|
||||||
let expanded_search = expand_raw(&mut search.chars().peekable()).unwrap_or(search);
|
let expanded_search =
|
||||||
let expanded_replace = expand_raw(&mut replace.chars().peekable()).unwrap_or(replace);
|
strip_escape_markers(&expand_raw(&mut search.chars().peekable()).unwrap_or(search));
|
||||||
|
let expanded_replace =
|
||||||
|
strip_escape_markers(&expand_raw(&mut replace.chars().peekable()).unwrap_or(replace));
|
||||||
let pattern = Pattern::new(&expanded_search).unwrap();
|
let pattern = Pattern::new(&expanded_search).unwrap();
|
||||||
for i in (0..=value.len()).rev() {
|
for i in (0..=value.len()).rev() {
|
||||||
let sliced = &value[i..];
|
let sliced = &value[i..];
|
||||||
@@ -1740,6 +2014,11 @@ pub fn expand_case_pattern(raw: &str) -> ShResult<String> {
|
|||||||
markers::DUB_QUOTE | markers::SNG_QUOTE => {
|
markers::DUB_QUOTE | markers::SNG_QUOTE => {
|
||||||
in_quote = !in_quote;
|
in_quote = !in_quote;
|
||||||
}
|
}
|
||||||
|
markers::ESCAPE => {
|
||||||
|
if let Some(next_ch) = chars.next() {
|
||||||
|
result.push(next_ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
'*' | '?' | '[' | ']' if in_quote => {
|
'*' | '?' | '[' | ']' if in_quote => {
|
||||||
result.push('\\');
|
result.push('\\');
|
||||||
result.push(ch);
|
result.push(ch);
|
||||||
@@ -1751,44 +2030,18 @@ pub fn expand_case_pattern(raw: &str) -> ShResult<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn glob_to_regex(glob: &str, anchored: bool) -> Regex {
|
pub fn glob_to_regex(glob: &str, anchored: bool) -> Regex {
|
||||||
let mut regex = String::new();
|
// fnmatch_regex always produces ^...$, so get the pattern string and strip if unanchored
|
||||||
if anchored {
|
let pattern = fnmatch_regex::glob_to_regex_pattern(glob)
|
||||||
regex.push('^');
|
.unwrap_or_else(|_| regex::escape(glob));
|
||||||
}
|
let pattern = if anchored {
|
||||||
let mut chars = glob.chars();
|
pattern
|
||||||
while let Some(ch) = chars.next() {
|
} else {
|
||||||
match ch {
|
pattern
|
||||||
'\\' => {
|
.strip_prefix('^').unwrap_or(&pattern)
|
||||||
// Shell escape: next char is literal
|
.strip_suffix('$').unwrap_or(&pattern)
|
||||||
if let Some(esc) = chars.next() {
|
.to_string()
|
||||||
// Some characters have special meaning after \ in regex
|
};
|
||||||
// (e.g. \< is word boundary), so use hex escape for safety
|
Regex::new(&pattern).unwrap()
|
||||||
regex.push_str(&format!("\\x{:02x}", esc as u32));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'*' => regex.push_str(".*"),
|
|
||||||
'?' => regex.push('.'),
|
|
||||||
'[' => {
|
|
||||||
// Pass through character class [...] as-is (glob and regex syntax match)
|
|
||||||
regex.push('[');
|
|
||||||
while let Some(bc) = chars.next() {
|
|
||||||
regex.push(bc);
|
|
||||||
if bc == ']' {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'.' | '+' | '(' | ')' | '|' | '^' | '$' | '{' | '}' => {
|
|
||||||
regex.push('\\');
|
|
||||||
regex.push(ch);
|
|
||||||
}
|
|
||||||
_ => regex.push(ch),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if anchored {
|
|
||||||
regex.push('$');
|
|
||||||
}
|
|
||||||
Regex::new(®ex).unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -2381,11 +2634,11 @@ pub fn parse_key_alias(alias: &str) -> Option<KeyEvent> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::time::Duration;
|
|
||||||
use crate::readline::keys::{KeyCode, KeyEvent, ModKeys};
|
|
||||||
use crate::state::{write_vars, read_vars, ArrIndex, VarKind, VarFlags};
|
|
||||||
use crate::parse::lex::Span;
|
use crate::parse::lex::Span;
|
||||||
|
use crate::readline::keys::{KeyCode, KeyEvent, ModKeys};
|
||||||
|
use crate::state::{ArrIndex, VarFlags, VarKind, read_vars, write_vars};
|
||||||
use crate::testutil::{TestGuard, test_input};
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
// ===================== has_braces =====================
|
// ===================== has_braces =====================
|
||||||
|
|
||||||
@@ -2525,10 +2778,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn braces_simple_list() {
|
fn braces_simple_list() {
|
||||||
assert_eq!(
|
assert_eq!(expand_braces_full("{a,b,c}").unwrap(), vec!["a", "b", "c"]);
|
||||||
expand_braces_full("{a,b,c}").unwrap(),
|
|
||||||
vec!["a", "b", "c"]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -2617,7 +2867,19 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn braces_cursed() {
|
fn braces_cursed() {
|
||||||
let result = expand_braces_full("foo{a,{1,2,3,{1..4},5},c}{5..1}bar").unwrap();
|
let result = expand_braces_full("foo{a,{1,2,3,{1..4},5},c}{5..1}bar").unwrap();
|
||||||
assert_eq!(result, vec![ "fooa5bar", "fooa4bar", "fooa3bar", "fooa2bar", "fooa1bar", "foo15bar", "foo14bar", "foo13bar", "foo12bar", "foo11bar", "foo25bar", "foo24bar", "foo23bar", "foo22bar", "foo21bar", "foo35bar", "foo34bar", "foo33bar", "foo32bar", "foo31bar", "foo15bar", "foo14bar", "foo13bar", "foo12bar", "foo11bar", "foo25bar", "foo24bar", "foo23bar", "foo22bar", "foo21bar", "foo35bar", "foo34bar", "foo33bar", "foo32bar", "foo31bar", "foo45bar", "foo44bar", "foo43bar", "foo42bar", "foo41bar", "foo55bar", "foo54bar", "foo53bar", "foo52bar", "foo51bar", "fooc5bar", "fooc4bar", "fooc3bar", "fooc2bar", "fooc1bar", ])
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
vec![
|
||||||
|
"fooa5bar", "fooa4bar", "fooa3bar", "fooa2bar", "fooa1bar", "foo15bar", "foo14bar",
|
||||||
|
"foo13bar", "foo12bar", "foo11bar", "foo25bar", "foo24bar", "foo23bar", "foo22bar",
|
||||||
|
"foo21bar", "foo35bar", "foo34bar", "foo33bar", "foo32bar", "foo31bar", "foo15bar",
|
||||||
|
"foo14bar", "foo13bar", "foo12bar", "foo11bar", "foo25bar", "foo24bar", "foo23bar",
|
||||||
|
"foo22bar", "foo21bar", "foo35bar", "foo34bar", "foo33bar", "foo32bar", "foo31bar",
|
||||||
|
"foo45bar", "foo44bar", "foo43bar", "foo42bar", "foo41bar", "foo55bar", "foo54bar",
|
||||||
|
"foo53bar", "foo52bar", "foo51bar", "fooc5bar", "fooc4bar", "fooc3bar", "fooc2bar",
|
||||||
|
"fooc1bar",
|
||||||
|
]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===================== Arithmetic =====================
|
// ===================== Arithmetic =====================
|
||||||
@@ -2858,7 +3120,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn unescape_backslash() {
|
fn unescape_backslash() {
|
||||||
let result = unescape_str("hello\\nworld");
|
let result = unescape_str("hello\\nworld");
|
||||||
assert_eq!(result, "hellonworld");
|
let expected = format!("hello{}nworld", markers::ESCAPE);
|
||||||
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -3089,10 +3352,22 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn key_alias_arrows() {
|
fn key_alias_arrows() {
|
||||||
assert_eq!(parse_key_alias("UP").unwrap(), KeyEvent(KeyCode::Up, ModKeys::NONE));
|
assert_eq!(
|
||||||
assert_eq!(parse_key_alias("DOWN").unwrap(), KeyEvent(KeyCode::Down, ModKeys::NONE));
|
parse_key_alias("UP").unwrap(),
|
||||||
assert_eq!(parse_key_alias("LEFT").unwrap(), KeyEvent(KeyCode::Left, ModKeys::NONE));
|
KeyEvent(KeyCode::Up, ModKeys::NONE)
|
||||||
assert_eq!(parse_key_alias("RIGHT").unwrap(), KeyEvent(KeyCode::Right, ModKeys::NONE));
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_key_alias("DOWN").unwrap(),
|
||||||
|
KeyEvent(KeyCode::Down, ModKeys::NONE)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_key_alias("LEFT").unwrap(),
|
||||||
|
KeyEvent(KeyCode::Left, ModKeys::NONE)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parse_key_alias("RIGHT").unwrap(),
|
||||||
|
KeyEvent(KeyCode::Right, ModKeys::NONE)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -3104,7 +3379,13 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn key_alias_ctrl_shift_alt_modifier() {
|
fn key_alias_ctrl_shift_alt_modifier() {
|
||||||
let key = parse_key_alias("C-S-A-b").unwrap();
|
let key = parse_key_alias("C-S-A-b").unwrap();
|
||||||
assert_eq!(key, KeyEvent(KeyCode::Char('B'), ModKeys::CTRL | ModKeys::SHIFT | ModKeys::ALT));
|
assert_eq!(
|
||||||
|
key,
|
||||||
|
KeyEvent(
|
||||||
|
KeyCode::Char('B'),
|
||||||
|
ModKeys::CTRL | ModKeys::SHIFT | ModKeys::ALT
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -3296,7 +3577,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn param_remove_shortest_prefix() {
|
fn param_remove_shortest_prefix() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
write_vars(|v| v.set_var("PATH", VarKind::Str("/usr/local/bin".into()), VarFlags::NONE)).unwrap();
|
write_vars(|v| {
|
||||||
|
v.set_var(
|
||||||
|
"PATH",
|
||||||
|
VarKind::Str("/usr/local/bin".into()),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let result = perform_param_expansion("PATH#*/").unwrap();
|
let result = perform_param_expansion("PATH#*/").unwrap();
|
||||||
assert_eq!(result, "usr/local/bin");
|
assert_eq!(result, "usr/local/bin");
|
||||||
@@ -3305,7 +3593,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn param_remove_longest_prefix() {
|
fn param_remove_longest_prefix() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
write_vars(|v| v.set_var("PATH", VarKind::Str("/usr/local/bin".into()), VarFlags::NONE)).unwrap();
|
write_vars(|v| {
|
||||||
|
v.set_var(
|
||||||
|
"PATH",
|
||||||
|
VarKind::Str("/usr/local/bin".into()),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let result = perform_param_expansion("PATH##*/").unwrap();
|
let result = perform_param_expansion("PATH##*/").unwrap();
|
||||||
assert_eq!(result, "bin");
|
assert_eq!(result, "bin");
|
||||||
@@ -3419,7 +3714,10 @@ mod tests {
|
|||||||
fn word_split_default_ifs() {
|
fn word_split_default_ifs() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
|
|
||||||
let mut exp = Expander { raw: "hello world\tfoo".to_string() };
|
let mut exp = Expander {
|
||||||
|
raw: "hello world\tfoo".to_string(),
|
||||||
|
flags: TkFlags::empty(),
|
||||||
|
};
|
||||||
let words = exp.split_words();
|
let words = exp.split_words();
|
||||||
assert_eq!(words, vec!["hello", "world", "foo"]);
|
assert_eq!(words, vec!["hello", "world", "foo"]);
|
||||||
}
|
}
|
||||||
@@ -3427,9 +3725,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn word_split_custom_ifs() {
|
fn word_split_custom_ifs() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
unsafe { std::env::set_var("IFS", ":"); }
|
unsafe {
|
||||||
|
std::env::set_var("IFS", ":");
|
||||||
|
}
|
||||||
|
|
||||||
let mut exp = Expander { raw: "a:b:c".to_string() };
|
let mut exp = Expander {
|
||||||
|
raw: "a:b:c".to_string(),
|
||||||
|
flags: TkFlags::empty(),
|
||||||
|
};
|
||||||
let words = exp.split_words();
|
let words = exp.split_words();
|
||||||
assert_eq!(words, vec!["a", "b", "c"]);
|
assert_eq!(words, vec!["a", "b", "c"]);
|
||||||
}
|
}
|
||||||
@@ -3437,9 +3740,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn word_split_empty_ifs() {
|
fn word_split_empty_ifs() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
unsafe { std::env::set_var("IFS", ""); }
|
unsafe {
|
||||||
|
std::env::set_var("IFS", "");
|
||||||
|
}
|
||||||
|
|
||||||
let mut exp = Expander { raw: "hello world".to_string() };
|
let mut exp = Expander {
|
||||||
|
raw: "hello world".to_string(),
|
||||||
|
flags: TkFlags::empty(),
|
||||||
|
};
|
||||||
let words = exp.split_words();
|
let words = exp.split_words();
|
||||||
assert_eq!(words, vec!["hello world"]);
|
assert_eq!(words, vec!["hello world"]);
|
||||||
}
|
}
|
||||||
@@ -3449,11 +3757,82 @@ mod tests {
|
|||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
|
|
||||||
let raw = format!("{}hello world{}", markers::DUB_QUOTE, markers::DUB_QUOTE);
|
let raw = format!("{}hello world{}", markers::DUB_QUOTE, markers::DUB_QUOTE);
|
||||||
let mut exp = Expander { raw };
|
let mut exp = Expander {
|
||||||
|
raw,
|
||||||
|
flags: TkFlags::empty(),
|
||||||
|
};
|
||||||
let words = exp.split_words();
|
let words = exp.split_words();
|
||||||
assert_eq!(words, vec!["hello world"]);
|
assert_eq!(words, vec!["hello world"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ===================== Escaped Word Splitting =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn word_split_escaped_space() {
|
||||||
|
let _guard = TestGuard::new();
|
||||||
|
|
||||||
|
let raw = format!("hello{}world", unescape_str("\\ "));
|
||||||
|
let mut exp = Expander {
|
||||||
|
raw,
|
||||||
|
flags: TkFlags::empty(),
|
||||||
|
};
|
||||||
|
let words = exp.split_words();
|
||||||
|
assert_eq!(words, vec!["hello world"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn word_split_escaped_tab() {
|
||||||
|
let _guard = TestGuard::new();
|
||||||
|
|
||||||
|
let raw = format!("hello{}world", unescape_str("\\\t"));
|
||||||
|
let mut exp = Expander {
|
||||||
|
raw,
|
||||||
|
flags: TkFlags::empty(),
|
||||||
|
};
|
||||||
|
let words = exp.split_words();
|
||||||
|
assert_eq!(words, vec!["hello\tworld"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn word_split_escaped_custom_ifs() {
|
||||||
|
let _guard = TestGuard::new();
|
||||||
|
unsafe {
|
||||||
|
std::env::set_var("IFS", ":");
|
||||||
|
}
|
||||||
|
|
||||||
|
let raw = format!("a{}b:c", unescape_str("\\:"));
|
||||||
|
let mut exp = Expander {
|
||||||
|
raw,
|
||||||
|
flags: TkFlags::empty(),
|
||||||
|
};
|
||||||
|
let words = exp.split_words();
|
||||||
|
assert_eq!(words, vec!["a:b", "c"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== Parameter Expansion with Escapes (TestGuard) =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn param_exp_prefix_removal_escaped() {
|
||||||
|
let guard = TestGuard::new();
|
||||||
|
write_vars(|v| v.set_var("branch", VarKind::Str("## main".into()), VarFlags::NONE)).unwrap();
|
||||||
|
|
||||||
|
test_input("echo \"${branch#\\#\\# }\"").unwrap();
|
||||||
|
|
||||||
|
let out = guard.read_output();
|
||||||
|
assert_eq!(out, "main\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn param_exp_suffix_removal_escaped() {
|
||||||
|
let guard = TestGuard::new();
|
||||||
|
write_vars(|v| v.set_var("val", VarKind::Str("hello world!!".into()), VarFlags::NONE)).unwrap();
|
||||||
|
|
||||||
|
test_input("echo \"${val%\\!\\!}\"").unwrap();
|
||||||
|
|
||||||
|
let out = guard.read_output();
|
||||||
|
assert_eq!(out, "hello world\n");
|
||||||
|
}
|
||||||
|
|
||||||
// ===================== Arithmetic with Variables (TestGuard) =====================
|
// ===================== Arithmetic with Variables (TestGuard) =====================
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -3478,8 +3857,13 @@ mod tests {
|
|||||||
fn array_index_first() {
|
fn array_index_first() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
write_vars(|v| {
|
write_vars(|v| {
|
||||||
v.set_var("arr", VarKind::arr_from_vec(vec!["a".into(), "b".into(), "c".into()]), VarFlags::NONE)
|
v.set_var(
|
||||||
}).unwrap();
|
"arr",
|
||||||
|
VarKind::arr_from_vec(vec!["a".into(), "b".into(), "c".into()]),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let val = read_vars(|v| v.index_var("arr", ArrIndex::Literal(0))).unwrap();
|
let val = read_vars(|v| v.index_var("arr", ArrIndex::Literal(0))).unwrap();
|
||||||
assert_eq!(val, "a");
|
assert_eq!(val, "a");
|
||||||
@@ -3489,8 +3873,13 @@ mod tests {
|
|||||||
fn array_index_second() {
|
fn array_index_second() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
write_vars(|v| {
|
write_vars(|v| {
|
||||||
v.set_var("arr", VarKind::arr_from_vec(vec!["x".into(), "y".into(), "z".into()]), VarFlags::NONE)
|
v.set_var(
|
||||||
}).unwrap();
|
"arr",
|
||||||
|
VarKind::arr_from_vec(vec!["x".into(), "y".into(), "z".into()]),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let val = read_vars(|v| v.index_var("arr", ArrIndex::Literal(1))).unwrap();
|
let val = read_vars(|v| v.index_var("arr", ArrIndex::Literal(1))).unwrap();
|
||||||
assert_eq!(val, "y");
|
assert_eq!(val, "y");
|
||||||
@@ -3500,8 +3889,13 @@ mod tests {
|
|||||||
fn array_all_elems() {
|
fn array_all_elems() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
write_vars(|v| {
|
write_vars(|v| {
|
||||||
v.set_var("arr", VarKind::arr_from_vec(vec!["a".into(), "b".into(), "c".into()]), VarFlags::NONE)
|
v.set_var(
|
||||||
}).unwrap();
|
"arr",
|
||||||
|
VarKind::arr_from_vec(vec!["a".into(), "b".into(), "c".into()]),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let elems = read_vars(|v| v.get_arr_elems("arr")).unwrap();
|
let elems = read_vars(|v| v.get_arr_elems("arr")).unwrap();
|
||||||
assert_eq!(elems, vec!["a", "b", "c"]);
|
assert_eq!(elems, vec!["a", "b", "c"]);
|
||||||
@@ -3511,8 +3905,13 @@ mod tests {
|
|||||||
fn array_elem_count() {
|
fn array_elem_count() {
|
||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
write_vars(|v| {
|
write_vars(|v| {
|
||||||
v.set_var("arr", VarKind::arr_from_vec(vec!["a".into(), "b".into(), "c".into()]), VarFlags::NONE)
|
v.set_var(
|
||||||
}).unwrap();
|
"arr",
|
||||||
|
VarKind::arr_from_vec(vec!["a".into(), "b".into(), "c".into()]),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let elems = read_vars(|v| v.get_arr_elems("arr")).unwrap();
|
let elems = read_vars(|v| v.get_arr_elems("arr")).unwrap();
|
||||||
assert_eq!(elems.len(), 3);
|
assert_eq!(elems.len(), 3);
|
||||||
@@ -3525,7 +3924,9 @@ mod tests {
|
|||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
let dummy_span = Span::default();
|
let dummy_span = Span::default();
|
||||||
crate::state::SHED.with(|s| {
|
crate::state::SHED.with(|s| {
|
||||||
s.logic.borrow_mut().insert_alias("ll", "ls -la", dummy_span.clone());
|
s.logic
|
||||||
|
.borrow_mut()
|
||||||
|
.insert_alias("ll", "ls -la", dummy_span.clone());
|
||||||
});
|
});
|
||||||
|
|
||||||
let log_tab = crate::state::SHED.with(|s| s.logic.borrow().clone());
|
let log_tab = crate::state::SHED.with(|s| s.logic.borrow().clone());
|
||||||
@@ -3538,7 +3939,9 @@ mod tests {
|
|||||||
let _guard = TestGuard::new();
|
let _guard = TestGuard::new();
|
||||||
let dummy_span = Span::default();
|
let dummy_span = Span::default();
|
||||||
crate::state::SHED.with(|s| {
|
crate::state::SHED.with(|s| {
|
||||||
s.logic.borrow_mut().insert_alias("foo", "foo --verbose", dummy_span.clone());
|
s.logic
|
||||||
|
.borrow_mut()
|
||||||
|
.insert_alias("foo", "foo --verbose", dummy_span.clone());
|
||||||
});
|
});
|
||||||
|
|
||||||
let log_tab = crate::state::SHED.with(|s| s.logic.borrow().clone());
|
let log_tab = crate::state::SHED.with(|s| s.logic.borrow().clone());
|
||||||
@@ -3553,7 +3956,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn index_simple() {
|
fn index_simple() {
|
||||||
let guard = TestGuard::new();
|
let guard = TestGuard::new();
|
||||||
write_vars(|v| v.set_var("arr", VarKind::Arr(VecDeque::from(["foo".into(), "bar".into(), "biz".into()])), VarFlags::NONE)).unwrap();
|
write_vars(|v| {
|
||||||
|
v.set_var(
|
||||||
|
"arr",
|
||||||
|
VarKind::Arr(VecDeque::from(["foo".into(), "bar".into(), "biz".into()])),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
test_input("echo $arr").unwrap();
|
test_input("echo $arr").unwrap();
|
||||||
|
|
||||||
@@ -3564,8 +3974,22 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn index_cursed() {
|
fn index_cursed() {
|
||||||
let guard = TestGuard::new();
|
let guard = TestGuard::new();
|
||||||
write_vars(|v| v.set_var("arr", VarKind::Arr(VecDeque::from(["foo".into(), "bar".into(), "biz".into()])), VarFlags::NONE)).unwrap();
|
write_vars(|v| {
|
||||||
write_vars(|v| v.set_var("i", VarKind::Arr(VecDeque::from(["0".into(), "1".into(), "2".into()])), VarFlags::NONE)).unwrap();
|
v.set_var(
|
||||||
|
"arr",
|
||||||
|
VarKind::Arr(VecDeque::from(["foo".into(), "bar".into(), "biz".into()])),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
write_vars(|v| {
|
||||||
|
v.set_var(
|
||||||
|
"i",
|
||||||
|
VarKind::Arr(VecDeque::from(["0".into(), "1".into(), "2".into()])),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
test_input("echo $echo ${var:-${arr[$(($(echo ${i[0]}) + 1))]}}").unwrap();
|
test_input("echo $echo ${var:-${arr[$(($(echo ${i[0]}) + 1))]}}").unwrap();
|
||||||
|
|
||||||
|
|||||||
129
src/getopt.rs
129
src/getopt.rs
@@ -3,7 +3,11 @@ use std::sync::Arc;
|
|||||||
use ariadne::Fmt;
|
use ariadne::Fmt;
|
||||||
use fmt::Display;
|
use fmt::Display;
|
||||||
|
|
||||||
use crate::{libsh::error::{ShErr, ShErrKind, ShResult, next_color}, parse::lex::Tk, prelude::*};
|
use crate::{
|
||||||
|
libsh::error::{ShErr, ShErrKind, ShResult, next_color},
|
||||||
|
parse::lex::Tk,
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
|
||||||
pub type OptSet = Arc<[Opt]>;
|
pub type OptSet = Arc<[Opt]>;
|
||||||
|
|
||||||
@@ -82,17 +86,23 @@ pub fn get_opts_from_tokens(
|
|||||||
sort_tks(tokens, opt_specs, false)
|
sort_tks(tokens, opt_specs, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sort_tks(tokens: Vec<Tk>, opt_specs: &[OptSpec], strict: bool) -> ShResult<(Vec<Tk>, Vec<Opt>)> {
|
pub fn sort_tks(
|
||||||
|
tokens: Vec<Tk>,
|
||||||
|
opt_specs: &[OptSpec],
|
||||||
|
strict: bool,
|
||||||
|
) -> ShResult<(Vec<Tk>, Vec<Opt>)> {
|
||||||
let mut tokens_iter = tokens
|
let mut tokens_iter = tokens
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|t| t.expand())
|
.map(|t| t.expand())
|
||||||
.collect::<ShResult<Vec<_>>>()?
|
.collect::<ShResult<Vec<_>>>()?
|
||||||
.into_iter();
|
.into_iter()
|
||||||
|
.peekable();
|
||||||
let mut opts = vec![];
|
let mut opts = vec![];
|
||||||
let mut non_opts = vec![];
|
let mut non_opts = vec![];
|
||||||
|
|
||||||
while let Some(token) = tokens_iter.next() {
|
while let Some(token) = tokens_iter.next() {
|
||||||
if &token.to_string() == "--" {
|
if &token.to_string() == "--" {
|
||||||
|
non_opts.push(token);
|
||||||
non_opts.extend(tokens_iter);
|
non_opts.extend(tokens_iter);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -140,7 +150,6 @@ pub fn sort_tks(tokens: Vec<Tk>, opt_specs: &[OptSpec], strict: bool) -> ShResul
|
|||||||
Ok((non_opts, opts))
|
Ok((non_opts, opts))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::parse::lex::{LexFlags, LexStream};
|
use crate::parse::lex::{LexFlags, LexStream};
|
||||||
@@ -156,7 +165,10 @@ use super::*;
|
|||||||
#[test]
|
#[test]
|
||||||
fn parse_short_combined() {
|
fn parse_short_combined() {
|
||||||
let opts = Opt::parse("-abc");
|
let opts = Opt::parse("-abc");
|
||||||
assert_eq!(opts, vec![Opt::Short('a'), Opt::Short('b'), Opt::Short('c')]);
|
assert_eq!(
|
||||||
|
opts,
|
||||||
|
vec![Opt::Short('a'), Opt::Short('b'), Opt::Short('c')]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -173,7 +185,12 @@ use super::*;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get_opts_basic() {
|
fn get_opts_basic() {
|
||||||
let words = vec!["file.txt".into(), "-v".into(), "--help".into(), "arg".into()];
|
let words = vec![
|
||||||
|
"file.txt".into(),
|
||||||
|
"-v".into(),
|
||||||
|
"--help".into(),
|
||||||
|
"arg".into(),
|
||||||
|
];
|
||||||
let (non_opts, opts) = get_opts(words);
|
let (non_opts, opts) = get_opts(words);
|
||||||
assert_eq!(non_opts, vec!["file.txt", "arg"]);
|
assert_eq!(non_opts, vec!["file.txt", "arg"]);
|
||||||
assert_eq!(opts, vec![Opt::Short('v'), Opt::Long("help".into())]);
|
assert_eq!(opts, vec![Opt::Short('v'), Opt::Long("help".into())]);
|
||||||
@@ -191,7 +208,10 @@ use super::*;
|
|||||||
fn get_opts_combined_short() {
|
fn get_opts_combined_short() {
|
||||||
let words = vec!["-abc".into(), "file".into()];
|
let words = vec!["-abc".into(), "file".into()];
|
||||||
let (non_opts, opts) = get_opts(words);
|
let (non_opts, opts) = get_opts(words);
|
||||||
assert_eq!(opts, vec![Opt::Short('a'), Opt::Short('b'), Opt::Short('c')]);
|
assert_eq!(
|
||||||
|
opts,
|
||||||
|
vec![Opt::Short('a'), Opt::Short('b'), Opt::Short('c')]
|
||||||
|
);
|
||||||
assert_eq!(non_opts, vec!["file"]);
|
assert_eq!(non_opts, vec!["file"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +235,10 @@ use super::*;
|
|||||||
assert_eq!(Opt::Short('v').to_string(), "-v");
|
assert_eq!(Opt::Short('v').to_string(), "-v");
|
||||||
assert_eq!(Opt::Long("help".into()).to_string(), "--help");
|
assert_eq!(Opt::Long("help".into()).to_string(), "--help");
|
||||||
assert_eq!(Opt::ShortWithArg('o', "file".into()).to_string(), "-o file");
|
assert_eq!(Opt::ShortWithArg('o', "file".into()).to_string(), "-o file");
|
||||||
assert_eq!(Opt::LongWithArg("output".into(), "file".into()).to_string(), "--output file");
|
assert_eq!(
|
||||||
|
Opt::LongWithArg("output".into(), "file".into()).to_string(),
|
||||||
|
"--output file"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lex(input: &str) -> Vec<Tk> {
|
fn lex(input: &str) -> Vec<Tk> {
|
||||||
@@ -229,8 +252,14 @@ use super::*;
|
|||||||
let tokens = lex("file.txt --help -v arg");
|
let tokens = lex("file.txt --help -v arg");
|
||||||
|
|
||||||
let opt_spec = vec![
|
let opt_spec = vec![
|
||||||
OptSpec { opt: Opt::Short('v'), takes_arg: false },
|
OptSpec {
|
||||||
OptSpec { opt: Opt::Long("help".into()), takes_arg: false },
|
opt: Opt::Short('v'),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
|
OptSpec {
|
||||||
|
opt: Opt::Long("help".into()),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
||||||
@@ -248,9 +277,10 @@ use super::*;
|
|||||||
fn tks_short_with_arg() {
|
fn tks_short_with_arg() {
|
||||||
let tokens = lex("-o output.txt file.txt");
|
let tokens = lex("-o output.txt file.txt");
|
||||||
|
|
||||||
let opt_spec = vec![
|
let opt_spec = vec![OptSpec {
|
||||||
OptSpec { opt: Opt::Short('o'), takes_arg: true },
|
opt: Opt::Short('o'),
|
||||||
];
|
takes_arg: true,
|
||||||
|
}];
|
||||||
|
|
||||||
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
||||||
|
|
||||||
@@ -263,13 +293,17 @@ use super::*;
|
|||||||
fn tks_long_with_arg() {
|
fn tks_long_with_arg() {
|
||||||
let tokens = lex("--output result.txt input.txt");
|
let tokens = lex("--output result.txt input.txt");
|
||||||
|
|
||||||
let opt_spec = vec![
|
let opt_spec = vec![OptSpec {
|
||||||
OptSpec { opt: Opt::Long("output".into()), takes_arg: true },
|
opt: Opt::Long("output".into()),
|
||||||
];
|
takes_arg: true,
|
||||||
|
}];
|
||||||
|
|
||||||
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
||||||
|
|
||||||
assert_eq!(opts, vec![Opt::LongWithArg("output".into(), "result.txt".into())]);
|
assert_eq!(
|
||||||
|
opts,
|
||||||
|
vec![Opt::LongWithArg("output".into(), "result.txt".into())]
|
||||||
|
);
|
||||||
let non_opts: Vec<String> = non_opts.into_iter().map(|s| s.to_string()).collect();
|
let non_opts: Vec<String> = non_opts.into_iter().map(|s| s.to_string()).collect();
|
||||||
assert!(non_opts.contains(&"input.txt".to_string()));
|
assert!(non_opts.contains(&"input.txt".to_string()));
|
||||||
}
|
}
|
||||||
@@ -279,8 +313,14 @@ use super::*;
|
|||||||
let tokens = lex("-v -- -a --foo");
|
let tokens = lex("-v -- -a --foo");
|
||||||
|
|
||||||
let opt_spec = vec![
|
let opt_spec = vec![
|
||||||
OptSpec { opt: Opt::Short('v'), takes_arg: false },
|
OptSpec {
|
||||||
OptSpec { opt: Opt::Short('a'), takes_arg: false },
|
opt: Opt::Short('v'),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
|
OptSpec {
|
||||||
|
opt: Opt::Short('a'),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
||||||
@@ -296,29 +336,47 @@ use super::*;
|
|||||||
let tokens = lex("-abc");
|
let tokens = lex("-abc");
|
||||||
|
|
||||||
let opt_spec = vec![
|
let opt_spec = vec![
|
||||||
OptSpec { opt: Opt::Short('a'), takes_arg: false },
|
OptSpec {
|
||||||
OptSpec { opt: Opt::Short('b'), takes_arg: false },
|
opt: Opt::Short('a'),
|
||||||
OptSpec { opt: Opt::Short('c'), takes_arg: false },
|
takes_arg: false,
|
||||||
|
},
|
||||||
|
OptSpec {
|
||||||
|
opt: Opt::Short('b'),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
|
OptSpec {
|
||||||
|
opt: Opt::Short('c'),
|
||||||
|
takes_arg: false,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let (_non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
let (_non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
||||||
|
|
||||||
assert_eq!(opts, vec![Opt::Short('a'), Opt::Short('b'), Opt::Short('c')]);
|
assert_eq!(
|
||||||
|
opts,
|
||||||
|
vec![Opt::Short('a'), Opt::Short('b'), Opt::Short('c')]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tks_unknown_opt_becomes_non_opt() {
|
fn tks_unknown_opt_becomes_non_opt() {
|
||||||
let tokens = lex("-v -x file");
|
let tokens = lex("-v -x file");
|
||||||
|
|
||||||
let opt_spec = vec![
|
let opt_spec = vec![OptSpec {
|
||||||
OptSpec { opt: Opt::Short('v'), takes_arg: false },
|
opt: Opt::Short('v'),
|
||||||
];
|
takes_arg: false,
|
||||||
|
}];
|
||||||
|
|
||||||
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
||||||
|
|
||||||
assert_eq!(opts, vec![Opt::Short('v')]);
|
assert_eq!(opts, vec![Opt::Short('v')]);
|
||||||
// -x is not in spec, so its token goes to non_opts
|
// -x is not in spec, so its token goes to non_opts
|
||||||
assert!(non_opts.into_iter().map(|s| s.to_string()).any(|s| s == "-x" || s == "file"));
|
assert!(
|
||||||
|
non_opts
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.any(|s| s == "-x" || s == "file")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -326,16 +384,25 @@ use super::*;
|
|||||||
let tokens = lex("-n 5 --output file.txt input");
|
let tokens = lex("-n 5 --output file.txt input");
|
||||||
|
|
||||||
let opt_spec = vec![
|
let opt_spec = vec![
|
||||||
OptSpec { opt: Opt::Short('n'), takes_arg: true },
|
OptSpec {
|
||||||
OptSpec { opt: Opt::Long("output".into()), takes_arg: true },
|
opt: Opt::Short('n'),
|
||||||
|
takes_arg: true,
|
||||||
|
},
|
||||||
|
OptSpec {
|
||||||
|
opt: Opt::Long("output".into()),
|
||||||
|
takes_arg: true,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
let (non_opts, opts) = get_opts_from_tokens(tokens, &opt_spec).unwrap();
|
||||||
|
|
||||||
assert_eq!(opts, vec![
|
assert_eq!(
|
||||||
|
opts,
|
||||||
|
vec![
|
||||||
Opt::ShortWithArg('n', "5".into()),
|
Opt::ShortWithArg('n', "5".into()),
|
||||||
Opt::LongWithArg("output".into(), "file.txt".into()),
|
Opt::LongWithArg("output".into(), "file.txt".into()),
|
||||||
]);
|
]
|
||||||
|
);
|
||||||
let non_opts: Vec<String> = non_opts.into_iter().map(|s| s.to_string()).collect();
|
let non_opts: Vec<String> = non_opts.into_iter().map(|s| s.to_string()).collect();
|
||||||
assert!(non_opts.contains(&"input".to_string()));
|
assert!(non_opts.contains(&"input".to_string()));
|
||||||
}
|
}
|
||||||
|
|||||||
51
src/jobs.rs
51
src/jobs.rs
@@ -1,4 +1,7 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use ariadne::Fmt;
|
use ariadne::Fmt;
|
||||||
|
use nix::unistd::getpid;
|
||||||
use scopeguard::defer;
|
use scopeguard::defer;
|
||||||
use yansi::Color;
|
use yansi::Color;
|
||||||
|
|
||||||
@@ -10,7 +13,7 @@ use crate::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
procio::{IoMode, borrow_fd},
|
procio::{IoMode, borrow_fd},
|
||||||
signal::{disable_reaping, enable_reaping},
|
signal::{disable_reaping, enable_reaping},
|
||||||
state::{self, ShellParam, set_status, write_jobs, write_vars},
|
state::{self, ShellParam, VarFlags, VarKind, set_status, write_jobs, write_vars},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const SIG_EXIT_OFFSET: i32 = 128;
|
pub const SIG_EXIT_OFFSET: i32 = 128;
|
||||||
@@ -596,6 +599,29 @@ impl Job {
|
|||||||
.map(|chld| chld.stat())
|
.map(|chld| chld.stat())
|
||||||
.collect::<Vec<WtStat>>()
|
.collect::<Vec<WtStat>>()
|
||||||
}
|
}
|
||||||
|
pub fn pipe_status(stats: &[WtStat]) -> Option<Vec<i32>> {
|
||||||
|
if stats.iter().any(|stat| {
|
||||||
|
matches!(
|
||||||
|
stat,
|
||||||
|
WtStat::StillAlive | WtStat::Continued(_) | WtStat::PtraceSyscall(_)
|
||||||
|
)
|
||||||
|
}) || stats.len() <= 1
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(
|
||||||
|
stats
|
||||||
|
.iter()
|
||||||
|
.map(|stat| match stat {
|
||||||
|
WtStat::Exited(_, code) => *code,
|
||||||
|
WtStat::Signaled(_, signal, _) => SIG_EXIT_OFFSET + *signal as i32,
|
||||||
|
WtStat::Stopped(_, signal) => SIG_EXIT_OFFSET + *signal as i32,
|
||||||
|
WtStat::PtraceEvent(_, signal, _) => SIG_EXIT_OFFSET + *signal as i32,
|
||||||
|
WtStat::PtraceSyscall(_) | WtStat::Continued(_) | WtStat::StillAlive => unreachable!(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
pub fn get_pids(&self) -> Vec<Pid> {
|
pub fn get_pids(&self) -> Vec<Pid> {
|
||||||
self
|
self
|
||||||
.children
|
.children
|
||||||
@@ -839,22 +865,35 @@ pub fn wait_fg(job: Job, interactive: bool) -> ShResult<()> {
|
|||||||
enable_reaping();
|
enable_reaping();
|
||||||
}
|
}
|
||||||
let statuses = write_jobs(|j| j.new_fg(job))?;
|
let statuses = write_jobs(|j| j.new_fg(job))?;
|
||||||
for status in statuses {
|
for status in &statuses {
|
||||||
code = code_from_status(&status).unwrap_or(0);
|
code = code_from_status(status).unwrap_or(0);
|
||||||
match status {
|
match status {
|
||||||
WtStat::Stopped(_, _) => {
|
WtStat::Stopped(_, _) => {
|
||||||
was_stopped = true;
|
was_stopped = true;
|
||||||
write_jobs(|j| j.fg_to_bg(status))?;
|
write_jobs(|j| j.fg_to_bg(*status))?;
|
||||||
}
|
}
|
||||||
WtStat::Signaled(_, sig, _) => {
|
WtStat::Signaled(_, sig, _) => {
|
||||||
if sig == Signal::SIGTSTP {
|
if *sig == Signal::SIGINT {
|
||||||
|
// interrupt propagates to the shell
|
||||||
|
// necessary for interrupting stuff like
|
||||||
|
// while/for loops
|
||||||
|
kill(getpid(), Signal::SIGINT)?;
|
||||||
|
} else if *sig == Signal::SIGTSTP {
|
||||||
was_stopped = true;
|
was_stopped = true;
|
||||||
write_jobs(|j| j.fg_to_bg(status))?;
|
write_jobs(|j| j.fg_to_bg(*status))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => { /* Do nothing */ }
|
_ => { /* Do nothing */ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(pipe_status) = Job::pipe_status(&statuses) {
|
||||||
|
let pipe_status = pipe_status
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.collect::<VecDeque<String>>();
|
||||||
|
|
||||||
|
write_vars(|v| v.set_var("PIPESTATUS", VarKind::Arr(pipe_status), VarFlags::NONE))?;
|
||||||
|
}
|
||||||
// If job wasn't stopped (moved to bg), clear the fg slot
|
// If job wasn't stopped (moved to bg), clear the fg slot
|
||||||
if !was_stopped {
|
if !was_stopped {
|
||||||
write_jobs(|j| {
|
write_jobs(|j| {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use ariadne::{Color, Fmt};
|
use ariadne::{Color, Fmt};
|
||||||
use ariadne::{Report, ReportKind};
|
use ariadne::{Report, ReportKind};
|
||||||
use rand::TryRng;
|
use rand::TryRng;
|
||||||
use yansi::Paint;
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
use yansi::Paint;
|
||||||
|
|
||||||
use crate::procio::RedirGuard;
|
use crate::procio::RedirGuard;
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -201,6 +201,7 @@ impl ShErr {
|
|||||||
pub fn is_flow_control(&self) -> bool {
|
pub fn is_flow_control(&self) -> bool {
|
||||||
self.kind.is_flow_control()
|
self.kind.is_flow_control()
|
||||||
}
|
}
|
||||||
|
/// Promotes a shell error from a simple error to an error that blames a span
|
||||||
pub fn promote(mut self, span: Span) -> Self {
|
pub fn promote(mut self, span: Span) -> Self {
|
||||||
if self.notes.is_empty() {
|
if self.notes.is_empty() {
|
||||||
return self;
|
return self;
|
||||||
@@ -208,6 +209,8 @@ impl ShErr {
|
|||||||
let first = self.notes[0].clone();
|
let first = self.notes[0].clone();
|
||||||
if self.notes.len() > 1 {
|
if self.notes.len() > 1 {
|
||||||
self.notes = self.notes[1..].to_vec();
|
self.notes = self.notes[1..].to_vec();
|
||||||
|
} else {
|
||||||
|
self.notes = vec![];
|
||||||
}
|
}
|
||||||
|
|
||||||
self.labeled(span, first)
|
self.labeled(span, first)
|
||||||
@@ -456,7 +459,7 @@ pub enum ShErrKind {
|
|||||||
FuncReturn(i32),
|
FuncReturn(i32),
|
||||||
LoopContinue(i32),
|
LoopContinue(i32),
|
||||||
LoopBreak(i32),
|
LoopBreak(i32),
|
||||||
ClearReadline,
|
Interrupt,
|
||||||
Null,
|
Null,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -468,7 +471,7 @@ impl ShErrKind {
|
|||||||
| Self::FuncReturn(_)
|
| Self::FuncReturn(_)
|
||||||
| Self::LoopContinue(_)
|
| Self::LoopContinue(_)
|
||||||
| Self::LoopBreak(_)
|
| Self::LoopBreak(_)
|
||||||
| Self::ClearReadline
|
| Self::Interrupt
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -493,7 +496,7 @@ impl Display for ShErrKind {
|
|||||||
Self::LoopBreak(_) => "Syntax Error",
|
Self::LoopBreak(_) => "Syntax Error",
|
||||||
Self::ReadlineErr => "Readline Error",
|
Self::ReadlineErr => "Readline Error",
|
||||||
Self::ExCommand => "Ex Command Error",
|
Self::ExCommand => "Ex Command Error",
|
||||||
Self::ClearReadline => "",
|
Self::Interrupt => "",
|
||||||
Self::Null => "",
|
Self::Null => "",
|
||||||
};
|
};
|
||||||
write!(f, "{output}")
|
write!(f, "{output}")
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::collections::HashSet;
|
|||||||
use std::os::fd::{BorrowedFd, RawFd};
|
use std::os::fd::{BorrowedFd, RawFd};
|
||||||
|
|
||||||
use nix::sys::termios::{self, LocalFlags, Termios, tcgetattr, tcsetattr};
|
use nix::sys::termios::{self, LocalFlags, Termios, tcgetattr, tcsetattr};
|
||||||
use nix::unistd::isatty;
|
use nix::unistd::{isatty, write};
|
||||||
use scopeguard::guard;
|
use scopeguard::guard;
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
@@ -147,11 +147,10 @@ impl RawModeGuard {
|
|||||||
let orig = ORIG_TERMIOS
|
let orig = ORIG_TERMIOS
|
||||||
.with(|cell| cell.borrow().clone())
|
.with(|cell| cell.borrow().clone())
|
||||||
.expect("with_cooked_mode called before raw_mode()");
|
.expect("with_cooked_mode called before raw_mode()");
|
||||||
tcsetattr(borrow_fd(*TTY_FILENO), termios::SetArg::TCSANOW, &orig)
|
tcsetattr(borrow_fd(*TTY_FILENO), termios::SetArg::TCSANOW, &orig).ok();
|
||||||
.expect("Failed to restore cooked mode");
|
|
||||||
let res = f();
|
let res = f();
|
||||||
tcsetattr(borrow_fd(*TTY_FILENO), termios::SetArg::TCSANOW, ¤t)
|
tcsetattr(borrow_fd(*TTY_FILENO), termios::SetArg::TCSANOW, ¤t).ok();
|
||||||
.expect("Failed to restore raw mode");
|
unsafe { write(BorrowedFd::borrow_raw(*TTY_FILENO), b"\x1b[?1l\x1b>").ok() };
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -159,11 +158,12 @@ impl RawModeGuard {
|
|||||||
impl Drop for RawModeGuard {
|
impl Drop for RawModeGuard {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let _ = termios::tcsetattr(
|
termios::tcsetattr(
|
||||||
BorrowedFd::borrow_raw(self.fd),
|
BorrowedFd::borrow_raw(self.fd),
|
||||||
termios::SetArg::TCSANOW,
|
termios::SetArg::TCSANOW,
|
||||||
&self.orig,
|
&self.orig,
|
||||||
);
|
)
|
||||||
|
.ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,15 @@ use std::sync::LazyLock;
|
|||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
/// Minimum fd number for shell-internal file descriptors.
|
||||||
|
const MIN_INTERNAL_FD: RawFd = 10;
|
||||||
|
|
||||||
pub static TTY_FILENO: LazyLock<RawFd> = LazyLock::new(|| {
|
pub static TTY_FILENO: LazyLock<RawFd> = LazyLock::new(|| {
|
||||||
open("/dev/tty", OFlag::O_RDWR, Mode::empty()).expect("Failed to open /dev/tty")
|
let fd = open("/dev/tty", OFlag::O_RDWR, Mode::empty()).expect("Failed to open /dev/tty");
|
||||||
|
// Move the tty fd above the user-accessible range so that
|
||||||
|
// `exec 3>&-` and friends don't collide with shell internals.
|
||||||
|
let high =
|
||||||
|
fcntl(fd, FcntlArg::F_DUPFD_CLOEXEC(MIN_INTERNAL_FD)).expect("Failed to dup /dev/tty high");
|
||||||
|
close(fd).ok();
|
||||||
|
high
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,6 +8,33 @@ use crate::parse::{Node, Redir, RedirType};
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::state::AutoCmd;
|
use crate::state::AutoCmd;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
/// Defines a two-way mapping between an enum and its string representation, implementing both Display and FromStr.
|
||||||
|
macro_rules! two_way_display {
|
||||||
|
($name:ident, $($member:ident <=> $val:expr;)*) => {
|
||||||
|
impl Display for $name {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
$(Self::$member => write!(f, $val),)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for $name {
|
||||||
|
type Err = ShErr;
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s {
|
||||||
|
$($val => Ok(Self::$member),)*
|
||||||
|
_ => Err(ShErr::simple(
|
||||||
|
ShErrKind::ParseErr,
|
||||||
|
format!("Invalid {} kind: {}",stringify!($name),s),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub trait VecDequeExt<T> {
|
pub trait VecDequeExt<T> {
|
||||||
fn to_vec(self) -> Vec<T>;
|
fn to_vec(self) -> Vec<T>;
|
||||||
}
|
}
|
||||||
|
|||||||
156
src/main.rs
156
src/main.rs
@@ -23,6 +23,7 @@ pub mod testutil;
|
|||||||
use std::os::fd::BorrowedFd;
|
use std::os::fd::BorrowedFd;
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
use std::sync::atomic::Ordering;
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use nix::errno::Errno;
|
use nix::errno::Errno;
|
||||||
use nix::poll::{PollFd, PollFlags, PollTimeout, poll};
|
use nix::poll::{PollFd, PollFlags, PollTimeout, poll};
|
||||||
@@ -38,15 +39,18 @@ use crate::prelude::*;
|
|||||||
use crate::procio::borrow_fd;
|
use crate::procio::borrow_fd;
|
||||||
use crate::readline::term::{LineWriter, RawModeGuard, raw_mode};
|
use crate::readline::term::{LineWriter, RawModeGuard, raw_mode};
|
||||||
use crate::readline::{Prompt, ReadlineEvent, ShedVi};
|
use crate::readline::{Prompt, ReadlineEvent, ShedVi};
|
||||||
use crate::signal::{GOT_SIGWINCH, JOB_DONE, QUIT_CODE, check_signals, sig_setup, signals_pending};
|
use crate::signal::{
|
||||||
use crate::state::{AutoCmdKind, read_logic, read_shopts, source_rc, write_jobs, write_meta};
|
GOT_SIGUSR1, GOT_SIGWINCH, JOB_DONE, QUIT_CODE, check_signals, sig_setup, signals_pending,
|
||||||
|
};
|
||||||
|
use crate::state::{
|
||||||
|
AutoCmdKind, read_logic, read_shopts, source_env, source_login, source_rc, write_jobs,
|
||||||
|
write_meta, write_shopts,
|
||||||
|
};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use state::{read_vars, write_vars};
|
use state::write_vars;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct ShedArgs {
|
struct ShedArgs {
|
||||||
script: Option<String>,
|
|
||||||
|
|
||||||
#[arg(short)]
|
#[arg(short)]
|
||||||
command: Option<String>,
|
command: Option<String>,
|
||||||
|
|
||||||
@@ -59,24 +63,13 @@ struct ShedArgs {
|
|||||||
#[arg(short)]
|
#[arg(short)]
|
||||||
interactive: bool,
|
interactive: bool,
|
||||||
|
|
||||||
|
#[arg(short)]
|
||||||
|
stdin: bool,
|
||||||
|
|
||||||
#[arg(long, short)]
|
#[arg(long, short)]
|
||||||
login_shell: bool,
|
login_shell: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Force evaluation of lazily-initialized values early in shell startup.
|
|
||||||
///
|
|
||||||
/// In particular, this ensures that the variable table is initialized, which
|
|
||||||
/// populates environment variables from the system. If this initialization is
|
|
||||||
/// deferred too long, features like prompt expansion may fail due to missing
|
|
||||||
/// environment variables.
|
|
||||||
///
|
|
||||||
/// This function triggers initialization by calling `read_vars` with a no-op
|
|
||||||
/// closure, which forces access to the variable table and causes its `LazyLock`
|
|
||||||
/// constructor to run.
|
|
||||||
fn kickstart_lazy_evals() {
|
|
||||||
read_vars(|_| {});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// We need to make sure that even if we panic, our child processes get sighup
|
/// We need to make sure that even if we panic, our child processes get sighup
|
||||||
fn setup_panic_handler() {
|
fn setup_panic_handler() {
|
||||||
let default_panic_hook = std::panic::take_hook();
|
let default_panic_hook = std::panic::take_hook();
|
||||||
@@ -111,7 +104,6 @@ fn setup_panic_handler() {
|
|||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
yansi::enable();
|
yansi::enable();
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
kickstart_lazy_evals();
|
|
||||||
setup_panic_handler();
|
setup_panic_handler();
|
||||||
|
|
||||||
let mut args = ShedArgs::parse();
|
let mut args = ShedArgs::parse();
|
||||||
@@ -130,10 +122,27 @@ fn main() -> ExitCode {
|
|||||||
return ExitCode::SUCCESS;
|
return ExitCode::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = if let Some(path) = args.script {
|
// Increment SHLVL, or set to 1 if not present or invalid.
|
||||||
run_script(path, args.script_args)
|
// This var represents how many nested shell instances we're in
|
||||||
} else if let Some(cmd) = args.command {
|
if let Ok(var) = env::var("SHLVL")
|
||||||
|
&& let Ok(lvl) = var.parse::<u32>()
|
||||||
|
{
|
||||||
|
unsafe { env::set_var("SHLVL", (lvl + 1).to_string()) };
|
||||||
|
} else {
|
||||||
|
unsafe { env::set_var("SHLVL", "1") };
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(e) = source_env() {
|
||||||
|
e.print_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(e) = if let Some(cmd) = args.command {
|
||||||
exec_dash_c(cmd)
|
exec_dash_c(cmd)
|
||||||
|
} else if args.stdin || !isatty(STDIN_FILENO).unwrap_or(false) {
|
||||||
|
read_commands(args.script_args)
|
||||||
|
} else if !args.script_args.is_empty() {
|
||||||
|
let path = args.script_args.remove(0);
|
||||||
|
run_script(path, args.script_args)
|
||||||
} else {
|
} else {
|
||||||
let res = shed_interactive(args);
|
let res = shed_interactive(args);
|
||||||
write(borrow_fd(*TTY_FILENO), b"\x1b[?2004l").ok(); // disable bracketed paste mode on exit
|
write(borrow_fd(*TTY_FILENO), b"\x1b[?2004l").ok(); // disable bracketed paste mode on exit
|
||||||
@@ -152,9 +161,41 @@ fn main() -> ExitCode {
|
|||||||
on_exit_autocmds.exec();
|
on_exit_autocmds.exec();
|
||||||
|
|
||||||
write_jobs(|j| j.hang_up());
|
write_jobs(|j| j.hang_up());
|
||||||
|
|
||||||
|
let code = QUIT_CODE.load(Ordering::SeqCst) as u8;
|
||||||
|
if code == 0 && isatty(STDIN_FILENO).unwrap_or_default() {
|
||||||
|
write(borrow_fd(STDERR_FILENO), b"\nexit\n").ok();
|
||||||
|
}
|
||||||
|
|
||||||
ExitCode::from(QUIT_CODE.load(Ordering::SeqCst) as u8)
|
ExitCode::from(QUIT_CODE.load(Ordering::SeqCst) as u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_commands(args: Vec<String>) -> ShResult<()> {
|
||||||
|
let mut input = vec![];
|
||||||
|
let mut read_buf = [0u8; 4096];
|
||||||
|
loop {
|
||||||
|
match read(STDIN_FILENO, &mut read_buf) {
|
||||||
|
Ok(0) => break,
|
||||||
|
Ok(n) => input.extend_from_slice(&read_buf[..n]),
|
||||||
|
Err(Errno::EINTR) => continue,
|
||||||
|
Err(e) => {
|
||||||
|
QUIT_CODE.store(1, Ordering::SeqCst);
|
||||||
|
return Err(ShErr::simple(
|
||||||
|
ShErrKind::CleanExit(1),
|
||||||
|
format!("error reading from stdin: {e}"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let commands = String::from_utf8_lossy(&input).to_string();
|
||||||
|
for arg in args {
|
||||||
|
write_vars(|v| v.cur_scope_mut().bpush_arg(arg))
|
||||||
|
}
|
||||||
|
|
||||||
|
exec_input(commands, None, false, None)
|
||||||
|
}
|
||||||
|
|
||||||
fn run_script<P: AsRef<Path>>(path: P, args: Vec<String>) -> ShResult<()> {
|
fn run_script<P: AsRef<Path>>(path: P, args: Vec<String>) -> ShResult<()> {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let path_raw = path.to_string_lossy().to_string();
|
let path_raw = path.to_string_lossy().to_string();
|
||||||
@@ -190,6 +231,12 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> {
|
|||||||
let _raw_mode = raw_mode(); // sets raw mode, restores termios on drop
|
let _raw_mode = raw_mode(); // sets raw mode, restores termios on drop
|
||||||
sig_setup(args.login_shell);
|
sig_setup(args.login_shell);
|
||||||
|
|
||||||
|
if args.login_shell
|
||||||
|
&& let Err(e) = source_login()
|
||||||
|
{
|
||||||
|
e.print_error();
|
||||||
|
}
|
||||||
|
|
||||||
if let Err(e) = source_rc() {
|
if let Err(e) = source_rc() {
|
||||||
e.print_error();
|
e.print_error();
|
||||||
}
|
}
|
||||||
@@ -209,6 +256,8 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> {
|
|||||||
|
|
||||||
readline.writer.flush_write("\x1b[?2004h")?; // enable bracketed paste mode
|
readline.writer.flush_write("\x1b[?2004h")?; // enable bracketed paste mode
|
||||||
|
|
||||||
|
let mut screensaver_deadline: Option<Instant> = None;
|
||||||
|
|
||||||
// Main poll loop
|
// Main poll loop
|
||||||
loop {
|
loop {
|
||||||
write_meta(|m| {
|
write_meta(|m| {
|
||||||
@@ -221,7 +270,7 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> {
|
|||||||
while signals_pending() {
|
while signals_pending() {
|
||||||
if let Err(e) = check_signals() {
|
if let Err(e) = check_signals() {
|
||||||
match e.kind() {
|
match e.kind() {
|
||||||
ShErrKind::ClearReadline => {
|
ShErrKind::Interrupt => {
|
||||||
// We got Ctrl+C - clear current input and redraw
|
// We got Ctrl+C - clear current input and redraw
|
||||||
readline.reset_active_widget(false)?;
|
readline.reset_active_widget(false)?;
|
||||||
}
|
}
|
||||||
@@ -247,22 +296,71 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> {
|
|||||||
readline.prompt_mut().refresh();
|
readline.prompt_mut().refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if GOT_SIGUSR1.swap(false, Ordering::SeqCst) {
|
||||||
|
log::info!("SIGUSR1 received: refreshing readline state");
|
||||||
|
readline.mark_dirty();
|
||||||
|
readline.prompt_mut().refresh();
|
||||||
|
}
|
||||||
|
|
||||||
readline.print_line(false)?;
|
readline.print_line(false)?;
|
||||||
|
|
||||||
// Poll for stdin input
|
// Poll for
|
||||||
|
// stdin input
|
||||||
let mut fds = [PollFd::new(
|
let mut fds = [PollFd::new(
|
||||||
unsafe { BorrowedFd::borrow_raw(*TTY_FILENO) },
|
unsafe { BorrowedFd::borrow_raw(*TTY_FILENO) },
|
||||||
PollFlags::POLLIN,
|
PollFlags::POLLIN,
|
||||||
)];
|
)];
|
||||||
|
|
||||||
|
let mut exec_if_timeout = None;
|
||||||
|
|
||||||
let timeout = if readline.pending_keymap.is_empty() {
|
let timeout = if readline.pending_keymap.is_empty() {
|
||||||
|
let screensaver_cmd = read_shopts(|o| o.prompt.screensaver_cmd.clone())
|
||||||
|
.trim()
|
||||||
|
.to_string();
|
||||||
|
let screensaver_idle_time = read_shopts(|o| o.prompt.screensaver_idle_time);
|
||||||
|
if screensaver_idle_time > 0 && !screensaver_cmd.is_empty() {
|
||||||
|
exec_if_timeout = Some(screensaver_cmd);
|
||||||
|
if screensaver_deadline.is_none() {
|
||||||
|
screensaver_deadline = Some(Instant::now() + Duration::from_secs(screensaver_idle_time as u64));
|
||||||
|
}
|
||||||
|
// We unfortunately cant just set the PollTimeout to use 'idle_time * 1000' as the timeout
|
||||||
|
// because u16 overflows after 65 seconds (65535 ms).
|
||||||
|
// So we set a one second timeout and check against the Instant in 'screensaver_deadline'
|
||||||
|
PollTimeout::from(1000u16)
|
||||||
|
} else {
|
||||||
|
screensaver_deadline = None;
|
||||||
PollTimeout::MAX
|
PollTimeout::MAX
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
PollTimeout::from(1000u16)
|
PollTimeout::from(1000u16)
|
||||||
};
|
};
|
||||||
|
|
||||||
match poll(&mut fds, timeout) {
|
match poll(&mut fds, timeout) {
|
||||||
Ok(_) => {}
|
Ok(0) => {
|
||||||
|
// We timed out.
|
||||||
|
if let Some(cmd) = exec_if_timeout
|
||||||
|
&& screensaver_deadline.is_some_and(|d| Instant::now() >= d) {
|
||||||
|
screensaver_deadline = None;
|
||||||
|
let prepared = ReadlineEvent::Line(cmd.clone());
|
||||||
|
let saved_hist_opt = read_shopts(|o| o.core.auto_hist);
|
||||||
|
let _guard = scopeguard::guard(saved_hist_opt, |opt| {
|
||||||
|
write_shopts(|o| o.core.auto_hist = opt);
|
||||||
|
});
|
||||||
|
write_shopts(|o| o.core.auto_hist = false); // don't save screensaver command to history
|
||||||
|
let pre_cmds = read_logic(|l| l.get_autocmds(AutoCmdKind::OnScreensaverExec));
|
||||||
|
pre_cmds.exec_with(&cmd);
|
||||||
|
|
||||||
|
let res = handle_readline_event(&mut readline, Ok(prepared))?;
|
||||||
|
|
||||||
|
let post_cmds = read_logic(|l| l.get_autocmds(AutoCmdKind::OnScreensaverReturn));
|
||||||
|
post_cmds.exec_with(&cmd);
|
||||||
|
|
||||||
|
match res {
|
||||||
|
true => return Ok(()),
|
||||||
|
false => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Err(Errno::EINTR) => {
|
Err(Errno::EINTR) => {
|
||||||
// Interrupted by signal, loop back to handle it
|
// Interrupted by signal, loop back to handle it
|
||||||
continue;
|
continue;
|
||||||
@@ -271,6 +369,7 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> {
|
|||||||
eprintln!("poll error: {e}");
|
eprintln!("poll error: {e}");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Ok(_) => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timeout — resolve pending keymap ambiguity
|
// Timeout — resolve pending keymap ambiguity
|
||||||
@@ -333,6 +432,7 @@ fn shed_interactive(args: ShedArgs) -> ShResult<()> {
|
|||||||
}
|
}
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
readline.feed_bytes(&buffer[..n]);
|
readline.feed_bytes(&buffer[..n]);
|
||||||
|
screensaver_deadline = None;
|
||||||
}
|
}
|
||||||
Err(Errno::EINTR) => {
|
Err(Errno::EINTR) => {
|
||||||
// Interrupted, continue to handle signals
|
// Interrupted, continue to handle signals
|
||||||
@@ -372,6 +472,10 @@ fn handle_readline_event(readline: &mut ShedVi, event: ShResult<ReadlineEvent>)
|
|||||||
}) {
|
}) {
|
||||||
// CleanExit signals an intentional shell exit; any other error is printed.
|
// CleanExit signals an intentional shell exit; any other error is printed.
|
||||||
match e.kind() {
|
match e.kind() {
|
||||||
|
ShErrKind::Interrupt => {
|
||||||
|
// We got Ctrl+C during command execution
|
||||||
|
// Just fall through here
|
||||||
|
}
|
||||||
ShErrKind::CleanExit(code) => {
|
ShErrKind::CleanExit(code) => {
|
||||||
QUIT_CODE.store(*code, Ordering::SeqCst);
|
QUIT_CODE.store(*code, Ordering::SeqCst);
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
|
|||||||
@@ -8,7 +8,30 @@ use ariadne::Fmt;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
builtin::{
|
builtin::{
|
||||||
alias::{alias, unalias}, arrops::{arr_fpop, arr_fpush, arr_pop, arr_push, arr_rotate}, autocmd::autocmd, cd::cd, complete::{compgen_builtin, complete_builtin}, dirstack::{dirs, popd, pushd}, echo::echo, eval, exec, flowctl::flowctl, getopts::getopts, intro, jobctl::{self, JobBehavior, continue_job, disown, jobs}, keymap, map, pwd::pwd, read::{self, read_builtin}, resource::{ulimit, umask_builtin}, shift::shift, shopt::shopt, source::source, test::double_bracket_test, trap::{TrapTarget, trap}, varcmds::{export, local, readonly, unset}
|
alias::{alias, unalias},
|
||||||
|
arrops::{arr_fpop, arr_fpush, arr_pop, arr_push, arr_rotate},
|
||||||
|
autocmd::autocmd,
|
||||||
|
cd::cd,
|
||||||
|
complete::{compgen_builtin, complete_builtin},
|
||||||
|
dirstack::{dirs, popd, pushd},
|
||||||
|
echo::echo,
|
||||||
|
eval, exec,
|
||||||
|
flowctl::flowctl,
|
||||||
|
getopts::getopts,
|
||||||
|
help::help,
|
||||||
|
intro,
|
||||||
|
jobctl::{self, JobBehavior, continue_job, disown, jobs},
|
||||||
|
keymap, map,
|
||||||
|
pwd::pwd,
|
||||||
|
read::{self, read_builtin},
|
||||||
|
resource::{ulimit, umask_builtin},
|
||||||
|
seek::seek,
|
||||||
|
shift::shift,
|
||||||
|
shopt::shopt,
|
||||||
|
source::source,
|
||||||
|
test::double_bracket_test,
|
||||||
|
trap::{TrapTarget, trap},
|
||||||
|
varcmds::{export, local, readonly, unset},
|
||||||
},
|
},
|
||||||
expand::{expand_aliases, expand_case_pattern, glob_to_regex},
|
expand::{expand_aliases, expand_case_pattern, glob_to_regex},
|
||||||
jobs::{ChildProc, JobStack, attach_tty, dispatch_job},
|
jobs::{ChildProc, JobStack, attach_tty, dispatch_job},
|
||||||
@@ -19,6 +42,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
procio::{IoMode, IoStack, PipeGenerator},
|
procio::{IoMode, IoStack, PipeGenerator},
|
||||||
|
signal::{check_signals, signals_pending},
|
||||||
state::{
|
state::{
|
||||||
self, ShFunc, VarFlags, VarKind, read_logic, read_shopts, write_jobs, write_logic, write_vars,
|
self, ShFunc, VarFlags, VarKind, read_logic, read_shopts, write_jobs, write_logic, write_vars,
|
||||||
},
|
},
|
||||||
@@ -136,10 +160,15 @@ pub fn exec_dash_c(input: String) -> ShResult<()> {
|
|||||||
if nodes.len() == 1 {
|
if nodes.len() == 1 {
|
||||||
let is_single_cmd = match &nodes[0].class {
|
let is_single_cmd = match &nodes[0].class {
|
||||||
NdRule::Command { .. } => true,
|
NdRule::Command { .. } => true,
|
||||||
NdRule::Pipeline { cmds } => cmds.len() == 1 && matches!(cmds[0].class, NdRule::Command { .. }),
|
NdRule::Pipeline { cmds } => {
|
||||||
|
cmds.len() == 1 && matches!(cmds[0].class, NdRule::Command { .. })
|
||||||
|
}
|
||||||
NdRule::Conjunction { elements } => {
|
NdRule::Conjunction { elements } => {
|
||||||
elements.len() == 1 && match &elements[0].cmd.class {
|
elements.len() == 1
|
||||||
NdRule::Pipeline { cmds } => cmds.len() == 1 && matches!(cmds[0].class, NdRule::Command { .. }),
|
&& match &elements[0].cmd.class {
|
||||||
|
NdRule::Pipeline { cmds } => {
|
||||||
|
cmds.len() == 1 && matches!(cmds[0].class, NdRule::Command { .. })
|
||||||
|
}
|
||||||
NdRule::Command { .. } => true,
|
NdRule::Command { .. } => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@@ -151,8 +180,12 @@ pub fn exec_dash_c(input: String) -> ShResult<()> {
|
|||||||
let mut node = nodes.remove(0);
|
let mut node = nodes.remove(0);
|
||||||
loop {
|
loop {
|
||||||
match node.class {
|
match node.class {
|
||||||
NdRule::Conjunction { mut elements } => { node = *elements.remove(0).cmd; }
|
NdRule::Conjunction { mut elements } => {
|
||||||
NdRule::Pipeline { mut cmds } => { node = cmds.remove(0); }
|
node = *elements.remove(0).cmd;
|
||||||
|
}
|
||||||
|
NdRule::Pipeline { mut cmds } => {
|
||||||
|
node = cmds.remove(0);
|
||||||
|
}
|
||||||
NdRule::Command { .. } => break,
|
NdRule::Command { .. } => break,
|
||||||
_ => break,
|
_ => break,
|
||||||
}
|
}
|
||||||
@@ -241,6 +274,13 @@ impl Dispatcher {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn dispatch_node(&mut self, node: Node) -> ShResult<()> {
|
pub fn dispatch_node(&mut self, node: Node) -> ShResult<()> {
|
||||||
|
while signals_pending() {
|
||||||
|
// If we have received SIGINT,
|
||||||
|
// this will stop the execution here
|
||||||
|
// and propagate back to the functions in main.rs
|
||||||
|
check_signals()?;
|
||||||
|
}
|
||||||
|
|
||||||
match node.class {
|
match node.class {
|
||||||
NdRule::Conjunction { .. } => self.exec_conjunction(node)?,
|
NdRule::Conjunction { .. } => self.exec_conjunction(node)?,
|
||||||
NdRule::Pipeline { .. } => self.exec_pipeline(node)?,
|
NdRule::Pipeline { .. } => self.exec_pipeline(node)?,
|
||||||
@@ -250,6 +290,7 @@ impl Dispatcher {
|
|||||||
NdRule::CaseNode { .. } => self.exec_case(node)?,
|
NdRule::CaseNode { .. } => self.exec_case(node)?,
|
||||||
NdRule::BraceGrp { .. } => self.exec_brc_grp(node)?,
|
NdRule::BraceGrp { .. } => self.exec_brc_grp(node)?,
|
||||||
NdRule::FuncDef { .. } => self.exec_func_def(node)?,
|
NdRule::FuncDef { .. } => self.exec_func_def(node)?,
|
||||||
|
NdRule::Negate { .. } => self.exec_negated(node)?,
|
||||||
NdRule::Command { .. } => self.dispatch_cmd(node)?,
|
NdRule::Command { .. } => self.dispatch_cmd(node)?,
|
||||||
NdRule::Test { .. } => self.exec_test(node)?,
|
NdRule::Test { .. } => self.exec_test(node)?,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
@@ -257,6 +298,15 @@ impl Dispatcher {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn dispatch_cmd(&mut self, node: Node) -> ShResult<()> {
|
pub fn dispatch_cmd(&mut self, node: Node) -> ShResult<()> {
|
||||||
|
let (line, _) = node.get_span().clone().line_and_col();
|
||||||
|
write_vars(|v| {
|
||||||
|
v.set_var(
|
||||||
|
"LINENO",
|
||||||
|
VarKind::Str((line + 1).to_string()),
|
||||||
|
VarFlags::NONE,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
let Some(cmd) = node.get_command() else {
|
let Some(cmd) = node.get_command() else {
|
||||||
return self.exec_cmd(node); // Argv is empty, probably an assignment
|
return self.exec_cmd(node); // Argv is empty, probably an assignment
|
||||||
};
|
};
|
||||||
@@ -284,30 +334,35 @@ impl Dispatcher {
|
|||||||
self.exec_cmd(node)
|
self.exec_cmd(node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn exec_negated(&mut self, node: Node) -> ShResult<()> {
|
||||||
|
let NdRule::Negate { cmd } = node.class else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
self.dispatch_node(*cmd)?;
|
||||||
|
let status = state::get_status();
|
||||||
|
state::set_status(if status == 0 { 1 } else { 0 });
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
pub fn exec_conjunction(&mut self, conjunction: Node) -> ShResult<()> {
|
pub fn exec_conjunction(&mut self, conjunction: Node) -> ShResult<()> {
|
||||||
let NdRule::Conjunction { elements } = conjunction.class else {
|
let NdRule::Conjunction { elements } = conjunction.class else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut elem_iter = elements.into_iter();
|
let mut elem_iter = elements.into_iter();
|
||||||
|
let mut skip = false;
|
||||||
while let Some(element) = elem_iter.next() {
|
while let Some(element) = elem_iter.next() {
|
||||||
let ConjunctNode { cmd, operator } = element;
|
let ConjunctNode { cmd, operator } = element;
|
||||||
|
if !skip {
|
||||||
self.dispatch_node(*cmd)?;
|
self.dispatch_node(*cmd)?;
|
||||||
|
}
|
||||||
|
|
||||||
let status = state::get_status();
|
let status = state::get_status();
|
||||||
match operator {
|
skip = match operator {
|
||||||
ConjunctOp::And => {
|
ConjunctOp::And => status != 0,
|
||||||
if status != 0 {
|
ConjunctOp::Or => status == 0,
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ConjunctOp::Or => {
|
|
||||||
if status == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ConjunctOp::Null => break,
|
ConjunctOp::Null => break,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -327,7 +382,11 @@ impl Dispatcher {
|
|||||||
};
|
};
|
||||||
let body_span = body.get_span();
|
let body_span = body.get_span();
|
||||||
let body = body_span.as_str().to_string();
|
let body = body_span.as_str().to_string();
|
||||||
let name = name.span.as_str().strip_suffix("()").unwrap();
|
let name = name
|
||||||
|
.span
|
||||||
|
.as_str()
|
||||||
|
.strip_suffix("()")
|
||||||
|
.unwrap_or(name.span.as_str());
|
||||||
|
|
||||||
if KEYWORDS.contains(&name) {
|
if KEYWORDS.contains(&name) {
|
||||||
return Err(ShErr::at(
|
return Err(ShErr::at(
|
||||||
@@ -578,6 +637,7 @@ impl Dispatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
state::set_status(0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -714,10 +774,14 @@ impl Dispatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !matched && !else_block.is_empty() {
|
if !matched {
|
||||||
|
if !else_block.is_empty() {
|
||||||
for node in else_block {
|
for node in else_block {
|
||||||
s.dispatch_node(node)?;
|
s.dispatch_node(node)?;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
state::set_status(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -749,7 +813,18 @@ impl Dispatcher {
|
|||||||
if cmds.len() == 1 {
|
if cmds.len() == 1 {
|
||||||
self.fg_job = !is_bg && self.interactive;
|
self.fg_job = !is_bg && self.interactive;
|
||||||
let cmd = cmds.into_iter().next().unwrap();
|
let cmd = cmds.into_iter().next().unwrap();
|
||||||
|
if is_bg && !matches!(cmd.class, NdRule::Command { .. }) {
|
||||||
|
self.run_fork(
|
||||||
|
&cmd.get_command().map(|t| t.to_string()).unwrap_or_default(),
|
||||||
|
|s| {
|
||||||
|
if let Err(e) = s.dispatch_node(cmd) {
|
||||||
|
e.print_error();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
self.dispatch_node(cmd)?;
|
self.dispatch_node(cmd)?;
|
||||||
|
}
|
||||||
|
|
||||||
// Give the pipeline terminal control as soon as the first child
|
// Give the pipeline terminal control as soon as the first child
|
||||||
// establishes the PGID, so later children (e.g. nvim) don't get
|
// establishes the PGID, so later children (e.g. nvim) don't get
|
||||||
@@ -822,7 +897,10 @@ impl Dispatcher {
|
|||||||
|
|
||||||
if fork_builtins {
|
if fork_builtins {
|
||||||
log::trace!("Forking builtin: {}", cmd_raw);
|
log::trace!("Forking builtin: {}", cmd_raw);
|
||||||
let _guard = self.io_stack.pop_frame().redirect()?;
|
let guard = self.io_stack.pop_frame().redirect()?;
|
||||||
|
if cmd_raw.as_str() == "exec" {
|
||||||
|
guard.persist();
|
||||||
|
}
|
||||||
self.run_fork(&cmd_raw, |s| {
|
self.run_fork(&cmd_raw, |s| {
|
||||||
if let Err(e) = s.dispatch_builtin(cmd) {
|
if let Err(e) = s.dispatch_builtin(cmd) {
|
||||||
e.print_error();
|
e.print_error();
|
||||||
@@ -947,6 +1025,8 @@ impl Dispatcher {
|
|||||||
"autocmd" => autocmd(cmd),
|
"autocmd" => autocmd(cmd),
|
||||||
"ulimit" => ulimit(cmd),
|
"ulimit" => ulimit(cmd),
|
||||||
"umask" => umask_builtin(cmd),
|
"umask" => umask_builtin(cmd),
|
||||||
|
"seek" => seek(cmd),
|
||||||
|
"help" => help(cmd),
|
||||||
"true" | ":" => {
|
"true" | ":" => {
|
||||||
state::set_status(0);
|
state::set_status(0);
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -1084,6 +1164,7 @@ impl Dispatcher {
|
|||||||
match unsafe { fork()? } {
|
match unsafe { fork()? } {
|
||||||
ForkResult::Child => {
|
ForkResult::Child => {
|
||||||
let _ = setpgid(Pid::from_raw(0), existing_pgid.unwrap_or(Pid::from_raw(0)));
|
let _ = setpgid(Pid::from_raw(0), existing_pgid.unwrap_or(Pid::from_raw(0)));
|
||||||
|
self.interactive = false;
|
||||||
f(self);
|
f(self);
|
||||||
exit(state::get_status())
|
exit(state::get_status())
|
||||||
}
|
}
|
||||||
@@ -1195,3 +1276,202 @@ pub fn is_func(tk: Option<Tk>) -> bool {
|
|||||||
pub fn is_subsh(tk: Option<Tk>) -> bool {
|
pub fn is_subsh(tk: Option<Tk>) -> bool {
|
||||||
tk.is_some_and(|tk| tk.flags.contains(TkFlags::IS_SUBSH))
|
tk.is_some_and(|tk| tk.flags.contains(TkFlags::IS_SUBSH))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::state;
|
||||||
|
use crate::testutil::{TestGuard, test_input};
|
||||||
|
|
||||||
|
// ===================== while/until status =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_loop_status_zero_after_completion() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("while false; do :; done").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_loop_status_zero_after_iterations() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("X=0; while [[ $X -lt 3 ]]; do X=$((X+1)); done").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn until_loop_status_zero_after_completion() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("until true; do :; done").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn until_loop_status_zero_after_iterations() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("X=3; until [[ $X -le 0 ]]; do X=$((X-1)); done").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_break_preserves_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("while true; do break; done").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_body_status_propagates() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("X=0; while [[ $X -lt 1 ]]; do X=$((X+1)); false; done").unwrap();
|
||||||
|
// Loop body ended with `false` (status 1), but the loop itself
|
||||||
|
// completed normally when the condition failed, so status should be 0
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== if/elif/else status =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_true_body_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("if true; then echo ok; fi").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_false_no_else_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("if false; then echo ok; fi").unwrap();
|
||||||
|
// No branch taken, POSIX says status is 0
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_else_branch_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("if false; then true; else false; fi").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== for loop status =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn for_loop_empty_list_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("for x in; do echo $x; done").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn for_loop_body_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("for x in a b c; do true; done").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== case status =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn case_match_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("case foo in foo) true;; esac").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn case_no_match_status() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("case foo in bar) true;; esac").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== other stuff =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn for_loop_var_zip() {
|
||||||
|
let g = TestGuard::new();
|
||||||
|
test_input("for a b in 1 2 3 4 5 6; do echo $a $b; done").unwrap();
|
||||||
|
let out = g.read_output();
|
||||||
|
assert_eq!(out, "1 2\n3 4\n5 6\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn for_loop_unsets_zipped() {
|
||||||
|
let g = TestGuard::new();
|
||||||
|
test_input("for a b c d in 1 2 3 4 5 6; do echo $a $b $c $d; done").unwrap();
|
||||||
|
let out = g.read_output();
|
||||||
|
assert_eq!(out, "1 2 3 4\n5 6\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== negation (!) status =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negate_true() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("! true").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negate_false() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("! false").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn double_negate_true() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("! ! true").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn double_negate_false() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
test_input("! ! false").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negate_pipeline_last_cmd() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
// pipeline status = last cmd (false) = 1, negated → 0
|
||||||
|
test_input("! true | false").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negate_pipeline_last_cmd_true() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
// pipeline status = last cmd (true) = 0, negated → 1
|
||||||
|
test_input("! false | true").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negate_in_conjunction() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
// ! binds to pipeline, not conjunction: (! (true && false)) && true
|
||||||
|
test_input("! (true && false) && true").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negate_in_if_condition() {
|
||||||
|
let g = TestGuard::new();
|
||||||
|
test_input("if ! false; then echo yes; fi").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
assert_eq!(g.read_output(), "yes\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_var_in_test() {
|
||||||
|
let _g = TestGuard::new();
|
||||||
|
// POSIX specifies that a quoted unset variable expands to an empty string, so the shell actually sees `[ -n "" ]`, which returns false
|
||||||
|
test_input("[ -n \"$EMPTYVAR_PROBABLY_NOT_SET_TO_ANYTHING\" ]").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 1);
|
||||||
|
// Without quotes, word splitting causes an empty var to be removed entirely, so the shell actually sees `[ -n ]`, testing the value of ']', which returns true
|
||||||
|
test_input("[ -n $EMPTYVAR_PROBABLY_NOT_SET_TO_ANYTHING ]").unwrap();
|
||||||
|
assert_eq!(state::get_status(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
330
src/parse/lex.rs
330
src/parse/lex.rs
@@ -17,9 +17,9 @@ use crate::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const KEYWORDS: [&str; 16] = [
|
pub const KEYWORDS: [&str; 17] = [
|
||||||
"if", "then", "elif", "else", "fi", "while", "until", "select", "for", "in", "do", "done",
|
"if", "then", "elif", "else", "fi", "while", "until", "select", "for", "in", "do", "done",
|
||||||
"case", "esac", "[[", "]]",
|
"case", "esac", "[[", "]]", "!",
|
||||||
];
|
];
|
||||||
|
|
||||||
pub const OPENERS: [&str; 6] = ["if", "while", "until", "for", "select", "case"];
|
pub const OPENERS: [&str; 6] = ["if", "while", "until", "for", "select", "case"];
|
||||||
@@ -166,6 +166,7 @@ pub enum TkRule {
|
|||||||
ErrPipe,
|
ErrPipe,
|
||||||
And,
|
And,
|
||||||
Or,
|
Or,
|
||||||
|
Bang,
|
||||||
Bg,
|
Bg,
|
||||||
Sep,
|
Sep,
|
||||||
Redir,
|
Redir,
|
||||||
@@ -216,6 +217,31 @@ impl Tk {
|
|||||||
};
|
};
|
||||||
self.span.as_str().trim() == ";;"
|
self.span.as_str().trim() == ";;"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_opener(&self) -> bool {
|
||||||
|
OPENERS.contains(&self.as_str())
|
||||||
|
|| matches!(self.class, TkRule::BraceGrpStart)
|
||||||
|
|| matches!(self.class, TkRule::CasePattern)
|
||||||
|
}
|
||||||
|
pub fn is_closer(&self) -> bool {
|
||||||
|
matches!(self.as_str(), "fi" | "done" | "esac")
|
||||||
|
|| self.has_double_semi()
|
||||||
|
|| matches!(self.class, TkRule::BraceGrpEnd)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_closer_for(&self, other: &Tk) -> bool {
|
||||||
|
if (matches!(other.class, TkRule::BraceGrpStart) && matches!(self.class, TkRule::BraceGrpEnd))
|
||||||
|
|| (matches!(other.class, TkRule::CasePattern) && self.has_double_semi())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
match other.as_str() {
|
||||||
|
"for" | "while" | "until" => matches!(self.as_str(), "done"),
|
||||||
|
"if" => matches!(self.as_str(), "fi"),
|
||||||
|
"case" => matches!(self.as_str(), "esac"),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Tk {
|
impl Display for Tk {
|
||||||
@@ -240,19 +266,12 @@ bitflags! {
|
|||||||
const ASSIGN = 0b0000000001000000;
|
const ASSIGN = 0b0000000001000000;
|
||||||
const BUILTIN = 0b0000000010000000;
|
const BUILTIN = 0b0000000010000000;
|
||||||
const IS_PROCSUB = 0b0000000100000000;
|
const IS_PROCSUB = 0b0000000100000000;
|
||||||
|
const IS_HEREDOC = 0b0000001000000000;
|
||||||
|
const LIT_HEREDOC = 0b0000010000000000;
|
||||||
|
const TAB_HEREDOC = 0b0000100000000000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LexStream {
|
|
||||||
source: Arc<String>,
|
|
||||||
pub cursor: usize,
|
|
||||||
pub name: String,
|
|
||||||
quote_state: QuoteState,
|
|
||||||
brc_grp_depth: usize,
|
|
||||||
brc_grp_start: Option<usize>,
|
|
||||||
flags: LexFlags,
|
|
||||||
}
|
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct LexFlags: u32 {
|
pub struct LexFlags: u32 {
|
||||||
@@ -271,7 +290,6 @@ bitflags! {
|
|||||||
/// The lexer has no more tokens to produce
|
/// The lexer has no more tokens to produce
|
||||||
const STALE = 0b0001000000;
|
const STALE = 0b0001000000;
|
||||||
const EXPECTING_IN = 0b0010000000;
|
const EXPECTING_IN = 0b0010000000;
|
||||||
const IN_CASE = 0b0100000000;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,6 +313,18 @@ pub fn clean_input(input: &str) -> String {
|
|||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct LexStream {
|
||||||
|
source: Arc<String>,
|
||||||
|
pub cursor: usize,
|
||||||
|
pub name: String,
|
||||||
|
quote_state: QuoteState,
|
||||||
|
brc_grp_depth: usize,
|
||||||
|
brc_grp_start: Option<usize>,
|
||||||
|
case_depth: usize,
|
||||||
|
heredoc_skip: Option<usize>,
|
||||||
|
flags: LexFlags,
|
||||||
|
}
|
||||||
|
|
||||||
impl LexStream {
|
impl LexStream {
|
||||||
pub fn new(source: Arc<String>, flags: LexFlags) -> Self {
|
pub fn new(source: Arc<String>, flags: LexFlags) -> Self {
|
||||||
let flags = flags | LexFlags::FRESH | LexFlags::NEXT_IS_CMD;
|
let flags = flags | LexFlags::FRESH | LexFlags::NEXT_IS_CMD;
|
||||||
@@ -306,6 +336,8 @@ impl LexStream {
|
|||||||
quote_state: QuoteState::default(),
|
quote_state: QuoteState::default(),
|
||||||
brc_grp_depth: 0,
|
brc_grp_depth: 0,
|
||||||
brc_grp_start: None,
|
brc_grp_start: None,
|
||||||
|
heredoc_skip: None,
|
||||||
|
case_depth: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Returns a slice of the source input using the given range
|
/// Returns a slice of the source input using the given range
|
||||||
@@ -365,7 +397,7 @@ impl LexStream {
|
|||||||
}
|
}
|
||||||
pub fn read_redir(&mut self) -> Option<ShResult<Tk>> {
|
pub fn read_redir(&mut self) -> Option<ShResult<Tk>> {
|
||||||
assert!(self.cursor <= self.source.len());
|
assert!(self.cursor <= self.source.len());
|
||||||
let slice = self.slice(self.cursor..)?;
|
let slice = self.slice(self.cursor..)?.to_string();
|
||||||
let mut pos = self.cursor;
|
let mut pos = self.cursor;
|
||||||
let mut chars = slice.chars().peekable();
|
let mut chars = slice.chars().peekable();
|
||||||
let mut tk = Tk::default();
|
let mut tk = Tk::default();
|
||||||
@@ -377,20 +409,38 @@ impl LexStream {
|
|||||||
return None; // It's a process sub
|
return None; // It's a process sub
|
||||||
}
|
}
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
if let Some('|') = chars.peek() {
|
||||||
|
// noclobber force '>|'
|
||||||
|
chars.next();
|
||||||
|
pos += 1;
|
||||||
|
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some('>') = chars.peek() {
|
if let Some('>') = chars.peek() {
|
||||||
chars.next();
|
chars.next();
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
if let Some('&') = chars.peek() {
|
let Some('&') = chars.peek() else {
|
||||||
|
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
chars.next();
|
chars.next();
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
let mut found_fd = false;
|
let mut found_fd = false;
|
||||||
|
if chars.peek().is_some_and(|ch| *ch == '-') {
|
||||||
|
chars.next();
|
||||||
|
found_fd = true;
|
||||||
|
pos += 1;
|
||||||
|
} else {
|
||||||
while chars.peek().is_some_and(|ch| ch.is_ascii_digit()) {
|
while chars.peek().is_some_and(|ch| ch.is_ascii_digit()) {
|
||||||
chars.next();
|
chars.next();
|
||||||
found_fd = true;
|
found_fd = true;
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !found_fd && !self.flags.contains(LexFlags::LEX_UNFINISHED) {
|
if !found_fd && !self.flags.contains(LexFlags::LEX_UNFINISHED) {
|
||||||
let span_start = self.cursor;
|
let span_start = self.cursor;
|
||||||
@@ -404,10 +454,6 @@ impl LexStream {
|
|||||||
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
'<' => {
|
'<' => {
|
||||||
if chars.peek() == Some(&'(') {
|
if chars.peek() == Some(&'(') {
|
||||||
@@ -415,14 +461,94 @@ impl LexStream {
|
|||||||
}
|
}
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
for _ in 0..2 {
|
match chars.peek() {
|
||||||
if let Some('<') = chars.peek() {
|
Some('<') => {
|
||||||
chars.next();
|
chars.next();
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
|
match chars.peek() {
|
||||||
|
Some('<') => {
|
||||||
|
chars.next();
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(ch) => {
|
||||||
|
let mut ch = *ch;
|
||||||
|
while is_field_sep(ch) {
|
||||||
|
let Some(next_ch) = chars.next() else {
|
||||||
|
// Incomplete input — fall through to emit << as Redir
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
pos += next_ch.len_utf8();
|
||||||
|
ch = next_ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_field_sep(ch) {
|
||||||
|
// Ran out of input while skipping whitespace — fall through
|
||||||
} else {
|
} else {
|
||||||
|
let saved_cursor = self.cursor;
|
||||||
|
match self.read_heredoc(pos) {
|
||||||
|
Ok(Some(heredoc_tk)) => {
|
||||||
|
// cursor is set to after the delimiter word;
|
||||||
|
// heredoc_skip is set to after the body
|
||||||
|
pos = self.cursor;
|
||||||
|
self.cursor = saved_cursor;
|
||||||
|
tk = heredoc_tk;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Ok(None) => {
|
||||||
|
// Incomplete heredoc — restore cursor and fall through
|
||||||
|
self.cursor = saved_cursor;
|
||||||
|
}
|
||||||
|
Err(e) => return Some(Err(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// No delimiter yet — input is incomplete
|
||||||
|
// Fall through to emit the << as a Redir token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some('>') => {
|
||||||
|
chars.next();
|
||||||
|
pos += 1;
|
||||||
|
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Some('&') => {
|
||||||
|
chars.next();
|
||||||
|
pos += 1;
|
||||||
|
|
||||||
|
let mut found_fd = false;
|
||||||
|
if chars.peek().is_some_and(|ch| *ch == '-') {
|
||||||
|
chars.next();
|
||||||
|
found_fd = true;
|
||||||
|
pos += 1;
|
||||||
|
} else {
|
||||||
|
while chars.peek().is_some_and(|ch| ch.is_ascii_digit()) {
|
||||||
|
chars.next();
|
||||||
|
found_fd = true;
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found_fd && !self.flags.contains(LexFlags::LEX_UNFINISHED) {
|
||||||
|
let span_start = self.cursor;
|
||||||
|
self.cursor = pos;
|
||||||
|
return Some(Err(ShErr::at(
|
||||||
|
ShErrKind::ParseErr,
|
||||||
|
Span::new(span_start..pos, self.source.clone()),
|
||||||
|
"Invalid redirection",
|
||||||
|
)));
|
||||||
|
} else {
|
||||||
|
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
tk = self.get_token(self.cursor..pos, TkRule::Redir);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -446,6 +572,133 @@ impl LexStream {
|
|||||||
self.cursor = pos;
|
self.cursor = pos;
|
||||||
Some(Ok(tk))
|
Some(Ok(tk))
|
||||||
}
|
}
|
||||||
|
pub fn read_heredoc(&mut self, mut pos: usize) -> ShResult<Option<Tk>> {
|
||||||
|
let slice = self.slice(pos..).unwrap_or_default().to_string();
|
||||||
|
let mut chars = slice.chars();
|
||||||
|
let mut delim = String::new();
|
||||||
|
let mut flags = TkFlags::empty();
|
||||||
|
let mut first_char = true;
|
||||||
|
// Parse the delimiter word, stripping quotes
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
match ch {
|
||||||
|
'-' if first_char => {
|
||||||
|
pos += 1;
|
||||||
|
flags |= TkFlags::TAB_HEREDOC;
|
||||||
|
}
|
||||||
|
'\"' => {
|
||||||
|
pos += 1;
|
||||||
|
self.quote_state.toggle_double();
|
||||||
|
flags |= TkFlags::LIT_HEREDOC;
|
||||||
|
}
|
||||||
|
'\'' => {
|
||||||
|
pos += 1;
|
||||||
|
self.quote_state.toggle_single();
|
||||||
|
flags |= TkFlags::LIT_HEREDOC;
|
||||||
|
}
|
||||||
|
_ if self.quote_state.in_quote() => {
|
||||||
|
pos += ch.len_utf8();
|
||||||
|
delim.push(ch);
|
||||||
|
}
|
||||||
|
ch if is_hard_sep(ch) => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ch => {
|
||||||
|
pos += ch.len_utf8();
|
||||||
|
delim.push(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
first_char = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// pos is now right after the delimiter word — this is where
|
||||||
|
// the cursor should return so the rest of the line gets lexed
|
||||||
|
let cursor_after_delim = pos;
|
||||||
|
|
||||||
|
// Re-slice from cursor_after_delim so iterator and pos are in sync
|
||||||
|
// (the old chars iterator consumed the hard_sep without advancing pos)
|
||||||
|
let rest = self
|
||||||
|
.slice(cursor_after_delim..)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.to_string();
|
||||||
|
let mut chars = rest.chars();
|
||||||
|
|
||||||
|
// Scan forward to the newline (or use heredoc_skip from a previous heredoc)
|
||||||
|
let body_start = if let Some(skip) = self.heredoc_skip {
|
||||||
|
// A previous heredoc on this line already read its body;
|
||||||
|
// our body starts where that one ended
|
||||||
|
let skip_offset = skip - cursor_after_delim;
|
||||||
|
for _ in 0..skip_offset {
|
||||||
|
chars.next();
|
||||||
|
}
|
||||||
|
skip
|
||||||
|
} else {
|
||||||
|
// Skip the rest of the current line to find where the body begins
|
||||||
|
let mut scan = pos;
|
||||||
|
let mut found_newline = false;
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
scan += ch.len_utf8();
|
||||||
|
if ch == '\n' {
|
||||||
|
found_newline = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found_newline {
|
||||||
|
if self.flags.contains(LexFlags::LEX_UNFINISHED) {
|
||||||
|
return Ok(None);
|
||||||
|
} else {
|
||||||
|
return Err(ShErr::at(
|
||||||
|
ShErrKind::ParseErr,
|
||||||
|
Span::new(pos..pos, self.source.clone()),
|
||||||
|
"Heredoc delimiter not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scan
|
||||||
|
};
|
||||||
|
|
||||||
|
pos = body_start;
|
||||||
|
let start = pos;
|
||||||
|
|
||||||
|
// Read lines until we find one that matches the delimiter exactly
|
||||||
|
let mut line = String::new();
|
||||||
|
let mut line_start = pos;
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
pos += ch.len_utf8();
|
||||||
|
if ch == '\n' {
|
||||||
|
let trimmed = line.trim_end_matches('\r');
|
||||||
|
if trimmed == delim {
|
||||||
|
let mut tk = self.get_token(start..line_start, TkRule::Redir);
|
||||||
|
tk.flags |= TkFlags::IS_HEREDOC | flags;
|
||||||
|
self.heredoc_skip = Some(pos);
|
||||||
|
self.cursor = cursor_after_delim;
|
||||||
|
return Ok(Some(tk));
|
||||||
|
}
|
||||||
|
line.clear();
|
||||||
|
line_start = pos;
|
||||||
|
} else {
|
||||||
|
line.push(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check the last line (no trailing newline)
|
||||||
|
let trimmed = line.trim_end_matches('\r');
|
||||||
|
if trimmed == delim {
|
||||||
|
let mut tk = self.get_token(start..line_start, TkRule::Redir);
|
||||||
|
tk.flags |= TkFlags::IS_HEREDOC | flags;
|
||||||
|
self.heredoc_skip = Some(pos);
|
||||||
|
self.cursor = cursor_after_delim;
|
||||||
|
return Ok(Some(tk));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.flags.contains(LexFlags::LEX_UNFINISHED) {
|
||||||
|
Err(ShErr::at(
|
||||||
|
ShErrKind::ParseErr,
|
||||||
|
Span::new(start..pos, self.source.clone()),
|
||||||
|
format!("Heredoc delimiter '{}' not found", delim),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn read_string(&mut self) -> ShResult<Tk> {
|
pub fn read_string(&mut self) -> ShResult<Tk> {
|
||||||
assert!(self.cursor <= self.source.len());
|
assert!(self.cursor <= self.source.len());
|
||||||
let slice = self.slice_from_cursor().unwrap().to_string();
|
let slice = self.slice_from_cursor().unwrap().to_string();
|
||||||
@@ -453,7 +706,7 @@ impl LexStream {
|
|||||||
let mut chars = slice.chars().peekable();
|
let mut chars = slice.chars().peekable();
|
||||||
let can_be_subshell = chars.peek() == Some(&'(');
|
let can_be_subshell = chars.peek() == Some(&'(');
|
||||||
|
|
||||||
if self.flags.contains(LexFlags::IN_CASE)
|
if self.case_depth > 0
|
||||||
&& let Some(count) = case_pat_lookahead(chars.clone())
|
&& let Some(count) = case_pat_lookahead(chars.clone())
|
||||||
{
|
{
|
||||||
pos += count;
|
pos += count;
|
||||||
@@ -623,6 +876,16 @@ impl LexStream {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
'(' if can_be_subshell && chars.peek() == Some(&')') => {
|
||||||
|
// standalone "()" — function definition marker
|
||||||
|
pos += 2;
|
||||||
|
chars.next();
|
||||||
|
let mut tk = self.get_token(self.cursor..pos, TkRule::Str);
|
||||||
|
tk.mark(TkFlags::KEYWORD);
|
||||||
|
self.cursor = pos;
|
||||||
|
self.set_next_is_cmd(true);
|
||||||
|
return Ok(tk);
|
||||||
|
}
|
||||||
'(' if self.next_is_cmd() && can_be_subshell => {
|
'(' if self.next_is_cmd() && can_be_subshell => {
|
||||||
pos += 1;
|
pos += 1;
|
||||||
let mut paren_count = 1;
|
let mut paren_count = 1;
|
||||||
@@ -731,7 +994,7 @@ impl LexStream {
|
|||||||
"case" | "select" | "for" => {
|
"case" | "select" | "for" => {
|
||||||
new_tk.mark(TkFlags::KEYWORD);
|
new_tk.mark(TkFlags::KEYWORD);
|
||||||
self.flags |= LexFlags::EXPECTING_IN;
|
self.flags |= LexFlags::EXPECTING_IN;
|
||||||
self.flags |= LexFlags::IN_CASE;
|
self.case_depth += 1;
|
||||||
self.set_next_is_cmd(false);
|
self.set_next_is_cmd(false);
|
||||||
}
|
}
|
||||||
"in" if self.flags.contains(LexFlags::EXPECTING_IN) => {
|
"in" if self.flags.contains(LexFlags::EXPECTING_IN) => {
|
||||||
@@ -739,8 +1002,8 @@ impl LexStream {
|
|||||||
self.flags &= !LexFlags::EXPECTING_IN;
|
self.flags &= !LexFlags::EXPECTING_IN;
|
||||||
}
|
}
|
||||||
_ if is_keyword(text) => {
|
_ if is_keyword(text) => {
|
||||||
if text == "esac" && self.flags.contains(LexFlags::IN_CASE) {
|
if text == "esac" && self.case_depth > 0 {
|
||||||
self.flags &= !LexFlags::IN_CASE;
|
self.case_depth -= 1;
|
||||||
}
|
}
|
||||||
new_tk.mark(TkFlags::KEYWORD);
|
new_tk.mark(TkFlags::KEYWORD);
|
||||||
}
|
}
|
||||||
@@ -843,10 +1106,19 @@ impl Iterator for LexStream {
|
|||||||
|
|
||||||
let token = match get_char(&self.source, self.cursor).unwrap() {
|
let token = match get_char(&self.source, self.cursor).unwrap() {
|
||||||
'\r' | '\n' | ';' => {
|
'\r' | '\n' | ';' => {
|
||||||
|
let ch = get_char(&self.source, self.cursor).unwrap();
|
||||||
let ch_idx = self.cursor;
|
let ch_idx = self.cursor;
|
||||||
self.cursor += 1;
|
self.cursor += 1;
|
||||||
self.set_next_is_cmd(true);
|
self.set_next_is_cmd(true);
|
||||||
|
|
||||||
|
// If a heredoc was parsed on this line, skip past the body
|
||||||
|
// Only on newline — ';' is a command separator within the same line
|
||||||
|
if (ch == '\n' || ch == '\r')
|
||||||
|
&& let Some(skip) = self.heredoc_skip.take()
|
||||||
|
{
|
||||||
|
self.cursor = skip;
|
||||||
|
}
|
||||||
|
|
||||||
while let Some(ch) = get_char(&self.source, self.cursor) {
|
while let Some(ch) = get_char(&self.source, self.cursor) {
|
||||||
match ch {
|
match ch {
|
||||||
'\\' if get_char(&self.source, self.cursor + 1) == Some('\n') => {
|
'\\' if get_char(&self.source, self.cursor + 1) == Some('\n') => {
|
||||||
@@ -881,6 +1153,14 @@ impl Iterator for LexStream {
|
|||||||
return self.next();
|
return self.next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
'!' if self.next_is_cmd() => {
|
||||||
|
self.cursor += 1;
|
||||||
|
let tk_type = TkRule::Bang;
|
||||||
|
|
||||||
|
let mut tk = self.get_token((self.cursor - 1)..self.cursor, tk_type);
|
||||||
|
tk.flags |= TkFlags::KEYWORD;
|
||||||
|
tk
|
||||||
|
}
|
||||||
'|' => {
|
'|' => {
|
||||||
let ch_idx = self.cursor;
|
let ch_idx = self.cursor;
|
||||||
self.cursor += 1;
|
self.cursor += 1;
|
||||||
|
|||||||
1238
src/parse/mod.rs
1238
src/parse/mod.rs
File diff suppressed because one or more lines are too long
@@ -19,7 +19,7 @@ pub use std::os::unix::io::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd,
|
|||||||
pub use bitflags::bitflags;
|
pub use bitflags::bitflags;
|
||||||
pub use nix::{
|
pub use nix::{
|
||||||
errno::Errno,
|
errno::Errno,
|
||||||
fcntl::{OFlag, open},
|
fcntl::{FcntlArg, OFlag, fcntl, open},
|
||||||
libc::{self, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO},
|
libc::{self, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO},
|
||||||
sys::{
|
sys::{
|
||||||
signal::{self, SigHandler, SigSet, SigmaskHow, Signal, kill, killpg, pthread_sigmask, signal},
|
signal::{self, SigHandler, SigSet, SigmaskHow, Signal, kill, killpg, pthread_sigmask, signal},
|
||||||
@@ -33,5 +33,4 @@ pub use nix::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Additional utilities, if needed, can be added here
|
// Additional utilities, if needed, can be added here
|
||||||
|
|||||||
188
src/procio.rs
188
src/procio.rs
@@ -8,15 +8,27 @@ use crate::{
|
|||||||
expand::Expander,
|
expand::Expander,
|
||||||
libsh::{
|
libsh::{
|
||||||
error::{ShErr, ShErrKind, ShResult},
|
error::{ShErr, ShErrKind, ShResult},
|
||||||
|
sys::TTY_FILENO,
|
||||||
utils::RedirVecUtils,
|
utils::RedirVecUtils,
|
||||||
},
|
},
|
||||||
parse::{Redir, RedirType, get_redir_file},
|
parse::{Redir, RedirType, get_redir_file, lex::TkFlags},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
state,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Credit to fish-shell for many of the implementation ideas present in this
|
// Credit to fish-shell for many of the implementation ideas present in this
|
||||||
// module https://fishshell.com/
|
// module https://fishshell.com/
|
||||||
|
|
||||||
|
/// Minimum fd number for shell-internal file descriptors.
|
||||||
|
/// User-visible fds (0-9) are kept clear so `exec 3>&-` etc. work as expected.
|
||||||
|
const MIN_INTERNAL_FD: RawFd = 10;
|
||||||
|
|
||||||
|
/// Like `dup()`, but places the new fd at `MIN_INTERNAL_FD` or above so it
|
||||||
|
/// doesn't collide with user-managed fds.
|
||||||
|
fn dup_high(fd: RawFd) -> nix::Result<RawFd> {
|
||||||
|
fcntl(fd, FcntlArg::F_DUPFD_CLOEXEC(MIN_INTERNAL_FD))
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum IoMode {
|
pub enum IoMode {
|
||||||
Fd {
|
Fd {
|
||||||
@@ -37,8 +49,9 @@ pub enum IoMode {
|
|||||||
pipe: Arc<OwnedFd>,
|
pipe: Arc<OwnedFd>,
|
||||||
},
|
},
|
||||||
Buffer {
|
Buffer {
|
||||||
|
tgt_fd: RawFd,
|
||||||
buf: String,
|
buf: String,
|
||||||
pipe: Arc<OwnedFd>,
|
flags: TkFlags, // so we can see if its a heredoc or not
|
||||||
},
|
},
|
||||||
Close {
|
Close {
|
||||||
tgt_fd: RawFd,
|
tgt_fd: RawFd,
|
||||||
@@ -79,19 +92,37 @@ impl IoMode {
|
|||||||
if let IoMode::File { tgt_fd, path, mode } = self {
|
if let IoMode::File { tgt_fd, path, mode } = self {
|
||||||
let path_raw = path.as_os_str().to_str().unwrap_or_default().to_string();
|
let path_raw = path.as_os_str().to_str().unwrap_or_default().to_string();
|
||||||
|
|
||||||
let expanded_path = Expander::from_raw(&path_raw)?.expand()?.join(" "); // should just be one string, will have to find some way to handle a return of
|
let expanded_path = Expander::from_raw(&path_raw, TkFlags::empty())?
|
||||||
// multiple
|
.expand()?
|
||||||
|
.join(" "); // should just be one string, will have to find some way to handle a return of multiple paths
|
||||||
|
|
||||||
let expanded_pathbuf = PathBuf::from(expanded_path);
|
let expanded_pathbuf = PathBuf::from(expanded_path);
|
||||||
|
|
||||||
let file = get_redir_file(mode, expanded_pathbuf)?;
|
let file = get_redir_file(mode, expanded_pathbuf)?;
|
||||||
|
// Move the opened fd above the user-accessible range so it never
|
||||||
|
// collides with the target fd (e.g. `3>/tmp/foo` where open() returns 3,
|
||||||
|
// causing dup2(3,3) to be a no-op and then OwnedFd drop closes it).
|
||||||
|
let raw = file.as_raw_fd();
|
||||||
|
let high = fcntl(raw, FcntlArg::F_DUPFD_CLOEXEC(MIN_INTERNAL_FD)).map_err(ShErr::from)?;
|
||||||
|
drop(file); // closes the original low fd
|
||||||
self = IoMode::OpenedFile {
|
self = IoMode::OpenedFile {
|
||||||
tgt_fd,
|
tgt_fd,
|
||||||
file: Arc::new(OwnedFd::from(file)),
|
file: Arc::new(unsafe { OwnedFd::from_raw_fd(high) }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
pub fn buffer(tgt_fd: RawFd, buf: String, flags: TkFlags) -> ShResult<Self> {
|
||||||
|
Ok(Self::Buffer { tgt_fd, buf, flags })
|
||||||
|
}
|
||||||
|
pub fn loaded_pipe(tgt_fd: RawFd, buf: &[u8]) -> ShResult<Self> {
|
||||||
|
let (rpipe, wpipe) = nix::unistd::pipe()?;
|
||||||
|
write(wpipe, buf)?;
|
||||||
|
Ok(Self::Pipe {
|
||||||
|
tgt_fd,
|
||||||
|
pipe: rpipe.into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
pub fn get_pipes() -> (Self, Self) {
|
pub fn get_pipes() -> (Self, Self) {
|
||||||
let (rpipe, wpipe) = nix::unistd::pipe2(OFlag::O_CLOEXEC).unwrap();
|
let (rpipe, wpipe) = nix::unistd::pipe2(OFlag::O_CLOEXEC).unwrap();
|
||||||
(
|
(
|
||||||
@@ -206,24 +237,107 @@ impl<'e> IoFrame {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
pub fn save(&'e mut self) {
|
pub fn save(&'e mut self) {
|
||||||
let saved_in = dup(STDIN_FILENO).unwrap();
|
let saved_in = dup_high(STDIN_FILENO).unwrap();
|
||||||
let saved_out = dup(STDOUT_FILENO).unwrap();
|
let saved_out = dup_high(STDOUT_FILENO).unwrap();
|
||||||
let saved_err = dup(STDERR_FILENO).unwrap();
|
let saved_err = dup_high(STDERR_FILENO).unwrap();
|
||||||
self.saved_io = Some(IoGroup(saved_in, saved_out, saved_err));
|
self.saved_io = Some(IoGroup(saved_in, saved_out, saved_err));
|
||||||
}
|
}
|
||||||
pub fn redirect(mut self) -> ShResult<RedirGuard> {
|
pub fn redirect(mut self) -> ShResult<RedirGuard> {
|
||||||
self.save();
|
self.save();
|
||||||
for redir in &mut self.redirs {
|
if let Err(e) = self.apply_redirs() {
|
||||||
let io_mode = &mut redir.io_mode;
|
// Restore saved fds before propagating the error so they don't leak.
|
||||||
if let IoMode::File { .. } = io_mode {
|
self.restore().ok();
|
||||||
*io_mode = io_mode.clone().open_file()?;
|
return Err(e);
|
||||||
};
|
|
||||||
let tgt_fd = io_mode.tgt_fd();
|
|
||||||
let src_fd = io_mode.src_fd();
|
|
||||||
dup2(src_fd, tgt_fd)?;
|
|
||||||
}
|
}
|
||||||
Ok(RedirGuard::new(self))
|
Ok(RedirGuard::new(self))
|
||||||
}
|
}
|
||||||
|
fn apply_redirs(&mut self) -> ShResult<()> {
|
||||||
|
for redir in &mut self.redirs {
|
||||||
|
let io_mode = &mut redir.io_mode;
|
||||||
|
match io_mode {
|
||||||
|
IoMode::Close { tgt_fd } => {
|
||||||
|
if *tgt_fd == *TTY_FILENO {
|
||||||
|
// Don't let user close the shell's tty fd.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
close(*tgt_fd).ok();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
IoMode::File { .. } => match io_mode.clone().open_file() {
|
||||||
|
Ok(file) => *io_mode = file,
|
||||||
|
Err(e) => {
|
||||||
|
if let Some(span) = redir.span.as_ref() {
|
||||||
|
return Err(e.promote(span.clone()));
|
||||||
|
}
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
IoMode::Buffer { tgt_fd, buf, flags } => {
|
||||||
|
let (rpipe, wpipe) = nix::unistd::pipe()?;
|
||||||
|
let mut text = if flags.contains(TkFlags::LIT_HEREDOC) {
|
||||||
|
buf.clone()
|
||||||
|
} else {
|
||||||
|
let words = Expander::from_raw(buf, *flags)?.expand()?;
|
||||||
|
if flags.contains(TkFlags::IS_HEREDOC) {
|
||||||
|
words.into_iter().next().unwrap_or_default()
|
||||||
|
} else {
|
||||||
|
let ifs = state::get_separator();
|
||||||
|
words.join(&ifs).trim().to_string() + "\n"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if flags.contains(TkFlags::TAB_HEREDOC) {
|
||||||
|
let lines = text.lines();
|
||||||
|
let mut min_tabs = usize::MAX;
|
||||||
|
for line in lines {
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let line_len = line.len();
|
||||||
|
let after_strip = line.trim_start_matches('\t').len();
|
||||||
|
let delta = line_len - after_strip;
|
||||||
|
min_tabs = min_tabs.min(delta);
|
||||||
|
}
|
||||||
|
if min_tabs == usize::MAX {
|
||||||
|
// let's avoid possibly allocating a string with 18 quintillion tabs
|
||||||
|
min_tabs = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if min_tabs > 0 {
|
||||||
|
let stripped = text
|
||||||
|
.lines()
|
||||||
|
.fold(vec![], |mut acc, ln| {
|
||||||
|
if ln.is_empty() {
|
||||||
|
acc.push("");
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
let stripped_ln = ln.strip_prefix(&"\t".repeat(min_tabs)).unwrap();
|
||||||
|
acc.push(stripped_ln);
|
||||||
|
acc
|
||||||
|
})
|
||||||
|
.join("\n");
|
||||||
|
text = stripped + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write(wpipe, text.as_bytes())?;
|
||||||
|
*io_mode = IoMode::Pipe {
|
||||||
|
tgt_fd: *tgt_fd,
|
||||||
|
pipe: rpipe.into(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
let tgt_fd = io_mode.tgt_fd();
|
||||||
|
let src_fd = io_mode.src_fd();
|
||||||
|
if let Err(e) = dup2(src_fd, tgt_fd) {
|
||||||
|
if let Some(span) = redir.span.as_ref() {
|
||||||
|
return Err(ShErr::from(e).promote(span.clone()));
|
||||||
|
} else {
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
pub fn restore(&mut self) -> ShResult<()> {
|
pub fn restore(&mut self) -> ShResult<()> {
|
||||||
if let Some(saved) = self.saved_io.take() {
|
if let Some(saved) = self.saved_io.take() {
|
||||||
dup2(saved.0, STDIN_FILENO)?;
|
dup2(saved.0, STDIN_FILENO)?;
|
||||||
@@ -334,6 +448,8 @@ pub fn borrow_fd<'f>(fd: i32) -> BorrowedFd<'f> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PipeFrames = Map<PipeGenerator, fn((Option<Redir>, Option<Redir>)) -> IoFrame>;
|
type PipeFrames = Map<PipeGenerator, fn((Option<Redir>, Option<Redir>)) -> IoFrame>;
|
||||||
|
|
||||||
|
/// An iterator that lazily creates a specific number of pipes.
|
||||||
pub struct PipeGenerator {
|
pub struct PipeGenerator {
|
||||||
num_cmds: usize,
|
num_cmds: usize,
|
||||||
cursor: usize,
|
cursor: usize,
|
||||||
@@ -394,7 +510,9 @@ pub mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pipeline_simple() {
|
fn pipeline_simple() {
|
||||||
if !has_cmd("sed") { return };
|
if !has_cmd("sed") {
|
||||||
|
return;
|
||||||
|
};
|
||||||
let g = TestGuard::new();
|
let g = TestGuard::new();
|
||||||
|
|
||||||
test_input("echo foo | sed 's/foo/bar/'").unwrap();
|
test_input("echo foo | sed 's/foo/bar/'").unwrap();
|
||||||
@@ -405,10 +523,9 @@ pub mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pipeline_multi() {
|
fn pipeline_multi() {
|
||||||
if !has_cmds(&[
|
if !has_cmds(&["cut", "sed"]) {
|
||||||
"cut",
|
return;
|
||||||
"sed"
|
}
|
||||||
]) { return; }
|
|
||||||
let g = TestGuard::new();
|
let g = TestGuard::new();
|
||||||
|
|
||||||
test_input("echo foo bar baz | cut -d ' ' -f 2 | sed 's/a/A/'").unwrap();
|
test_input("echo foo bar baz | cut -d ' ' -f 2 | sed 's/a/A/'").unwrap();
|
||||||
@@ -419,10 +536,9 @@ pub mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rube_goldberg_pipeline() {
|
fn rube_goldberg_pipeline() {
|
||||||
if !has_cmds(&[
|
if !has_cmds(&["sed", "cat"]) {
|
||||||
"sed",
|
return;
|
||||||
"cat",
|
}
|
||||||
]) { return }
|
|
||||||
let g = TestGuard::new();
|
let g = TestGuard::new();
|
||||||
|
|
||||||
test_input("{ echo foo; echo bar } | if cat; then :; else echo failed; fi | (read line && echo $line | sed 's/foo/baz/'; sed 's/bar/buzz/')").unwrap();
|
test_input("{ echo foo; echo bar } | if cat; then :; else echo failed; fi | (read line && echo $line | sed 's/foo/baz/'; sed 's/bar/buzz/')").unwrap();
|
||||||
@@ -437,7 +553,9 @@ pub mod tests {
|
|||||||
|
|
||||||
test_input("echo this is in a file > /tmp/simple_file_redir.txt").unwrap();
|
test_input("echo this is in a file > /tmp/simple_file_redir.txt").unwrap();
|
||||||
|
|
||||||
g.add_cleanup(|| { std::fs::remove_file("/tmp/simple_file_redir.txt").ok(); });
|
g.add_cleanup(|| {
|
||||||
|
std::fs::remove_file("/tmp/simple_file_redir.txt").ok();
|
||||||
|
});
|
||||||
let contents = std::fs::read_to_string("/tmp/simple_file_redir.txt").unwrap();
|
let contents = std::fs::read_to_string("/tmp/simple_file_redir.txt").unwrap();
|
||||||
|
|
||||||
assert_eq!(contents, "this is in a file\n");
|
assert_eq!(contents, "this is in a file\n");
|
||||||
@@ -458,7 +576,9 @@ pub mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn input_redir() {
|
fn input_redir() {
|
||||||
if !has_cmd("cat") { return; }
|
if !has_cmd("cat") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let dir = tempfile::TempDir::new().unwrap();
|
let dir = tempfile::TempDir::new().unwrap();
|
||||||
let path = dir.path().join("input.txt");
|
let path = dir.path().join("input.txt");
|
||||||
std::fs::write(&path, "hello from file\n").unwrap();
|
std::fs::write(&path, "hello from file\n").unwrap();
|
||||||
@@ -487,7 +607,9 @@ pub mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pipe_and_stderr() {
|
fn pipe_and_stderr() {
|
||||||
if !has_cmd("cat") { return; }
|
if !has_cmd("cat") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let g = TestGuard::new();
|
let g = TestGuard::new();
|
||||||
|
|
||||||
test_input("echo on stderr >&2 |& cat").unwrap();
|
test_input("echo on stderr >&2 |& cat").unwrap();
|
||||||
@@ -511,7 +633,9 @@ pub mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pipeline_preserves_exit_status() {
|
fn pipeline_preserves_exit_status() {
|
||||||
if !has_cmd("cat") { return; }
|
if !has_cmd("cat") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let _g = TestGuard::new();
|
let _g = TestGuard::new();
|
||||||
|
|
||||||
test_input("false | cat").unwrap();
|
test_input("false | cat").unwrap();
|
||||||
@@ -533,7 +657,11 @@ pub mod tests {
|
|||||||
let _g = TestGuard::new();
|
let _g = TestGuard::new();
|
||||||
|
|
||||||
// Redirect stdout to file, then dup stderr to stdout — both should go to file
|
// Redirect stdout to file, then dup stderr to stdout — both should go to file
|
||||||
test_input(format!("{{ echo out; echo err >&2 }} > {} 2>&1", path.display())).unwrap();
|
test_input(format!(
|
||||||
|
"{{ echo out; echo err >&2 }} > {} 2>&1",
|
||||||
|
path.display()
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let contents = std::fs::read_to_string(&path).unwrap();
|
let contents = std::fs::read_to_string(&path).unwrap();
|
||||||
assert!(contents.contains("out"));
|
assert!(contents.contains("out"));
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -88,7 +88,9 @@ impl Highlighter {
|
|||||||
while prefix_chars.peek().is_some() {
|
while prefix_chars.peek().is_some() {
|
||||||
match chars.next() {
|
match chars.next() {
|
||||||
Some(c) if c == markers::VISUAL_MODE_START || c == markers::VISUAL_MODE_END => continue,
|
Some(c) if c == markers::VISUAL_MODE_START || c == markers::VISUAL_MODE_END => continue,
|
||||||
Some(c) if Some(&c) == prefix_chars.peek() => { prefix_chars.next(); }
|
Some(c) if Some(&c) == prefix_chars.peek() => {
|
||||||
|
prefix_chars.next();
|
||||||
|
}
|
||||||
_ => return text.to_string(), // mismatch, return original
|
_ => return text.to_string(), // mismatch, return original
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,7 +106,9 @@ impl Highlighter {
|
|||||||
let mut si = suffix_chars.len();
|
let mut si = suffix_chars.len();
|
||||||
|
|
||||||
while si > 0 {
|
while si > 0 {
|
||||||
if ti == 0 { return text.to_string(); }
|
if ti == 0 {
|
||||||
|
return text.to_string();
|
||||||
|
}
|
||||||
ti -= 1;
|
ti -= 1;
|
||||||
if chars[ti] == markers::VISUAL_MODE_START || chars[ti] == markers::VISUAL_MODE_END {
|
if chars[ti] == markers::VISUAL_MODE_START || chars[ti] == markers::VISUAL_MODE_END {
|
||||||
continue; // skip visual markers
|
continue; // skip visual markers
|
||||||
@@ -346,7 +350,9 @@ impl Highlighter {
|
|||||||
recursive_highlighter.highlight();
|
recursive_highlighter.highlight();
|
||||||
// Read back visual state — selection may have started/ended inside
|
// Read back visual state — selection may have started/ended inside
|
||||||
self.in_selection = recursive_highlighter.in_selection;
|
self.in_selection = recursive_highlighter.in_selection;
|
||||||
self.style_stack.append(&mut recursive_highlighter.style_stack);
|
self
|
||||||
|
.style_stack
|
||||||
|
.append(&mut recursive_highlighter.style_stack);
|
||||||
if selection_at_entry {
|
if selection_at_entry {
|
||||||
self.emit_style(Style::BgWhite | Style::Black);
|
self.emit_style(Style::BgWhite | Style::Black);
|
||||||
self.output.push_str(prefix);
|
self.output.push_str(prefix);
|
||||||
|
|||||||
@@ -203,6 +203,7 @@ fn dedupe_entries(entries: &[HistEntry]) -> Vec<HistEntry> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Clone, Debug)]
|
||||||
pub struct History {
|
pub struct History {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
pub pending: Option<LineBuf>, // command, cursor_pos
|
pub pending: Option<LineBuf>, // command, cursor_pos
|
||||||
@@ -214,21 +215,41 @@ pub struct History {
|
|||||||
//search_direction: Direction,
|
//search_direction: Direction,
|
||||||
ignore_dups: bool,
|
ignore_dups: bool,
|
||||||
max_size: Option<u32>,
|
max_size: Option<u32>,
|
||||||
|
stateless: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl History {
|
impl History {
|
||||||
|
pub fn empty() -> Self {
|
||||||
|
Self {
|
||||||
|
path: PathBuf::new(),
|
||||||
|
pending: None,
|
||||||
|
entries: Vec::new(),
|
||||||
|
search_mask: Vec::new(),
|
||||||
|
fuzzy_finder: FuzzySelector::new("History").number_candidates(true),
|
||||||
|
no_matches: false,
|
||||||
|
cursor: 0,
|
||||||
|
//search_direction: Direction::Backward,
|
||||||
|
ignore_dups: false,
|
||||||
|
max_size: None,
|
||||||
|
stateless: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn new() -> ShResult<Self> {
|
pub fn new() -> ShResult<Self> {
|
||||||
let ignore_dups = crate::state::read_shopts(|s| s.core.hist_ignore_dupes);
|
let ignore_dups = crate::state::read_shopts(|s| s.core.hist_ignore_dupes);
|
||||||
let max_hist = crate::state::read_shopts(|s| s.core.max_hist);
|
let max_hist = crate::state::read_shopts(|s| s.core.max_hist);
|
||||||
|
|
||||||
let path = PathBuf::from(env::var("SHEDHIST").unwrap_or({
|
let path = PathBuf::from(env::var("SHEDHIST").unwrap_or({
|
||||||
let home = env::var("HOME").unwrap();
|
let home = env::var("HOME").unwrap();
|
||||||
format!("{home}/.shed_history")
|
format!("{home}/.shed_history")
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let mut entries = read_hist_file(&path)?;
|
let mut entries = read_hist_file(&path)?;
|
||||||
|
|
||||||
// Enforce max_hist limit on loaded entries (negative = unlimited)
|
// Enforce max_hist limit on loaded entries (negative = unlimited)
|
||||||
if max_hist >= 0 && entries.len() > max_hist as usize {
|
if max_hist >= 0 && entries.len() > max_hist as usize {
|
||||||
entries = entries.split_off(entries.len() - max_hist as usize);
|
entries = entries.split_off(entries.len() - max_hist as usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
let search_mask = dedupe_entries(&entries);
|
let search_mask = dedupe_entries(&entries);
|
||||||
let cursor = search_mask.len();
|
let cursor = search_mask.len();
|
||||||
let max_size = if max_hist < 0 {
|
let max_size = if max_hist < 0 {
|
||||||
@@ -236,6 +257,7 @@ impl History {
|
|||||||
} else {
|
} else {
|
||||||
Some(max_hist as u32)
|
Some(max_hist as u32)
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
path,
|
path,
|
||||||
entries,
|
entries,
|
||||||
@@ -247,6 +269,7 @@ impl History {
|
|||||||
//search_direction: Direction::Backward,
|
//search_direction: Direction::Backward,
|
||||||
ignore_dups,
|
ignore_dups,
|
||||||
max_size,
|
max_size,
|
||||||
|
stateless: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,7 +284,7 @@ impl History {
|
|||||||
.search_mask
|
.search_mask
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|ent| ent.command().to_string());
|
.map(|ent| super::complete::Candidate::from(ent.command()));
|
||||||
self.fuzzy_finder.activate(raw_entries.collect());
|
self.fuzzy_finder.activate(raw_entries.collect());
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -294,7 +317,7 @@ impl History {
|
|||||||
|
|
||||||
pub fn update_pending_cmd(&mut self, buf: (&str, usize)) {
|
pub fn update_pending_cmd(&mut self, buf: (&str, usize)) {
|
||||||
let cursor_pos = if let Some(pending) = &self.pending {
|
let cursor_pos = if let Some(pending) = &self.pending {
|
||||||
pending.cursor.get()
|
pending.cursor_to_flat()
|
||||||
} else {
|
} else {
|
||||||
buf.1
|
buf.1
|
||||||
};
|
};
|
||||||
@@ -306,7 +329,7 @@ impl History {
|
|||||||
|
|
||||||
if let Some(pending) = &mut self.pending {
|
if let Some(pending) = &mut self.pending {
|
||||||
pending.set_buffer(cmd);
|
pending.set_buffer(cmd);
|
||||||
pending.cursor.set(cursor_pos);
|
pending.set_cursor_from_flat(cursor_pos);
|
||||||
} else {
|
} else {
|
||||||
self.pending = Some(LineBuf::new().with_initial(&cmd, cursor_pos));
|
self.pending = Some(LineBuf::new().with_initial(&cmd, cursor_pos));
|
||||||
}
|
}
|
||||||
@@ -391,7 +414,12 @@ impl History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_hint(&self) -> Option<String> {
|
pub fn get_hint(&self) -> Option<String> {
|
||||||
if self.at_pending() && self.pending.as_ref().is_some_and(|p| !p.buffer.is_empty()) {
|
if self.at_pending()
|
||||||
|
&& self
|
||||||
|
.pending
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|p| !p.joined().is_empty())
|
||||||
|
{
|
||||||
let entry = self.hint_entry()?;
|
let entry = self.hint_entry()?;
|
||||||
Some(entry.command().to_string())
|
Some(entry.command().to_string())
|
||||||
} else {
|
} else {
|
||||||
@@ -431,6 +459,9 @@ impl History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&mut self) -> ShResult<()> {
|
pub fn save(&mut self) -> ShResult<()> {
|
||||||
|
if self.stateless {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.create(true)
|
.create(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
@@ -481,12 +512,8 @@ mod tests {
|
|||||||
env::set_var(key, val);
|
env::set_var(key, val);
|
||||||
}
|
}
|
||||||
guard(prev, move |p| match p {
|
guard(prev, move |p| match p {
|
||||||
Some(v) => unsafe {
|
Some(v) => unsafe { env::set_var(key, v) },
|
||||||
env::set_var(key, v)
|
None => unsafe { env::remove_var(key) },
|
||||||
},
|
|
||||||
None => unsafe {
|
|
||||||
env::remove_var(key)
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -503,12 +530,7 @@ mod tests {
|
|||||||
fn write_history_file(path: &Path) {
|
fn write_history_file(path: &Path) {
|
||||||
fs::write(
|
fs::write(
|
||||||
path,
|
path,
|
||||||
[
|
[": 1;1;first\n", ": 2;1;second\n", ": 3;1;third\n"].concat(),
|
||||||
": 1;1;first\n",
|
|
||||||
": 2;1;second\n",
|
|
||||||
": 3;1;third\n",
|
|
||||||
]
|
|
||||||
.concat(),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
@@ -567,12 +589,7 @@ mod tests {
|
|||||||
let hist_path = tmp.path().join("history");
|
let hist_path = tmp.path().join("history");
|
||||||
fs::write(
|
fs::write(
|
||||||
&hist_path,
|
&hist_path,
|
||||||
[
|
[": 1;1;repeat\n", ": 2;1;unique\n", ": 3;1;repeat\n"].concat(),
|
||||||
": 1;1;repeat\n",
|
|
||||||
": 2;1;unique\n",
|
|
||||||
": 3;1;repeat\n",
|
|
||||||
]
|
|
||||||
.concat(),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,27 @@
|
|||||||
use std::{fmt::Display, sync::Mutex};
|
use std::{fmt::Display, sync::Mutex};
|
||||||
|
|
||||||
|
use crate::readline::linebuf::Line;
|
||||||
|
|
||||||
pub static REGISTERS: Mutex<Registers> = Mutex::new(Registers::new());
|
pub static REGISTERS: Mutex<Registers> = Mutex::new(Registers::new());
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub static SAVED_REGISTERS: Mutex<Option<Registers>> = Mutex::new(None);
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn save_registers() {
|
||||||
|
let mut saved = SAVED_REGISTERS.lock().unwrap();
|
||||||
|
*saved = Some(REGISTERS.lock().unwrap().clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn restore_registers() {
|
||||||
|
let mut saved = SAVED_REGISTERS.lock().unwrap();
|
||||||
|
if let Some(ref registers) = *saved {
|
||||||
|
*REGISTERS.lock().unwrap() = registers.clone();
|
||||||
|
}
|
||||||
|
*saved = None;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn read_register(ch: Option<char>) -> Option<RegisterContent> {
|
pub fn read_register(ch: Option<char>) -> Option<RegisterContent> {
|
||||||
let lock = REGISTERS.lock().unwrap();
|
let lock = REGISTERS.lock().unwrap();
|
||||||
lock.get_reg(ch).map(|r| r.content().clone())
|
lock.get_reg(ch).map(|r| r.content().clone())
|
||||||
@@ -23,8 +43,9 @@ pub fn append_register(ch: Option<char>, buf: RegisterContent) {
|
|||||||
|
|
||||||
#[derive(Default, Clone, Debug)]
|
#[derive(Default, Clone, Debug)]
|
||||||
pub enum RegisterContent {
|
pub enum RegisterContent {
|
||||||
Span(String),
|
Span(Vec<Line>),
|
||||||
Line(String),
|
Line(Vec<Line>),
|
||||||
|
Block(Vec<Line>),
|
||||||
#[default]
|
#[default]
|
||||||
Empty,
|
Empty,
|
||||||
}
|
}
|
||||||
@@ -32,8 +53,16 @@ pub enum RegisterContent {
|
|||||||
impl Display for RegisterContent {
|
impl Display for RegisterContent {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Span(s) => write!(f, "{}", s),
|
Self::Block(s) | Self::Line(s) | Self::Span(s) => {
|
||||||
Self::Line(s) => write!(f, "{}", s),
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
s.iter()
|
||||||
|
.map(|l| l.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n")
|
||||||
|
)
|
||||||
|
}
|
||||||
Self::Empty => write!(f, ""),
|
Self::Empty => write!(f, ""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,16 +70,11 @@ impl Display for RegisterContent {
|
|||||||
|
|
||||||
impl RegisterContent {
|
impl RegisterContent {
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
match self {
|
*self = Self::Empty
|
||||||
Self::Span(s) => s.clear(),
|
|
||||||
Self::Line(s) => s.clear(),
|
|
||||||
Self::Empty => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
match self {
|
match self {
|
||||||
Self::Span(s) => s.len(),
|
Self::Span(s) | Self::Line(s) | Self::Block(s) => s.len(),
|
||||||
Self::Line(s) => s.len(),
|
|
||||||
Self::Empty => 0,
|
Self::Empty => 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,28 +82,25 @@ impl RegisterContent {
|
|||||||
match self {
|
match self {
|
||||||
Self::Span(s) => s.is_empty(),
|
Self::Span(s) => s.is_empty(),
|
||||||
Self::Line(s) => s.is_empty(),
|
Self::Line(s) => s.is_empty(),
|
||||||
|
Self::Block(s) => s.is_empty(),
|
||||||
Self::Empty => true,
|
Self::Empty => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn is_block(&self) -> bool {
|
||||||
|
matches!(self, Self::Block(_))
|
||||||
|
}
|
||||||
pub fn is_line(&self) -> bool {
|
pub fn is_line(&self) -> bool {
|
||||||
matches!(self, Self::Line(_))
|
matches!(self, Self::Line(_))
|
||||||
}
|
}
|
||||||
pub fn is_span(&self) -> bool {
|
pub fn is_span(&self) -> bool {
|
||||||
matches!(self, Self::Span(_))
|
matches!(self, Self::Span(_))
|
||||||
}
|
}
|
||||||
pub fn as_str(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
Self::Span(s) => s,
|
|
||||||
Self::Line(s) => s,
|
|
||||||
Self::Empty => "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn char_count(&self) -> usize {
|
pub fn char_count(&self) -> usize {
|
||||||
self.as_str().chars().count()
|
self.to_string().chars().count()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Clone, Debug)]
|
||||||
pub struct Registers {
|
pub struct Registers {
|
||||||
default: Register,
|
default: Register,
|
||||||
a: Register,
|
a: Register,
|
||||||
@@ -220,7 +241,7 @@ pub struct Register {
|
|||||||
impl Register {
|
impl Register {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
content: RegisterContent::Span(String::new()),
|
content: RegisterContent::Empty,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn content(&self) -> &RegisterContent {
|
pub fn content(&self) -> &RegisterContent {
|
||||||
@@ -229,13 +250,16 @@ impl Register {
|
|||||||
pub fn write(&mut self, buf: RegisterContent) {
|
pub fn write(&mut self, buf: RegisterContent) {
|
||||||
self.content = buf
|
self.content = buf
|
||||||
}
|
}
|
||||||
pub fn append(&mut self, buf: RegisterContent) {
|
pub fn append(&mut self, mut buf: RegisterContent) {
|
||||||
match buf {
|
match buf {
|
||||||
RegisterContent::Empty => {}
|
RegisterContent::Empty => {}
|
||||||
RegisterContent::Span(ref s) | RegisterContent::Line(ref s) => match &mut self.content {
|
RegisterContent::Span(ref mut s)
|
||||||
|
| RegisterContent::Block(ref mut s)
|
||||||
|
| RegisterContent::Line(ref mut s) => match &mut self.content {
|
||||||
RegisterContent::Empty => self.content = buf,
|
RegisterContent::Empty => self.content = buf,
|
||||||
RegisterContent::Span(existing) => existing.push_str(s),
|
RegisterContent::Span(existing)
|
||||||
RegisterContent::Line(existing) => existing.push_str(s),
|
| RegisterContent::Line(existing)
|
||||||
|
| RegisterContent::Block(existing) => existing.append(s),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,17 +69,19 @@ pub fn get_win_size(fd: RawFd) -> (Col, Row) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enumerate_lines(s: &str, left_pad: usize, show_numbers: bool) -> String {
|
fn enumerate_lines(s: &str, left_pad: usize, show_numbers: bool, offset: usize, _total_buf_lines: usize) -> String {
|
||||||
let total_lines = s.lines().count();
|
let lines: Vec<&str> = s.split('\n').collect();
|
||||||
let max_num_len = total_lines.to_string().len();
|
let visible_count = lines.len();
|
||||||
s.lines()
|
let max_num_len = (offset + visible_count).to_string().len();
|
||||||
|
lines
|
||||||
|
.into_iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.fold(String::new(), |mut acc, (i, ln)| {
|
.fold(String::new(), |mut acc, (i, ln)| {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
acc.push_str(ln);
|
acc.push_str(ln);
|
||||||
acc.push('\n');
|
acc.push('\n');
|
||||||
} else {
|
} else {
|
||||||
let num = (i + 1).to_string();
|
let num = (i + offset + 1).to_string();
|
||||||
let num_pad = max_num_len - num.len();
|
let num_pad = max_num_len - num.len();
|
||||||
// " 2 | " — num + padding + " | "
|
// " 2 | " — num + padding + " | "
|
||||||
let prefix_len = max_num_len + 3; // "N | "
|
let prefix_len = max_num_len + 3; // "N | "
|
||||||
@@ -89,7 +91,7 @@ fn enumerate_lines(s: &str, left_pad: usize, show_numbers: bool) -> String {
|
|||||||
} else {
|
} else {
|
||||||
" ".repeat(prefix_len + 1).to_string()
|
" ".repeat(prefix_len + 1).to_string()
|
||||||
};
|
};
|
||||||
if i == total_lines - 1 {
|
if i == visible_count - 1 {
|
||||||
write!(acc, "{prefix}{}{ln}", " ".repeat(trail_pad)).unwrap();
|
write!(acc, "{prefix}{}{ln}", " ".repeat(trail_pad)).unwrap();
|
||||||
} else {
|
} else {
|
||||||
writeln!(acc, "{prefix}{}{ln}", " ".repeat(trail_pad)).unwrap();
|
writeln!(acc, "{prefix}{}{ln}", " ".repeat(trail_pad)).unwrap();
|
||||||
@@ -218,7 +220,7 @@ pub trait KeyReader {
|
|||||||
|
|
||||||
pub trait LineWriter {
|
pub trait LineWriter {
|
||||||
fn clear_rows(&mut self, layout: &Layout) -> ShResult<()>;
|
fn clear_rows(&mut self, layout: &Layout) -> ShResult<()>;
|
||||||
fn redraw(&mut self, prompt: &str, line: &str, new_layout: &Layout) -> ShResult<()>;
|
fn redraw(&mut self, prompt: &str, line: &str, new_layout: &Layout, offset: usize, total_buf_lines: usize) -> ShResult<()>;
|
||||||
fn flush_write(&mut self, buf: &str) -> ShResult<()>;
|
fn flush_write(&mut self, buf: &str) -> ShResult<()>;
|
||||||
fn send_bell(&mut self) -> ShResult<()>;
|
fn send_bell(&mut self) -> ShResult<()>;
|
||||||
}
|
}
|
||||||
@@ -294,12 +296,14 @@ impl Read for TermBuffer {
|
|||||||
|
|
||||||
struct KeyCollector {
|
struct KeyCollector {
|
||||||
events: VecDeque<KeyEvent>,
|
events: VecDeque<KeyEvent>,
|
||||||
|
ss3_pending: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyCollector {
|
impl KeyCollector {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
events: VecDeque::new(),
|
events: VecDeque::new(),
|
||||||
|
ss3_pending: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,7 +341,55 @@ impl Default for KeyCollector {
|
|||||||
|
|
||||||
impl Perform for KeyCollector {
|
impl Perform for KeyCollector {
|
||||||
fn print(&mut self, c: char) {
|
fn print(&mut self, c: char) {
|
||||||
|
log::trace!("print: {c:?}");
|
||||||
// vte routes 0x7f (DEL) to print instead of execute
|
// vte routes 0x7f (DEL) to print instead of execute
|
||||||
|
if self.ss3_pending {
|
||||||
|
self.ss3_pending = false;
|
||||||
|
match c {
|
||||||
|
'A' => {
|
||||||
|
self.push(KeyEvent(KeyCode::Up, ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'B' => {
|
||||||
|
self.push(KeyEvent(KeyCode::Down, ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'C' => {
|
||||||
|
self.push(KeyEvent(KeyCode::Right, ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'D' => {
|
||||||
|
self.push(KeyEvent(KeyCode::Left, ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'H' => {
|
||||||
|
self.push(KeyEvent(KeyCode::Home, ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'F' => {
|
||||||
|
self.push(KeyEvent(KeyCode::End, ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'P' => {
|
||||||
|
self.push(KeyEvent(KeyCode::F(1), ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'Q' => {
|
||||||
|
self.push(KeyEvent(KeyCode::F(2), ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'R' => {
|
||||||
|
self.push(KeyEvent(KeyCode::F(3), ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
'S' => {
|
||||||
|
self.push(KeyEvent(KeyCode::F(4), ModKeys::empty()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if c == '\x7f' {
|
if c == '\x7f' {
|
||||||
self.push(KeyEvent(KeyCode::Backspace, ModKeys::empty()));
|
self.push(KeyEvent(KeyCode::Backspace, ModKeys::empty()));
|
||||||
} else {
|
} else {
|
||||||
@@ -346,6 +398,7 @@ impl Perform for KeyCollector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&mut self, byte: u8) {
|
fn execute(&mut self, byte: u8) {
|
||||||
|
log::trace!("execute: {byte:#04x}");
|
||||||
let event = match byte {
|
let event = match byte {
|
||||||
0x00 => KeyEvent(KeyCode::Char(' '), ModKeys::CTRL), // Ctrl+Space / Ctrl+@
|
0x00 => KeyEvent(KeyCode::Char(' '), ModKeys::CTRL), // Ctrl+Space / Ctrl+@
|
||||||
0x09 => KeyEvent(KeyCode::Tab, ModKeys::empty()), // Tab (Ctrl+I)
|
0x09 => KeyEvent(KeyCode::Tab, ModKeys::empty()), // Tab (Ctrl+I)
|
||||||
@@ -370,6 +423,9 @@ impl Perform for KeyCollector {
|
|||||||
_ignore: bool,
|
_ignore: bool,
|
||||||
action: char,
|
action: char,
|
||||||
) {
|
) {
|
||||||
|
log::trace!(
|
||||||
|
"CSI dispatch: params={params:?}, intermediates={intermediates:?}, action={action:?}"
|
||||||
|
);
|
||||||
let params: Vec<u16> = params
|
let params: Vec<u16> = params
|
||||||
.iter()
|
.iter()
|
||||||
.map(|p| p.first().copied().unwrap_or(0))
|
.map(|p| p.first().copied().unwrap_or(0))
|
||||||
@@ -481,16 +537,10 @@ impl Perform for KeyCollector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn esc_dispatch(&mut self, intermediates: &[u8], _ignore: bool, byte: u8) {
|
fn esc_dispatch(&mut self, intermediates: &[u8], _ignore: bool, byte: u8) {
|
||||||
// SS3 sequences (ESC O P/Q/R/S for F1-F4)
|
log::trace!("ESC dispatch: intermediates={intermediates:?}, byte={byte:#04x}");
|
||||||
if intermediates == [b'O'] {
|
// SS3 sequences
|
||||||
let key = match byte {
|
if byte == b'O' {
|
||||||
b'P' => KeyCode::F(1),
|
self.ss3_pending = true;
|
||||||
b'Q' => KeyCode::F(2),
|
|
||||||
b'R' => KeyCode::F(3),
|
|
||||||
b'S' => KeyCode::F(4),
|
|
||||||
_ => return,
|
|
||||||
};
|
|
||||||
self.push(KeyEvent(key, ModKeys::empty()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -499,6 +549,7 @@ pub struct PollReader {
|
|||||||
parser: Parser,
|
parser: Parser,
|
||||||
collector: KeyCollector,
|
collector: KeyCollector,
|
||||||
byte_buf: VecDeque<u8>,
|
byte_buf: VecDeque<u8>,
|
||||||
|
pub verbatim_single: bool,
|
||||||
pub verbatim: bool,
|
pub verbatim: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,6 +559,7 @@ impl PollReader {
|
|||||||
parser: Parser::new(),
|
parser: Parser::new(),
|
||||||
collector: KeyCollector::new(),
|
collector: KeyCollector::new(),
|
||||||
byte_buf: VecDeque::new(),
|
byte_buf: VecDeque::new(),
|
||||||
|
verbatim_single: false,
|
||||||
verbatim: false,
|
verbatim: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -531,6 +583,18 @@ impl PollReader {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_one_verbatim(&mut self) -> Option<KeyEvent> {
|
||||||
|
if self.byte_buf.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let bytes: Vec<u8> = self.byte_buf.drain(..).collect();
|
||||||
|
let verbatim_str = String::from_utf8_lossy(&bytes).to_string();
|
||||||
|
Some(KeyEvent(
|
||||||
|
KeyCode::Verbatim(verbatim_str.into()),
|
||||||
|
ModKeys::empty(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn feed_bytes(&mut self, bytes: &[u8]) {
|
pub fn feed_bytes(&mut self, bytes: &[u8]) {
|
||||||
self.byte_buf.extend(bytes);
|
self.byte_buf.extend(bytes);
|
||||||
}
|
}
|
||||||
@@ -544,18 +608,27 @@ impl Default for PollReader {
|
|||||||
|
|
||||||
impl KeyReader for PollReader {
|
impl KeyReader for PollReader {
|
||||||
fn read_key(&mut self) -> Result<Option<KeyEvent>, ShErr> {
|
fn read_key(&mut self) -> Result<Option<KeyEvent>, ShErr> {
|
||||||
|
if self.verbatim_single {
|
||||||
|
if let Some(key) = self.read_one_verbatim() {
|
||||||
|
self.verbatim_single = false;
|
||||||
|
return Ok(Some(key));
|
||||||
|
}
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
if self.verbatim {
|
if self.verbatim {
|
||||||
if let Some(paste) = self.handle_bracket_paste() {
|
if let Some(paste) = self.handle_bracket_paste() {
|
||||||
return Ok(Some(paste));
|
return Ok(Some(paste));
|
||||||
}
|
}
|
||||||
// If we're in verbatim mode but haven't seen the end marker yet, don't attempt to parse keys
|
// If we're in verbatim mode but haven't seen the end marker yet, don't attempt to parse keys
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
} else if self.byte_buf.len() == 1
|
} else if self.byte_buf.front() == Some(&b'\x1b') {
|
||||||
&& self.byte_buf.front() == Some(&b'\x1b') {
|
// Escape: if it's the only byte, or the next byte isn't a valid
|
||||||
// User pressed escape
|
// escape sequence prefix ([ or O), emit a standalone Escape
|
||||||
self.byte_buf.pop_front(); // Consume the escape byte
|
if self.byte_buf.len() == 1 || !matches!(self.byte_buf.get(1), Some(b'[') | Some(b'O')) {
|
||||||
|
self.byte_buf.pop_front();
|
||||||
return Ok(Some(KeyEvent(KeyCode::Esc, ModKeys::empty())));
|
return Ok(Some(KeyEvent(KeyCode::Esc, ModKeys::empty())));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while let Some(byte) = self.byte_buf.pop_front() {
|
while let Some(byte) = self.byte_buf.pop_front() {
|
||||||
self.parser.advance(&mut self.collector, &[byte]);
|
self.parser.advance(&mut self.collector, &[byte]);
|
||||||
if let Some(key) = self.collector.pop() {
|
if let Some(key) = self.collector.pop() {
|
||||||
@@ -567,7 +640,7 @@ impl KeyReader for PollReader {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => return Ok(Some(key))
|
_ => return Ok(Some(key)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -821,6 +894,7 @@ impl Default for Layout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
pub struct TermWriter {
|
pub struct TermWriter {
|
||||||
last_bell: Option<Instant>,
|
last_bell: Option<Instant>,
|
||||||
out: RawFd,
|
out: RawFd,
|
||||||
@@ -1020,7 +1094,7 @@ impl LineWriter for TermWriter {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redraw(&mut self, prompt: &str, line: &str, new_layout: &Layout) -> ShResult<()> {
|
fn redraw(&mut self, prompt: &str, line: &str, new_layout: &Layout, offset: usize, total_buf_lines: usize) -> ShResult<()> {
|
||||||
let err = |_| {
|
let err = |_| {
|
||||||
ShErr::simple(
|
ShErr::simple(
|
||||||
ShErrKind::InternalErr,
|
ShErrKind::InternalErr,
|
||||||
@@ -1046,7 +1120,7 @@ impl LineWriter for TermWriter {
|
|||||||
if multiline {
|
if multiline {
|
||||||
let prompt_end = Layout::calc_pos(self.t_cols, prompt, Pos { col: 0, row: 0 }, 0, false);
|
let prompt_end = Layout::calc_pos(self.t_cols, prompt, Pos { col: 0, row: 0 }, 0, false);
|
||||||
let show_numbers = read_shopts(|o| o.prompt.line_numbers);
|
let show_numbers = read_shopts(|o| o.prompt.line_numbers);
|
||||||
let display_line = enumerate_lines(line, prompt_end.col as usize, show_numbers);
|
let display_line = enumerate_lines(line, prompt_end.col as usize, show_numbers, offset, total_buf_lines);
|
||||||
self.buffer.push_str(&display_line);
|
self.buffer.push_str(&display_line);
|
||||||
} else {
|
} else {
|
||||||
self.buffer.push_str(line);
|
self.buffer.push_str(line);
|
||||||
|
|||||||
529
src/readline/tests.rs
Normal file
529
src/readline/tests.rs
Normal file
@@ -0,0 +1,529 @@
|
|||||||
|
#![allow(non_snake_case)]
|
||||||
|
use std::os::fd::AsRawFd;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
readline::{Prompt, ShedVi, annotate_input},
|
||||||
|
testutil::TestGuard,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn assert_annotated(input: &str, expected: &str) {
|
||||||
|
let result = annotate_input(input);
|
||||||
|
assert_eq!(result, expected, "\nInput: {input:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tests for our vim logic emulation. Each test consists of an initial text, a sequence of keys to feed, and the expected final text and cursor position.
|
||||||
|
macro_rules! vi_test {
|
||||||
|
{ $($name:ident: $input:expr => $op:expr => $expected_text:expr,$expected_cursor:expr);* } => {
|
||||||
|
$(
|
||||||
|
#[test]
|
||||||
|
fn $name() {
|
||||||
|
let (mut vi, _g) = test_vi($input);
|
||||||
|
vi.feed_bytes(b"\x1b"); // Start in normal mode
|
||||||
|
vi.process_input().unwrap();
|
||||||
|
|
||||||
|
vi.feed_bytes($op.as_bytes());
|
||||||
|
vi.process_input().unwrap();
|
||||||
|
assert_eq!(vi.editor.joined(), $expected_text);
|
||||||
|
assert_eq!(vi.editor.cursor_to_flat(), $expected_cursor);
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== Annotation Tests =====================
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_simple_command() {
|
||||||
|
assert_annotated("echo hello", "\u{e101}echo\u{e11a} \u{e102}hello\u{e11a}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_pipeline() {
|
||||||
|
assert_annotated(
|
||||||
|
"ls | grep foo",
|
||||||
|
"\u{e100}ls\u{e11a} \u{e104}|\u{e11a} \u{e100}grep\u{e11a} \u{e102}foo\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_conjunction() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo foo && echo bar",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}foo\u{e11a} \u{e104}&&\u{e11a} \u{e101}echo\u{e11a} \u{e102}bar\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_redirect_output() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo hello > file.txt",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}hello\u{e11a} \u{e105}>\u{e11a} \u{e102}file.txt\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_redirect_append() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo hello >> file.txt",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}hello\u{e11a} \u{e105}>>\u{e11a} \u{e102}file.txt\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_redirect_input() {
|
||||||
|
assert_annotated(
|
||||||
|
"cat < file.txt",
|
||||||
|
"\u{e100}cat\u{e11a} \u{e105}<\u{e11a} \u{e102}file.txt\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_fd_redirect() {
|
||||||
|
assert_annotated("cmd 2>&1", "\u{e100}cmd\u{e11a} \u{e105}2>&1\u{e11a}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_variable_sub() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo $HOME",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}\u{e10c}$HOME\u{e10d}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_variable_brace_sub() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo ${HOME}",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}\u{e10c}${HOME}\u{e10d}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_command_sub() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo $(ls)",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}\u{e10e}$(ls)\u{e10f}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_single_quoted_string() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo 'hello world'",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}\u{e114}'hello world'\u{e115}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_double_quoted_string() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo \"hello world\"",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}\u{e112}\"hello world\"\u{e113}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_assignment() {
|
||||||
|
assert_annotated("FOO=bar", "\u{e107}FOO=bar\u{e11a}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_assignment_with_command() {
|
||||||
|
assert_annotated(
|
||||||
|
"FOO=bar echo hello",
|
||||||
|
"\u{e107}FOO=bar\u{e11a} \u{e101}echo\u{e11a} \u{e102}hello\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_if_statement() {
|
||||||
|
assert_annotated(
|
||||||
|
"if true; then echo yes; fi",
|
||||||
|
"\u{e103}if\u{e11a} \u{e101}true\u{e11a}\u{e108}; \u{e11a}\u{e103}then\u{e11a} \u{e101}echo\u{e11a} \u{e102}yes\u{e11a}\u{e108}; \u{e11a}\u{e103}fi\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_for_loop() {
|
||||||
|
assert_annotated(
|
||||||
|
"for i in a b c; do echo $i; done",
|
||||||
|
"\u{e103}for\u{e11a} \u{e102}i\u{e11a} \u{e103}in\u{e11a} \u{e102}a\u{e11a} \u{e102}b\u{e11a} \u{e102}c\u{e11a}\u{e108}; \u{e11a}\u{e103}do\u{e11a} \u{e101}echo\u{e11a} \u{e102}\u{e10c}$i\u{e10d}\u{e11a}\u{e108}; \u{e11a}\u{e103}done\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_while_loop() {
|
||||||
|
assert_annotated(
|
||||||
|
"while true; do echo hello; done",
|
||||||
|
"\u{e103}while\u{e11a} \u{e101}true\u{e11a}\u{e108}; \u{e11a}\u{e103}do\u{e11a} \u{e101}echo\u{e11a} \u{e102}hello\u{e11a}\u{e108}; \u{e11a}\u{e103}done\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_case_statement() {
|
||||||
|
assert_annotated(
|
||||||
|
"case foo in bar) echo bar;; esac",
|
||||||
|
"\u{e103}case\u{e11a} \u{e102}foo\u{e11a} \u{e103}in\u{e11a} \u{e104}bar\u{e109})\u{e11a} \u{e101}echo\u{e11a} \u{e102}bar\u{e11a}\u{e108};; \u{e11a}\u{e103}esac\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_brace_group() {
|
||||||
|
assert_annotated(
|
||||||
|
"{ echo hello; }",
|
||||||
|
"\u{e104}{\u{e11a} \u{e101}echo\u{e11a} \u{e102}hello\u{e11a}\u{e108}; \u{e11a}\u{e104}}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_comment() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo hello # this is a comment",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}hello\u{e11a} \u{e106}# this is a comment\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_semicolon_sep() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo foo; echo bar",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}foo\u{e11a}\u{e108}; \u{e11a}\u{e101}echo\u{e11a} \u{e102}bar\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_escaped_char() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo hello\\ world",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}hello\\ world\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_glob() {
|
||||||
|
assert_annotated(
|
||||||
|
"ls *.txt",
|
||||||
|
"\u{e100}ls\u{e11a} \u{e102}\u{e117}*\u{e11a}.txt\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_heredoc_operator() {
|
||||||
|
assert_annotated(
|
||||||
|
"cat <<EOF",
|
||||||
|
"\u{e100}cat\u{e11a} \u{e105}<<\u{e11a}\u{e102}EOF\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_herestring_operator() {
|
||||||
|
assert_annotated(
|
||||||
|
"cat <<< hello",
|
||||||
|
"\u{e100}cat\u{e11a} \u{e105}<<<\u{e11a} \u{e102}hello\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_nested_command_sub() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo $(echo $(ls))",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}\u{e10e}$(echo $(ls))\u{e10f}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_var_in_double_quotes() {
|
||||||
|
assert_annotated(
|
||||||
|
"echo \"hello $USER\"",
|
||||||
|
"\u{e101}echo\u{e11a} \u{e102}\u{e112}\"hello \u{e10c}$USER\u{e10d}\"\u{e113}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_func_def() {
|
||||||
|
assert_annotated(
|
||||||
|
"foo() { echo hello; }",
|
||||||
|
"\u{e103}foo()\u{e11a} \u{e104}{\u{e11a} \u{e101}echo\u{e11a} \u{e102}hello\u{e11a}\u{e108}; \u{e11a}\u{e104}}\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_negate() {
|
||||||
|
assert_annotated(
|
||||||
|
"! echo hello",
|
||||||
|
"\u{e104}!\u{e11a} \u{e101}echo\u{e11a} \u{e102}hello\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_or_conjunction() {
|
||||||
|
assert_annotated(
|
||||||
|
"false || echo fallback",
|
||||||
|
"\u{e101}false\u{e11a} \u{e104}||\u{e11a} \u{e101}echo\u{e11a} \u{e102}fallback\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_complex_pipeline() {
|
||||||
|
assert_annotated(
|
||||||
|
"cat file.txt | grep pattern | wc -l",
|
||||||
|
"\u{e100}cat\u{e11a} \u{e102}file.txt\u{e11a} \u{e104}|\u{e11a} \u{e100}grep\u{e11a} \u{e102}pattern\u{e11a} \u{e104}|\u{e11a} \u{e100}wc\u{e11a} \u{e102}-l\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn annotate_multiple_redirects() {
|
||||||
|
assert_annotated(
|
||||||
|
"cmd > out.txt 2> err.txt",
|
||||||
|
"\u{e100}cmd\u{e11a} \u{e105}>\u{e11a} \u{e102}out.txt\u{e11a} \u{e105}2>\u{e11a} \u{e102}err.txt\u{e11a}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== Vi Tests =====================
|
||||||
|
|
||||||
|
fn test_vi(initial: &str) -> (ShedVi, TestGuard) {
|
||||||
|
let g = TestGuard::new();
|
||||||
|
let prompt = Prompt::default();
|
||||||
|
let vi = ShedVi::new_no_hist(prompt, g.pty_slave().as_raw_fd())
|
||||||
|
.unwrap()
|
||||||
|
.with_initial(initial);
|
||||||
|
|
||||||
|
(vi, g)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Why can't I marry a programming language
|
||||||
|
vi_test! {
|
||||||
|
vi_dw_basic : "hello world" => "dw" => "world", 0;
|
||||||
|
vi_dw_middle : "one two three" => "wdw" => "one three", 4;
|
||||||
|
vi_dd_whole_line : "hello world" => "dd" => "", 0;
|
||||||
|
vi_x_single : "hello" => "x" => "ello", 0;
|
||||||
|
vi_x_middle : "hello" => "llx" => "helo", 2;
|
||||||
|
vi_X_backdelete : "hello" => "llX" => "hllo", 1;
|
||||||
|
vi_h_motion : "hello" => "$h" => "hello", 3;
|
||||||
|
vi_l_motion : "hello" => "l" => "hello", 1;
|
||||||
|
vi_h_at_start : "hello" => "h" => "hello", 0;
|
||||||
|
vi_l_at_end : "hello" => "$l" => "hello", 4;
|
||||||
|
vi_w_forward : "one two three" => "w" => "one two three", 4;
|
||||||
|
vi_b_backward : "one two three" => "$b" => "one two three", 8;
|
||||||
|
vi_e_end : "one two three" => "e" => "one two three", 2;
|
||||||
|
vi_ge_back_end : "one two three" => "$ge" => "one two three", 6;
|
||||||
|
vi_w_punctuation : "foo.bar baz" => "w" => "foo.bar baz", 3;
|
||||||
|
vi_e_punctuation : "foo.bar baz" => "e" => "foo.bar baz", 2;
|
||||||
|
vi_b_punctuation : "foo.bar baz" => "$b" => "foo.bar baz", 8;
|
||||||
|
vi_w_at_eol : "hello" => "$w" => "hello", 4;
|
||||||
|
vi_b_at_bol : "hello" => "b" => "hello", 0;
|
||||||
|
vi_W_forward : "foo.bar baz" => "W" => "foo.bar baz", 8;
|
||||||
|
vi_B_backward : "foo.bar baz" => "$B" => "foo.bar baz", 8;
|
||||||
|
vi_E_end : "foo.bar baz" => "E" => "foo.bar baz", 6;
|
||||||
|
vi_gE_back_end : "one two three" => "$gE" => "one two three", 6;
|
||||||
|
vi_W_skip_punct : "one-two three" => "W" => "one-two three", 8;
|
||||||
|
vi_B_skip_punct : "one two-three" => "$B" => "one two-three", 4;
|
||||||
|
vi_E_skip_punct : "one-two three" => "E" => "one-two three", 6;
|
||||||
|
vi_dW_big : "foo.bar baz" => "dW" => "baz", 0;
|
||||||
|
vi_cW_big : "foo.bar baz" => "cWx\x1b" => "x baz", 0;
|
||||||
|
vi_zero_bol : " hello" => "$0" => " hello", 0;
|
||||||
|
vi_caret_first_char : " hello" => "$^" => " hello", 2;
|
||||||
|
vi_dollar_eol : "hello world" => "$" => "hello world", 10;
|
||||||
|
vi_g_last_nonws : "hello " => "g_" => "hello ", 4;
|
||||||
|
vi_g_no_trailing : "hello" => "g_" => "hello", 4;
|
||||||
|
vi_pipe_column : "hello world" => "6|" => "hello world", 5;
|
||||||
|
vi_pipe_col1 : "hello world" => "1|" => "hello world", 0;
|
||||||
|
vi_I_insert_front : " hello" => "Iworld \x1b" => " world hello", 7;
|
||||||
|
vi_A_append_end : "hello" => "A world\x1b" => "hello world", 10;
|
||||||
|
vi_f_find : "hello world" => "fo" => "hello world", 4;
|
||||||
|
vi_F_find_back : "hello world" => "$Fo" => "hello world", 7;
|
||||||
|
vi_t_till : "hello world" => "tw" => "hello world", 5;
|
||||||
|
vi_T_till_back : "hello world" => "$To" => "hello world", 8;
|
||||||
|
vi_f_no_match : "hello" => "fz" => "hello", 0;
|
||||||
|
vi_semicolon_repeat : "abcabc" => "fa;;" => "abcabc", 3;
|
||||||
|
vi_comma_reverse : "abcabc" => "fa;;," => "abcabc", 0;
|
||||||
|
vi_df_semicolon : "abcabc" => "fa;;dfa" => "abcabc", 3;
|
||||||
|
vi_t_at_target : "aab" => "lta" => "aab", 1;
|
||||||
|
vi_D_to_end : "hello world" => "wD" => "hello ", 5;
|
||||||
|
vi_d_dollar : "hello world" => "wd$" => "hello ", 5;
|
||||||
|
vi_d0_to_start : "hello world" => "$d0" => "d", 0;
|
||||||
|
vi_dw_multiple : "one two three" => "d2w" => "three", 0;
|
||||||
|
vi_dt_char : "hello world" => "dtw" => "world", 0;
|
||||||
|
vi_df_char : "hello world" => "dfw" => "orld", 0;
|
||||||
|
vi_dh_back : "hello" => "lldh" => "hllo", 1;
|
||||||
|
vi_dl_forward : "hello" => "dl" => "ello", 0;
|
||||||
|
vi_dge_back_end : "one two three" => "$dge" => "one tw", 5;
|
||||||
|
vi_dG_to_end : "hello world" => "dG" => "", 0;
|
||||||
|
vi_dgg_to_start : "hello world" => "$dgg" => "", 0;
|
||||||
|
vi_d_semicolon : "abcabc" => "fad;" => "abcabc", 3;
|
||||||
|
vi_cw_basic : "hello world" => "cwfoo\x1b" => "foo world", 2;
|
||||||
|
vi_C_to_end : "hello world" => "wCfoo\x1b" => "hello foo", 8;
|
||||||
|
vi_cc_whole : "hello world" => "ccfoo\x1b" => "foo", 2;
|
||||||
|
vi_ct_char : "hello world" => "ctwfoo\x1b" => "fooworld", 2;
|
||||||
|
vi_s_single : "hello" => "sfoo\x1b" => "fooello", 2;
|
||||||
|
vi_S_whole_line : "hello world" => "Sfoo\x1b" => "foo", 2;
|
||||||
|
vi_cl_forward : "hello" => "clX\x1b" => "Xello", 0;
|
||||||
|
vi_ch_backward : "hello" => "llchX\x1b" => "hXllo", 1;
|
||||||
|
vi_cb_word_back : "hello world" => "$cbfoo\x1b" => "hello food", 8;
|
||||||
|
vi_ce_word_end : "hello world" => "cefoo\x1b" => "foo world", 2;
|
||||||
|
vi_c0_to_start : "hello world" => "wc0foo\x1b" => "fooworld", 2;
|
||||||
|
vi_yw_p_basic : "hello world" => "ywwP" => "hello hello world", 11;
|
||||||
|
vi_dw_p_paste : "hello world" => "dwP" => "hello world", 5;
|
||||||
|
vi_dd_p_paste : "hello world" => "ddp" => "\nhello world", 1;
|
||||||
|
vi_y_dollar_p : "hello world" => "wy$P" => "hello worldworld", 10;
|
||||||
|
vi_ye_p : "hello world" => "yewP" => "hello helloworld", 10;
|
||||||
|
vi_yy_p : "hello world" => "yyp" => "hello world\nhello world", 12;
|
||||||
|
vi_Y_p : "hello world" => "Yp" => "hhello worldello world", 11;
|
||||||
|
vi_p_after_x : "hello" => "xp" => "ehllo", 1;
|
||||||
|
vi_P_before : "hello" => "llxP" => "hello", 2;
|
||||||
|
vi_paste_empty : "hello" => "p" => "hello", 0;
|
||||||
|
vi_r_replace : "hello" => "ra" => "aello", 0;
|
||||||
|
vi_r_middle : "hello" => "llra" => "healo", 2;
|
||||||
|
vi_r_at_end : "hello" => "$ra" => "hella", 4;
|
||||||
|
vi_r_space : "hello" => "r " => " ello", 0;
|
||||||
|
vi_r_with_count : "hello" => "3rx" => "xxxlo", 2;
|
||||||
|
vi_tilde_single : "hello" => "~" => "Hello", 1;
|
||||||
|
vi_tilde_count : "hello" => "3~" => "HELlo", 3;
|
||||||
|
vi_tilde_at_end : "HELLO" => "$~" => "HELLo", 4;
|
||||||
|
vi_tilde_mixed : "hElLo" => "5~" => "HeLlO", 4;
|
||||||
|
vi_gu_word : "HELLO world" => "guw" => "hello world", 0;
|
||||||
|
vi_gU_word : "hello WORLD" => "gUw" => "HELLO WORLD", 0;
|
||||||
|
vi_gu_dollar : "HELLO WORLD" => "gu$" => "hello world", 0;
|
||||||
|
vi_gU_dollar : "hello world" => "gU$" => "HELLO WORLD", 0;
|
||||||
|
vi_gu_0 : "HELLO WORLD" => "$gu0" => "hello worlD", 0;
|
||||||
|
vi_gU_0 : "hello world" => "$gU0" => "HELLO WORLd", 0;
|
||||||
|
vi_gtilde_word : "hello WORLD" => "g~w" => "HELLO WORLD", 0;
|
||||||
|
vi_gtilde_dollar : "hello WORLD" => "g~$" => "HELLO world", 0;
|
||||||
|
vi_diw_inner : "one two three" => "wdiw" => "one three", 4;
|
||||||
|
vi_ciw_replace : "hello world" => "ciwfoo\x1b" => "foo world", 2;
|
||||||
|
vi_daw_around : "one two three" => "wdaw" => "one three", 4;
|
||||||
|
vi_yiw_p : "hello world" => "yiwAp \x1bp" => "hello worldp hello", 17;
|
||||||
|
vi_diW_big_inner : "one-two three" => "diW" => " three", 0;
|
||||||
|
vi_daW_big_around : "one two-three end" => "wdaW" => "one end", 4;
|
||||||
|
vi_ciW_big : "one-two three" => "ciWx\x1b" => "x three", 0;
|
||||||
|
vi_di_dquote : "one \"two\" three" => "f\"di\"" => "one \"\" three", 5;
|
||||||
|
vi_da_dquote : "one \"two\" three" => "f\"da\"" => "one three", 4;
|
||||||
|
vi_ci_dquote : "one \"two\" three" => "f\"ci\"x\x1b" => "one \"x\" three", 5;
|
||||||
|
vi_di_squote : "one 'two' three" => "f'di'" => "one '' three", 5;
|
||||||
|
vi_da_squote : "one 'two' three" => "f'da'" => "one three", 4;
|
||||||
|
vi_di_backtick : "one `two` three" => "f`di`" => "one `` three", 5;
|
||||||
|
vi_da_backtick : "one `two` three" => "f`da`" => "one three", 4;
|
||||||
|
vi_ci_dquote_empty : "one \"\" three" => "f\"ci\"x\x1b" => "one \"x\" three", 5;
|
||||||
|
vi_di_paren : "one (two) three" => "f(di(" => "one () three", 5;
|
||||||
|
vi_da_paren : "one (two) three" => "f(da(" => "one three", 4;
|
||||||
|
vi_ci_paren : "one (two) three" => "f(ci(x\x1b" => "one (x) three", 5;
|
||||||
|
vi_di_brace : "one {two} three" => "f{di{" => "one {} three", 5;
|
||||||
|
vi_da_brace : "one {two} three" => "f{da{" => "one three", 4;
|
||||||
|
vi_di_bracket : "one [two] three" => "f[di[" => "one [] three", 5;
|
||||||
|
vi_da_bracket : "one [two] three" => "f[da[" => "one three", 4;
|
||||||
|
vi_di_angle : "one <two> three" => "f<di<" => "one <> three", 5;
|
||||||
|
vi_da_angle : "one <two> three" => "f<da<" => "one three", 4;
|
||||||
|
vi_di_paren_nested : "fn(a, (b, c))" => "f(di(" => "fn()", 3;
|
||||||
|
vi_di_paren_empty : "fn() end" => "f(di(" => "fn() end", 3;
|
||||||
|
vi_dib_alias : "one (two) three" => "f(dib" => "one () three", 5;
|
||||||
|
vi_diB_alias : "one {two} three" => "f{diB" => "one {} three", 5;
|
||||||
|
vi_percent_paren : "(hello) world" => "%" => "(hello) world", 6;
|
||||||
|
vi_percent_brace : "{hello} world" => "%" => "{hello} world", 6;
|
||||||
|
vi_percent_bracket : "[hello] world" => "%" => "[hello] world", 6;
|
||||||
|
vi_percent_from_close: "(hello) world" => "f)%" => "(hello) world", 0;
|
||||||
|
vi_d_percent_paren : "(hello) world" => "d%" => " world", 0;
|
||||||
|
vi_to_paren_fwd : "foo (bar) baz" => "])" => "foo (bar) baz", 8;
|
||||||
|
vi_to_paren_bkwd : "foo (bar) baz" => "f)[(" => "foo (bar) baz", 4;
|
||||||
|
vi_to_brace_fwd : "foo {bar} baz" => "]}" => "foo {bar} baz", 8;
|
||||||
|
vi_to_brace_bkwd : "foo {bar} baz" => "f}[{" => "foo {bar} baz", 4;
|
||||||
|
vi_to_paren_nested : "((a)(b)) end" => "])" => "((a)(b)) end", 7;
|
||||||
|
vi_to_brace_nested : "{{a}{b}} end" => "]}" => "{{a}{b}} end", 7;
|
||||||
|
vi_d_to_paren_fwd : "foo (bar) baz" => "wd])" => "foo baz", 4;
|
||||||
|
vi_d_to_brace_fwd : "foo {bar} baz" => "wd]}" => "foo baz", 4;
|
||||||
|
vi_to_paren_no_match : "foo bar baz" => "])" => "foo bar baz", 0;
|
||||||
|
vi_to_brace_no_match : "foo bar baz" => "]}" => "foo bar baz", 0;
|
||||||
|
vi_i_insert : "hello" => "iX\x1b" => "Xhello", 0;
|
||||||
|
vi_a_append : "hello" => "aX\x1b" => "hXello", 1;
|
||||||
|
vi_I_front : " hello" => "IX\x1b" => " Xhello", 2;
|
||||||
|
vi_A_end : "hello" => "AX\x1b" => "helloX", 5;
|
||||||
|
vi_o_open_below : "hello" => "oworld\x1b" => "hello\nworld", 10;
|
||||||
|
vi_O_open_above : "hello" => "Oworld\x1b" => "world\nhello", 4;
|
||||||
|
vi_empty_input : "" => "i hello\x1b" => " hello", 5;
|
||||||
|
vi_insert_escape : "hello" => "aX\x1b" => "hXello", 1;
|
||||||
|
vi_ctrl_w_del_word : "hello world" => "A\x17\x1b" => "hello ", 5;
|
||||||
|
vi_ctrl_h_backspace : "hello" => "A\x08\x1b" => "hell", 3;
|
||||||
|
vi_u_undo_delete : "hello world" => "dwu" => "hello world", 0;
|
||||||
|
vi_u_undo_change : "hello world" => "ciwfoo\x1bu" => "hello world", 0;
|
||||||
|
vi_u_undo_x : "hello" => "xu" => "hello", 0;
|
||||||
|
vi_ctrl_r_redo : "hello" => "xu\x12" => "ello", 0;
|
||||||
|
vi_u_multiple : "hello world" => "xdwu" => "ello world", 0;
|
||||||
|
vi_redo_after_undo : "hello world" => "dwu\x12" => "world", 0;
|
||||||
|
vi_dot_repeat_x : "hello" => "x." => "llo", 0;
|
||||||
|
vi_dot_repeat_dw : "one two three" => "dw." => "three", 0;
|
||||||
|
vi_dot_repeat_cw : "one two three" => "cwfoo\x1bw." => "foo foo three", 6;
|
||||||
|
vi_dot_repeat_r : "hello" => "ra.." => "aello", 0;
|
||||||
|
vi_dot_repeat_s : "hello" => "sX\x1bl." => "XXllo", 1;
|
||||||
|
vi_count_h : "hello world" => "$3h" => "hello world", 7;
|
||||||
|
vi_count_l : "hello world" => "3l" => "hello world", 3;
|
||||||
|
vi_count_w : "one two three four" => "2w" => "one two three four", 8;
|
||||||
|
vi_count_b : "one two three four" => "$2b" => "one two three four", 8;
|
||||||
|
vi_count_x : "hello" => "3x" => "lo", 0;
|
||||||
|
vi_count_dw : "one two three four" => "2dw" => "three four", 0;
|
||||||
|
vi_verb_count_motion : "one two three four" => "d2w" => "three four", 0;
|
||||||
|
vi_count_s : "hello" => "3sX\x1b" => "Xlo", 0;
|
||||||
|
vi_indent_line : "hello" => ">>" => "\thello", 1;
|
||||||
|
vi_dedent_line : "\thello" => "<<" => "hello", 0;
|
||||||
|
vi_indent_double : "hello" => ">>>>" => "\t\thello", 2;
|
||||||
|
vi_J_join_lines : "hello\nworld" => "J" => "hello world", 5;
|
||||||
|
vi_v_u_lower : "HELLO" => "vlllu" => "hellO", 0;
|
||||||
|
vi_v_U_upper : "hello" => "vlllU" => "HELLo", 0;
|
||||||
|
vi_v_d_delete : "hello world" => "vwwd" => "", 0;
|
||||||
|
vi_v_x_delete : "hello world" => "vwwx" => "", 0;
|
||||||
|
vi_v_c_change : "hello world" => "vwcfoo\x1b" => "fooorld", 2;
|
||||||
|
vi_v_y_p_yank : "hello world" => "vwyAp \x1bp" => "hello worldp hello w", 19;
|
||||||
|
vi_v_dollar_d : "hello world" => "wv$d" => "hello ", 5;
|
||||||
|
vi_v_0_d : "hello world" => "$v0d" => "", 0;
|
||||||
|
vi_ve_d : "hello world" => "ved" => " world", 0;
|
||||||
|
vi_v_o_swap : "hello world" => "vllod" => "lo world", 0;
|
||||||
|
vi_v_r_replace : "hello" => "vlllrx" => "xxxxo", 0;
|
||||||
|
vi_v_tilde_case : "hello" => "vlll~" => "HELLo", 0;
|
||||||
|
vi_V_d_delete : "hello world" => "Vd" => "", 0;
|
||||||
|
vi_V_y_p : "hello world" => "Vyp" => "hello world\nhello world", 12;
|
||||||
|
vi_V_S_change : "hello world" => "VSfoo\x1b" => "foo", 2;
|
||||||
|
vi_ctrl_a_inc : "num 5 end" => "w\x01" => "num 6 end", 4;
|
||||||
|
vi_ctrl_x_dec : "num 5 end" => "w\x18" => "num 4 end", 4;
|
||||||
|
vi_ctrl_a_negative : "num -3 end" => "w\x01" => "num -2 end", 5;
|
||||||
|
vi_ctrl_x_to_neg : "num 0 end" => "w\x18" => "num -1 end", 5;
|
||||||
|
vi_ctrl_a_count : "num 5 end" => "w3\x01" => "num 8 end", 4;
|
||||||
|
vi_ctrl_a_width : "num -00001 end" => "w\x01" => "num 00000 end", 8;
|
||||||
|
vi_delete_empty : "" => "x" => "", 0;
|
||||||
|
vi_undo_on_empty : "" => "u" => "", 0;
|
||||||
|
vi_w_single_char : "a b c" => "w" => "a b c", 2;
|
||||||
|
vi_dw_last_word : "hello" => "dw" => "", 0;
|
||||||
|
vi_dollar_single : "h" => "$" => "h", 0;
|
||||||
|
vi_caret_no_ws : "hello" => "$^" => "hello", 0;
|
||||||
|
vi_f_last_char : "hello" => "fo" => "hello", 4;
|
||||||
|
vi_r_on_space : "hello world" => "5|r-" => "hell- world", 4;
|
||||||
|
vi_vw_doesnt_crash : "" => "vw" => "", 0;
|
||||||
|
vi_indent_cursor_pos : "echo foo" => ">>" => "\techo foo", 1;
|
||||||
|
vi_join_indent_lines : "echo foo\n\t\techo bar" => "J" => "echo foo echo bar", 8;
|
||||||
|
vi_cw_stays_on_line : "echo foo\necho bar" => "wcw" => "echo \necho bar", 5
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vi_auto_indent() {
|
||||||
|
let (mut vi, _g) = test_vi("");
|
||||||
|
|
||||||
|
// Type each line and press Enter separately so auto-indent triggers
|
||||||
|
let lines = [
|
||||||
|
"func() {",
|
||||||
|
"case foo in",
|
||||||
|
"bar)",
|
||||||
|
"while true; do",
|
||||||
|
"echo foo \\\rbar \\\rbiz \\\rbazz\rbreak\rdone\r;;\resac\r}",
|
||||||
|
];
|
||||||
|
|
||||||
|
for (i, line) in lines.iter().enumerate() {
|
||||||
|
vi.feed_bytes(line.as_bytes());
|
||||||
|
if i != lines.len() - 1 {
|
||||||
|
vi.feed_bytes(b"\r");
|
||||||
|
}
|
||||||
|
vi.process_input().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
vi.editor.joined(),
|
||||||
|
"func() {\n\tcase foo in\n\t\tbar)\n\t\t\twhile true; do\n\t\t\t\techo foo \\\n\t\t\t\tbar \\\n\t\t\t\tbiz \\\n\t\t\t\tbazz\n\t\t\t\tbreak\n\t\t\tdone\n\t\t;;\n\tesac\n}"
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,5 +1,12 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
|
|
||||||
|
use crate::readline::{
|
||||||
|
linebuf::{Grapheme, Pos},
|
||||||
|
vimode::ex::SubFlags,
|
||||||
|
};
|
||||||
|
|
||||||
use super::register::{RegisterContent, append_register, read_register, write_register};
|
use super::register::{RegisterContent, append_register, read_register, write_register};
|
||||||
|
|
||||||
//TODO: write tests that take edit results and cursor positions from actual
|
//TODO: write tests that take edit results and cursor positions from actual
|
||||||
@@ -64,6 +71,7 @@ bitflags! {
|
|||||||
const VISUAL_LINE = 1<<1;
|
const VISUAL_LINE = 1<<1;
|
||||||
const VISUAL_BLOCK = 1<<2;
|
const VISUAL_BLOCK = 1<<2;
|
||||||
const EXIT_CUR_MODE = 1<<3;
|
const EXIT_CUR_MODE = 1<<3;
|
||||||
|
const IS_EX_CMD = 1<<4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,26 +161,12 @@ impl ViCmd {
|
|||||||
}) && self.motion.is_none()
|
}) && self.motion.is_none()
|
||||||
}
|
}
|
||||||
pub fn is_line_motion(&self) -> bool {
|
pub fn is_line_motion(&self) -> bool {
|
||||||
self.motion.as_ref().is_some_and(|m| {
|
self
|
||||||
matches!(
|
.motion
|
||||||
m.1,
|
.as_ref()
|
||||||
Motion::LineUp | Motion::LineDown | Motion::LineUpCharwise | Motion::LineDownCharwise
|
.is_some_and(|m| matches!(m.1, Motion::LineUp | Motion::LineDown))
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
/// If a ViCmd has a linewise motion, but no verb, we change it to charwise
|
/// If a ViCmd has a linewise motion, but no verb, we change it to charwise
|
||||||
pub fn alter_line_motion_if_no_verb(&mut self) {
|
|
||||||
if self.is_line_motion()
|
|
||||||
&& self.verb.is_none()
|
|
||||||
&& let Some(motion) = self.motion.as_mut()
|
|
||||||
{
|
|
||||||
match motion.1 {
|
|
||||||
Motion::LineUp => motion.1 = Motion::LineUpCharwise,
|
|
||||||
Motion::LineDown => motion.1 = Motion::LineDownCharwise,
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn is_mode_transition(&self) -> bool {
|
pub fn is_mode_transition(&self) -> bool {
|
||||||
self.verb.as_ref().is_some_and(|v| {
|
self.verb.as_ref().is_some_and(|v| {
|
||||||
matches!(
|
matches!(
|
||||||
@@ -249,13 +243,16 @@ pub enum Verb {
|
|||||||
Equalize,
|
Equalize,
|
||||||
AcceptLineOrNewline,
|
AcceptLineOrNewline,
|
||||||
EndOfFile,
|
EndOfFile,
|
||||||
|
PrintPosition,
|
||||||
// Ex-mode verbs
|
// Ex-mode verbs
|
||||||
ExMode,
|
ExMode,
|
||||||
ShellCmd(String),
|
ShellCmd(String),
|
||||||
Normal(String),
|
Normal(String),
|
||||||
Read(ReadSrc),
|
Read(ReadSrc),
|
||||||
Write(WriteDest),
|
Write(WriteDest),
|
||||||
Substitute(String, String, super::vimode::ex::SubFlags),
|
Edit(PathBuf),
|
||||||
|
Quit,
|
||||||
|
Substitute(String, String, SubFlags),
|
||||||
RepeatSubstitute,
|
RepeatSubstitute,
|
||||||
RepeatGlobal,
|
RepeatGlobal,
|
||||||
}
|
}
|
||||||
@@ -301,6 +298,9 @@ impl Verb {
|
|||||||
| Self::JoinLines
|
| Self::JoinLines
|
||||||
| Self::InsertChar(_)
|
| Self::InsertChar(_)
|
||||||
| Self::Insert(_)
|
| Self::Insert(_)
|
||||||
|
| Self::Dedent
|
||||||
|
| Self::Indent
|
||||||
|
| Self::Equalize
|
||||||
| Self::Rot13
|
| Self::Rot13
|
||||||
| Self::EndOfFile
|
| Self::EndOfFile
|
||||||
| Self::IncrementNumber(_)
|
| Self::IncrementNumber(_)
|
||||||
@@ -310,47 +310,40 @@ impl Verb {
|
|||||||
pub fn is_char_insert(&self) -> bool {
|
pub fn is_char_insert(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
self,
|
self,
|
||||||
Self::Change | Self::InsertChar(_) | Self::ReplaceChar(_) | Self::ReplaceCharInplace(_, _)
|
Self::InsertChar(_) | Self::ReplaceChar(_)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum Motion {
|
pub enum Motion {
|
||||||
WholeLineInclusive, // whole line including the linebreak
|
WholeLine,
|
||||||
WholeLineExclusive, // whole line excluding the linebreak
|
|
||||||
TextObj(TextObj),
|
TextObj(TextObj),
|
||||||
EndOfLastWord,
|
EndOfLastWord,
|
||||||
BeginningOfFirstWord,
|
StartOfFirstWord,
|
||||||
BeginningOfLine,
|
StartOfLine,
|
||||||
EndOfLine,
|
EndOfLine,
|
||||||
WordMotion(To, Word, Direction),
|
WordMotion(To, Word, Direction),
|
||||||
CharSearch(Direction, Dest, char),
|
CharSearch(Direction, Dest, Grapheme),
|
||||||
BackwardChar,
|
BackwardChar,
|
||||||
ForwardChar,
|
ForwardChar,
|
||||||
BackwardCharForced, // These two variants can cross line boundaries
|
BackwardCharForced, // These two variants can cross line boundaries
|
||||||
ForwardCharForced,
|
ForwardCharForced,
|
||||||
LineUp,
|
LineUp,
|
||||||
LineUpCharwise,
|
|
||||||
ScreenLineUp,
|
|
||||||
ScreenLineUpCharwise,
|
|
||||||
LineDown,
|
LineDown,
|
||||||
LineDownCharwise,
|
|
||||||
ScreenLineDown,
|
|
||||||
ScreenLineDownCharwise,
|
|
||||||
BeginningOfScreenLine,
|
|
||||||
FirstGraphicalOnScreenLine,
|
|
||||||
HalfOfScreen,
|
|
||||||
HalfOfScreenLineText,
|
|
||||||
WholeBuffer,
|
WholeBuffer,
|
||||||
BeginningOfBuffer,
|
StartOfBuffer,
|
||||||
EndOfBuffer,
|
EndOfBuffer,
|
||||||
ToColumn,
|
ToColumn,
|
||||||
ToDelimMatch,
|
ToDelimMatch,
|
||||||
|
HalfScreenDown,
|
||||||
|
HalfScreenUp,
|
||||||
ToBrace(Direction),
|
ToBrace(Direction),
|
||||||
ToBracket(Direction),
|
ToBracket(Direction),
|
||||||
ToParen(Direction),
|
ToParen(Direction),
|
||||||
Range(usize, usize),
|
CharRange(Pos, Pos),
|
||||||
|
LineRange(usize, usize),
|
||||||
|
BlockRange(Pos, Pos),
|
||||||
RepeatMotion,
|
RepeatMotion,
|
||||||
RepeatMotionRev,
|
RepeatMotionRev,
|
||||||
Null,
|
Null,
|
||||||
@@ -379,14 +372,8 @@ impl Motion {
|
|||||||
pub fn is_exclusive(&self) -> bool {
|
pub fn is_exclusive(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
&self,
|
&self,
|
||||||
Self::BeginningOfLine
|
Self::StartOfLine
|
||||||
| Self::BeginningOfFirstWord
|
| Self::StartOfFirstWord
|
||||||
| Self::BeginningOfScreenLine
|
|
||||||
| Self::FirstGraphicalOnScreenLine
|
|
||||||
| Self::LineDownCharwise
|
|
||||||
| Self::LineUpCharwise
|
|
||||||
| Self::ScreenLineUpCharwise
|
|
||||||
| Self::ScreenLineDownCharwise
|
|
||||||
| Self::ToColumn
|
| Self::ToColumn
|
||||||
| Self::TextObj(TextObj::Sentence(_))
|
| Self::TextObj(TextObj::Sentence(_))
|
||||||
| Self::TextObj(TextObj::Paragraph(_))
|
| Self::TextObj(TextObj::Paragraph(_))
|
||||||
@@ -395,21 +382,11 @@ impl Motion {
|
|||||||
| Self::ToBrace(_)
|
| Self::ToBrace(_)
|
||||||
| Self::ToBracket(_)
|
| Self::ToBracket(_)
|
||||||
| Self::ToParen(_)
|
| Self::ToParen(_)
|
||||||
| Self::ScreenLineDown
|
| Self::CharRange(_, _)
|
||||||
| Self::ScreenLineUp
|
|
||||||
| Self::Range(_, _)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
pub fn is_linewise(&self) -> bool {
|
pub fn is_linewise(&self) -> bool {
|
||||||
matches!(
|
matches!(self, Self::WholeLine | Self::LineUp | Self::LineDown)
|
||||||
self,
|
|
||||||
Self::WholeLineInclusive
|
|
||||||
| Self::WholeLineExclusive
|
|
||||||
| Self::LineUp
|
|
||||||
| Self::LineDown
|
|
||||||
| Self::ScreenLineDown
|
|
||||||
| Self::ScreenLineUp
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ use std::str::Chars;
|
|||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use crate::bitflags;
|
use crate::bitflags;
|
||||||
|
use crate::expand::Expander;
|
||||||
use crate::libsh::error::{ShErr, ShErrKind, ShResult};
|
use crate::libsh::error::{ShErr, ShErrKind, ShResult};
|
||||||
|
use crate::parse::lex::TkFlags;
|
||||||
|
use crate::readline::history::History;
|
||||||
use crate::readline::keys::KeyEvent;
|
use crate::readline::keys::KeyEvent;
|
||||||
use crate::readline::linebuf::LineBuf;
|
use crate::readline::linebuf::LineBuf;
|
||||||
use crate::readline::vicmd::{
|
use crate::readline::vicmd::{
|
||||||
@@ -33,16 +36,61 @@ bitflags! {
|
|||||||
struct ExEditor {
|
struct ExEditor {
|
||||||
buf: LineBuf,
|
buf: LineBuf,
|
||||||
mode: ViInsert,
|
mode: ViInsert,
|
||||||
|
history: History,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ExEditor {
|
impl ExEditor {
|
||||||
|
pub fn new(history: History) -> Self {
|
||||||
|
Self {
|
||||||
|
history,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
*self = Self::default()
|
*self = Self::default()
|
||||||
}
|
}
|
||||||
|
pub fn should_grab_history(&mut self, cmd: &ViCmd) -> bool {
|
||||||
|
cmd.verb().is_none()
|
||||||
|
&& (cmd
|
||||||
|
.motion()
|
||||||
|
.is_some_and(|m| matches!(m, MotionCmd(_, Motion::LineUp)))
|
||||||
|
&& self.buf.start_of_line() == 0)
|
||||||
|
|| (cmd
|
||||||
|
.motion()
|
||||||
|
.is_some_and(|m| matches!(m, MotionCmd(_, Motion::LineDown)))
|
||||||
|
&& self.buf.on_last_line())
|
||||||
|
}
|
||||||
|
pub fn scroll_history(&mut self, cmd: ViCmd) {
|
||||||
|
let count = &cmd.motion().unwrap().0;
|
||||||
|
let motion = &cmd.motion().unwrap().1;
|
||||||
|
let count = match motion {
|
||||||
|
Motion::LineUp => -(*count as isize),
|
||||||
|
Motion::LineDown => *count as isize,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
let entry = self.history.scroll(count);
|
||||||
|
if let Some(entry) = entry {
|
||||||
|
let buf = std::mem::take(&mut self.buf);
|
||||||
|
self.buf.set_buffer(entry.command().to_string());
|
||||||
|
if self.history.pending.is_none() {
|
||||||
|
self.history.pending = Some(buf);
|
||||||
|
}
|
||||||
|
self.buf.set_hint(None);
|
||||||
|
self.buf.move_cursor_to_end();
|
||||||
|
} else if let Some(pending) = self.history.pending.take() {
|
||||||
|
self.buf = pending;
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn handle_key(&mut self, key: KeyEvent) -> ShResult<()> {
|
pub fn handle_key(&mut self, key: KeyEvent) -> ShResult<()> {
|
||||||
let Some(cmd) = self.mode.handle_key(key) else {
|
let Some(cmd) = self.mode.handle_key(key) else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
log::debug!("ExEditor got cmd: {:?}", cmd);
|
||||||
|
if self.should_grab_history(&cmd) {
|
||||||
|
log::debug!("Grabbing history for cmd: {:?}", cmd);
|
||||||
|
self.scroll_history(cmd);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
self.buf.exec_cmd(cmd)
|
self.buf.exec_cmd(cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,8 +101,10 @@ pub struct ViEx {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ViEx {
|
impl ViEx {
|
||||||
pub fn new() -> Self {
|
pub fn new(history: History) -> Self {
|
||||||
Self::default()
|
Self {
|
||||||
|
pending_cmd: ExEditor::new(history),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,48 +112,34 @@ impl ViMode for ViEx {
|
|||||||
// Ex mode can return errors, so we use this fallible method instead of the normal one
|
// Ex mode can return errors, so we use this fallible method instead of the normal one
|
||||||
fn handle_key_fallible(&mut self, key: KeyEvent) -> ShResult<Option<ViCmd>> {
|
fn handle_key_fallible(&mut self, key: KeyEvent) -> ShResult<Option<ViCmd>> {
|
||||||
use crate::readline::keys::{KeyCode as C, KeyEvent as E, ModKeys as M};
|
use crate::readline::keys::{KeyCode as C, KeyEvent as E, ModKeys as M};
|
||||||
log::debug!("[ViEx] handle_key_fallible: key={:?}", key);
|
|
||||||
match key {
|
match key {
|
||||||
E(C::Char('\r'), M::NONE) | E(C::Enter, M::NONE) => {
|
E(C::Char('\r'), M::NONE) | E(C::Enter, M::NONE) => {
|
||||||
let input = self.pending_cmd.buf.as_str();
|
let input = self.pending_cmd.buf.joined();
|
||||||
log::debug!("[ViEx] Enter pressed, pending_cmd={:?}", input);
|
match parse_ex_cmd(&input) {
|
||||||
match parse_ex_cmd(input) {
|
Ok(cmd) => Ok(cmd),
|
||||||
Ok(cmd) => {
|
|
||||||
log::debug!("[ViEx] parse_ex_cmd Ok: {:?}", cmd);
|
|
||||||
Ok(cmd)
|
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::debug!("[ViEx] parse_ex_cmd Err: {:?}", e);
|
let msg = e.unwrap_or(format!("Not an editor command: {}", &input));
|
||||||
let msg = e.unwrap_or(format!("Not an editor command: {}", input));
|
write_meta(|m| m.post_status_message(msg.clone()));
|
||||||
write_meta(|m| m.post_system_message(msg.clone()));
|
|
||||||
Err(ShErr::simple(ShErrKind::ParseErr, msg))
|
Err(ShErr::simple(ShErrKind::ParseErr, msg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
E(C::Char('C'), M::CTRL) => {
|
E(C::Char('C'), M::CTRL) => {
|
||||||
log::debug!("[ViEx] Ctrl-C, clearing");
|
|
||||||
self.pending_cmd.clear();
|
self.pending_cmd.clear();
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
E(C::Esc, M::NONE) => {
|
E(C::Esc, M::NONE) => Ok(Some(ViCmd {
|
||||||
log::debug!("[ViEx] Esc, returning to normal mode");
|
|
||||||
Ok(Some(ViCmd {
|
|
||||||
register: RegisterName::default(),
|
register: RegisterName::default(),
|
||||||
verb: Some(VerbCmd(1, Verb::NormalMode)),
|
verb: Some(VerbCmd(1, Verb::NormalMode)),
|
||||||
motion: None,
|
motion: None,
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
raw_seq: "".into(),
|
raw_seq: "".into(),
|
||||||
}))
|
})),
|
||||||
}
|
_ => self.pending_cmd.handle_key(key).map(|_| None),
|
||||||
_ => {
|
|
||||||
log::debug!("[ViEx] forwarding key to ExEditor");
|
|
||||||
self.pending_cmd.handle_key(key).map(|_| None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn handle_key(&mut self, key: KeyEvent) -> Option<ViCmd> {
|
fn handle_key(&mut self, key: KeyEvent) -> Option<ViCmd> {
|
||||||
let result = self.handle_key_fallible(key);
|
let result = self.handle_key_fallible(key);
|
||||||
log::debug!("[ViEx] handle_key result: {:?}", result);
|
|
||||||
result.ok().flatten()
|
result.ok().flatten()
|
||||||
}
|
}
|
||||||
fn is_repeatable(&self) -> bool {
|
fn is_repeatable(&self) -> bool {
|
||||||
@@ -114,16 +150,24 @@ impl ViMode for ViEx {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn editor(&mut self) -> Option<&mut LineBuf> {
|
||||||
|
Some(&mut self.pending_cmd.buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn history(&mut self) -> Option<&mut History> {
|
||||||
|
Some(&mut self.pending_cmd.history)
|
||||||
|
}
|
||||||
|
|
||||||
fn cursor_style(&self) -> String {
|
fn cursor_style(&self) -> String {
|
||||||
"\x1b[3 q".to_string()
|
"\x1b[3 q".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pending_seq(&self) -> Option<String> {
|
fn pending_seq(&self) -> Option<String> {
|
||||||
Some(self.pending_cmd.buf.as_str().to_string())
|
Some(self.pending_cmd.buf.joined())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pending_cursor(&self) -> Option<usize> {
|
fn pending_cursor(&self) -> Option<usize> {
|
||||||
Some(self.pending_cmd.buf.cursor.get())
|
Some(self.pending_cmd.buf.cursor_to_flat())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_cursor_on_undo(&self) -> bool {
|
fn move_cursor_on_undo(&self) -> bool {
|
||||||
@@ -177,15 +221,12 @@ fn parse_ex_cmd(raw: &str) -> Result<Option<ViCmd>, Option<String>> {
|
|||||||
verb,
|
verb,
|
||||||
motion,
|
motion,
|
||||||
raw_seq: raw.to_string(),
|
raw_seq: raw.to_string(),
|
||||||
flags: CmdFlags::EXIT_CUR_MODE,
|
flags: CmdFlags::EXIT_CUR_MODE | CmdFlags::IS_EX_CMD,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unescape shell command arguments
|
/// Unescape shell command arguments
|
||||||
fn unescape_shell_cmd(cmd: &str) -> String {
|
fn unescape_shell_cmd(cmd: &str) -> String {
|
||||||
// The pest grammar uses double quotes for vicut commands
|
|
||||||
// So shell commands need to escape double quotes
|
|
||||||
// We will be removing a single layer of escaping from double quotes
|
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let mut chars = cmd.chars().peekable();
|
let mut chars = cmd.chars().peekable();
|
||||||
while let Some(ch) = chars.next() {
|
while let Some(ch) = chars.next() {
|
||||||
@@ -207,7 +248,7 @@ fn parse_ex_command(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Opt
|
|||||||
let mut cmd_name = String::new();
|
let mut cmd_name = String::new();
|
||||||
|
|
||||||
while let Some(ch) = chars.peek() {
|
while let Some(ch) = chars.peek() {
|
||||||
if ch == &'!' {
|
if cmd_name.is_empty() && ch == &'!' {
|
||||||
cmd_name.push(*ch);
|
cmd_name.push(*ch);
|
||||||
chars.next();
|
chars.next();
|
||||||
break;
|
break;
|
||||||
@@ -224,12 +265,19 @@ fn parse_ex_command(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Opt
|
|||||||
let cmd = unescape_shell_cmd(&cmd);
|
let cmd = unescape_shell_cmd(&cmd);
|
||||||
Ok(Some(Verb::ShellCmd(cmd)))
|
Ok(Some(Verb::ShellCmd(cmd)))
|
||||||
}
|
}
|
||||||
|
_ if "help".starts_with(&cmd_name) => {
|
||||||
|
let cmd = "help ".to_string() + chars.collect::<String>().trim();
|
||||||
|
log::debug!("Parsed help command: {}", cmd);
|
||||||
|
Ok(Some(Verb::ShellCmd(cmd)))
|
||||||
|
}
|
||||||
"normal!" => parse_normal(chars),
|
"normal!" => parse_normal(chars),
|
||||||
_ if "delete".starts_with(&cmd_name) => Ok(Some(Verb::Delete)),
|
_ if "delete".starts_with(&cmd_name) => Ok(Some(Verb::Delete)),
|
||||||
_ if "yank".starts_with(&cmd_name) => Ok(Some(Verb::Yank)),
|
_ if "yank".starts_with(&cmd_name) => Ok(Some(Verb::Yank)),
|
||||||
_ if "put".starts_with(&cmd_name) => Ok(Some(Verb::Put(Anchor::After))),
|
_ if "put".starts_with(&cmd_name) => Ok(Some(Verb::Put(Anchor::After))),
|
||||||
|
_ if "quit".starts_with(&cmd_name) => Ok(Some(Verb::Quit)),
|
||||||
_ if "read".starts_with(&cmd_name) => parse_read(chars),
|
_ if "read".starts_with(&cmd_name) => parse_read(chars),
|
||||||
_ if "write".starts_with(&cmd_name) => parse_write(chars),
|
_ if "write".starts_with(&cmd_name) => parse_write(chars),
|
||||||
|
_ if "edit".starts_with(&cmd_name) => parse_edit(chars),
|
||||||
_ if "substitute".starts_with(&cmd_name) => parse_substitute(chars),
|
_ if "substitute".starts_with(&cmd_name) => parse_substitute(chars),
|
||||||
_ => Err(None),
|
_ => Err(None),
|
||||||
}
|
}
|
||||||
@@ -244,6 +292,19 @@ fn parse_normal(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<
|
|||||||
Ok(Some(Verb::Normal(seq)))
|
Ok(Some(Verb::Normal(seq)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_edit(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<String>> {
|
||||||
|
chars
|
||||||
|
.peeking_take_while(|c| c.is_whitespace())
|
||||||
|
.for_each(drop);
|
||||||
|
|
||||||
|
let arg: String = chars.collect();
|
||||||
|
if arg.trim().is_empty() {
|
||||||
|
return Err(Some("Expected file path after ':edit'".into()));
|
||||||
|
}
|
||||||
|
let arg_path = get_path(arg.trim())?;
|
||||||
|
Ok(Some(Verb::Edit(arg_path)))
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_read(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<String>> {
|
fn parse_read(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<String>> {
|
||||||
chars
|
chars
|
||||||
.peeking_take_while(|c| c.is_whitespace())
|
.peeking_take_while(|c| c.is_whitespace())
|
||||||
@@ -266,23 +327,20 @@ fn parse_read(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<St
|
|||||||
if is_shell_read {
|
if is_shell_read {
|
||||||
Ok(Some(Verb::Read(ReadSrc::Cmd(arg))))
|
Ok(Some(Verb::Read(ReadSrc::Cmd(arg))))
|
||||||
} else {
|
} else {
|
||||||
let arg_path = get_path(arg.trim());
|
let arg_path = get_path(arg.trim())?;
|
||||||
Ok(Some(Verb::Read(ReadSrc::File(arg_path))))
|
Ok(Some(Verb::Read(ReadSrc::File(arg_path))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_path(path: &str) -> PathBuf {
|
fn get_path(path: &str) -> Result<PathBuf, Option<String>> {
|
||||||
if let Some(stripped) = path.strip_prefix("~/")
|
log::debug!("Expanding path: {}", path);
|
||||||
&& let Some(home) = std::env::var_os("HOME")
|
let expanded = Expander::from_raw(path, TkFlags::empty())
|
||||||
{
|
.map_err(|e| Some(format!("Error expanding path: {}", e)))?
|
||||||
return PathBuf::from(home).join(stripped);
|
.expand()
|
||||||
}
|
.map_err(|e| Some(format!("Error expanding path: {}", e)))?
|
||||||
if path == "~"
|
.join(" ");
|
||||||
&& let Some(home) = std::env::var_os("HOME")
|
log::debug!("Expanded path: {}", expanded);
|
||||||
{
|
Ok(PathBuf::from(&expanded))
|
||||||
return PathBuf::from(home);
|
|
||||||
}
|
|
||||||
PathBuf::from(path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_write(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<String>> {
|
fn parse_write(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<String>> {
|
||||||
@@ -305,7 +363,7 @@ fn parse_write(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>, Option<S
|
|||||||
}
|
}
|
||||||
|
|
||||||
let arg: String = chars.collect();
|
let arg: String = chars.collect();
|
||||||
let arg_path = get_path(arg.trim());
|
let arg_path = get_path(arg.trim())?;
|
||||||
|
|
||||||
let dest = if is_file_append {
|
let dest = if is_file_append {
|
||||||
WriteDest::FileAppend(arg_path)
|
WriteDest::FileAppend(arg_path)
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ impl ViInsert {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
pub fn record_cmd(mut self, cmd: ViCmd) -> Self {
|
||||||
|
self.cmds.push(cmd);
|
||||||
|
self
|
||||||
|
}
|
||||||
pub fn with_count(mut self, repeat_count: u16) -> Self {
|
pub fn with_count(mut self, repeat_count: u16) -> Self {
|
||||||
self.repeat_count = repeat_count;
|
self.repeat_count = repeat_count;
|
||||||
self
|
self
|
||||||
@@ -62,7 +66,9 @@ impl ViMode for ViInsert {
|
|||||||
flags: Default::default(),
|
flags: Default::default(),
|
||||||
}),
|
}),
|
||||||
E(K::Verbatim(seq), _) => {
|
E(K::Verbatim(seq), _) => {
|
||||||
self.pending_cmd.set_verb(VerbCmd(1, Verb::Insert(seq.to_string())));
|
self
|
||||||
|
.pending_cmd
|
||||||
|
.set_verb(VerbCmd(1, Verb::Insert(seq.to_string())));
|
||||||
self.register_and_return()
|
self.register_and_return()
|
||||||
}
|
}
|
||||||
E(K::Char('W'), M::CTRL) => {
|
E(K::Char('W'), M::CTRL) => {
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ use std::fmt::Display;
|
|||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
use crate::libsh::error::ShResult;
|
use crate::libsh::error::ShResult;
|
||||||
|
use crate::readline::history::History;
|
||||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||||
|
use crate::readline::linebuf::LineBuf;
|
||||||
use crate::readline::vicmd::{Motion, MotionCmd, To, Verb, VerbCmd, ViCmd};
|
use crate::readline::vicmd::{Motion, MotionCmd, To, Verb, VerbCmd, ViCmd};
|
||||||
|
|
||||||
pub mod ex;
|
pub mod ex;
|
||||||
@@ -82,6 +84,12 @@ pub trait ViMode {
|
|||||||
fn pending_cursor(&self) -> Option<usize> {
|
fn pending_cursor(&self) -> Option<usize> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
fn editor(&mut self) -> Option<&mut LineBuf> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
fn history(&mut self) -> Option<&mut History> {
|
||||||
|
None
|
||||||
|
}
|
||||||
fn move_cursor_on_undo(&self) -> bool;
|
fn move_cursor_on_undo(&self) -> bool;
|
||||||
fn clamp_cursor(&self) -> bool;
|
fn clamp_cursor(&self) -> bool;
|
||||||
fn hist_scroll_start_pos(&self) -> Option<To>;
|
fn hist_scroll_start_pos(&self) -> Option<To>;
|
||||||
@@ -102,21 +110,22 @@ pub trait ViMode {
|
|||||||
pub fn common_cmds(key: E) -> Option<ViCmd> {
|
pub fn common_cmds(key: E) -> Option<ViCmd> {
|
||||||
let mut pending_cmd = ViCmd::new();
|
let mut pending_cmd = ViCmd::new();
|
||||||
match key {
|
match key {
|
||||||
E(K::Home, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::BeginningOfLine)),
|
E(K::Home, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::StartOfLine)),
|
||||||
E(K::End, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::EndOfLine)),
|
E(K::End, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::EndOfLine)),
|
||||||
E(K::Left, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::BackwardChar)),
|
E(K::Left, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::BackwardChar)),
|
||||||
E(K::Right, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::ForwardChar)),
|
E(K::Right, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::ForwardChar)),
|
||||||
E(K::Up, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::LineUp)),
|
E(K::Up, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::LineUp)),
|
||||||
E(K::Down, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::LineDown)),
|
E(K::Down, M::NONE) => pending_cmd.set_motion(MotionCmd(1, Motion::LineDown)),
|
||||||
|
E(K::Enter, M::SHIFT) => pending_cmd.set_verb(VerbCmd(1, Verb::InsertChar('\n'))),
|
||||||
E(K::Enter, M::NONE) => pending_cmd.set_verb(VerbCmd(1, Verb::AcceptLineOrNewline)),
|
E(K::Enter, M::NONE) => pending_cmd.set_verb(VerbCmd(1, Verb::AcceptLineOrNewline)),
|
||||||
E(K::Char('D'), M::CTRL) => pending_cmd.set_verb(VerbCmd(1, Verb::EndOfFile)),
|
E(K::Char('D'), M::CTRL) => pending_cmd.set_verb(VerbCmd(1, Verb::EndOfFile)),
|
||||||
E(K::Delete, M::NONE) => {
|
E(K::Delete, M::NONE) => {
|
||||||
pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
||||||
pending_cmd.set_motion(MotionCmd(1, Motion::ForwardChar));
|
pending_cmd.set_motion(MotionCmd(1, Motion::ForwardCharForced));
|
||||||
}
|
}
|
||||||
E(K::Backspace, M::NONE) | E(K::Char('H'), M::CTRL) => {
|
E(K::Backspace, M::NONE) | E(K::Char('H'), M::CTRL) => {
|
||||||
pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
||||||
pending_cmd.set_motion(MotionCmd(1, Motion::BackwardChar));
|
pending_cmd.set_motion(MotionCmd(1, Motion::BackwardCharForced));
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::str::Chars;
|
|||||||
|
|
||||||
use super::{CmdReplay, CmdState, ModeReport, ViMode, common_cmds};
|
use super::{CmdReplay, CmdState, ModeReport, ViMode, common_cmds};
|
||||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||||
|
use crate::readline::linebuf::Grapheme;
|
||||||
use crate::readline::vicmd::{
|
use crate::readline::vicmd::{
|
||||||
Anchor, Bound, CmdFlags, Dest, Direction, Motion, MotionCmd, RegisterName, TextObj, To, Verb,
|
Anchor, Bound, CmdFlags, Dest, Direction, Motion, MotionCmd, RegisterName, TextObj, To, Verb,
|
||||||
VerbCmd, ViCmd, Word,
|
VerbCmd, ViCmd, Word,
|
||||||
@@ -197,7 +198,7 @@ impl ViNormal {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(count, Verb::Change)),
|
verb: Some(VerbCmd(count, Verb::Change)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineExclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: self.flags(),
|
flags: self.flags(),
|
||||||
});
|
});
|
||||||
@@ -331,7 +332,7 @@ impl ViNormal {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||||
motion: Some(MotionCmd(1, Motion::BeginningOfFirstWord)),
|
motion: Some(MotionCmd(1, Motion::StartOfFirstWord)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: self.flags(),
|
flags: self.flags(),
|
||||||
});
|
});
|
||||||
@@ -411,10 +412,10 @@ impl ViNormal {
|
|||||||
| ('~', Some(VerbCmd(_, Verb::ToggleCaseRange)))
|
| ('~', Some(VerbCmd(_, Verb::ToggleCaseRange)))
|
||||||
| ('>', Some(VerbCmd(_, Verb::Indent)))
|
| ('>', Some(VerbCmd(_, Verb::Indent)))
|
||||||
| ('<', Some(VerbCmd(_, Verb::Dedent))) => {
|
| ('<', Some(VerbCmd(_, Verb::Dedent))) => {
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineInclusive));
|
break 'motion_parse Some(MotionCmd(count, Motion::WholeLine));
|
||||||
}
|
}
|
||||||
('c', Some(VerbCmd(_, Verb::Change))) => {
|
('c', Some(VerbCmd(_, Verb::Change))) => {
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineExclusive));
|
break 'motion_parse Some(MotionCmd(count, Motion::WholeLine));
|
||||||
}
|
}
|
||||||
('W', Some(VerbCmd(_, Verb::Change))) => {
|
('W', Some(VerbCmd(_, Verb::Change))) => {
|
||||||
// Same with 'W'
|
// Same with 'W'
|
||||||
@@ -434,7 +435,7 @@ impl ViNormal {
|
|||||||
'g' => {
|
'g' => {
|
||||||
chars_clone.next();
|
chars_clone.next();
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfBuffer));
|
break 'motion_parse Some(MotionCmd(count, Motion::StartOfBuffer));
|
||||||
}
|
}
|
||||||
'e' => {
|
'e' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
@@ -450,26 +451,10 @@ impl ViNormal {
|
|||||||
Motion::WordMotion(To::End, Word::Big, Direction::Backward),
|
Motion::WordMotion(To::End, Word::Big, Direction::Backward),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
'k' => {
|
|
||||||
chars = chars_clone;
|
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineUp));
|
|
||||||
}
|
|
||||||
'j' => {
|
|
||||||
chars = chars_clone;
|
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineDown));
|
|
||||||
}
|
|
||||||
'_' => {
|
'_' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::EndOfLastWord));
|
break 'motion_parse Some(MotionCmd(count, Motion::EndOfLastWord));
|
||||||
}
|
}
|
||||||
'0' => {
|
|
||||||
chars = chars_clone;
|
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfScreenLine));
|
|
||||||
}
|
|
||||||
'^' => {
|
|
||||||
chars = chars_clone;
|
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::FirstGraphicalOnScreenLine));
|
|
||||||
}
|
|
||||||
_ => return self.quit_parse(),
|
_ => return self.quit_parse(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -551,7 +536,7 @@ impl ViNormal {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Forward, Dest::On, *ch),
|
Motion::CharSearch(Direction::Forward, Dest::On, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
'F' => {
|
'F' => {
|
||||||
@@ -561,7 +546,7 @@ impl ViNormal {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Backward, Dest::On, *ch),
|
Motion::CharSearch(Direction::Backward, Dest::On, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
't' => {
|
't' => {
|
||||||
@@ -571,7 +556,7 @@ impl ViNormal {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Forward, Dest::Before, *ch),
|
Motion::CharSearch(Direction::Forward, Dest::Before, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
'T' => {
|
'T' => {
|
||||||
@@ -581,7 +566,7 @@ impl ViNormal {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Backward, Dest::Before, *ch),
|
Motion::CharSearch(Direction::Backward, Dest::Before, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
';' => {
|
';' => {
|
||||||
@@ -598,11 +583,11 @@ impl ViNormal {
|
|||||||
}
|
}
|
||||||
'^' => {
|
'^' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfFirstWord));
|
break 'motion_parse Some(MotionCmd(count, Motion::StartOfFirstWord));
|
||||||
}
|
}
|
||||||
'0' => {
|
'0' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfLine));
|
break 'motion_parse Some(MotionCmd(count, Motion::StartOfLine));
|
||||||
}
|
}
|
||||||
'$' => {
|
'$' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
@@ -791,6 +776,36 @@ impl ViMode for ViNormal {
|
|||||||
flags: self.flags(),
|
flags: self.flags(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
E(K::Char('G'), M::CTRL) => {
|
||||||
|
self.pending_seq.clear();
|
||||||
|
Some(ViCmd {
|
||||||
|
register: Default::default(),
|
||||||
|
verb: Some(VerbCmd(1, Verb::PrintPosition)),
|
||||||
|
motion: None,
|
||||||
|
raw_seq: "".into(),
|
||||||
|
flags: self.flags(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
E(K::Char('D'), M::CTRL) => {
|
||||||
|
self.pending_seq.clear();
|
||||||
|
Some(ViCmd {
|
||||||
|
register: Default::default(),
|
||||||
|
verb: None,
|
||||||
|
motion: Some(MotionCmd(1, Motion::HalfScreenDown)),
|
||||||
|
raw_seq: "".into(),
|
||||||
|
flags: self.flags(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
E(K::Char('U'), M::CTRL) => {
|
||||||
|
self.pending_seq.clear();
|
||||||
|
Some(ViCmd {
|
||||||
|
register: Default::default(),
|
||||||
|
verb: None,
|
||||||
|
motion: Some(MotionCmd(1, Motion::HalfScreenUp)),
|
||||||
|
raw_seq: "".into(),
|
||||||
|
flags: self.flags(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
E(K::Char(ch), M::NONE) => self.try_parse(ch),
|
E(K::Char(ch), M::NONE) => self.try_parse(ch),
|
||||||
E(K::Backspace, M::NONE) => Some(ViCmd {
|
E(K::Backspace, M::NONE) => Some(ViCmd {
|
||||||
|
|||||||
@@ -4,19 +4,11 @@ use crate::readline::vicmd::{CmdFlags, RegisterName, To, Verb, VerbCmd, ViCmd};
|
|||||||
|
|
||||||
#[derive(Default, Clone, Debug)]
|
#[derive(Default, Clone, Debug)]
|
||||||
pub struct ViVerbatim {
|
pub struct ViVerbatim {
|
||||||
pending_seq: String,
|
|
||||||
sent_cmd: Vec<ViCmd>,
|
sent_cmd: Vec<ViCmd>,
|
||||||
repeat_count: u16,
|
repeat_count: u16,
|
||||||
read_one: bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ViVerbatim {
|
impl ViVerbatim {
|
||||||
pub fn read_one() -> Self {
|
|
||||||
Self {
|
|
||||||
read_one: true,
|
|
||||||
..Self::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
@@ -31,7 +23,7 @@ impl ViVerbatim {
|
|||||||
impl ViMode for ViVerbatim {
|
impl ViMode for ViVerbatim {
|
||||||
fn handle_key(&mut self, key: E) -> Option<ViCmd> {
|
fn handle_key(&mut self, key: E) -> Option<ViCmd> {
|
||||||
match key {
|
match key {
|
||||||
E(K::Verbatim(seq), _mods) if self.read_one => {
|
E(K::Verbatim(seq), _mods) => {
|
||||||
log::debug!("Received verbatim key sequence: {:?}", seq);
|
log::debug!("Received verbatim key sequence: {:?}", seq);
|
||||||
let cmd = ViCmd {
|
let cmd = ViCmd {
|
||||||
register: RegisterName::default(),
|
register: RegisterName::default(),
|
||||||
@@ -43,22 +35,6 @@ impl ViMode for ViVerbatim {
|
|||||||
self.sent_cmd.push(cmd.clone());
|
self.sent_cmd.push(cmd.clone());
|
||||||
Some(cmd)
|
Some(cmd)
|
||||||
}
|
}
|
||||||
E(K::Verbatim(seq), _mods) => {
|
|
||||||
self.pending_seq.push_str(&seq);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
E(K::BracketedPasteEnd, _mods) => {
|
|
||||||
log::debug!("Received verbatim paste: {:?}", self.pending_seq);
|
|
||||||
let cmd = ViCmd {
|
|
||||||
register: RegisterName::default(),
|
|
||||||
verb: Some(VerbCmd(1, Verb::Insert(self.pending_seq.clone()))),
|
|
||||||
motion: None,
|
|
||||||
raw_seq: std::mem::take(&mut self.pending_seq),
|
|
||||||
flags: CmdFlags::EXIT_CUR_MODE,
|
|
||||||
};
|
|
||||||
self.sent_cmd.push(cmd.clone());
|
|
||||||
Some(cmd)
|
|
||||||
}
|
|
||||||
_ => common_cmds(key),
|
_ => common_cmds(key),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::str::Chars;
|
|||||||
|
|
||||||
use super::{CmdReplay, CmdState, ModeReport, ViMode, common_cmds};
|
use super::{CmdReplay, CmdState, ModeReport, ViMode, common_cmds};
|
||||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||||
|
use crate::readline::linebuf::Grapheme;
|
||||||
use crate::readline::vicmd::{
|
use crate::readline::vicmd::{
|
||||||
Anchor, Bound, CmdFlags, Dest, Direction, Motion, MotionCmd, RegisterName, TextObj, To, Verb,
|
Anchor, Bound, CmdFlags, Dest, Direction, Motion, MotionCmd, RegisterName, TextObj, To, Verb,
|
||||||
VerbCmd, ViCmd, Word,
|
VerbCmd, ViCmd, Word,
|
||||||
@@ -146,7 +147,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(1, Verb::Delete)),
|
verb: Some(VerbCmd(1, Verb::Delete)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -155,7 +156,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(1, Verb::Yank)),
|
verb: Some(VerbCmd(1, Verb::Yank)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -164,7 +165,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(1, Verb::Delete)),
|
verb: Some(VerbCmd(1, Verb::Delete)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -173,7 +174,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(1, Verb::Change)),
|
verb: Some(VerbCmd(1, Verb::Change)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineExclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -182,7 +183,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(1, Verb::Indent)),
|
verb: Some(VerbCmd(1, Verb::Indent)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -191,7 +192,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(1, Verb::Dedent)),
|
verb: Some(VerbCmd(1, Verb::Dedent)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -200,7 +201,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(1, Verb::Equalize)),
|
verb: Some(VerbCmd(1, Verb::Equalize)),
|
||||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
motion: Some(MotionCmd(1, Motion::WholeLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -237,6 +238,24 @@ impl ViVisual {
|
|||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
's' => {
|
||||||
|
return Some(ViCmd {
|
||||||
|
register,
|
||||||
|
verb: Some(VerbCmd(count, Verb::Delete)),
|
||||||
|
motion: None,
|
||||||
|
raw_seq: self.take_cmd(),
|
||||||
|
flags: CmdFlags::empty(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
'S' => {
|
||||||
|
return Some(ViCmd {
|
||||||
|
register,
|
||||||
|
verb: Some(VerbCmd(count, Verb::Change)),
|
||||||
|
motion: None,
|
||||||
|
raw_seq: self.take_cmd(),
|
||||||
|
flags: CmdFlags::empty(),
|
||||||
|
});
|
||||||
|
}
|
||||||
'U' => {
|
'U' => {
|
||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
@@ -268,7 +287,7 @@ impl ViVisual {
|
|||||||
return Some(ViCmd {
|
return Some(ViCmd {
|
||||||
register,
|
register,
|
||||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||||
motion: Some(MotionCmd(1, Motion::BeginningOfLine)),
|
motion: Some(MotionCmd(1, Motion::StartOfLine)),
|
||||||
raw_seq: self.take_cmd(),
|
raw_seq: self.take_cmd(),
|
||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
});
|
});
|
||||||
@@ -283,8 +302,13 @@ impl ViVisual {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
'y' => {
|
'y' => {
|
||||||
chars = chars_clone;
|
return Some(ViCmd {
|
||||||
break 'verb_parse Some(VerbCmd(count, Verb::Yank));
|
register,
|
||||||
|
verb: Some(VerbCmd(count, Verb::Yank)),
|
||||||
|
motion: None,
|
||||||
|
raw_seq: self.take_cmd(),
|
||||||
|
flags: CmdFlags::empty(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
'd' => {
|
'd' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
@@ -321,10 +345,10 @@ impl ViVisual {
|
|||||||
| ('=', Some(VerbCmd(_, Verb::Equalize)))
|
| ('=', Some(VerbCmd(_, Verb::Equalize)))
|
||||||
| ('>', Some(VerbCmd(_, Verb::Indent)))
|
| ('>', Some(VerbCmd(_, Verb::Indent)))
|
||||||
| ('<', Some(VerbCmd(_, Verb::Dedent))) => {
|
| ('<', Some(VerbCmd(_, Verb::Dedent))) => {
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineInclusive));
|
break 'motion_parse Some(MotionCmd(count, Motion::WholeLine));
|
||||||
}
|
}
|
||||||
('c', Some(VerbCmd(_, Verb::Change))) => {
|
('c', Some(VerbCmd(_, Verb::Change))) => {
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineExclusive));
|
break 'motion_parse Some(MotionCmd(count, Motion::WholeLine));
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -335,7 +359,7 @@ impl ViVisual {
|
|||||||
'g' => {
|
'g' => {
|
||||||
chars_clone.next();
|
chars_clone.next();
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfBuffer));
|
break 'motion_parse Some(MotionCmd(count, Motion::StartOfBuffer));
|
||||||
}
|
}
|
||||||
'e' => {
|
'e' => {
|
||||||
chars_clone.next();
|
chars_clone.next();
|
||||||
@@ -353,16 +377,6 @@ impl ViVisual {
|
|||||||
Motion::WordMotion(To::End, Word::Big, Direction::Backward),
|
Motion::WordMotion(To::End, Word::Big, Direction::Backward),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
'k' => {
|
|
||||||
chars_clone.next();
|
|
||||||
chars = chars_clone;
|
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineUp));
|
|
||||||
}
|
|
||||||
'j' => {
|
|
||||||
chars_clone.next();
|
|
||||||
chars = chars_clone;
|
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineDown));
|
|
||||||
}
|
|
||||||
_ => return self.quit_parse(),
|
_ => return self.quit_parse(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -412,7 +426,7 @@ impl ViVisual {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Forward, Dest::On, *ch),
|
Motion::CharSearch(Direction::Forward, Dest::On, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
'F' => {
|
'F' => {
|
||||||
@@ -422,7 +436,7 @@ impl ViVisual {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Backward, Dest::On, *ch),
|
Motion::CharSearch(Direction::Backward, Dest::On, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
't' => {
|
't' => {
|
||||||
@@ -432,7 +446,7 @@ impl ViVisual {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Forward, Dest::Before, *ch),
|
Motion::CharSearch(Direction::Forward, Dest::Before, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
'T' => {
|
'T' => {
|
||||||
@@ -442,7 +456,7 @@ impl ViVisual {
|
|||||||
|
|
||||||
break 'motion_parse Some(MotionCmd(
|
break 'motion_parse Some(MotionCmd(
|
||||||
count,
|
count,
|
||||||
Motion::CharSearch(Direction::Backward, Dest::Before, *ch),
|
Motion::CharSearch(Direction::Backward, Dest::Before, Grapheme::from(*ch)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
';' => {
|
';' => {
|
||||||
@@ -459,7 +473,7 @@ impl ViVisual {
|
|||||||
}
|
}
|
||||||
'0' => {
|
'0' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfLine));
|
break 'motion_parse Some(MotionCmd(count, Motion::StartOfLine));
|
||||||
}
|
}
|
||||||
'$' => {
|
'$' => {
|
||||||
chars = chars_clone;
|
chars = chars_clone;
|
||||||
@@ -649,6 +663,36 @@ impl ViMode for ViVisual {
|
|||||||
flags: CmdFlags::empty(),
|
flags: CmdFlags::empty(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
E(K::Char('G'), M::CTRL) => {
|
||||||
|
self.pending_seq.clear();
|
||||||
|
Some(ViCmd {
|
||||||
|
register: Default::default(),
|
||||||
|
verb: Some(VerbCmd(1, Verb::PrintPosition)),
|
||||||
|
motion: None,
|
||||||
|
raw_seq: "".into(),
|
||||||
|
flags: CmdFlags::empty()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
E(K::Char('D'), M::CTRL) => {
|
||||||
|
self.pending_seq.clear();
|
||||||
|
Some(ViCmd {
|
||||||
|
register: Default::default(),
|
||||||
|
verb: None,
|
||||||
|
motion: Some(MotionCmd(1, Motion::HalfScreenDown)),
|
||||||
|
raw_seq: "".into(),
|
||||||
|
flags: CmdFlags::empty(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
E(K::Char('U'), M::CTRL) => {
|
||||||
|
self.pending_seq.clear();
|
||||||
|
Some(ViCmd {
|
||||||
|
register: Default::default(),
|
||||||
|
verb: None,
|
||||||
|
motion: Some(MotionCmd(1, Motion::HalfScreenUp)),
|
||||||
|
raw_seq: "".into(),
|
||||||
|
flags: CmdFlags::empty(),
|
||||||
|
})
|
||||||
|
}
|
||||||
E(K::Char('R'), M::CTRL) => {
|
E(K::Char('R'), M::CTRL) => {
|
||||||
let mut chars = self.pending_seq.chars().peekable();
|
let mut chars = self.pending_seq.chars().peekable();
|
||||||
let count = self.parse_count(&mut chars).unwrap_or(1);
|
let count = self.parse_count(&mut chars).unwrap_or(1);
|
||||||
|
|||||||
611
src/shopt.rs
611
src/shopt.rs
@@ -2,6 +2,35 @@ use std::{fmt::Display, str::FromStr};
|
|||||||
|
|
||||||
use crate::libsh::error::{ShErr, ShErrKind, ShResult};
|
use crate::libsh::error::{ShErr, ShErrKind, ShResult};
|
||||||
|
|
||||||
|
/// Escapes a string for embedding inside single quotes.
|
||||||
|
/// Only escapes unescaped `\` and `'` characters.
|
||||||
|
pub fn escape_for_single_quote(s: &str) -> String {
|
||||||
|
let mut result = String::with_capacity(s.len());
|
||||||
|
let mut chars = s.chars().peekable();
|
||||||
|
while let Some(ch) = chars.next() {
|
||||||
|
if ch == '\\' {
|
||||||
|
match chars.peek() {
|
||||||
|
Some(&'\\') | Some(&'\'') => {
|
||||||
|
// Already escaped — pass through both characters
|
||||||
|
result.push(ch);
|
||||||
|
result.push(chars.next().unwrap());
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// Lone backslash — escape it
|
||||||
|
result.push('\\');
|
||||||
|
result.push('\\');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if ch == '\'' {
|
||||||
|
result.push('\\');
|
||||||
|
result.push('\'');
|
||||||
|
} else {
|
||||||
|
result.push(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum ShedBellStyle {
|
pub enum ShedBellStyle {
|
||||||
Audible,
|
Audible,
|
||||||
@@ -24,49 +53,113 @@ impl FromStr for ShedBellStyle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, Copy, Debug)]
|
/// Generates a shopt group struct with `set`, `get`, `Display`, and `Default` impls.
|
||||||
pub enum ShedEditMode {
|
///
|
||||||
#[default]
|
/// Doc comments on each field become the description shown by `shopt get`.
|
||||||
Vi,
|
/// Every field type must implement `FromStr + Display`.
|
||||||
Emacs,
|
///
|
||||||
|
/// Optional per-field validation: `#[validate(|val| expr)]` runs after parsing
|
||||||
|
/// and must return `Result<(), String>` where the error string is the message.
|
||||||
|
macro_rules! shopt_group {
|
||||||
|
(
|
||||||
|
$(#[$struct_meta:meta])*
|
||||||
|
pub struct $name:ident ($group_name:literal) {
|
||||||
|
$(
|
||||||
|
$(#[doc = $desc:literal])*
|
||||||
|
$(#[validate($validator:expr)])?
|
||||||
|
$field:ident : $ty:ty = $default:expr
|
||||||
|
),* $(,)?
|
||||||
|
}
|
||||||
|
) => {
|
||||||
|
$(#[$struct_meta])*
|
||||||
|
pub struct $name {
|
||||||
|
$(pub $field: $ty,)*
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for ShedEditMode {
|
impl Default for $name {
|
||||||
type Err = ShErr;
|
fn default() -> Self {
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
Self {
|
||||||
match s.to_ascii_lowercase().as_str() {
|
$($field: $default,)*
|
||||||
"vi" => Ok(Self::Vi),
|
}
|
||||||
"emacs" => Ok(Self::Emacs),
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl $name {
|
||||||
|
pub fn set(&mut self, opt: &str, val: &str) -> ShResult<()> {
|
||||||
|
match opt {
|
||||||
|
$(
|
||||||
|
stringify!($field) => {
|
||||||
|
let parsed = val.parse::<$ty>().map_err(|_| {
|
||||||
|
ShErr::simple(
|
||||||
|
ShErrKind::SyntaxErr,
|
||||||
|
format!("shopt: invalid value '{}' for {}.{}", val, $group_name, opt),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
$(
|
||||||
|
let validate: fn(&$ty) -> Result<(), String> = $validator;
|
||||||
|
validate(&parsed).map_err(|msg| {
|
||||||
|
ShErr::simple(ShErrKind::SyntaxErr, format!("shopt: {msg}"))
|
||||||
|
})?;
|
||||||
|
)?
|
||||||
|
self.$field = parsed;
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
_ => {
|
||||||
|
return Err(ShErr::simple(
|
||||||
|
ShErrKind::SyntaxErr,
|
||||||
|
format!("shopt: unexpected '{}' option '{opt}'", $group_name),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, query: &str) -> ShResult<Option<String>> {
|
||||||
|
if query.is_empty() {
|
||||||
|
return Ok(Some(format!("{self}")));
|
||||||
|
}
|
||||||
|
match query {
|
||||||
|
$(
|
||||||
|
stringify!($field) => {
|
||||||
|
let desc = concat!($($desc, "\n",)*);
|
||||||
|
let output = format!("{}{}", desc, self.$field);
|
||||||
|
Ok(Some(output))
|
||||||
|
}
|
||||||
|
)*
|
||||||
_ => Err(ShErr::simple(
|
_ => Err(ShErr::simple(
|
||||||
ShErrKind::SyntaxErr,
|
ShErrKind::SyntaxErr,
|
||||||
format!("Invalid edit mode '{s}'"),
|
format!("shopt: unexpected '{}' option '{query}'", $group_name),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ShedEditMode {
|
impl Display for $name {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
let output = [
|
||||||
ShedEditMode::Vi => write!(f, "vi"),
|
$(format!("{}.{}='{}'", $group_name, stringify!($field),
|
||||||
ShedEditMode::Emacs => write!(f, "emacs"),
|
$crate::shopt::escape_for_single_quote(&self.$field.to_string())),)*
|
||||||
|
];
|
||||||
|
writeln!(f, "{}", output.join("\n"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ShOpts {
|
pub struct ShOpts {
|
||||||
pub core: ShOptCore,
|
pub core: ShOptCore,
|
||||||
|
pub line: ShOptLine,
|
||||||
pub prompt: ShOptPrompt,
|
pub prompt: ShOptPrompt,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ShOpts {
|
impl Default for ShOpts {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let core = ShOptCore::default();
|
let core = ShOptCore::default();
|
||||||
|
let line = ShOptLine::default();
|
||||||
let prompt = ShOptPrompt::default();
|
let prompt = ShOptPrompt::default();
|
||||||
|
|
||||||
Self { core, prompt }
|
Self { core, line, prompt }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,8 +175,9 @@ impl ShOpts {
|
|||||||
|
|
||||||
pub fn display_opts(&mut self) -> ShResult<String> {
|
pub fn display_opts(&mut self) -> ShResult<String> {
|
||||||
let output = [
|
let output = [
|
||||||
format!("core:\n{}", self.query("core")?.unwrap_or_default()),
|
self.query("core")?.unwrap_or_default().to_string(),
|
||||||
format!("prompt:\n{}", self.query("prompt")?.unwrap_or_default()),
|
self.query("line")?.unwrap_or_default().to_string(),
|
||||||
|
self.query("prompt")?.unwrap_or_default().to_string(),
|
||||||
];
|
];
|
||||||
|
|
||||||
Ok(output.join("\n"))
|
Ok(output.join("\n"))
|
||||||
@@ -102,6 +196,7 @@ impl ShOpts {
|
|||||||
|
|
||||||
match key {
|
match key {
|
||||||
"core" => self.core.set(&remainder, val)?,
|
"core" => self.core.set(&remainder, val)?,
|
||||||
|
"line" => self.line.set(&remainder, val)?,
|
||||||
"prompt" => self.prompt.set(&remainder, val)?,
|
"prompt" => self.prompt.set(&remainder, val)?,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ShErr::simple(
|
return Err(ShErr::simple(
|
||||||
@@ -126,6 +221,7 @@ impl ShOpts {
|
|||||||
|
|
||||||
match key {
|
match key {
|
||||||
"core" => self.core.get(&remainder),
|
"core" => self.core.get(&remainder),
|
||||||
|
"line" => self.line.get(&remainder),
|
||||||
"prompt" => self.prompt.get(&remainder),
|
"prompt" => self.prompt.get(&remainder),
|
||||||
_ => Err(ShErr::simple(
|
_ => Err(ShErr::simple(
|
||||||
ShErrKind::SyntaxErr,
|
ShErrKind::SyntaxErr,
|
||||||
@@ -135,409 +231,88 @@ impl ShOpts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shopt_group! {
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ShOptCore {
|
pub struct ShOptLine ("line") {
|
||||||
pub dotglob: bool,
|
/// The maximum height of the line editor viewport window. Can be a positive number or a percentage of terminal height like "50%"
|
||||||
pub autocd: bool,
|
viewport_height: String = "50%".to_string(),
|
||||||
pub hist_ignore_dupes: bool,
|
/// The line offset from the top or bottom of the viewport to trigger scrolling
|
||||||
pub max_hist: isize,
|
scroll_offset: usize = 2,
|
||||||
pub interactive_comments: bool,
|
|
||||||
pub auto_hist: bool,
|
|
||||||
pub bell_enabled: bool,
|
|
||||||
pub max_recurse_depth: usize,
|
|
||||||
pub xpg_echo: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ShOptCore {
|
|
||||||
pub fn set(&mut self, opt: &str, val: &str) -> ShResult<()> {
|
|
||||||
match opt {
|
|
||||||
"dotglob" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for dotglob value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.dotglob = val;
|
|
||||||
}
|
|
||||||
"autocd" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for autocd value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.autocd = val;
|
|
||||||
}
|
|
||||||
"hist_ignore_dupes" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for hist_ignore_dupes value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.hist_ignore_dupes = val;
|
|
||||||
}
|
|
||||||
"max_hist" => {
|
|
||||||
let Ok(val) = val.parse::<isize>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected an integer for max_hist value (-1 for unlimited)",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
if val < -1 {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected a non-negative integer or -1 for max_hist value",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
self.max_hist = val;
|
|
||||||
}
|
|
||||||
"interactive_comments" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for interactive_comments value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.interactive_comments = val;
|
|
||||||
}
|
|
||||||
"auto_hist" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for auto_hist value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.auto_hist = val;
|
|
||||||
}
|
|
||||||
"bell_enabled" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for bell_enabled value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.bell_enabled = val;
|
|
||||||
}
|
|
||||||
"max_recurse_depth" => {
|
|
||||||
let Ok(val) = val.parse::<usize>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected a positive integer for max_recurse_depth value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.max_recurse_depth = val;
|
|
||||||
}
|
|
||||||
"xpg_echo" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for xpg_echo value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.xpg_echo = val;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
format!("shopt: Unexpected 'core' option '{opt}'"),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
pub fn get(&self, query: &str) -> ShResult<Option<String>> {
|
|
||||||
if query.is_empty() {
|
|
||||||
return Ok(Some(format!("{self}")));
|
|
||||||
}
|
|
||||||
|
|
||||||
match query {
|
|
||||||
"dotglob" => {
|
|
||||||
let mut output = String::from("Include hidden files in glob patterns\n");
|
|
||||||
output.push_str(&format!("{}", self.dotglob));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"autocd" => {
|
|
||||||
let mut output = String::from(
|
|
||||||
"Allow navigation to directories by passing the directory as a command directly\n",
|
|
||||||
);
|
|
||||||
output.push_str(&format!("{}", self.autocd));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"hist_ignore_dupes" => {
|
|
||||||
let mut output = String::from("Ignore consecutive duplicate command history entries\n");
|
|
||||||
output.push_str(&format!("{}", self.hist_ignore_dupes));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"max_hist" => {
|
|
||||||
let mut output = String::from(
|
|
||||||
"Maximum number of entries in the command history file (-1 for unlimited)\n",
|
|
||||||
);
|
|
||||||
output.push_str(&format!("{}", self.max_hist));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"interactive_comments" => {
|
|
||||||
let mut output = String::from("Whether or not to allow comments in interactive mode\n");
|
|
||||||
output.push_str(&format!("{}", self.interactive_comments));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"auto_hist" => {
|
|
||||||
let mut output = String::from(
|
|
||||||
"Whether or not to automatically save commands to the command history file\n",
|
|
||||||
);
|
|
||||||
output.push_str(&format!("{}", self.auto_hist));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"bell_enabled" => {
|
|
||||||
let mut output = String::from("Whether or not to allow shed to trigger the terminal bell");
|
|
||||||
output.push_str(&format!("{}", self.bell_enabled));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"max_recurse_depth" => {
|
|
||||||
let mut output = String::from("Maximum limit of recursive shell function calls\n");
|
|
||||||
output.push_str(&format!("{}", self.max_recurse_depth));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"xpg_echo" => {
|
|
||||||
let mut output = String::from("Whether echo expands escape sequences by default\n");
|
|
||||||
output.push_str(&format!("{}", self.xpg_echo));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
_ => Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
format!("shopt: Unexpected 'core' option '{query}'"),
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for ShOptCore {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let mut output = vec![];
|
|
||||||
output.push(format!("dotglob = {}", self.dotglob));
|
|
||||||
output.push(format!("autocd = {}", self.autocd));
|
|
||||||
output.push(format!("hist_ignore_dupes = {}", self.hist_ignore_dupes));
|
|
||||||
output.push(format!("max_hist = {}", self.max_hist));
|
|
||||||
output.push(format!(
|
|
||||||
"interactive_comments = {}",
|
|
||||||
self.interactive_comments
|
|
||||||
));
|
|
||||||
output.push(format!("auto_hist = {}", self.auto_hist));
|
|
||||||
output.push(format!("bell_enabled = {}", self.bell_enabled));
|
|
||||||
output.push(format!("max_recurse_depth = {}", self.max_recurse_depth));
|
|
||||||
output.push(format!("xpg_echo = {}", self.xpg_echo));
|
|
||||||
|
|
||||||
let final_output = output.join("\n");
|
|
||||||
|
|
||||||
writeln!(f, "{final_output}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ShOptCore {
|
|
||||||
fn default() -> Self {
|
|
||||||
ShOptCore {
|
|
||||||
dotglob: false,
|
|
||||||
autocd: false,
|
|
||||||
hist_ignore_dupes: true,
|
|
||||||
max_hist: 10_000,
|
|
||||||
interactive_comments: true,
|
|
||||||
auto_hist: true,
|
|
||||||
bell_enabled: true,
|
|
||||||
max_recurse_depth: 1000,
|
|
||||||
xpg_echo: false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shopt_group! {
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ShOptPrompt {
|
pub struct ShOptCore ("core") {
|
||||||
pub trunc_prompt_path: usize,
|
/// Include hidden files in glob patterns
|
||||||
pub edit_mode: ShedEditMode,
|
dotglob: bool = false,
|
||||||
pub comp_limit: usize,
|
|
||||||
pub highlight: bool,
|
|
||||||
pub auto_indent: bool,
|
|
||||||
pub linebreak_on_incomplete: bool,
|
|
||||||
pub leader: String,
|
|
||||||
pub line_numbers: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ShOptPrompt {
|
/// Allow navigation to directories by passing the directory as a command directly
|
||||||
pub fn set(&mut self, opt: &str, val: &str) -> ShResult<()> {
|
autocd: bool = false,
|
||||||
match opt {
|
|
||||||
"trunc_prompt_path" => {
|
/// Ignore consecutive duplicate command history entries
|
||||||
let Ok(val) = val.parse::<usize>() else {
|
hist_ignore_dupes: bool = true,
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
/// Maximum number of entries in the command history file (-1 for unlimited)
|
||||||
"shopt: expected a positive integer for trunc_prompt_path value",
|
#[validate(|v: &isize| if *v < -1 {
|
||||||
));
|
Err("expected a non-negative integer or -1 for max_hist value".into())
|
||||||
};
|
} else {
|
||||||
self.trunc_prompt_path = val;
|
|
||||||
}
|
|
||||||
"edit_mode" => {
|
|
||||||
let Ok(val) = val.parse::<ShedEditMode>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'vi' or 'emacs' for edit_mode value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.edit_mode = val;
|
|
||||||
}
|
|
||||||
"comp_limit" => {
|
|
||||||
let Ok(val) = val.parse::<usize>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected a positive integer for comp_limit value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.comp_limit = val;
|
|
||||||
}
|
|
||||||
"highlight" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for highlight value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.highlight = val;
|
|
||||||
}
|
|
||||||
"auto_indent" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for auto_indent value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.auto_indent = val;
|
|
||||||
}
|
|
||||||
"linebreak_on_incomplete" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for linebreak_on_incomplete value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.linebreak_on_incomplete = val;
|
|
||||||
}
|
|
||||||
"leader" => {
|
|
||||||
self.leader = val.to_string();
|
|
||||||
}
|
|
||||||
"line_numbers" => {
|
|
||||||
let Ok(val) = val.parse::<bool>() else {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
"shopt: expected 'true' or 'false' for line_numbers value",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
self.line_numbers = val;
|
|
||||||
}
|
|
||||||
"custom" => {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
format!("shopt: Unexpected 'prompt' option '{opt}'"),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
})]
|
||||||
pub fn get(&self, query: &str) -> ShResult<Option<String>> {
|
max_hist: isize = 10_000,
|
||||||
if query.is_empty() {
|
|
||||||
return Ok(Some(format!("{self}")));
|
|
||||||
}
|
|
||||||
|
|
||||||
match query {
|
/// Whether or not to allow comments in interactive mode
|
||||||
"trunc_prompt_path" => {
|
interactive_comments: bool = true,
|
||||||
let mut output = String::from(
|
|
||||||
"Maximum number of path segments used in the '\\W' prompt escape sequence\n",
|
/// Whether or not to automatically save commands to the command history file
|
||||||
);
|
auto_hist: bool = true,
|
||||||
output.push_str(&format!("{}", self.trunc_prompt_path));
|
|
||||||
Ok(Some(output))
|
/// Whether or not to allow shed to trigger the terminal bell
|
||||||
}
|
bell_enabled: bool = true,
|
||||||
"edit_mode" => {
|
|
||||||
let mut output =
|
/// Maximum limit of recursive shell function calls
|
||||||
String::from("The style of editor shortcuts used in the line-editing of the prompt\n");
|
max_recurse_depth: usize = 1000,
|
||||||
output.push_str(&format!("{}", self.edit_mode));
|
|
||||||
Ok(Some(output))
|
/// Whether echo expands escape sequences by default
|
||||||
}
|
xpg_echo: bool = false,
|
||||||
"comp_limit" => {
|
|
||||||
let mut output =
|
/// Prevent > from overwriting existing files (use >| to override)
|
||||||
String::from("Maximum number of completion candidates displayed upon pressing tab\n");
|
noclobber: bool = false,
|
||||||
output.push_str(&format!("{}", self.comp_limit));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"highlight" => {
|
|
||||||
let mut output =
|
|
||||||
String::from("Whether to enable or disable syntax highlighting on the prompt\n");
|
|
||||||
output.push_str(&format!("{}", self.highlight));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"auto_indent" => {
|
|
||||||
let mut output =
|
|
||||||
String::from("Whether to automatically indent new lines in multiline commands\n");
|
|
||||||
output.push_str(&format!("{}", self.auto_indent));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"linebreak_on_incomplete" => {
|
|
||||||
let mut output =
|
|
||||||
String::from("Whether to automatically insert a newline when the input is incomplete\n");
|
|
||||||
output.push_str(&format!("{}", self.linebreak_on_incomplete));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"leader" => {
|
|
||||||
let mut output = String::from("The leader key sequence used in keymap bindings\n");
|
|
||||||
output.push_str(&self.leader);
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
"line_numbers" => {
|
|
||||||
let mut output = String::from("Whether to display line numbers in multiline input\n");
|
|
||||||
output.push_str(&format!("{}", self.line_numbers));
|
|
||||||
Ok(Some(output))
|
|
||||||
}
|
|
||||||
_ => Err(ShErr::simple(
|
|
||||||
ShErrKind::SyntaxErr,
|
|
||||||
format!("shopt: Unexpected 'prompt' option '{query}'"),
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ShOptPrompt {
|
shopt_group! {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
#[derive(Clone, Debug)]
|
||||||
let mut output = vec![];
|
pub struct ShOptPrompt ("prompt") {
|
||||||
|
/// Maximum number of path segments used in the '\W' prompt escape sequence
|
||||||
|
trunc_prompt_path: usize = 4,
|
||||||
|
|
||||||
output.push(format!("trunc_prompt_path = {}", self.trunc_prompt_path));
|
/// Maximum number of completion candidates displayed upon pressing tab
|
||||||
output.push(format!("edit_mode = {}", self.edit_mode));
|
comp_limit: usize = 100,
|
||||||
output.push(format!("comp_limit = {}", self.comp_limit));
|
|
||||||
output.push(format!("highlight = {}", self.highlight));
|
|
||||||
output.push(format!("auto_indent = {}", self.auto_indent));
|
|
||||||
output.push(format!(
|
|
||||||
"linebreak_on_incomplete = {}",
|
|
||||||
self.linebreak_on_incomplete
|
|
||||||
));
|
|
||||||
output.push(format!("leader = {}", self.leader));
|
|
||||||
output.push(format!("line_numbers = {}", self.line_numbers));
|
|
||||||
|
|
||||||
let final_output = output.join("\n");
|
/// Whether to enable or disable syntax highlighting on the prompt
|
||||||
|
highlight: bool = true,
|
||||||
|
|
||||||
writeln!(f, "{final_output}")
|
/// Whether to automatically indent new lines in multiline commands
|
||||||
}
|
auto_indent: bool = true,
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ShOptPrompt {
|
/// Whether to automatically insert a newline when the input is incomplete
|
||||||
fn default() -> Self {
|
linebreak_on_incomplete: bool = true,
|
||||||
ShOptPrompt {
|
|
||||||
trunc_prompt_path: 4,
|
/// The leader key sequence used in keymap bindings
|
||||||
edit_mode: ShedEditMode::Vi,
|
leader: String = " ".to_string(),
|
||||||
comp_limit: 100,
|
|
||||||
highlight: true,
|
/// Whether to display line numbers in multiline input
|
||||||
auto_indent: true,
|
line_numbers: bool = true,
|
||||||
linebreak_on_incomplete: true,
|
|
||||||
leader: "\\".to_string(),
|
/// Command to execute as a screensaver after idle timeout
|
||||||
line_numbers: true,
|
screensaver_cmd: String = String::new(),
|
||||||
}
|
|
||||||
|
/// Idle time in seconds before running screensaver_cmd (0 = disabled)
|
||||||
|
screensaver_idle_time: usize = 0,
|
||||||
|
|
||||||
|
/// Whether tab completion matching is case-insensitive
|
||||||
|
completion_ignore_case: bool = false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -548,9 +323,16 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn all_core_fields_covered() {
|
fn all_core_fields_covered() {
|
||||||
let ShOptCore {
|
let ShOptCore {
|
||||||
dotglob, autocd, hist_ignore_dupes, max_hist,
|
dotglob,
|
||||||
interactive_comments, auto_hist, bell_enabled, max_recurse_depth,
|
autocd,
|
||||||
|
hist_ignore_dupes,
|
||||||
|
max_hist,
|
||||||
|
interactive_comments,
|
||||||
|
auto_hist,
|
||||||
|
bell_enabled,
|
||||||
|
max_recurse_depth,
|
||||||
xpg_echo,
|
xpg_echo,
|
||||||
|
noclobber,
|
||||||
} = ShOptCore::default();
|
} = ShOptCore::default();
|
||||||
// If a field is added to the struct, this destructure fails to compile.
|
// If a field is added to the struct, this destructure fails to compile.
|
||||||
let _ = (
|
let _ = (
|
||||||
@@ -563,6 +345,7 @@ mod tests {
|
|||||||
bell_enabled,
|
bell_enabled,
|
||||||
max_recurse_depth,
|
max_recurse_depth,
|
||||||
xpg_echo,
|
xpg_echo,
|
||||||
|
noclobber,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -596,12 +379,6 @@ mod tests {
|
|||||||
fn set_and_get_prompt_opts() {
|
fn set_and_get_prompt_opts() {
|
||||||
let mut opts = ShOpts::default();
|
let mut opts = ShOpts::default();
|
||||||
|
|
||||||
opts.set("prompt.edit_mode", "emacs").unwrap();
|
|
||||||
assert!(matches!(opts.prompt.edit_mode, ShedEditMode::Emacs));
|
|
||||||
|
|
||||||
opts.set("prompt.edit_mode", "vi").unwrap();
|
|
||||||
assert!(matches!(opts.prompt.edit_mode, ShedEditMode::Vi));
|
|
||||||
|
|
||||||
opts.set("prompt.comp_limit", "50").unwrap();
|
opts.set("prompt.comp_limit", "50").unwrap();
|
||||||
assert_eq!(opts.prompt.comp_limit, 50);
|
assert_eq!(opts.prompt.comp_limit, 50);
|
||||||
|
|
||||||
@@ -646,7 +423,6 @@ mod tests {
|
|||||||
assert!(opts.set("core.dotglob", "notabool").is_err());
|
assert!(opts.set("core.dotglob", "notabool").is_err());
|
||||||
assert!(opts.set("core.max_hist", "notanint").is_err());
|
assert!(opts.set("core.max_hist", "notanint").is_err());
|
||||||
assert!(opts.set("core.max_recurse_depth", "-5").is_err());
|
assert!(opts.set("core.max_recurse_depth", "-5").is_err());
|
||||||
assert!(opts.set("prompt.edit_mode", "notepad").is_err());
|
|
||||||
assert!(opts.set("prompt.comp_limit", "abc").is_err());
|
assert!(opts.set("prompt.comp_limit", "abc").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -660,7 +436,6 @@ mod tests {
|
|||||||
assert!(core_output.contains("bell_enabled"));
|
assert!(core_output.contains("bell_enabled"));
|
||||||
|
|
||||||
let prompt_output = opts.get("prompt").unwrap().unwrap();
|
let prompt_output = opts.get("prompt").unwrap().unwrap();
|
||||||
assert!(prompt_output.contains("edit_mode"));
|
|
||||||
assert!(prompt_output.contains("comp_limit"));
|
assert!(prompt_output.contains("comp_limit"));
|
||||||
assert!(prompt_output.contains("highlight"));
|
assert!(prompt_output.contains("highlight"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,45 @@
|
|||||||
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, Ordering};
|
use std::{
|
||||||
|
collections::VecDeque,
|
||||||
|
sync::atomic::{AtomicBool, AtomicI32, AtomicU64, Ordering},
|
||||||
|
};
|
||||||
|
|
||||||
use nix::sys::signal::{SaFlags, SigAction, sigaction};
|
use nix::{
|
||||||
|
sys::signal::{SaFlags, SigAction, sigaction},
|
||||||
|
unistd::getpid,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
builtin::trap::TrapTarget,
|
builtin::trap::TrapTarget,
|
||||||
jobs::{JobCmdFlags, JobID, take_term},
|
jobs::{Job, JobCmdFlags, JobID, take_term},
|
||||||
libsh::error::{ShErr, ShErrKind, ShResult},
|
libsh::error::{ShErr, ShErrKind, ShResult},
|
||||||
parse::execute::exec_input,
|
parse::execute::exec_input,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
state::{AutoCmd, AutoCmdKind, read_jobs, read_logic, write_jobs, write_meta},
|
state::{
|
||||||
|
AutoCmd, AutoCmdKind, VarFlags, VarKind, read_jobs, read_logic, write_jobs, write_meta,
|
||||||
|
write_vars,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
static SIGNALS: AtomicU64 = AtomicU64::new(0);
|
static SIGNALS: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
pub static REAPING_ENABLED: AtomicBool = AtomicBool::new(true);
|
pub static REAPING_ENABLED: AtomicBool = AtomicBool::new(true);
|
||||||
pub static SHOULD_QUIT: AtomicBool = AtomicBool::new(false);
|
pub static SHOULD_QUIT: AtomicBool = AtomicBool::new(false);
|
||||||
pub static GOT_SIGWINCH: AtomicBool = AtomicBool::new(false);
|
|
||||||
pub static JOB_DONE: AtomicBool = AtomicBool::new(false);
|
pub static JOB_DONE: AtomicBool = AtomicBool::new(false);
|
||||||
pub static QUIT_CODE: AtomicI32 = AtomicI32::new(0);
|
pub static QUIT_CODE: AtomicI32 = AtomicI32::new(0);
|
||||||
|
|
||||||
const MISC_SIGNALS: [Signal; 22] = [
|
/// Window size change signal
|
||||||
|
pub static GOT_SIGWINCH: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
|
/// SIGUSR1 tells the prompt that it needs to fully refresh.
|
||||||
|
/// Useful for dynamic prompt content and asynchronous refreshing
|
||||||
|
pub static GOT_SIGUSR1: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
|
const MISC_SIGNALS: [Signal; 21] = [
|
||||||
Signal::SIGILL,
|
Signal::SIGILL,
|
||||||
Signal::SIGTRAP,
|
Signal::SIGTRAP,
|
||||||
Signal::SIGABRT,
|
Signal::SIGABRT,
|
||||||
Signal::SIGBUS,
|
Signal::SIGBUS,
|
||||||
Signal::SIGFPE,
|
Signal::SIGFPE,
|
||||||
Signal::SIGUSR1,
|
|
||||||
Signal::SIGSEGV,
|
Signal::SIGSEGV,
|
||||||
Signal::SIGUSR2,
|
Signal::SIGUSR2,
|
||||||
Signal::SIGPIPE,
|
Signal::SIGPIPE,
|
||||||
@@ -65,7 +79,7 @@ pub fn check_signals() -> ShResult<()> {
|
|||||||
if got_signal(Signal::SIGINT) {
|
if got_signal(Signal::SIGINT) {
|
||||||
interrupt()?;
|
interrupt()?;
|
||||||
run_trap(Signal::SIGINT)?;
|
run_trap(Signal::SIGINT)?;
|
||||||
return Err(ShErr::simple(ShErrKind::ClearReadline, ""));
|
return Err(ShErr::simple(ShErrKind::Interrupt, ""));
|
||||||
}
|
}
|
||||||
if got_signal(Signal::SIGHUP) {
|
if got_signal(Signal::SIGHUP) {
|
||||||
run_trap(Signal::SIGHUP)?;
|
run_trap(Signal::SIGHUP)?;
|
||||||
@@ -87,6 +101,10 @@ pub fn check_signals() -> ShResult<()> {
|
|||||||
GOT_SIGWINCH.store(true, Ordering::SeqCst);
|
GOT_SIGWINCH.store(true, Ordering::SeqCst);
|
||||||
run_trap(Signal::SIGWINCH)?;
|
run_trap(Signal::SIGWINCH)?;
|
||||||
}
|
}
|
||||||
|
if got_signal(Signal::SIGUSR1) {
|
||||||
|
GOT_SIGUSR1.store(true, Ordering::SeqCst);
|
||||||
|
run_trap(Signal::SIGUSR1)?;
|
||||||
|
}
|
||||||
|
|
||||||
for sig in MISC_SIGNALS {
|
for sig in MISC_SIGNALS {
|
||||||
if got_signal(sig) {
|
if got_signal(sig) {
|
||||||
@@ -316,6 +334,25 @@ pub fn child_exited(pid: Pid, status: WtStat) -> ShResult<()> {
|
|||||||
let result = read_jobs(|j| j.query(JobID::Pgid(pgid)).cloned());
|
let result = read_jobs(|j| j.query(JobID::Pgid(pgid)).cloned());
|
||||||
if let Some(job) = result {
|
if let Some(job) = result {
|
||||||
let job_complete_msg = job.display(&job_order, JobCmdFlags::PIDS).to_string();
|
let job_complete_msg = job.display(&job_order, JobCmdFlags::PIDS).to_string();
|
||||||
|
let statuses = job.get_stats();
|
||||||
|
|
||||||
|
for status in &statuses {
|
||||||
|
if let WtStat::Signaled(_, sig, _) = status
|
||||||
|
&& *sig == Signal::SIGINT
|
||||||
|
{
|
||||||
|
// Necessary to interrupt stuff like shell loops
|
||||||
|
kill(getpid(), Signal::SIGINT).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(pipe_status) = Job::pipe_status(&statuses) {
|
||||||
|
let pipe_status = pipe_status
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.collect::<VecDeque<String>>();
|
||||||
|
|
||||||
|
write_vars(|v| v.set_var("PIPESTATUS", VarKind::Arr(pipe_status), VarFlags::NONE))?;
|
||||||
|
}
|
||||||
|
|
||||||
let post_job_hooks = read_logic(|l| l.get_autocmds(AutoCmdKind::OnJobFinish));
|
let post_job_hooks = read_logic(|l| l.get_autocmds(AutoCmdKind::OnJobFinish));
|
||||||
for cmd in post_job_hooks {
|
for cmd in post_job_hooks {
|
||||||
|
|||||||
297
src/state.rs
297
src/state.rs
@@ -1,8 +1,14 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cell::RefCell, collections::{HashMap, HashSet, VecDeque, hash_map::Entry}, fmt::Display, ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign}, os::unix::fs::PermissionsExt, str::FromStr, time::Duration
|
cell::RefCell,
|
||||||
|
collections::{HashMap, HashSet, VecDeque, hash_map::Entry},
|
||||||
|
fmt::Display,
|
||||||
|
ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign},
|
||||||
|
os::unix::fs::PermissionsExt,
|
||||||
|
str::FromStr,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use nix::unistd::{User, gethostname, getppid};
|
use nix::unistd::{User, gethostname, getppid, getuid};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -25,7 +31,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
readline::{
|
readline::{
|
||||||
complete::{BashCompSpec, CompSpec},
|
complete::{BashCompSpec, Candidate, CompSpec},
|
||||||
keys::KeyEvent,
|
keys::KeyEvent,
|
||||||
markers,
|
markers,
|
||||||
},
|
},
|
||||||
@@ -315,6 +321,34 @@ impl ScopeStack {
|
|||||||
};
|
};
|
||||||
scope.set_var(var_name, val, flags)
|
scope.set_var(var_name, val, flags)
|
||||||
}
|
}
|
||||||
|
pub fn get_magic_var(&self, var_name: &str) -> Option<String> {
|
||||||
|
match var_name {
|
||||||
|
"SECONDS" => {
|
||||||
|
let shell_time = read_meta(|m| m.shell_time());
|
||||||
|
let secs = Instant::now().duration_since(shell_time).as_secs();
|
||||||
|
Some(secs.to_string())
|
||||||
|
}
|
||||||
|
"EPOCHREALTIME" => {
|
||||||
|
let epoch = std::time::SystemTime::now()
|
||||||
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
|
.unwrap_or(Duration::from_secs(0))
|
||||||
|
.as_secs_f64();
|
||||||
|
Some(epoch.to_string())
|
||||||
|
}
|
||||||
|
"EPOCHSECONDS" => {
|
||||||
|
let epoch = std::time::SystemTime::now()
|
||||||
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
|
.unwrap_or(Duration::from_secs(0))
|
||||||
|
.as_secs();
|
||||||
|
Some(epoch.to_string())
|
||||||
|
}
|
||||||
|
"RANDOM" => {
|
||||||
|
let random = rand::random_range(0..32768);
|
||||||
|
Some(random.to_string())
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn get_arr_elems(&self, var_name: &str) -> ShResult<Vec<String>> {
|
pub fn get_arr_elems(&self, var_name: &str) -> ShResult<Vec<String>> {
|
||||||
for scope in self.scopes.iter().rev() {
|
for scope in self.scopes.iter().rev() {
|
||||||
if scope.var_exists(var_name)
|
if scope.var_exists(var_name)
|
||||||
@@ -440,7 +474,9 @@ impl ScopeStack {
|
|||||||
pub fn try_get_var(&self, var_name: &str) -> Option<String> {
|
pub fn try_get_var(&self, var_name: &str) -> Option<String> {
|
||||||
// This version of get_var() is mainly used internally
|
// This version of get_var() is mainly used internally
|
||||||
// so that we have access to Option methods
|
// so that we have access to Option methods
|
||||||
if let Ok(param) = var_name.parse::<ShellParam>() {
|
if let Some(magic) = self.get_magic_var(var_name) {
|
||||||
|
return Some(magic);
|
||||||
|
} else if let Ok(param) = var_name.parse::<ShellParam>() {
|
||||||
let val = self.get_param(param);
|
let val = self.get_param(param);
|
||||||
if !val.is_empty() {
|
if !val.is_empty() {
|
||||||
return Some(val);
|
return Some(val);
|
||||||
@@ -463,6 +499,9 @@ impl ScopeStack {
|
|||||||
var
|
var
|
||||||
}
|
}
|
||||||
pub fn get_var(&self, var_name: &str) -> String {
|
pub fn get_var(&self, var_name: &str) -> String {
|
||||||
|
if let Some(magic) = self.get_magic_var(var_name) {
|
||||||
|
return magic;
|
||||||
|
}
|
||||||
if let Ok(param) = var_name.parse::<ShellParam>() {
|
if let Ok(param) = var_name.parse::<ShellParam>() {
|
||||||
return self.get_param(param);
|
return self.get_param(param);
|
||||||
}
|
}
|
||||||
@@ -495,7 +534,10 @@ impl ScopeStack {
|
|||||||
return val.clone();
|
return val.clone();
|
||||||
}
|
}
|
||||||
// Positional params are scope-local; only check the current scope
|
// Positional params are scope-local; only check the current scope
|
||||||
if matches!(param, ShellParam::Pos(_) | ShellParam::AllArgs | ShellParam::AllArgsStr | ShellParam::ArgCount) {
|
if matches!(
|
||||||
|
param,
|
||||||
|
ShellParam::Pos(_) | ShellParam::AllArgs | ShellParam::AllArgsStr | ShellParam::ArgCount
|
||||||
|
) {
|
||||||
if let Some(scope) = self.scopes.last() {
|
if let Some(scope) = self.scopes.last() {
|
||||||
return scope.get_param(param);
|
return scope.get_param(param);
|
||||||
}
|
}
|
||||||
@@ -589,59 +631,31 @@ pub enum AutoCmdKind {
|
|||||||
OnCompletionStart,
|
OnCompletionStart,
|
||||||
OnCompletionCancel,
|
OnCompletionCancel,
|
||||||
OnCompletionSelect,
|
OnCompletionSelect,
|
||||||
|
OnScreensaverExec,
|
||||||
|
OnScreensaverReturn,
|
||||||
OnExit,
|
OnExit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for AutoCmdKind {
|
crate::two_way_display!(AutoCmdKind,
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
PreCmd <=> "pre-cmd";
|
||||||
match self {
|
PostCmd <=> "post-cmd";
|
||||||
Self::PreCmd => write!(f, "pre-cmd"),
|
PreChangeDir <=> "pre-change-dir";
|
||||||
Self::PostCmd => write!(f, "post-cmd"),
|
PostChangeDir <=> "post-change-dir";
|
||||||
Self::PreChangeDir => write!(f, "pre-change-dir"),
|
OnJobFinish <=> "on-job-finish";
|
||||||
Self::PostChangeDir => write!(f, "post-change-dir"),
|
PrePrompt <=> "pre-prompt";
|
||||||
Self::OnJobFinish => write!(f, "on-job-finish"),
|
PostPrompt <=> "post-prompt";
|
||||||
Self::PrePrompt => write!(f, "pre-prompt"),
|
PreModeChange <=> "pre-mode-change";
|
||||||
Self::PostPrompt => write!(f, "post-prompt"),
|
PostModeChange <=> "post-mode-change";
|
||||||
Self::PreModeChange => write!(f, "pre-mode-change"),
|
OnHistoryOpen <=> "on-history-open";
|
||||||
Self::PostModeChange => write!(f, "post-mode-change"),
|
OnHistoryClose <=> "on-history-close";
|
||||||
Self::OnHistoryOpen => write!(f, "on-history-open"),
|
OnHistorySelect <=> "on-history-select";
|
||||||
Self::OnHistoryClose => write!(f, "on-history-close"),
|
OnCompletionStart <=> "on-completion-start";
|
||||||
Self::OnHistorySelect => write!(f, "on-history-select"),
|
OnCompletionCancel <=> "on-completion-cancel";
|
||||||
Self::OnCompletionStart => write!(f, "on-completion-start"),
|
OnCompletionSelect <=> "on-completion-select";
|
||||||
Self::OnCompletionCancel => write!(f, "on-completion-cancel"),
|
OnScreensaverExec <=> "on-screensaver-exec";
|
||||||
Self::OnCompletionSelect => write!(f, "on-completion-select"),
|
OnScreensaverReturn <=> "on-screensaver-return";
|
||||||
Self::OnExit => write!(f, "on-exit"),
|
OnExit <=> "on-exit";
|
||||||
}
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for AutoCmdKind {
|
|
||||||
type Err = ShErr;
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
match s {
|
|
||||||
"pre-cmd" => Ok(Self::PreCmd),
|
|
||||||
"post-cmd" => Ok(Self::PostCmd),
|
|
||||||
"pre-change-dir" => Ok(Self::PreChangeDir),
|
|
||||||
"post-change-dir" => Ok(Self::PostChangeDir),
|
|
||||||
"on-job-finish" => Ok(Self::OnJobFinish),
|
|
||||||
"pre-prompt" => Ok(Self::PrePrompt),
|
|
||||||
"post-prompt" => Ok(Self::PostPrompt),
|
|
||||||
"pre-mode-change" => Ok(Self::PreModeChange),
|
|
||||||
"post-mode-change" => Ok(Self::PostModeChange),
|
|
||||||
"on-history-open" => Ok(Self::OnHistoryOpen),
|
|
||||||
"on-history-close" => Ok(Self::OnHistoryClose),
|
|
||||||
"on-history-select" => Ok(Self::OnHistorySelect),
|
|
||||||
"on-completion-start" => Ok(Self::OnCompletionStart),
|
|
||||||
"on-completion-cancel" => Ok(Self::OnCompletionCancel),
|
|
||||||
"on-completion-select" => Ok(Self::OnCompletionSelect),
|
|
||||||
"on-exit" => Ok(Self::OnExit),
|
|
||||||
_ => Err(ShErr::simple(
|
|
||||||
ShErrKind::ParseErr,
|
|
||||||
format!("Invalid autocmd kind: {}", s),
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AutoCmd {
|
pub struct AutoCmd {
|
||||||
@@ -959,6 +973,13 @@ impl From<Vec<String>> for Var {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Vec<Candidate>> for Var {
|
||||||
|
fn from(value: Vec<Candidate>) -> Self {
|
||||||
|
let as_strs = value.into_iter().map(|c| c.0).collect::<Vec<_>>();
|
||||||
|
Self::new(VarKind::Arr(as_strs.into()), VarFlags::NONE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<&[String]> for Var {
|
impl From<&[String]> for Var {
|
||||||
fn from(value: &[String]) -> Self {
|
fn from(value: &[String]) -> Self {
|
||||||
let mut new = VecDeque::new();
|
let mut new = VecDeque::new();
|
||||||
@@ -978,19 +999,7 @@ macro_rules! impl_var_from {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl_var_from!(
|
impl_var_from!(
|
||||||
i8,
|
i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, String, &str, bool
|
||||||
i16,
|
|
||||||
i32,
|
|
||||||
i64,
|
|
||||||
isize,
|
|
||||||
u8,
|
|
||||||
u16,
|
|
||||||
u32,
|
|
||||||
u64,
|
|
||||||
usize,
|
|
||||||
String,
|
|
||||||
&str,
|
|
||||||
bool
|
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Default, Clone, Debug)]
|
#[derive(Default, Clone, Debug)]
|
||||||
@@ -1012,7 +1021,7 @@ impl VarTab {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let vars = HashMap::new();
|
let vars = Self::init_sh_vars();
|
||||||
let params = Self::init_params();
|
let params = Self::init_params();
|
||||||
Self::init_env();
|
Self::init_env();
|
||||||
let mut var_tab = Self {
|
let mut var_tab = Self {
|
||||||
@@ -1031,6 +1040,11 @@ impl VarTab {
|
|||||||
params.insert(ShellParam::LastJob, "".into()); // PID of the last background job (if any)
|
params.insert(ShellParam::LastJob, "".into()); // PID of the last background job (if any)
|
||||||
params
|
params
|
||||||
}
|
}
|
||||||
|
fn init_sh_vars() -> HashMap<String, Var> {
|
||||||
|
let mut vars = HashMap::new();
|
||||||
|
vars.insert("COMP_WORDBREAKS".into(), " \t\n\"'@><=;|&(".into());
|
||||||
|
vars
|
||||||
|
}
|
||||||
fn init_env() {
|
fn init_env() {
|
||||||
let pathbuf_to_string =
|
let pathbuf_to_string =
|
||||||
|pb: Result<PathBuf, std::io::Error>| pb.unwrap_or_default().to_string_lossy().to_string();
|
|pb: Result<PathBuf, std::io::Error>| pb.unwrap_or_default().to_string_lossy().to_string();
|
||||||
@@ -1063,6 +1077,8 @@ impl VarTab {
|
|||||||
.map(|hname| hname.to_string_lossy().to_string())
|
.map(|hname| hname.to_string_lossy().to_string())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let help_paths = format!("/usr/share/shed/doc:{home}/.local/share/shed/doc");
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
env::set_var("IFS", " \t\n");
|
env::set_var("IFS", " \t\n");
|
||||||
env::set_var("HOST", hostname.clone());
|
env::set_var("HOST", hostname.clone());
|
||||||
@@ -1079,6 +1095,7 @@ impl VarTab {
|
|||||||
env::set_var("SHELL", pathbuf_to_string(std::env::current_exe()));
|
env::set_var("SHELL", pathbuf_to_string(std::env::current_exe()));
|
||||||
env::set_var("SHED_HIST", format!("{}/.shedhist", home));
|
env::set_var("SHED_HIST", format!("{}/.shedhist", home));
|
||||||
env::set_var("SHED_RC", format!("{}/.shedrc", home));
|
env::set_var("SHED_RC", format!("{}/.shedrc", home));
|
||||||
|
env::set_var("SHED_HPATH", help_paths);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn init_sh_argv(&mut self) {
|
pub fn init_sh_argv(&mut self) {
|
||||||
@@ -1295,6 +1312,15 @@ impl VarTab {
|
|||||||
.get(&ShellParam::Status)
|
.get(&ShellParam::Status)
|
||||||
.map(|s| s.to_string())
|
.map(|s| s.to_string())
|
||||||
.unwrap_or("0".into()),
|
.unwrap_or("0".into()),
|
||||||
|
ShellParam::AllArgsStr => {
|
||||||
|
let ifs = get_separator();
|
||||||
|
self
|
||||||
|
.params
|
||||||
|
.get(&ShellParam::AllArgs)
|
||||||
|
.map(|s| s.replace(markers::ARG_SEP, &ifs).to_string())
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
_ => self
|
_ => self
|
||||||
.params
|
.params
|
||||||
.get(¶m)
|
.get(¶m)
|
||||||
@@ -1305,14 +1331,22 @@ impl VarTab {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A table of metadata for the shell
|
/// A table of metadata for the shell
|
||||||
#[derive(Clone, Default, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct MetaTab {
|
pub struct MetaTab {
|
||||||
|
// Time when the shell was started, used for calculating shell uptime
|
||||||
|
shell_time: Instant,
|
||||||
|
|
||||||
// command running duration
|
// command running duration
|
||||||
runtime_start: Option<Instant>,
|
runtime_start: Option<Instant>,
|
||||||
runtime_stop: Option<Instant>,
|
runtime_stop: Option<Instant>,
|
||||||
|
|
||||||
// pending system messages
|
// pending system messages
|
||||||
system_msg: Vec<String>,
|
// are drawn above the prompt and survive redraws
|
||||||
|
system_msg: VecDeque<String>,
|
||||||
|
|
||||||
|
// same as system messages,
|
||||||
|
// but they appear under the prompt and are erased on redraw
|
||||||
|
status_msg: VecDeque<String>,
|
||||||
|
|
||||||
// pushd/popd stack
|
// pushd/popd stack
|
||||||
dir_stack: VecDeque<PathBuf>,
|
dir_stack: VecDeque<PathBuf>,
|
||||||
@@ -1331,6 +1365,26 @@ pub struct MetaTab {
|
|||||||
pending_widget_keys: Vec<KeyEvent>,
|
pending_widget_keys: Vec<KeyEvent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for MetaTab {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
shell_time: Instant::now(),
|
||||||
|
runtime_start: None,
|
||||||
|
runtime_stop: None,
|
||||||
|
system_msg: VecDeque::new(),
|
||||||
|
status_msg: VecDeque::new(),
|
||||||
|
dir_stack: VecDeque::new(),
|
||||||
|
getopts_offset: 0,
|
||||||
|
old_path: None,
|
||||||
|
old_pwd: None,
|
||||||
|
path_cache: HashSet::new(),
|
||||||
|
cwd_cache: HashSet::new(),
|
||||||
|
comp_specs: HashMap::new(),
|
||||||
|
pending_widget_keys: vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MetaTab {
|
impl MetaTab {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -1338,6 +1392,9 @@ impl MetaTab {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn shell_time(&self) -> Instant {
|
||||||
|
self.shell_time
|
||||||
|
}
|
||||||
pub fn set_pending_widget_keys(&mut self, keys: &str) {
|
pub fn set_pending_widget_keys(&mut self, keys: &str) {
|
||||||
let exp = expand_keymap(keys);
|
let exp = expand_keymap(keys);
|
||||||
self.pending_widget_keys = exp;
|
self.pending_widget_keys = exp;
|
||||||
@@ -1535,14 +1592,23 @@ impl MetaTab {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn post_system_message(&mut self, message: String) {
|
pub fn post_system_message(&mut self, message: String) {
|
||||||
self.system_msg.push(message);
|
self.system_msg.push_back(message);
|
||||||
}
|
}
|
||||||
pub fn pop_system_message(&mut self) -> Option<String> {
|
pub fn pop_system_message(&mut self) -> Option<String> {
|
||||||
self.system_msg.pop()
|
self.system_msg.pop_front()
|
||||||
}
|
}
|
||||||
pub fn system_msg_pending(&self) -> bool {
|
pub fn system_msg_pending(&self) -> bool {
|
||||||
!self.system_msg.is_empty()
|
!self.system_msg.is_empty()
|
||||||
}
|
}
|
||||||
|
pub fn post_status_message(&mut self, message: String) {
|
||||||
|
self.status_msg.push_back(message);
|
||||||
|
}
|
||||||
|
pub fn pop_status_message(&mut self) -> Option<String> {
|
||||||
|
self.status_msg.pop_front()
|
||||||
|
}
|
||||||
|
pub fn status_msg_pending(&self) -> bool {
|
||||||
|
!self.status_msg.is_empty()
|
||||||
|
}
|
||||||
pub fn dir_stack_top(&self) -> Option<&PathBuf> {
|
pub fn dir_stack_top(&self) -> Option<&PathBuf> {
|
||||||
self.dir_stack.front()
|
self.dir_stack.front()
|
||||||
}
|
}
|
||||||
@@ -1782,6 +1848,15 @@ pub fn change_dir<P: AsRef<Path>>(dir: P) -> ShResult<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_separator() -> String {
|
||||||
|
env::var("IFS")
|
||||||
|
.unwrap_or(String::from(" "))
|
||||||
|
.chars()
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_status() -> i32 {
|
pub fn get_status() -> i32 {
|
||||||
read_vars(|v| v.get_param(ShellParam::Status))
|
read_vars(|v| v.get_param(ShellParam::Status))
|
||||||
.parse::<i32>()
|
.parse::<i32>()
|
||||||
@@ -1791,19 +1866,44 @@ pub fn set_status(code: i32) {
|
|||||||
write_vars(|v| v.set_param(ShellParam::Status, &code.to_string()))
|
write_vars(|v| v.set_param(ShellParam::Status, &code.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source_rc() -> ShResult<()> {
|
pub fn source_runtime_file(name: &str, env_var_name: Option<&str>) -> ShResult<()> {
|
||||||
let path = if let Ok(path) = env::var("SHED_RC") {
|
let etc_path = PathBuf::from(format!("/etc/shed/{name}"));
|
||||||
|
if etc_path.is_file()
|
||||||
|
&& let Err(e) = source_file(etc_path)
|
||||||
|
{
|
||||||
|
e.print_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
let path = if let Some(name) = env_var_name
|
||||||
|
&& let Ok(path) = env::var(name)
|
||||||
|
{
|
||||||
PathBuf::from(&path)
|
PathBuf::from(&path)
|
||||||
|
} else if let Some(home) = get_home() {
|
||||||
|
home.join(format!(".{name}"))
|
||||||
} else {
|
} else {
|
||||||
let home = env::var("HOME").unwrap();
|
return Err(ShErr::simple(
|
||||||
PathBuf::from(format!("{home}/.shedrc"))
|
ShErrKind::InternalErr,
|
||||||
|
"could not determine home path",
|
||||||
|
));
|
||||||
};
|
};
|
||||||
if !path.exists() {
|
if !path.is_file() {
|
||||||
return Err(ShErr::simple(ShErrKind::InternalErr, ".shedrc not found"));
|
return Ok(());
|
||||||
}
|
}
|
||||||
source_file(path)
|
source_file(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn source_rc() -> ShResult<()> {
|
||||||
|
source_runtime_file("shedrc", Some("SHED_RC"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn source_login() -> ShResult<()> {
|
||||||
|
source_runtime_file("shed_profile", Some("SHED_PROFILE"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn source_env() -> ShResult<()> {
|
||||||
|
source_runtime_file("shedenv", Some("SHED_ENV"))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn source_file(path: PathBuf) -> ShResult<()> {
|
pub fn source_file(path: PathBuf) -> ShResult<()> {
|
||||||
let source_name = path.to_string_lossy().to_string();
|
let source_name = path.to_string_lossy().to_string();
|
||||||
let mut file = OpenOptions::new().read(true).open(path)?;
|
let mut file = OpenOptions::new().read(true).open(path)?;
|
||||||
@@ -1813,3 +1913,42 @@ pub fn source_file(path: PathBuf) -> ShResult<()> {
|
|||||||
exec_input(buf, None, false, Some(source_name))?;
|
exec_input(buf, None, false, Some(source_name))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
pub fn get_home_unchecked() -> PathBuf {
|
||||||
|
if let Some(home) = get_home() {
|
||||||
|
home
|
||||||
|
} else {
|
||||||
|
let caller = std::panic::Location::caller();
|
||||||
|
panic!(
|
||||||
|
"get_home_unchecked: could not determine home directory (called from {}:{})",
|
||||||
|
caller.file(),
|
||||||
|
caller.line()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
pub fn get_home_str_unchecked() -> String {
|
||||||
|
if let Some(home) = get_home() {
|
||||||
|
home.to_string_lossy().to_string()
|
||||||
|
} else {
|
||||||
|
let caller = std::panic::Location::caller();
|
||||||
|
panic!(
|
||||||
|
"get_home_str_unchecked: could not determine home directory (called from {}:{})",
|
||||||
|
caller.file(),
|
||||||
|
caller.line()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_home() -> Option<PathBuf> {
|
||||||
|
env::var("HOME")
|
||||||
|
.ok()
|
||||||
|
.map(PathBuf::from)
|
||||||
|
.or_else(|| User::from_uid(getuid()).ok().flatten().map(|u| u.dir))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_home_str() -> Option<String> {
|
||||||
|
get_home().map(|h| h.to_string_lossy().to_string())
|
||||||
|
}
|
||||||
|
|||||||
172
src/testutil.rs
172
src/testutil.rs
@@ -1,9 +1,9 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::{HashMap, HashSet},
|
||||||
env,
|
env,
|
||||||
os::fd::{AsRawFd, OwnedFd},
|
os::fd::{AsRawFd, BorrowedFd, OwnedFd},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{self, MutexGuard},
|
sync::{self, Arc, MutexGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
use nix::{
|
use nix::{
|
||||||
@@ -14,10 +14,12 @@ use nix::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
expand::expand_aliases,
|
||||||
libsh::error::ShResult,
|
libsh::error::ShResult,
|
||||||
parse::{Redir, RedirType, execute::exec_input},
|
parse::{ParsedSrc, Redir, RedirType, execute::exec_input, lex::LexFlags},
|
||||||
procio::{IoFrame, IoMode, RedirGuard},
|
procio::{IoFrame, IoMode, RedirGuard},
|
||||||
state::{MetaTab, SHED},
|
readline::register::{restore_registers, save_registers},
|
||||||
|
state::{MetaTab, SHED, read_logic},
|
||||||
};
|
};
|
||||||
|
|
||||||
static TEST_MUTEX: sync::Mutex<()> = sync::Mutex::new(());
|
static TEST_MUTEX: sync::Mutex<()> = sync::Mutex::new(());
|
||||||
@@ -41,9 +43,9 @@ pub struct TestGuard {
|
|||||||
old_cwd: PathBuf,
|
old_cwd: PathBuf,
|
||||||
saved_env: HashMap<String, String>,
|
saved_env: HashMap<String, String>,
|
||||||
pty_master: OwnedFd,
|
pty_master: OwnedFd,
|
||||||
_pty_slave: OwnedFd,
|
pty_slave: OwnedFd,
|
||||||
|
|
||||||
cleanups: Vec<Box<dyn FnOnce()>>
|
cleanups: Vec<Box<dyn FnOnce()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestGuard {
|
impl TestGuard {
|
||||||
@@ -57,50 +59,49 @@ impl TestGuard {
|
|||||||
tcsetattr(&pty_slave, SetArg::TCSANOW, &attrs).unwrap();
|
tcsetattr(&pty_slave, SetArg::TCSANOW, &attrs).unwrap();
|
||||||
|
|
||||||
let mut frame = IoFrame::new();
|
let mut frame = IoFrame::new();
|
||||||
frame.push(
|
frame.push(Redir::new(
|
||||||
Redir::new(
|
|
||||||
IoMode::Fd {
|
IoMode::Fd {
|
||||||
tgt_fd: 0,
|
tgt_fd: 0,
|
||||||
src_fd: pty_slave.as_raw_fd(),
|
src_fd: pty_slave.as_raw_fd(),
|
||||||
},
|
},
|
||||||
RedirType::Input,
|
RedirType::Input,
|
||||||
),
|
));
|
||||||
);
|
frame.push(Redir::new(
|
||||||
frame.push(
|
|
||||||
Redir::new(
|
|
||||||
IoMode::Fd {
|
IoMode::Fd {
|
||||||
tgt_fd: 1,
|
tgt_fd: 1,
|
||||||
src_fd: pty_slave.as_raw_fd(),
|
src_fd: pty_slave.as_raw_fd(),
|
||||||
},
|
},
|
||||||
RedirType::Output,
|
RedirType::Output,
|
||||||
),
|
));
|
||||||
);
|
frame.push(Redir::new(
|
||||||
frame.push(
|
|
||||||
Redir::new(
|
|
||||||
IoMode::Fd {
|
IoMode::Fd {
|
||||||
tgt_fd: 2,
|
tgt_fd: 2,
|
||||||
src_fd: pty_slave.as_raw_fd(),
|
src_fd: pty_slave.as_raw_fd(),
|
||||||
},
|
},
|
||||||
RedirType::Output,
|
RedirType::Output,
|
||||||
),
|
));
|
||||||
);
|
|
||||||
|
|
||||||
let _redir_guard = frame.redirect().unwrap();
|
let _redir_guard = frame.redirect().unwrap();
|
||||||
|
|
||||||
let old_cwd = env::current_dir().unwrap();
|
let old_cwd = env::current_dir().unwrap();
|
||||||
let saved_env = env::vars().collect();
|
let saved_env = env::vars().collect();
|
||||||
SHED.with(|s| s.save());
|
SHED.with(|s| s.save());
|
||||||
|
save_registers();
|
||||||
Self {
|
Self {
|
||||||
_lock,
|
_lock,
|
||||||
_redir_guard,
|
_redir_guard,
|
||||||
old_cwd,
|
old_cwd,
|
||||||
saved_env,
|
saved_env,
|
||||||
pty_master,
|
pty_master,
|
||||||
_pty_slave: pty_slave,
|
pty_slave,
|
||||||
cleanups: vec![],
|
cleanups: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pty_slave(&self) -> BorrowedFd<'_> {
|
||||||
|
unsafe { BorrowedFd::borrow_raw(self.pty_slave.as_raw_fd()) }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_cleanup(&mut self, f: impl FnOnce() + 'static) {
|
pub fn add_cleanup(&mut self, f: impl FnOnce() + 'static) {
|
||||||
self.cleanups.push(Box::new(f));
|
self.cleanups.push(Box::new(f));
|
||||||
}
|
}
|
||||||
@@ -111,7 +112,8 @@ impl TestGuard {
|
|||||||
fcntl(
|
fcntl(
|
||||||
self.pty_master.as_raw_fd(),
|
self.pty_master.as_raw_fd(),
|
||||||
FcntlArg::F_SETFL(flags | OFlag::O_NONBLOCK),
|
FcntlArg::F_SETFL(flags | OFlag::O_NONBLOCK),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut out = vec![];
|
let mut out = vec![];
|
||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
@@ -123,10 +125,7 @@ impl TestGuard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fcntl(
|
fcntl(self.pty_master.as_raw_fd(), FcntlArg::F_SETFL(flags)).unwrap();
|
||||||
self.pty_master.as_raw_fd(),
|
|
||||||
FcntlArg::F_SETFL(flags),
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
String::from_utf8_lossy(&out).to_string()
|
String::from_utf8_lossy(&out).to_string()
|
||||||
}
|
}
|
||||||
@@ -142,14 +141,133 @@ impl Drop for TestGuard {
|
|||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
env::set_current_dir(&self.old_cwd).ok();
|
env::set_current_dir(&self.old_cwd).ok();
|
||||||
for (k, _) in env::vars() {
|
for (k, _) in env::vars() {
|
||||||
unsafe { env::remove_var(&k); }
|
unsafe {
|
||||||
|
env::remove_var(&k);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (k, v) in &self.saved_env {
|
for (k, v) in &self.saved_env {
|
||||||
unsafe { env::set_var(k, v); }
|
unsafe {
|
||||||
|
env::set_var(k, v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for cleanup in self.cleanups.drain(..).rev() {
|
for cleanup in self.cleanups.drain(..).rev() {
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
SHED.with(|s| s.restore());
|
SHED.with(|s| s.restore());
|
||||||
|
restore_registers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_ast(input: &str) -> ShResult<Vec<crate::parse::Node>> {
|
||||||
|
let log_tab = read_logic(|l| l.clone());
|
||||||
|
let input = expand_aliases(input.into(), HashSet::new(), &log_tab);
|
||||||
|
|
||||||
|
let source_name = "test_input".to_string();
|
||||||
|
let mut parser = ParsedSrc::new(Arc::new(input))
|
||||||
|
.with_lex_flags(LexFlags::empty())
|
||||||
|
.with_name(source_name.clone());
|
||||||
|
|
||||||
|
parser
|
||||||
|
.parse_src()
|
||||||
|
.map_err(|e| e.into_iter().next().unwrap())?;
|
||||||
|
|
||||||
|
Ok(parser.extract_nodes())
|
||||||
|
}
|
||||||
|
|
||||||
|
impl crate::parse::Node {
|
||||||
|
pub fn assert_structure(
|
||||||
|
&mut self,
|
||||||
|
expected: &mut impl Iterator<Item = NdKind>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let mut full_structure = vec![];
|
||||||
|
let mut before = vec![];
|
||||||
|
let mut after = vec![];
|
||||||
|
let mut offender = None;
|
||||||
|
|
||||||
|
self.walk_tree(&mut |s| {
|
||||||
|
let expected_rule = expected.next();
|
||||||
|
full_structure.push(s.class.as_nd_kind());
|
||||||
|
|
||||||
|
if offender.is_none()
|
||||||
|
&& expected_rule
|
||||||
|
.as_ref()
|
||||||
|
.is_none_or(|e| *e != s.class.as_nd_kind())
|
||||||
|
{
|
||||||
|
offender = Some((s.class.as_nd_kind(), expected_rule));
|
||||||
|
} else if offender.is_none() {
|
||||||
|
before.push(s.class.as_nd_kind());
|
||||||
|
} else {
|
||||||
|
after.push(s.class.as_nd_kind());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
expected.next().is_none(),
|
||||||
|
"Expected structure has more nodes than actual structure"
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some((nd_kind, expected_rule)) = offender {
|
||||||
|
let expected_rule = expected_rule.map_or("(none — expected array too short)".into(), |e| {
|
||||||
|
format!("{e:?}")
|
||||||
|
});
|
||||||
|
let full_structure_hint = full_structure
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| format!("\tNdKind::{s:?},"))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("\n");
|
||||||
|
let full_structure_hint =
|
||||||
|
format!("let expected = &mut [\n{full_structure_hint}\n].into_iter();");
|
||||||
|
|
||||||
|
let output = [
|
||||||
|
"Structure assertion failed!\n".into(),
|
||||||
|
format!(
|
||||||
|
"Expected node type '{:?}', found '{:?}'",
|
||||||
|
expected_rule, nd_kind
|
||||||
|
),
|
||||||
|
format!("Before offender: {:?}", before),
|
||||||
|
format!("After offender: {:?}\n", after),
|
||||||
|
format!("hint: here is the full structure as an array\n {full_structure_hint}"),
|
||||||
|
]
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
Err(output)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum NdKind {
|
||||||
|
IfNode,
|
||||||
|
LoopNode,
|
||||||
|
ForNode,
|
||||||
|
CaseNode,
|
||||||
|
Command,
|
||||||
|
Pipeline,
|
||||||
|
Conjunction,
|
||||||
|
Assignment,
|
||||||
|
BraceGrp,
|
||||||
|
Negate,
|
||||||
|
Test,
|
||||||
|
FuncDef,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl crate::parse::NdRule {
|
||||||
|
pub fn as_nd_kind(&self) -> NdKind {
|
||||||
|
match self {
|
||||||
|
Self::Negate { .. } => NdKind::Negate,
|
||||||
|
Self::IfNode { .. } => NdKind::IfNode,
|
||||||
|
Self::LoopNode { .. } => NdKind::LoopNode,
|
||||||
|
Self::ForNode { .. } => NdKind::ForNode,
|
||||||
|
Self::CaseNode { .. } => NdKind::CaseNode,
|
||||||
|
Self::Command { .. } => NdKind::Command,
|
||||||
|
Self::Pipeline { .. } => NdKind::Pipeline,
|
||||||
|
Self::Conjunction { .. } => NdKind::Conjunction,
|
||||||
|
Self::Assignment { .. } => NdKind::Assignment,
|
||||||
|
Self::BraceGrp { .. } => NdKind::BraceGrp,
|
||||||
|
Self::Test { .. } => NdKind::Test,
|
||||||
|
Self::FuncDef { .. } => NdKind::FuncDef,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
319
tests/gen_vi_tests.lua
Normal file
319
tests/gen_vi_tests.lua
Normal file
@@ -0,0 +1,319 @@
|
|||||||
|
-- Generate Rust vi_test! macro invocations using neovim as oracle
|
||||||
|
-- Usage: nvim --headless --clean -l tests/gen_vi_tests.lua
|
||||||
|
--
|
||||||
|
-- Define test cases as { name, input_text, key_sequence }
|
||||||
|
-- Key sequences use vim notation: <Esc>, <CR>, <C-w>, etc.
|
||||||
|
-- The script executes each in a fresh buffer and captures the result.
|
||||||
|
|
||||||
|
local tests = {
|
||||||
|
-- ===================== basic char motions =====================
|
||||||
|
{ "dw_basic", "hello world", "dw" },
|
||||||
|
{ "dw_middle", "one two three", "wdw" },
|
||||||
|
{ "dd_whole_line", "hello world", "dd" },
|
||||||
|
{ "x_single", "hello", "x" },
|
||||||
|
{ "x_middle", "hello", "llx" },
|
||||||
|
{ "X_backdelete", "hello", "llX" },
|
||||||
|
{ "h_motion", "hello", "$h" },
|
||||||
|
{ "l_motion", "hello", "l" },
|
||||||
|
{ "h_at_start", "hello", "h" },
|
||||||
|
{ "l_at_end", "hello", "$l" },
|
||||||
|
|
||||||
|
-- ===================== word motions (small) =====================
|
||||||
|
{ "w_forward", "one two three", "w" },
|
||||||
|
{ "b_backward", "one two three", "$b" },
|
||||||
|
{ "e_end", "one two three", "e" },
|
||||||
|
{ "ge_back_end", "one two three", "$ge" },
|
||||||
|
{ "w_punctuation", "foo.bar baz", "w" },
|
||||||
|
{ "e_punctuation", "foo.bar baz", "e" },
|
||||||
|
{ "b_punctuation", "foo.bar baz", "$b" },
|
||||||
|
{ "w_at_eol", "hello", "$w" },
|
||||||
|
{ "b_at_bol", "hello", "b" },
|
||||||
|
|
||||||
|
-- ===================== word motions (big) =====================
|
||||||
|
{ "W_forward", "foo.bar baz", "W" },
|
||||||
|
{ "B_backward", "foo.bar baz", "$B" },
|
||||||
|
{ "E_end", "foo.bar baz", "E" },
|
||||||
|
{ "gE_back_end", "one two three", "$gE" },
|
||||||
|
{ "W_skip_punct", "one-two three", "W" },
|
||||||
|
{ "B_skip_punct", "one two-three", "$B" },
|
||||||
|
{ "E_skip_punct", "one-two three", "E" },
|
||||||
|
{ "dW_big", "foo.bar baz", "dW" },
|
||||||
|
{ "cW_big", "foo.bar baz", "cWx<Esc>" },
|
||||||
|
|
||||||
|
-- ===================== line motions =====================
|
||||||
|
{ "zero_bol", " hello", "$0" },
|
||||||
|
{ "caret_first_char", " hello", "$^" },
|
||||||
|
{ "dollar_eol", "hello world", "$" },
|
||||||
|
{ "g_last_nonws", "hello ", "g_" },
|
||||||
|
{ "g_no_trailing", "hello", "g_" },
|
||||||
|
{ "pipe_column", "hello world", "6|" },
|
||||||
|
{ "pipe_col1", "hello world", "1|" },
|
||||||
|
{ "I_insert_front", " hello", "Iworld <Esc>" },
|
||||||
|
{ "A_append_end", "hello", "A world<Esc>" },
|
||||||
|
|
||||||
|
-- ===================== find motions =====================
|
||||||
|
{ "f_find", "hello world", "fo" },
|
||||||
|
{ "F_find_back", "hello world", "$Fo" },
|
||||||
|
{ "t_till", "hello world", "tw" },
|
||||||
|
{ "T_till_back", "hello world", "$To" },
|
||||||
|
{ "f_no_match", "hello", "fz" },
|
||||||
|
{ "semicolon_repeat", "abcabc", "fa;;" },
|
||||||
|
{ "comma_reverse", "abcabc", "fa;;," },
|
||||||
|
{ "df_semicolon", "abcabc", "fa;;dfa" },
|
||||||
|
{ "t_at_target", "aab", "lta" },
|
||||||
|
|
||||||
|
-- ===================== delete operations =====================
|
||||||
|
{ "D_to_end", "hello world", "wD" },
|
||||||
|
{ "d_dollar", "hello world", "wd$" },
|
||||||
|
{ "d0_to_start", "hello world", "$d0" },
|
||||||
|
{ "dw_multiple", "one two three", "d2w" },
|
||||||
|
{ "dt_char", "hello world", "dtw" },
|
||||||
|
{ "df_char", "hello world", "dfw" },
|
||||||
|
{ "dh_back", "hello", "lldh" },
|
||||||
|
{ "dl_forward", "hello", "dl" },
|
||||||
|
{ "dge_back_end", "one two three", "$dge" },
|
||||||
|
{ "dG_to_end", "hello world", "dG" },
|
||||||
|
{ "dgg_to_start", "hello world", "$dgg" },
|
||||||
|
{ "d_semicolon", "abcabc", "fad;" },
|
||||||
|
|
||||||
|
-- ===================== change operations =====================
|
||||||
|
{ "cw_basic", "hello world", "cwfoo<Esc>" },
|
||||||
|
{ "C_to_end", "hello world", "wCfoo<Esc>" },
|
||||||
|
{ "cc_whole", "hello world", "ccfoo<Esc>" },
|
||||||
|
{ "ct_char", "hello world", "ctwfoo<Esc>" },
|
||||||
|
{ "s_single", "hello", "sfoo<Esc>" },
|
||||||
|
{ "S_whole_line", "hello world", "Sfoo<Esc>" },
|
||||||
|
{ "cl_forward", "hello", "clX<Esc>" },
|
||||||
|
{ "ch_backward", "hello", "llchX<Esc>" },
|
||||||
|
{ "cb_word_back", "hello world", "$cbfoo<Esc>" },
|
||||||
|
{ "ce_word_end", "hello world", "cefoo<Esc>" },
|
||||||
|
{ "c0_to_start", "hello world", "wc0foo<Esc>" },
|
||||||
|
|
||||||
|
-- ===================== yank and paste =====================
|
||||||
|
{ "yw_p_basic", "hello world", "ywwP" },
|
||||||
|
{ "dw_p_paste", "hello world", "dwP" },
|
||||||
|
{ "dd_p_paste", "hello world", "ddp" },
|
||||||
|
{ "y_dollar_p", "hello world", "wy$P" },
|
||||||
|
{ "ye_p", "hello world", "yewP" },
|
||||||
|
{ "yy_p", "hello world", "yyp" },
|
||||||
|
{ "Y_p", "hello world", "Yp" },
|
||||||
|
{ "p_after_x", "hello", "xp" },
|
||||||
|
{ "P_before", "hello", "llxP" },
|
||||||
|
{ "paste_empty", "hello", "p" },
|
||||||
|
|
||||||
|
-- ===================== replace =====================
|
||||||
|
{ "r_replace", "hello", "ra" },
|
||||||
|
{ "r_middle", "hello", "llra" },
|
||||||
|
{ "r_at_end", "hello", "$ra" },
|
||||||
|
{ "r_space", "hello", "r " },
|
||||||
|
{ "r_with_count", "hello", "3rx" },
|
||||||
|
|
||||||
|
-- ===================== case operations =====================
|
||||||
|
{ "tilde_single", "hello", "~" },
|
||||||
|
{ "tilde_count", "hello", "3~" },
|
||||||
|
{ "tilde_at_end", "HELLO", "$~" },
|
||||||
|
{ "tilde_mixed", "hElLo", "5~" },
|
||||||
|
{ "gu_word", "HELLO world", "guw" },
|
||||||
|
{ "gU_word", "hello WORLD", "gUw" },
|
||||||
|
{ "gu_dollar", "HELLO WORLD", "gu$" },
|
||||||
|
{ "gU_dollar", "hello world", "gU$" },
|
||||||
|
{ "gu_0", "HELLO WORLD", "$gu0" },
|
||||||
|
{ "gU_0", "hello world", "$gU0" },
|
||||||
|
{ "gtilde_word", "hello WORLD", "g~w" },
|
||||||
|
{ "gtilde_dollar", "hello WORLD", "g~$" },
|
||||||
|
|
||||||
|
-- ===================== text objects: word =====================
|
||||||
|
{ "diw_inner", "one two three", "wdiw" },
|
||||||
|
{ "ciw_replace", "hello world", "ciwfoo<Esc>" },
|
||||||
|
{ "daw_around", "one two three", "wdaw" },
|
||||||
|
{ "yiw_p", "hello world", "yiwAp <Esc>p" },
|
||||||
|
{ "diW_big_inner", "one-two three", "diW" },
|
||||||
|
{ "daW_big_around", "one two-three end", "wdaW" },
|
||||||
|
{ "ciW_big", "one-two three", "ciWx<Esc>" },
|
||||||
|
|
||||||
|
-- ===================== text objects: quotes =====================
|
||||||
|
{ "di_dquote", 'one "two" three', 'f"di"' },
|
||||||
|
{ "da_dquote", 'one "two" three', 'f"da"' },
|
||||||
|
{ "ci_dquote", 'one "two" three', 'f"ci"x<Esc>' },
|
||||||
|
{ "di_squote", "one 'two' three", "f'di'" },
|
||||||
|
{ "da_squote", "one 'two' three", "f'da'" },
|
||||||
|
{ "di_backtick", "one `two` three", "f`di`" },
|
||||||
|
{ "da_backtick", "one `two` three", "f`da`" },
|
||||||
|
{ "ci_dquote_empty", 'one "" three', 'f"ci"x<Esc>' },
|
||||||
|
|
||||||
|
-- ===================== text objects: delimiters =====================
|
||||||
|
{ "di_paren", "one (two) three", "f(di(" },
|
||||||
|
{ "da_paren", "one (two) three", "f(da(" },
|
||||||
|
{ "ci_paren", "one (two) three", "f(ci(x<Esc>" },
|
||||||
|
{ "di_brace", "one {two} three", "f{di{" },
|
||||||
|
{ "da_brace", "one {two} three", "f{da{" },
|
||||||
|
{ "di_bracket", "one [two] three", "f[di[" },
|
||||||
|
{ "da_bracket", "one [two] three", "f[da[" },
|
||||||
|
{ "di_angle", "one <two> three", "f<di<" },
|
||||||
|
{ "da_angle", "one <two> three", "f<da<" },
|
||||||
|
{ "di_paren_nested", "fn(a, (b, c))", "f(di(" },
|
||||||
|
{ "di_paren_empty", "fn() end", "f(di(" },
|
||||||
|
{ "dib_alias", "one (two) three", "f(dib" },
|
||||||
|
{ "diB_alias", "one {two} three", "f{diB" },
|
||||||
|
|
||||||
|
-- ===================== delimiter matching =====================
|
||||||
|
{ "percent_paren", "(hello) world", "%" },
|
||||||
|
{ "percent_brace", "{hello} world", "%" },
|
||||||
|
{ "percent_bracket", "[hello] world", "%" },
|
||||||
|
{ "percent_from_close", "(hello) world", "f)%" },
|
||||||
|
{ "d_percent_paren", "(hello) world", "d%" },
|
||||||
|
|
||||||
|
-- ===================== insert mode entry =====================
|
||||||
|
{ "i_insert", "hello", "iX<Esc>" },
|
||||||
|
{ "a_append", "hello", "aX<Esc>" },
|
||||||
|
{ "I_front", " hello", "IX<Esc>" },
|
||||||
|
{ "A_end", "hello", "AX<Esc>" },
|
||||||
|
{ "o_open_below", "hello", "oworld<Esc>" },
|
||||||
|
{ "O_open_above", "hello", "Oworld<Esc>" },
|
||||||
|
|
||||||
|
-- ===================== insert mode operations =====================
|
||||||
|
{ "empty_input", "", "i hello<Esc>" },
|
||||||
|
{ "insert_escape", "hello", "aX<Esc>" },
|
||||||
|
{ "ctrl_w_del_word", "hello world", "A<C-w><Esc>" },
|
||||||
|
{ "ctrl_h_backspace", "hello", "A<C-h><Esc>" },
|
||||||
|
|
||||||
|
-- ===================== undo / redo =====================
|
||||||
|
{ "u_undo_delete", "hello world", "dwu" },
|
||||||
|
{ "u_undo_change", "hello world", "ciwfoo<Esc>u" },
|
||||||
|
{ "u_undo_x", "hello", "xu" },
|
||||||
|
{ "ctrl_r_redo", "hello", "xu<C-r>" },
|
||||||
|
{ "u_multiple", "hello world", "xdwu" },
|
||||||
|
{ "redo_after_undo", "hello world", "dwu<C-r>" },
|
||||||
|
|
||||||
|
-- ===================== dot repeat =====================
|
||||||
|
{ "dot_repeat_x", "hello", "x." },
|
||||||
|
{ "dot_repeat_dw", "one two three", "dw." },
|
||||||
|
{ "dot_repeat_cw", "one two three", "cwfoo<Esc>w." },
|
||||||
|
{ "dot_repeat_r", "hello", "ra.." },
|
||||||
|
{ "dot_repeat_s", "hello", "sX<Esc>l." },
|
||||||
|
|
||||||
|
-- ===================== counts =====================
|
||||||
|
{ "count_h", "hello world", "$3h" },
|
||||||
|
{ "count_l", "hello world", "3l" },
|
||||||
|
{ "count_w", "one two three four", "2w" },
|
||||||
|
{ "count_b", "one two three four", "$2b" },
|
||||||
|
{ "count_x", "hello", "3x" },
|
||||||
|
{ "count_dw", "one two three four", "2dw" },
|
||||||
|
{ "verb_count_motion", "one two three four", "d2w" },
|
||||||
|
{ "count_s", "hello", "3sX<Esc>" },
|
||||||
|
|
||||||
|
-- ===================== indent / dedent =====================
|
||||||
|
{ "indent_line", "hello", ">>" },
|
||||||
|
{ "dedent_line", "\thello", "<<" },
|
||||||
|
{ "indent_double", "hello", ">>>>" },
|
||||||
|
|
||||||
|
-- ===================== join =====================
|
||||||
|
{ "J_join_lines", "hello\nworld", "J" },
|
||||||
|
|
||||||
|
-- ===================== case in visual =====================
|
||||||
|
{ "v_u_lower", "HELLO", "vlllu" },
|
||||||
|
{ "v_U_upper", "hello", "vlllU" },
|
||||||
|
|
||||||
|
-- ===================== visual mode =====================
|
||||||
|
{ "v_d_delete", "hello world", "vwwd" },
|
||||||
|
{ "v_x_delete", "hello world", "vwwx" },
|
||||||
|
{ "v_c_change", "hello world", "vwcfoo<Esc>" },
|
||||||
|
{ "v_y_p_yank", "hello world", "vwyAp <Esc>p" },
|
||||||
|
{ "v_dollar_d", "hello world", "wv$d" },
|
||||||
|
{ "v_0_d", "hello world", "$v0d" },
|
||||||
|
{ "ve_d", "hello world", "ved" },
|
||||||
|
{ "v_o_swap", "hello world", "vllod" },
|
||||||
|
{ "v_r_replace", "hello", "vlllrx" },
|
||||||
|
{ "v_tilde_case", "hello", "vlll~" },
|
||||||
|
|
||||||
|
-- ===================== visual line mode =====================
|
||||||
|
{ "V_d_delete", "hello world", "Vd" },
|
||||||
|
{ "V_y_p", "hello world", "Vyp" },
|
||||||
|
{ "V_S_change", "hello world", "VSfoo<Esc>" },
|
||||||
|
|
||||||
|
-- ===================== increment / decrement =====================
|
||||||
|
{ "ctrl_a_inc", "num 5 end", "w<C-a>" },
|
||||||
|
{ "ctrl_x_dec", "num 5 end", "w<C-x>" },
|
||||||
|
{ "ctrl_a_negative", "num -3 end", "w<C-a>" },
|
||||||
|
{ "ctrl_x_to_neg", "num 0 end", "w<C-x>" },
|
||||||
|
{ "ctrl_a_count", "num 5 end", "w3<C-a>" },
|
||||||
|
|
||||||
|
-- ===================== misc / edge cases =====================
|
||||||
|
{ "delete_empty", "", "x" },
|
||||||
|
{ "undo_on_empty", "", "u" },
|
||||||
|
{ "w_single_char", "a b c", "w" },
|
||||||
|
{ "dw_last_word", "hello", "dw" },
|
||||||
|
{ "dollar_single", "h", "$" },
|
||||||
|
{ "caret_no_ws", "hello", "$^" },
|
||||||
|
{ "f_last_char", "hello", "fo" },
|
||||||
|
{ "r_on_space", "hello world", "5|r-" },
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Map vim special key names to Rust string escape sequences
|
||||||
|
local key_to_bytes = {
|
||||||
|
["<Esc>"] = "\\x1b",
|
||||||
|
["<CR>"] = "\\r",
|
||||||
|
["<BS>"] = "\\x7f",
|
||||||
|
["<Tab>"] = "\\t",
|
||||||
|
["<Del>"] = "\\x1b[3~",
|
||||||
|
["<Up>"] = "\\x1b[A",
|
||||||
|
["<Down>"] = "\\x1b[B",
|
||||||
|
["<Right>"] = "\\x1b[C",
|
||||||
|
["<Left>"] = "\\x1b[D",
|
||||||
|
["<Home>"] = "\\x1b[H",
|
||||||
|
["<End>"] = "\\x1b[F",
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Convert vim key notation to Rust string escape sequences
|
||||||
|
local function keys_to_rust(keys)
|
||||||
|
local result = keys
|
||||||
|
result = result:gsub("<C%-(.)>", function(ch)
|
||||||
|
local byte = string.byte(ch:lower()) - string.byte('a') + 1
|
||||||
|
return string.format("\\x%02x", byte)
|
||||||
|
end)
|
||||||
|
for name, bytes in pairs(key_to_bytes) do
|
||||||
|
result = result:gsub(vim.pesc(name), bytes)
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Escape a string for use in a Rust string literal
|
||||||
|
local function rust_escape(s)
|
||||||
|
return s:gsub("\\", "\\\\"):gsub('"', '\\"'):gsub("\n", "\\n"):gsub("\t", "\\t")
|
||||||
|
end
|
||||||
|
|
||||||
|
io.write("vi_test! {\n")
|
||||||
|
|
||||||
|
for i, test in ipairs(tests) do
|
||||||
|
local name, input, keys = test[1], test[2], test[3]
|
||||||
|
|
||||||
|
-- Fresh buffer and register state
|
||||||
|
local input_lines = vim.split(input, "\n", { plain = true })
|
||||||
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, input_lines)
|
||||||
|
vim.api.nvim_win_set_cursor(0, { 1, 0 })
|
||||||
|
vim.fn.setreg('"', '')
|
||||||
|
|
||||||
|
-- Execute the key sequence synchronously
|
||||||
|
local translated = vim.api.nvim_replace_termcodes(keys, true, false, true)
|
||||||
|
vim.api.nvim_feedkeys(translated, "ntx", false)
|
||||||
|
vim.api.nvim_exec_autocmds("CursorMoved", {})
|
||||||
|
|
||||||
|
-- Capture result
|
||||||
|
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||||
|
local result = table.concat(lines, "\n")
|
||||||
|
local cursor_col = vim.api.nvim_win_get_cursor(0)[2]
|
||||||
|
|
||||||
|
local rust_keys = keys_to_rust(keys)
|
||||||
|
local rust_input = rust_escape(input)
|
||||||
|
local rust_result = rust_escape(result)
|
||||||
|
|
||||||
|
local sep = ";"
|
||||||
|
if i == #tests then sep = "" end
|
||||||
|
|
||||||
|
io.write(string.format('\tvi_%s: "%s" => "%s" => "%s", %d%s\n',
|
||||||
|
name, rust_input, rust_keys, rust_result, cursor_col, sep))
|
||||||
|
end
|
||||||
|
|
||||||
|
io.write("}\n")
|
||||||
|
|
||||||
|
vim.cmd("qa!")
|
||||||
Reference in New Issue
Block a user