Skip to content

Commit 228c4d8

Browse files
committed
main
1 parent 5b953ce commit 228c4d8

File tree

13 files changed

+305
-14
lines changed

13 files changed

+305
-14
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Enable Vi Mode in Your Shell
2+
3+
## Bash
4+
5+
```sh
6+
# ~/.inputrc
7+
set editing-mode vi # enable for all input box in shell
8+
set keymap vi-insert
9+
set show-mode-in-prompt on # enable mode indicator
10+
set vi-cmd-mode-string "\1\e[2 q\2" # block bar in normal mode
11+
set vi-ins-mode-string "\1\e[6 q\2" # vertical bar in insert mode
12+
```
13+
14+
## PowerShell
15+
16+
Make sure you have `PSReadLine` installed.
17+
18+
```ps1
19+
Set-PSReadLineOption -EditMode Vi
20+
$OnViModeChange = {
21+
if ($args[0] -eq 'Command') {
22+
# Set the cursor to a blinking block.
23+
Write-Host -NoNewLine "`e[2 q"
24+
} else {
25+
# Set the cursor to a blinking line.
26+
Write-Host -NoNewLine "`e[5 q"
27+
}
28+
}
29+
Set-PSReadLineOption -ViModeIndicator Script -ViModeChangeHandler $OnViModeChange
30+
```

docs/document/Nix/docs/1.Language/Object and Collection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Set is similar to object literal in some languages like javascript and lua.
1010
Properties of a set are called attributes in `nix`
1111

1212
```nix
13-
{ username = "john smith"; email = "[email protected]" }
13+
{ username = "john smith"; email = "[email protected]"; }
1414
```
1515

1616
Attribute name can be any string including interpolated string.

docs/document/Nix/docs/1.Language/String.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ stdenv.mkDerivation {
6363
}
6464
```
6565

66-
Some editors might support syntax injection for multi-line string.
66+
Some editors support syntax injection for multi-line string.
6767
Add filetype as comment before the string to inform syntax parser.
6868

6969
```nix

docs/document/Nix/docs/Nix CLI/Garbage Collection.md

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Inspect File System
2+
3+
## List Items
4+
5+
### Recursively
6+
7+
```ps1
8+
gci -rec
9+
```
10+
11+
### Include Hidden Items
12+
13+
```ps1
14+
gci -force
15+
```
16+
17+
## Size
18+
19+
### Directory Size
20+
21+
```ps1
22+
(gci -file -rec -force | measure Length -Sum).Sum / 1MB
23+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# History
2+
3+
## Search History
4+
5+
- Session history
6+
7+
```ps1
8+
h | Select-String <text>
9+
```
10+
11+
- All history
12+
13+
```ps1
14+
gc (Get-PSReadLineOption).HistorySavePath | Select-String <text>
15+
```

docs/document/PowerShell/docs/Language/String.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ Exception will be raised if conversion failed.
102102
[DateTime]::Now + '00:00:15:00' # adds 15 minutes
103103
```
104104

105+
## Comparison
106+
107+
All comparison operators have its case-sensitive version with leading `-c`
108+
109+
- `-gt` -> `-cgt`
110+
- `-lt` -> `-clt`
111+
- ...
112+
113+
```ps1
114+
'Apple' -eq 'apple' # True
115+
'Apple' -ceq 'apple' # False
116+
```
117+
105118
## Split & Join
106119

107120
```ps1

docs/document/PowerShell/docs/Object Manipulation/Compare Objects.md renamed to docs/document/PowerShell/docs/Object Manipulation/Compare.md

File renamed without changes.
Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,55 @@
11
# ForEach
22

3+
`ForEach-Object`:
4+
5+
- Collecting values from a member(property or method) by `-MemberName`
6+
- Value transformation by `-Process`
7+
- Mimicking a function that accepts pipeline input by `-Begin`, `-Process`, `-End` and `-RemainingScripts`
8+
- Parallel Jobs
9+
10+
Intrinsic `ForEach` has some overlap with `ForEach-Object`, it's not recommended but worth noting.
11+
12+
- Type conversion
13+
- Collecting values from a member
14+
- Replace a property by new value
15+
316
> [!TIP]
417
> Use `foreach` or `%` alias for `ForEach-Object`.
518
6-
## Works like Select
19+
## Collecting Values
720

8-
`ForEach-Object` can do the same thing as `Select-Object -ExpandProperty`.
21+
- What differs `ForEach-Object` from `Select-Object -ExpandProperty` is `-MemberName` can accept a method name as well.
22+
23+
> [!NOTE]
24+
> See: `help % -Parameter MemberName`
925
1026
```ps1
11-
gci | foreach Name
12-
# or
13-
gci | foreach { $_.Name }
27+
# -MemberName is positional
28+
gci | foreach -MemberName Name
1429
# equivalent to
1530
gci | select -ExpandProperty Name
1631
```
1732

33+
Evaluating from a method member is available, you can even pass parameters by `-ArgumentList`.
34+
35+
```ps1
36+
(1..3 | % GetType | select -first 1) -is [System.Type] # True
37+
38+
'1,2,3' | foreach -MemberName Split -ArgumentList ','
39+
# or
40+
'1,2,3' | foreach Split ','
41+
```
42+
43+
## Value Transformation
44+
1845
The way `ForEach-Object` behaves is collecting implicitly returned values as an array. Every implicitly returned value will be collected as a item.
1946

47+
> [!NOTE]
48+
> See: `help % -Parameter MemberName`
49+
2050
```ps1
21-
gci | foreach { $_.Exists, $false } # True, False, True, False...
51+
# -Process is positional at 0
52+
gci | foreach -Process { $_.Exists, $false } # True, False, True, False...
2253
```
2354

2455
If you do want a `$null` return, use `Out-Null` to swallow the value.
@@ -28,10 +59,25 @@ If you do want a `$null` return, use `Out-Null` to swallow the value.
2859
$null -eq (gci | foreach { $_.Name | Out-Null }) # True
2960
```
3061

31-
> [!NOTE]
32-
> One exception is `ForEach-Object` can execute method by name.
33-
> You might expect delegate objects to be returned, but no, values of the method being executed will be returned.
34-
>```ps1
35-
>(1,2,3 | % GetType | select -first 1) -is [System.Type] # True
36-
>```
62+
## Intrinsic ForEach
63+
64+
### Type Conversion
65+
66+
One of overloads of `ForEach` takes a `System.Type`, and trying to cast all of them to the type.
67+
68+
```ps1
69+
(65..90).ForEach([char]) # A-Z
70+
```
3771

72+
### Collecting Values
73+
74+
```ps1
75+
('1,2,3').ForEach('Split', ',') # ArgumentList allowed
76+
(gci -file).ForEach('Length')
77+
```
78+
79+
### Override Property Value
80+
81+
```ps1
82+
(gci -file).ForEach('CreationTime', (Get-Date))
83+
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,81 @@
11
# Measure
22

3+
`Measure-Object` is a common utility to calculate statistics of pipeline input with following usage:
34

5+
First kind of measurement is for property.
6+
7+
- Average of property by `-Average`
8+
- Sum of property by `-Sum`
9+
- Max of property by `-Maximun`
10+
- Min of property by `-Minimum`
11+
- Standard Derivation of property by `-StandardDerivation`
12+
- Measure all available statistics by `-AllStats`
13+
14+
Another kind of measurement is for string and string array.
15+
16+
- Line count of input by `-Line`
17+
- Word count of input by `-Word`
18+
- Character count of input by `-Character`
19+
20+
21+
> [!NOTE]
22+
> Use `measure` alias for `Measure-Object`.
23+
24+
## Calculate for Property
25+
26+
The usage can flexible depend on your need.
27+
`-Property` accepts a `PSPropertyExpression` which can be implicitly created from a **property name**, **scriptblock** or **wildcard**.
28+
`Measure-Object` will return a `Microsoft.PowerShell.Commands.GenericMeasureInfo` when calculate for property.
29+
30+
- Calculate for a same property by `-Property` and specify statistics options you need.
31+
32+
```ps1
33+
# Calculate sum and max for Length
34+
gci | measure -Property Length -Sum -Max
35+
# -Property is positional
36+
gci | measure Length -Sum -Max
37+
38+
# Calculate Max in unit GB
39+
gci | measure { $_.Length / 1GB } -Max
40+
```
41+
42+
> [!tip]
43+
> You can always pass one or more valid `PSPropertyExpression` to `-Property`, it returns one or more `GenericMeasureInfo`
44+
>```ps1
45+
>gci | measure Length, Name
46+
>```
47+
48+
Wildcard is a special case, all matched property will be calculated one by one. So it might return multiple `GenericMeasureInfo`.
49+
50+
```ps1
51+
# measures for properties like Name, FullName and so on.
52+
# Compared as string
53+
(gci | measure *ame -Max).Count # 5, multiple return!
54+
```
55+
56+
57+
### Measure a HashTable <Badge type="info" text="PowerShell 6+" />
58+
59+
I think Extended property should be preferred if name conflict exists, haven't test it yet.
60+
61+
```ps1
62+
@{ Name = 'John'; Age = 18 }, @{ Name = 'Jane'; Age = 21 } | measure Age -Max
63+
```
64+
65+
> [!NOTE]
66+
> `-Maximun` and `-Minimum` only requires the property to be comparable while other flags requires to be numeric.
67+
68+
> [!NOTE]
69+
> If no any flag were passed, `measure` only contains a available `Count` while other properties remain `$null`.
70+
71+
## Calculate for String
72+
73+
The usage is pretty much the same as property.
74+
The only thing worth noting is string array still treated as a whole instead of enumerating over it.
75+
And each element is treated as single line. This is useful for the source being piped from `Get-Content`.
76+
Measurement for text returns `Microsoft.PowerShell.Commands.TextMeasureInfo`.
77+
78+
```ps1
79+
('hello', 'world' | measure -Word -Line).Words # 2
80+
('hello', 'world' | measure -Word -Line).Lines # 2
81+
```

0 commit comments

Comments
 (0)