Skip to content

Commit 973f881

Browse files
authored
Fix hooks.md basic hooks examples and hooks list (nushell#1889)
Fix hooks syntax; they are a list of values. The code as it was did not run. Nushell has been using an inherent configuration and the user configuration only replacing parts. For the hooks it makes sense to set only the hooks value. Replace "you can also use a list", which is the default and used above already, with a "you can append additional hooks" example. For zh-CN, at least add the missing available hook docs in English instead of missing them altogether. For zh-CN, add the new example replacement with EN text rather than keeping the old outdated one, or missing it altogether.
1 parent 54e3bc2 commit 973f881

File tree

3 files changed

+62
-103
lines changed

3 files changed

+62
-103
lines changed

book/hooks.md

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ The steps to evaluate one line in the REPL mode are as follows:
2828
To enable hooks, define them in your [config](configuration.md):
2929

3030
```nu
31-
$env.config = {
32-
# ...other config...
33-
34-
hooks: {
35-
pre_prompt: { print "pre prompt hook" }
36-
pre_execution: { print "pre exec hook" }
37-
env_change: {
38-
PWD: {|before, after| print $"changing directory from ($before) to ($after)" }
39-
}
31+
$env.config.hooks = {
32+
pre_prompt: [{ print "pre prompt hook" }]
33+
pre_execution: [{ print "pre exec hook" }]
34+
env_change: {
35+
PWD: [{|before, after| print $"changing directory from ($before) to ($after)" }]
4036
}
4137
}
4238
```
@@ -47,38 +43,28 @@ When you change a directory, the `PWD` environment variable changes and the chan
4743
Instead of defining just a single hook per trigger, it is possible to define a **list of hooks** which will run in sequence:
4844

4945
```nu
50-
$env.config = {
51-
...other config...
52-
53-
hooks: {
54-
pre_prompt: [
55-
{ print "pre prompt hook" }
56-
{ print "pre prompt hook2" }
57-
]
58-
pre_execution: [
59-
{ print "pre exec hook" }
60-
{ print "pre exec hook2" }
46+
$env.config.hooks = {
47+
pre_prompt: [
48+
{ print "pre prompt hook" }
49+
{ print "pre prompt hook2" }
50+
]
51+
pre_execution: [
52+
{ print "pre exec hook" }
53+
{ print "pre exec hook2" }
54+
]
55+
env_change: {
56+
PWD: [
57+
{|before, after| print $"changing directory from ($before) to ($after)" }
58+
{|before, after| print $"changing directory from ($before) to ($after) 2" }
6159
]
62-
env_change: {
63-
PWD: [
64-
{|before, after| print $"changing directory from ($before) to ($after)" }
65-
{|before, after| print $"changing directory from ($before) to ($after) 2" }
66-
]
67-
}
6860
}
6961
}
7062
```
7163

72-
Also, it might be more practical to update the existing config with new hooks, instead of defining the whole config from scratch:
64+
Instead of replacing all hooks, you can append a new hook to existing configuration:
7365

7466
```nu
75-
$env.config = ($env.config | upsert hooks {
76-
pre_prompt: ...
77-
pre_execution: ...
78-
env_change: {
79-
PWD: ...
80-
}
81-
})
67+
$env.config.hooks.pre_execution = $env.config.hooks.pre_execution | append { print "pre exec hook3" }
8268
```
8369

8470
## Changing Environment

de/book/hooks.md

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ Die Schritte zur Auswertung einer Zeile im REPL-Modus sind wie folgt:
2828
Um Hooks zu aktivieren, werden sie in der [config](configuration.md) definiert:
2929

3030
```nu
31-
$env.config = {
32-
# ...other config...
33-
34-
hooks: {
35-
pre_prompt: { print "pre prompt hook" }
36-
pre_execution: { print "pre exec hook" }
37-
env_change: {
38-
PWD: {|before, after| print $"changing directory from ($before) to ($after)" }
39-
}
31+
$env.config.hooks = {
32+
pre_prompt: [{ print "pre prompt hook" }]
33+
pre_execution: [{ print "pre exec hook" }]
34+
env_change: {
35+
PWD: [{|before, after| print $"changing directory from ($before) to ($after)" }]
4036
}
4137
}
4238
```
@@ -47,39 +43,28 @@ Die Änderung löst den Hook aus und tauscht die entsprechenden Werte in `before
4743
Anstatt nur einen einzigen Hook pro Trigger zu definieren, ist es möglich, eine *Liste von Hooks* zu definieren, die nacheinander durchlaufen werden:
4844

4945
```nu
50-
$env.config = {
51-
...other config...
52-
53-
hooks: {
54-
pre_prompt: [
55-
{ print "pre prompt hook" }
56-
{ print "pre prompt hook2" }
57-
]
58-
pre_execution: [
59-
{ print "pre exec hook" }
60-
{ print "pre exec hook2" }
46+
$env.config.hooks = {
47+
pre_prompt: [
48+
{ print "pre prompt hook" }
49+
{ print "pre prompt hook2" }
50+
]
51+
pre_execution: [
52+
{ print "pre exec hook" }
53+
{ print "pre exec hook2" }
54+
]
55+
env_change: {
56+
PWD: [
57+
{|before, after| print $"changing directory from ($before) to ($after)" }
58+
{|before, after| print $"changing directory from ($before) to ($after) 2" }
6159
]
62-
env_change: {
63-
PWD: [
64-
{|before, after| print $"changing directory from ($before) to ($after)" }
65-
{|before, after| print $"changing directory from ($before) to ($after) 2" }
66-
]
67-
}
6860
}
6961
}
7062
```
7163

72-
Auch könnte es praktischer sein, die bestehende Konfiguration mit neuen Hooks zu aktualisieren,
73-
anstatt die gesamte Konfiguration von Grund auf neu zu definieren:
64+
Anstatt alle hooks zu ersetzen können neue Hooks der Liste vorhandener Hooks angehängt werden:
7465

7566
```nu
76-
$env.config = ($env.config | upsert hooks {
77-
pre_prompt: ...
78-
pre_execution: ...
79-
env_change: {
80-
PWD: ...
81-
}
82-
})
67+
$env.config.hooks.pre_execution = $env.config.hooks.pre_execution | append { print "pre exec hook3" }
8368
```
8469

8570
## Changing Environment

zh-CN/book/hooks.md

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- `pre_prompt` : 在命令提示显示之前被触发;
99
- `pre_execution` : 在行输入开始执行前被触发;
1010
- `env_change` : 当环境变量发生变化时被触发;
11+
- `display_output` : A block that the output is passed to
12+
- `command_not_found` : Triggered when a command is not found
1113

1214
为了更清晰地阐述,我们可以将 Nushell 的执行周期进行分解。
1315
在 REPL 模式下,评估一行(代码)的步骤如下:
@@ -24,15 +26,11 @@
2426
要想使用钩子需要先在 [配置](configuration.md) 中定义它们:
2527

2628
```nu
27-
$env.config = {
28-
# ...other config...
29-
30-
hooks: {
31-
pre_prompt: { print "pre prompt hook" }
32-
pre_execution: { print "pre exec hook" }
33-
env_change: {
34-
PWD: {|before, after| print $"changing directory from ($before) to ($after)" }
35-
}
29+
$env.config.hooks = {
30+
pre_prompt: [{ print "pre prompt hook" }]
31+
pre_execution: [{ print "pre exec hook" }]
32+
env_change: {
33+
PWD: [{|before, after| print $"changing directory from ($before) to ($after)" }]
3634
}
3735
}
3836
```
@@ -43,38 +41,28 @@ $env.config = {
4341
可以为每个触发器只定义一个钩子,也可以定义一个**钩子列表**,让其依次运行:
4442

4543
```nu
46-
$env.config = {
47-
...other config...
48-
49-
hooks: {
50-
pre_prompt: [
51-
{ print "pre prompt hook" }
52-
{ print "pre prompt hook2" }
53-
]
54-
pre_execution: [
55-
{ print "pre exec hook" }
56-
{ print "pre exec hook2" }
44+
$env.config.hooks = {
45+
pre_prompt: [
46+
{ print "pre prompt hook" }
47+
{ print "pre prompt hook2" }
48+
]
49+
pre_execution: [
50+
{ print "pre exec hook" }
51+
{ print "pre exec hook2" }
52+
]
53+
env_change: {
54+
PWD: [
55+
{|before, after| print $"changing directory from ($before) to ($after)" }
56+
{|before, after| print $"changing directory from ($before) to ($after) 2" }
5757
]
58-
env_change: {
59-
PWD: [
60-
{|before, after| print $"changing directory from ($before) to ($after)" }
61-
{|before, after| print $"changing directory from ($before) to ($after) 2" }
62-
]
63-
}
6458
}
6559
}
6660
```
6761

68-
另外,用新的钩子更新现有的配置,而不是从头开始定义整个配置可能更实用:
62+
Instead of replacing all hooks, you can append a new hook to existing configuration:
6963

7064
```nu
71-
$env.config = ($env.config | upsert hooks {
72-
pre_prompt: ...
73-
pre_execution: ...
74-
env_change: {
75-
PWD: ...
76-
}
77-
})
65+
$env.config.hooks.pre_execution = $env.config.hooks.pre_execution | append { print "pre exec hook3" }
7866
```
7967

8068
## 修改环境变量

0 commit comments

Comments
 (0)