Last active
June 23, 2021 10:05
-
-
Save trapezoid/cc8e01b3a3fdd080a97c1041882452d1 to your computer and use it in GitHub Desktop.
PowerShellでgigignore.ioから.gitignoreを作る & いい感じに補完する
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
function gig() { | |
param( | |
[Parameter(Mandatory=$true)] | |
[string[]]$list | |
) | |
$params = ($list | ForEach-Object { [uri]::EscapeDataString($_) }) -join "," | |
Invoke-WebRequest -Uri "https://www.toptal.com/developers/gitignore/api/$params" | select -ExpandProperty content | Out-File -FilePath $(Join-Path -path $pwd -ChildPath ".gitignore") -Encoding ascii | |
} | |
Register-ArgumentCompleter -CommandName "gig" ` | |
-ParameterName "list" ` | |
-ScriptBlock { | |
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) | |
$url = "https://www.toptal.com/developers/gitignore/api/list" | |
$res = Invoke-WebRequest -Uri $url | |
$list = $res.Content.Split(",`n".ToCharArray()) | |
$decidedIndex = $wordToComplete.LastIndexOf(",") | |
if ($decidedIndex -gt 0) { | |
$decided = $wordToComplete.Substring(0, $decidedIndex) | |
} else { | |
$decided = "" | |
} | |
foreach($item in $list) | |
{ | |
if($item -notmatch $wordToComplete) | |
{ | |
continue; | |
} | |
$completionText = $decided + $item | |
[System.Management.Automation.CompletionResult]::new( | |
$completionText, $item, | |
[System.Management.Automation.CompletionResultType]::ParameterValue, | |
$item | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment