diff --git a/docs/document/Articles/docs/Enable Vi Mode in Your Shell.md b/docs/document/Articles/docs/Enable Vi Mode in Your Shell.md new file mode 100644 index 00000000..1f1800cf --- /dev/null +++ b/docs/document/Articles/docs/Enable Vi Mode in Your Shell.md @@ -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 +``` diff --git a/docs/document/Nix/docs/1.Language/Object and Collection.md b/docs/document/Nix/docs/1.Language/Object and Collection.md index 5c86058e..6a2bdf3c 100644 --- a/docs/document/Nix/docs/1.Language/Object and Collection.md +++ b/docs/document/Nix/docs/1.Language/Object and Collection.md @@ -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 = "example@example.com" } +{ username = "john smith"; email = "example@example.com"; } ``` Attribute name can be any string including interpolated string. diff --git a/docs/document/Nix/docs/1.Language/String.md b/docs/document/Nix/docs/1.Language/String.md index 172b87a0..75c94bee 100644 --- a/docs/document/Nix/docs/1.Language/String.md +++ b/docs/document/Nix/docs/1.Language/String.md @@ -63,7 +63,7 @@ stdenv.mkDerivation { } ``` -Some editors might support syntax injection for multi-line string. +Some editors support syntax injection for multi-line string. Add filetype as comment before the string to inform syntax parser. ```nix diff --git a/docs/document/Nix/docs/Nix CLI/Garbage Collection.md b/docs/document/Nix/docs/Nix CLI/Garbage Collection.md new file mode 100644 index 00000000..e69de29b diff --git a/docs/document/PowerShell/docs/File System/6. Inspect File System.md b/docs/document/PowerShell/docs/File System/6. Inspect File System.md new file mode 100644 index 00000000..e0c21b97 --- /dev/null +++ b/docs/document/PowerShell/docs/File System/6. Inspect File System.md @@ -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 +``` diff --git a/docs/document/PowerShell/docs/History.md b/docs/document/PowerShell/docs/History.md new file mode 100644 index 00000000..a52f8cef --- /dev/null +++ b/docs/document/PowerShell/docs/History.md @@ -0,0 +1,15 @@ +# History + +## Search History + +- Session history + +```ps1 +h | Select-String +``` + +- All history + +```ps1 +gc (Get-PSReadLineOption).HistorySavePath | Select-String +``` diff --git a/docs/document/PowerShell/docs/Language/String.md b/docs/document/PowerShell/docs/Language/String.md index a31582c5..64fefabe 100644 --- a/docs/document/PowerShell/docs/Language/String.md +++ b/docs/document/PowerShell/docs/Language/String.md @@ -102,6 +102,19 @@ Exception will be raised if conversion failed. [DateTime]::Now + '00:00:15:00' # adds 15 minutes ``` +## Comparison + +All comparison operators have its case-sensitive version with leading `-c` + +- `-gt` -> `-cgt` +- `-lt` -> `-clt` +- ... + +```ps1 +'Apple' -eq 'apple' # True +'Apple' -ceq 'apple' # False +``` + ## Split & Join ```ps1 diff --git a/docs/document/PowerShell/docs/Object Manipulation/Compare Objects.md b/docs/document/PowerShell/docs/Object Manipulation/Compare.md similarity index 100% rename from docs/document/PowerShell/docs/Object Manipulation/Compare Objects.md rename to docs/document/PowerShell/docs/Object Manipulation/Compare.md diff --git a/docs/document/PowerShell/docs/Object Manipulation/ForEach.md b/docs/document/PowerShell/docs/Object Manipulation/ForEach.md index ce9f7ac1..4ff6fb5b 100644 --- a/docs/document/PowerShell/docs/Object Manipulation/ForEach.md +++ b/docs/document/PowerShell/docs/Object Manipulation/ForEach.md @@ -1,24 +1,55 @@ # ForEach +`ForEach-Object`: + +- Collecting values from a member(property or method) by `-MemberName` +- Value transformation by `-Process` +- Mimicking a function that accepts pipeline input by `-Begin`, `-Process`, `-End` and `-RemainingScripts` +- Parallel Jobs + +Intrinsic `ForEach` has some overlap with `ForEach-Object`, it's not recommended but worth noting. + +- Type conversion +- Collecting values from a member +- Replace a property by new value + > [!TIP] > Use `foreach` or `%` alias for `ForEach-Object`. -## Works like Select +## Collecting Values -`ForEach-Object` can do the same thing as `Select-Object -ExpandProperty`. +- What differs `ForEach-Object` from `Select-Object -ExpandProperty` is `-MemberName` can accept a method name as well. + +> [!NOTE] +> See: `help % -Parameter MemberName` ```ps1 -gci | foreach Name -# or -gci | foreach { $_.Name } +# -MemberName is positional +gci | foreach -MemberName Name # equivalent to gci | select -ExpandProperty Name ``` +Evaluating from a method member is available, you can even pass parameters by `-ArgumentList`. + +```ps1 +(1..3 | % GetType | select -first 1) -is [System.Type] # True + +'1,2,3' | foreach -MemberName Split -ArgumentList ',' +# or +'1,2,3' | foreach Split ',' +``` + +## Value Transformation + The way `ForEach-Object` behaves is collecting implicitly returned values as an array. Every implicitly returned value will be collected as a item. +> [!NOTE] +> See: `help % -Parameter MemberName` + ```ps1 -gci | foreach { $_.Exists, $false } # True, False, True, False... +# -Process is positional at 0 +gci | foreach -Process { $_.Exists, $false } # True, False, True, False... ``` 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. $null -eq (gci | foreach { $_.Name | Out-Null }) # True ``` -> [!NOTE] -> One exception is `ForEach-Object` can execute method by name. -> You might expect delegate objects to be returned, but no, values of the method being executed will be returned. ->```ps1 ->(1,2,3 | % GetType | select -first 1) -is [System.Type] # True ->``` +## Intrinsic ForEach + +### Type Conversion + +One of overloads of `ForEach` takes a `System.Type`, and trying to cast all of them to the type. + +```ps1 +(65..90).ForEach([char]) # A-Z +``` +### Collecting Values + +```ps1 +('1,2,3').ForEach('Split', ',') # ArgumentList allowed +(gci -file).ForEach('Length') +``` + +### Override Property Value + +```ps1 +(gci -file).ForEach('CreationTime', (Get-Date)) +``` diff --git a/docs/document/PowerShell/docs/Object Manipulation/Measure.md b/docs/document/PowerShell/docs/Object Manipulation/Measure.md index 8ea7afaa..b79262b3 100644 --- a/docs/document/PowerShell/docs/Object Manipulation/Measure.md +++ b/docs/document/PowerShell/docs/Object Manipulation/Measure.md @@ -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 + +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 +``` diff --git a/docs/document/PowerShell/docs/Object Manipulation/Sort.md b/docs/document/PowerShell/docs/Object Manipulation/Sort.md index 89b2e888..dbd8267d 100644 --- a/docs/document/PowerShell/docs/Object Manipulation/Sort.md +++ b/docs/document/PowerShell/docs/Object Manipulation/Sort.md @@ -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` +- Case-Sensitive sort for strings by `-CaseSensitive` +- Take a count from start or end of sorted by `-Top` or `-Bottom` +> [!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 } +``` diff --git a/docs/document/PowerShell/docs/Object Manipulation/Where.md b/docs/document/PowerShell/docs/Object Manipulation/Where.md index e69de29b..65d627ca 100644 --- a/docs/document/PowerShell/docs/Object Manipulation/Where.md +++ b/docs/document/PowerShell/docs/Object Manipulation/Where.md @@ -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` diff --git a/docs/services/DocumentService.ts b/docs/services/DocumentService.ts index 8ab5f204..9c341a06 100644 --- a/docs/services/DocumentService.ts +++ b/docs/services/DocumentService.ts @@ -136,4 +136,19 @@ class DocumentService implements IDocumentService { } } +class Paginater { + private page: number = 1; + private range: { start: number; end: number }; + constructor( + private readonly items: T[], + private readonly count: number, + ) {} + nextPage(): T[] { + // learn to debug + this.range.start = (this.page - 1) * this.count; + this.range.end = this.range.start + this.count; + return this.items.slice(this.range.start, this.range.end); + } +} + export const documentService: IDocumentService = new DocumentService();