-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
305 additions
and
14 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
docs/document/Articles/docs/Enable Vi Mode in Your Shell.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Enable Vi Mode in Your Shell | ||
|
||
## Bash | ||
|
||
```sh | ||
# ~/.inputrc | ||
set editing-mode vi # enable for all input box in shell | ||
set keymap vi-insert | ||
set show-mode-in-prompt on # enable mode indicator | ||
set vi-cmd-mode-string "\1\e[2 q\2" # block bar in normal mode | ||
set vi-ins-mode-string "\1\e[6 q\2" # vertical bar in insert mode | ||
``` | ||
|
||
## PowerShell | ||
|
||
Make sure you have `PSReadLine` installed. | ||
|
||
```ps1 | ||
Set-PSReadLineOption -EditMode Vi | ||
$OnViModeChange = { | ||
if ($args[0] -eq 'Command') { | ||
# Set the cursor to a blinking block. | ||
Write-Host -NoNewLine "`e[2 q" | ||
} else { | ||
# Set the cursor to a blinking line. | ||
Write-Host -NoNewLine "`e[5 q" | ||
} | ||
} | ||
Set-PSReadLineOption -ViModeIndicator Script -ViModeChangeHandler $OnViModeChange | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ Set is similar to object literal in some languages like javascript and lua. | |
Properties of a set are called attributes in `nix` | ||
|
||
```nix | ||
{ username = "john smith"; email = "[email protected]" } | ||
{ username = "john smith"; email = "[email protected]"; } | ||
``` | ||
|
||
Attribute name can be any string including interpolated string. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
23 changes: 23 additions & 0 deletions
23
docs/document/PowerShell/docs/File System/6. Inspect File System.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Inspect File System | ||
|
||
## List Items | ||
|
||
### Recursively | ||
|
||
```ps1 | ||
gci -rec | ||
``` | ||
|
||
### Include Hidden Items | ||
|
||
```ps1 | ||
gci -force | ||
``` | ||
|
||
## Size | ||
|
||
### Directory Size | ||
|
||
```ps1 | ||
(gci -file -rec -force | measure Length -Sum).Sum / 1MB | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# History | ||
|
||
## Search History | ||
|
||
- Session history | ||
|
||
```ps1 | ||
h | Select-String <text> | ||
``` | ||
|
||
- All history | ||
|
||
```ps1 | ||
gc (Get-PSReadLineOption).HistorySavePath | Select-String <text> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
docs/document/PowerShell/docs/Object Manipulation/Measure.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,81 @@ | ||
# Measure | ||
|
||
`Measure-Object` is a common utility to calculate statistics of pipeline input with following usage: | ||
|
||
First kind of measurement is for property. | ||
|
||
- Average of property by `-Average` | ||
- Sum of property by `-Sum` | ||
- Max of property by `-Maximun` | ||
- Min of property by `-Minimum` | ||
- Standard Derivation of property by `-StandardDerivation` | ||
- Measure all available statistics by `-AllStats` | ||
|
||
Another kind of measurement is for string and string array. | ||
|
||
- Line count of input by `-Line` | ||
- Word count of input by `-Word` | ||
- Character count of input by `-Character` | ||
|
||
|
||
> [!NOTE] | ||
> Use `measure` alias for `Measure-Object`. | ||
## Calculate for Property | ||
|
||
The usage can flexible depend on your need. | ||
`-Property` accepts a `PSPropertyExpression` which can be implicitly created from a **property name**, **scriptblock** or **wildcard**. | ||
`Measure-Object` will return a `Microsoft.PowerShell.Commands.GenericMeasureInfo` when calculate for property. | ||
|
||
- Calculate for a same property by `-Property` and specify statistics options you need. | ||
|
||
```ps1 | ||
# Calculate sum and max for Length | ||
gci | measure -Property Length -Sum -Max | ||
# -Property is positional | ||
gci | measure Length -Sum -Max | ||
# Calculate Max in unit GB | ||
gci | measure { $_.Length / 1GB } -Max | ||
``` | ||
|
||
> [!tip] | ||
> You can always pass one or more valid `PSPropertyExpression` to `-Property`, it returns one or more `GenericMeasureInfo` | ||
>```ps1 | ||
>gci | measure Length, Name | ||
>``` | ||
Wildcard is a special case, all matched property will be calculated one by one. So it might return multiple `GenericMeasureInfo`. | ||
```ps1 | ||
# measures for properties like Name, FullName and so on. | ||
# Compared as string | ||
(gci | measure *ame -Max).Count # 5, multiple return! | ||
``` | ||
|
||
### Measure a HashTable <Badge type="info" text="PowerShell 6+" /> | ||
|
||
I think Extended property should be preferred if name conflict exists, haven't test it yet. | ||
|
||
```ps1 | ||
@{ Name = 'John'; Age = 18 }, @{ Name = 'Jane'; Age = 21 } | measure Age -Max | ||
``` | ||
|
||
> [!NOTE] | ||
> `-Maximun` and `-Minimum` only requires the property to be comparable while other flags requires to be numeric. | ||
> [!NOTE] | ||
> If no any flag were passed, `measure` only contains a available `Count` while other properties remain `$null`. | ||
## Calculate for String | ||
|
||
The usage is pretty much the same as property. | ||
The only thing worth noting is string array still treated as a whole instead of enumerating over it. | ||
And each element is treated as single line. This is useful for the source being piped from `Get-Content`. | ||
Measurement for text returns `Microsoft.PowerShell.Commands.TextMeasureInfo`. | ||
|
||
```ps1 | ||
('hello', 'world' | measure -Word -Line).Words # 2 | ||
('hello', 'world' | measure -Word -Line).Lines # 2 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,37 @@ | ||
# Sort | ||
|
||
- Sort by `-Property` | ||
- Sort by single property | ||
- Sort by property asc/desc then by another property asc/desc | ||
- Sort by calculated value | ||
- Descending by `-Descending` | ||
- Distinct by `-Unique` | ||
- Preserve input order when two order weights equivalently by `-Stable` <Badge type="info" text="PowerShell 6.2.0+" /> | ||
- Case-Sensitive sort for strings by `-CaseSensitive` | ||
- Take a count from start or end of sorted by `-Top` or `-Bottom` <Badge type="info" text="PowerShell 6+" /> | ||
|
||
> [!WARNING] | ||
> `sort` alias is only available on Windows. | ||
## Sort By | ||
|
||
- By single property | ||
|
||
```ps1 | ||
gci -file | Sort-Object -Property Length | ||
``` | ||
|
||
- Then by | ||
|
||
```ps1 | ||
# sort by Extension then by Length, both ascending | ||
gci -file | Sort-Object Extension, Length | ||
# sort by Extension descending then by Length descending | ||
gci -file | Sort-Object @{ e = 'Extension'; desc = $true }, @{ e = 'Length'; desc = $true } # [!code highlight] | ||
``` | ||
|
||
- Sort by calculated value | ||
|
||
```ps1 | ||
gci -file | Sort-Object { $_.Name.Length } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Where | ||
|
||
- Filter with custom scriptblock by `-FilterScript` | ||
- Filter by property by `-Property` | ||
- Tons of overloads for binary operators for mimicking direct expression. | ||
|
||
> [!NOTE] | ||
> Use `?` or `where` alias for `Where-Object` | ||
## Filter By | ||
|
||
- Property | ||
|
||
```ps1 | ||
# -Property is positional | ||
gci -file | where -Property Exists | ||
``` | ||
|
||
- Script block | ||
|
||
```ps1 | ||
# -FilterScript is positional | ||
gci -file | where -FilterScript { $_.CreationTime -gt [DateTime]::Now.AddDays(-5) } | ||
``` | ||
|
||
## Expression-Like | ||
|
||
`Where-Object` contains a lot of parameters to emulate expression-like syntax with `-Property`. | ||
|
||
```ps1 | ||
gci -file | where Extension -eq '.ps1' | ||
# equivalent to | ||
gci -file | where { $_.Extension -eq '.ps1' } | ||
``` | ||
|
||
> [!NOTE] | ||
> See: `help where` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters