Skip to content

Commit 6cf07b7

Browse files
committed
main
1 parent 116b668 commit 6cf07b7

File tree

7 files changed

+197
-9
lines changed

7 files changed

+197
-9
lines changed

docs/document/Powershell/docs/Alias.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ New-Alias <alias> <cmd_name>
1515
## Override an Alias
1616

1717
```ps1
18-
Set-Alias <alias> <cmd_name>
18+
Set-Alias vim 'nvim --clean -c "source ~/.vimrc"'
19+
```
20+
21+
Or use `Set-Item` to manipulate the alias provider directly:
22+
23+
```ps1
24+
si alias:vim 'nvim --clean -c "source ~/.vimrc"'
1925
```
2026

2127
> [!TIP]

docs/document/Powershell/docs/File System/5.Write to File.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ ls > foo.txt
1111
# equivalent to
1212
ls | Out-File foo.txt
1313
```
14+
15+
## Write Binary Data from External Command<Badge type="info" text="PowerShell 7.4+" />
16+
17+
PowerShell 7.4 added a experimental feature called `PSNativeCommandPreserveBytePipe` to work with external cli that pipes binary stream.
18+
19+
```ps1
20+
curl -s -L 'I am a url' > haha.tar.gz
21+
```

docs/document/Powershell/docs/Language/Array.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ So it can be another way to spread out arrays into one.
5353
5454
## Access an Item
5555

56-
Powershell allows
56+
Powershell allows indexer syntax to access one or more items at a time.
57+
58+
```ps1
59+
@(1,2,3)[0] # 1
60+
@(1,2,3)[0, 2] # 1, 3
61+
```
5762

5863
## Concatenation
5964

docs/document/Powershell/docs/Language/Control Flow.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,49 @@
1616
1717
## Newline Indicator
1818
19+
Use `` ` `` to indicate a new line if you do want to finish a long command invocation across multiple lines.
20+
21+
```ps1
22+
gci `
23+
-File `
24+
-Filter '*.mp4'
25+
```
26+
27+
> [!warning]
28+
> **A space is required before the backtick.**
29+
> **And no any chacracter is allowed after the backtick.**
30+
31+
> [!TIP]
32+
> Stop using backticks! They're ugly! Use hashtable splatting instead.
33+
>```ps1
34+
>$table = {
35+
> Filter = '*.mp4';
36+
> File = $true
37+
>}
38+
>gci -file @table
39+
>```
40+
41+
## Multi-Line Piping
42+
43+
### Trailing Piper
44+
45+
In multiple piping, we can use trailing `|` to indicate the new line, PowerShell regconizes these lines as a whole command piping.
46+
47+
```ps1
48+
gps |
49+
foreach CPU |
50+
sort -desc
51+
```
52+
53+
### Leading Piper <Badge type="info" text="PowerShell 7+" />
54+
55+
Starting with PowerShell 7, `|` is allowed as the first non-space chacracter in a new line.
56+
57+
```ps1
58+
gps | foreach CPU
59+
| sort -desc # [!code highlight]
60+
```
61+
1962
## Command Chaining
2063

2164
### Multi-Command in Single Line
@@ -30,3 +73,24 @@ cd ..; gci -rec -file; echo hello
3073
### Chaining And & Or <Badge type="info" text="PowerShell 7+" />
3174

3275
In PowerShell 7, `&&` and `||` were introduced to do the same command chaining as bash does.
76+
77+
78+
## Pattern Matching
79+
80+
Switch statement behaves similarly when the input is a singular object but can enumerate when the input is a collection.
81+
It has a few different patterns available:
82+
83+
- Constant: matching by primitive literal.
84+
- Type: matching by target type with `-is` operator.
85+
- Regex: matching by regex string, specifically for `string`.
86+
- Wildcard: matching by wildcard string, specifically for `string`.
87+
88+
### Constant Pattern
89+
90+
### Type Pattern
91+
92+
### Regex Pattern
93+
94+
### Wildcard Pattern
95+
96+

docs/document/Powershell/docs/Language/Function.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ function Foo {
5151
>
5252
> Default type of a parameter is `System.Object`.
5353
54+
### Implicit Parameter
55+
56+
If no parameter name was set, all value passed in will be captured by `$args`, an `object[]`.
57+
58+
The following example makes an alias for Neovim to mimic the original Vim cli.
59+
60+
```ps1
61+
function vim {
62+
nvim --clean -c 'source ~/.vimrc' @args
63+
}
64+
65+
vim ./foo.txt
66+
```
67+
5468
### Positional Parameter
5569

5670
Positional parameters allows passing values with explicit names.
@@ -69,6 +83,24 @@ Foo -Foo foo -Bar bar
6983
Foo foo bar # it's the same # [!code highlight]
7084
```
7185

86+
Or use a explicit position argument on attribute.
87+
88+
```ps1
89+
function Foo {
90+
param (
91+
[Parameter(Position=1)] # [!code highlight]
92+
[string] $Bar
93+
[Parameter(Position=0)] # [!code highlight]
94+
[string] $Foo,
95+
)
96+
97+
Write-Output "$Foo $Bar"
98+
}
99+
100+
Foo -Foo foo -Bar bar
101+
Foo foo bar # it's the same # [!code highlight]
102+
```
103+
72104
### Default Parameter
73105

74106
```ps1
@@ -97,6 +129,12 @@ function Foo {
97129
Foo -Foo -Bar $true # [!code highlight]
98130
```
99131

132+
Manual assignment is also available:
133+
134+
```ps1
135+
Foo -f:$false -b $true
136+
```
137+
100138
### Required Parameter
101139

102140
All parameters are optional by default. Use `[Parameter(Mandatory=$true)]` to mark it as required.
@@ -108,17 +146,26 @@ param (
108146
)
109147
```
110148

149+
> [!NOTE]
150+
> You can omit assignment for boolean attribute parameter.
151+
>```ps1
152+
>param (
153+
> [Parameter(Mandatory)] # Mandatory is true now # [!code highlight]
154+
> [string]$RequiredName
155+
>)
156+
>```
157+
111158
### Parameter Alias
112159
113160
Parameters can have aliases. It's not needed for most of time though since pwsh can distinguish option by leading string.
114161
115162
```ps1
116163
function Person {
117164
param (
118-
[Alias("n")] # [!code highlight]
165+
[Alias('n')] # [!code highlight]
119166
[string]$Name,
120167
121-
[Alias("a")] # [!code highlight]
168+
[Alias('a', 'yearsold')] # can have multiple aliases! # [!code highlight]
122169
[int]$Age
123170
)
124171
Write-Host "Name: $Name, Age: $Age"
@@ -127,6 +174,39 @@ function Person {
127174
Person -n "Alice" -a 30 # [!code highlight]
128175
```
129176
177+
### Parameter Validation
178+
179+
Pass a validation logic as script block to `ValidateScript` attribute, `$_` represents singular value of the parameter or current item of a collection.
180+
Will throw an error if any parameter does not satisfies the condition.
181+
182+
```ps1
183+
param (
184+
[ValidateScript({ ($_ % 2) -ne 0 })]
185+
[int[]]$Odd
186+
[ValidateScript({ $_.Length < 5 })]
187+
[string]$Name
188+
)
189+
```
190+
191+
## Named Blocks
192+
193+
In a simple function where there's only one series of parameters being taken, we don't have to use any complex logic.
194+
But things will explode when we're dealing with a pipeline input which might bring multiple objects.
195+
196+
The pipeline mechanism is essentially based on the `Enumerator` so if we collect all items into a new collection as parameter value, it can be a huge performance issue.
197+
So named blocks are essentially to defined a shared process logic for each object in the pipeline input, and other logic like initializationa and finalization.
198+
199+
> [!NOTE]
200+
> When no named block were specified, `end` block is used to represent the whole logic of a simple function.
201+
202+
```ps1
203+
function Foo {
204+
begin {}
205+
process {}
206+
end {}
207+
}
208+
```
209+
130210
## Lifetime
131211

132212
- Function should be define before it was called.

docs/document/Powershell/docs/Understanding Pipeline.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,48 @@ Overview of pipeline in powershell:
99
## Pipeline Parameter Binding
1010

1111

12-
## How Cmdlet Accept a Pipeline Input
12+
## How Cmdlet Accept Pipeline Input
1313

1414
There's two solution when a pipeline input comes in as a fallback:
1515

16-
- ByValue: accepts when the coming object can be cast to the target type of the parameter.
16+
- ByValue: the default strategy. Accepts when the coming object can be cast or converted to the target type of the parameter.
1717
- ByPropertyName: accepts when the coming object has property name matched to any parameter name of the cmdlet.
1818

19+
### By Value
20+
21+
22+
23+
### By PropertyName
1924

2025
```ps1
2126
spps -Name (gci -File | foreach Name)
22-
# is equivalent to
27+
# is equivalent to the following
2328
# because FileInfo has Name which matches to -Name parameter of spps cmdlet
2429
gci -File | spps
2530
```
2631

32+
2733
> [!WARNING]
2834
> If multiple matches exist on ByPropertyName solution, powershell throws an error since these paramters might not be allowed to be used together.
2935
30-
By value is always tried first, and then use ByPropertyName, or it finally throws.
36+
ByValue is always tried first, and then use ByPropertyName, or it finally throws.
3137
A parameter accepts pipeline input does not necessarily have both solutions, it can have at least one of them.
38+
39+
## How PowerShell Enumerate Pipeline Input
40+
41+
As we know, PowerShell can handle multiple objects from an enumerator from object that implements `IEnumerable` or `IEnumerable<T>`, or even duck typed with `GetEnumerator`.
42+
43+
While for types that are not linear collection, manually invoking `GetEnumerator` is required when being passed as pipeline input.
44+
45+
- `IDictionary<,>` and `IDictionary`
46+
- HashTable has dynamic typing so we can't presume a uniformed calculation for our cmdlet
47+
- `string` is `IEnumerable` but we surely don't expect the auto enumeration.
48+
49+
```ps1
50+
$table = @{ Name = 'foo'; Age = 18 }
51+
($table | measure).Count # 1
52+
($table.GetEnumerator() | measure).Count # 2 # [!code highlight]
53+
```
54+
55+
This is simply because these types are more likely to be treated as a whole object, even when dictionaries are `IEnumerable<KeyValuePair<,>>`.
56+

docs/services/ISidebarService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export interface ISidebarService {
88
getSidebarOfDocument(name: DocumentName): DefaultTheme.SidebarItem[];
99
transformFolderToSidebarItem(
1010
folder: File.DirectoryInfo,
11-
baseLink: string
11+
baseLink: string,
1212
): DefaultTheme.SidebarItem[];
1313
}

0 commit comments

Comments
 (0)