Skip to content

Commit 8b3e224

Browse files
authored
Some cleanup of lang-guide.md (#1259)
1 parent 2f10752 commit 8b3e224

File tree

1 file changed

+53
-49
lines changed

1 file changed

+53
-49
lines changed

lang-guide/lang-guide.md

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Integer numbers.
3131

3232
Internally represented as a signed 64-bit number with two's complement arithmetic.
3333

34-
Numeric literals without a fractional components will evaluate as `Int`: `1`, `-2`, `1000`
34+
Numeric literals without a fractional component will evaluate as `Int`: `1`, `-2`, `1000`
3535

3636
Integers can be created using hex `0xff`, octal `0o234`, binary `0b10101`, and decimal `123`.
3737

@@ -116,9 +116,9 @@ As strings have to be valid UTF-8 for effective string operations, they can not
116116

117117
Different display operations might impose limitations on which non-printable or printable characters get shown. One relevant area are the ANSI escape sequences that can be used to affect the display on the terminal. Certain operations may choose to ignore those.
118118

119-
To input string data different [string literals](#string-literals) supporting escaping and [string interpolation](#string-interpolation) are available.
119+
To input string data, different [string literals](#string-literals) supporting escaping and [string interpolation](#string-interpolation) are available.
120120

121-
TBD: On which level string indexing should be performed: bytes or unicode scalars.
121+
TBD: On which level string indexing should be performed: bytes or Unicode scalars.
122122

123123
### Record
124124

@@ -184,7 +184,7 @@ Nushell offers a way of creating binary literals in your data. These are in one
184184

185185
- `0x[ffffffff]` - hex-based binary representation
186186
- `0o[1234567]` - octal-based binary representation
187-
- `0b[10101010101]` - binary-based binary reprentation
187+
- `0b[10101010101]` - binary-based binary representation
188188

189189
The data inside of the `[]` represents a single data value of bits.
190190

@@ -249,7 +249,7 @@ Nushell provides support for these bitwise operators:
249249

250250
### if
251251

252-
The`if` expression evaluates a condition and then chooses to run a block based on the condition.
252+
The `if` expression evaluates a condition and then chooses to run a block based on the condition.
253253

254254
For example, you can print "yes", based on a true condition:
255255

@@ -315,20 +315,20 @@ The code that follows the `else` is an expression rather than a block, allowing
315315

316316
## String interpolation
317317

318-
String interpolation uses either double quotes or single quotes with a preceeding dollar sign. However, when using double quotes, you have to be aware that escapes will be recognized and interpreted. (I(darren) really don't like that people have to be aware of this functionality with double quotes.)
318+
String interpolation uses either double quotes or single quotes with a preceding dollar sign. However, when using double quotes, you have to be aware that escapes will be recognized and interpreted. (I(darren) really don't like that people have to be aware of this functionality with double quotes.)
319319

320320
Example:
321321

322322
```nu
323-
let name = "nushell"
323+
let name = "Nushell"
324324
print $"My favorite shell is ($name)"
325325
```
326326

327327
There are a couple things to be aware of in the above example.
328328

329329
1. The trigger to recognize a string interpolated string is the `$` sign.
330-
2. Double quotes are use here but single quotes could be used as well. User be aware of escapes if using double quotes.
331-
3. Access variable names needs to be parenthesis as `$name` is in the example.
330+
2. Double quotes are used here, but single quotes could be as well. Be aware of escapes when using double quotes.
331+
3. Accessed variable names need to be in parentheses as `$name` is in the example.
332332

333333
### Executing String Interpolated strings
334334

@@ -344,58 +344,58 @@ let fn = "filename"
344344
^$"($path1)($path2)($fn)"
345345
```
346346

347-
The caret `^` before the string interpolation symbol `$` allows that external command to be exectued.
347+
The caret `^` before the string interpolation symbol `$` allows that external command to be executed.
348348

349349
## String Quoting
350350

351351
### Double quotes
352352

353-
Double quotes are used as you would think normal quotes should be used except for one thing. That one thing is escapes can be recognized and interpreted with double quotes.
353+
Double quotes are used as you would normal quotes, except for one thing: escapes can be recognized and interpreted with double quotes.
354354

355355
Example:
356356

357357
```nu
358358
"\e[31mHello\e[35m Nushell\e[0m"
359359
```
360360

361-
That would be interpreted with `Red` foreground Hello and `Purple aka Magenta` foreground Nushell becuase:
361+
This would be interpreted as a red foreground `Hello` and a magenta/purple foreground `Nushell` because:
362362

363-
1. `\e` means insert and `escape` character
363+
1. `\e` means insert an `escape` character
364364
2. `[31m` means use whatever is defined as `red` foreground in your terminal
365-
3. `[35m` means use whatever is defined as purple, sometimes called magenta, foreground in your terminal.
366-
4. `[0m` means reset all ansi escape sequences.
367-
368-
There are other escapes that are defined by nushell found in parser.rs around line 2426 in the `unescape_string` function.
369-
370-
Recognized nushell escapes:
371-
372-
- " - double quote
373-
- ' - single quote
374-
- \ - back slash
375-
- / - forward slash
376-
- ( - left parenthesis
377-
- ) - right parenthesis
378-
- { - left brace
379-
- } - right brace
380-
- $ - dollar sign
381-
- ^ - caret symbol
382-
- \# - hash / pound sign
383-
- \| - pipe character
384-
- ~ - tilde
385-
- a - bel
386-
- b - bs aka backspace
387-
- e - escape
388-
- f - form feed
389-
- n - line feed aka new line
390-
- r - carriage return
391-
- t - tab aka horizontal tab
392-
- uXXXX - unicode hex value for a char - requires 4 chars. It would be nice if \uXX was acceptible as well.
365+
3. `[35m` means use whatever is defined as `magenta/purple` foreground in your terminal.
366+
4. `[0m` means reset all ANSI escape sequences.
367+
368+
There are other escapes defined by Nushell found in [parser.rs](https://github.com/nushell/nushell/blob/main/crates/nu-parser/src/parser.rs#L2496) around line 2500 in the `unescape_string` function.
369+
370+
Recognized Nushell escapes:
371+
372+
- " - Double quote
373+
- ' - Single quote
374+
- \ - Back slash
375+
- / - Forward slash
376+
- ( - Left parenthesis
377+
- ) - Right parenthesis
378+
- { - Left brace
379+
- } - Right brace
380+
- $ - Dollar sign
381+
- ^ - Caret symbol
382+
- \# - Hash / pound sign
383+
- \| - Pipe character
384+
- ~ - Tilde
385+
- a - Bel
386+
- b - Bs aka Backspace
387+
- e - Escape
388+
- f - Form feed
389+
- n - Line feed aka New Line
390+
- r - Carriage return
391+
- t - Tab aka Horizontal Tab
392+
- uXXXX - Unicode hex value for a char - requires 4 chars. It would be nice if \uXX was acceptable as well.
393393

394394
Double quotes work within string interpolation as well.
395395

396396
### Single quotes
397397

398-
The single quote character should work identicaly to the double quote _except_ that escape characters will not be recognized and interpreted.
398+
The single quote character should work identically to the double quote _except_ that escape characters will not be recognized and interpreted.
399399

400400
Single quotes work within string interpolation as well.
401401

@@ -405,7 +405,7 @@ Backtick quotes are something I'm still fuzzy on. Originally I thought they were
405405

406406
Here are some ways we see/use backtick quotes.
407407

408-
1. If you're using tab to complete directories with spaces, backtick quotes will be used to wrap the string. The only issue with this is that when you want to complete the next folder after one with a space, you have to move the cursor backward inside the last backtick quote before you hit tab again to complete the next level or file.
408+
1. If you're using Tab to complete directories with spaces, backtick quotes will be used to wrap the string. The only issue with this is that when you want to complete the next folder after one with a space, you have to move the cursor backward inside the last backtick quote before you hit tab again to complete the next level or file.
409409
2. Backtick quotes do not work in string interpolation. Should they?
410410
3. I believe backtick quotes can be used the same way as double quotes and single quotes but they do not recognize and interpret escapes.
411411
4. Another definition from Kubouch is backtick quotes are supposed to be like `bare words` that support spaces. As an example JT just landed a PR that allows backtick quotes to autocd. So, in Windows, if you're at `C:\` you could type `` `Program Files` `` and it would change to that directory.
@@ -421,7 +421,7 @@ Example:
421421
'This is also a string "that needs an inner part quoted"'
422422
```
423423

424-
The key to always remember is that double quotes recognize and interpret escapes so if you have any `\` characters in your string, they will be interpreted as excapes. The following is an example of a question we get frequently on Discord.
424+
The key to always remember is that double quotes recognize and interpret escapes so if you have any `\` characters in your string, they will be interpreted as escapes. The following is an example of a question we get frequently on Discord.
425425

426426
```nu
427427
Why doesn't this work?
@@ -454,18 +454,19 @@ It doesn't work because it sees `\P` and `\s` as escapes that are not recognized
454454

455455
### Best practices for pipeline commands
456456

457-
### Interaction with unix pipes
457+
### Interaction with Unix pipes
458458

459459
### Handling stdout and stderr
460460

461461
You can handle stderr in multiple ways:
462-
1. do nothing, stderr will be printed directly
463-
2. pipe stderr to the next command, using `e>|` or `e+o>|`
464-
3. redirect stderr to a file, using `e> file_path`, or `e+o> file_path`
465-
4. use `do -i { cmd } | complete` to capture both stdout and stderr as structured data
466462

463+
1. Do nothing, stderr will be printed directly
464+
2. Pipe stderr to the next command, using `e>|` or `e+o>|`
465+
3. Redirect stderr to a file, using `e> file_path`, or `e+o> file_path`
466+
4. Use `do -i { cmd } | complete` to capture both stdout and stderr as structured data
467467

468468
For the next examples, let's assume this file:
469+
469470
```nushell
470471
# demo.nu
471472
print "foo"
@@ -497,6 +498,7 @@ Redirection to a file:
497498
| use `complete` | `let result = do { nu demo.nu } \| complete` | record containing both stdout and stderr
498499

499500
Note that `e>|` and `o+e>|` only work with external command, if you pipe internal commands' output through `e>|` and `o+e>|`, you will get an error:
501+
500502
```
501503
❯ ls e>| str length
502504
Error: × `e>|` only works with external streams
@@ -516,12 +518,14 @@ Error: × `o+e>|` only works with external streams
516518
```
517519

518520
You can also redirect `stdout` to a file, and pipe `stderr` to next command:
521+
519522
```
520523
nu demo.nu o> file.txt e>| str upcase
521524
nu demo.nu e> file.txt | str upcase
522525
```
523526

524527
But you can't use redirection along with `o+e>|`, because it's ambiguous:
528+
525529
```
526530
nu demo.nu o> file.txt o+e>| str upcase
527531
```

0 commit comments

Comments
 (0)