Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Dec 21, 2024
1 parent 5b953ce commit 228c4d8
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 14 deletions.
30 changes: 30 additions & 0 deletions docs/document/Articles/docs/Enable Vi Mode in Your Shell.md
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
```
2 changes: 1 addition & 1 deletion docs/document/Nix/docs/1.Language/Object and Collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/document/Nix/docs/1.Language/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
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
```
15 changes: 15 additions & 0 deletions docs/document/PowerShell/docs/History.md
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>
```
13 changes: 13 additions & 0 deletions docs/document/PowerShell/docs/Language/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 58 additions & 12 deletions docs/document/PowerShell/docs/Object Manipulation/ForEach.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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))
```
78 changes: 78 additions & 0 deletions docs/document/PowerShell/docs/Object Manipulation/Measure.md
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
```
34 changes: 34 additions & 0 deletions docs/document/PowerShell/docs/Object Manipulation/Sort.md
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 }
```
37 changes: 37 additions & 0 deletions docs/document/PowerShell/docs/Object Manipulation/Where.md
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`
15 changes: 15 additions & 0 deletions docs/services/DocumentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,19 @@ class DocumentService implements IDocumentService {
}
}

class Paginater<T> {
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();

0 comments on commit 228c4d8

Please sign in to comment.