Skip to content

Commit

Permalink
feat(git, pwsh): configure git to use delta, introducing Test-Command…
Browse files Browse the repository at this point in the history
… and fix, refactor

A complete refactorial fixing was needed because I realised that "inline splatting"
is still not realised in powershell. So I rather use backtick then polluting my namespace
with splatting variable assignments.
  • Loading branch information
kalocsaibotond committed Mar 26, 2024
1 parent 5be7f75 commit 8da6b2b
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 72 deletions.
249 changes: 180 additions & 69 deletions home/Documents/PowerShell/Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,127 +4,238 @@ $ContextMenuDirRegeditEntries = @(
"Folder\Background\shell"
"Folder\shell"
"Drive\shell"
)
) # For Add-ContextMenuDir and Remove-ContextMenuDir


function Test-Command
{
<#
.SYNOPSIS
Command existence tester.
.DESCRIPTION
Test whether a command exist or not.
.PARAMETER Name
The name of the command to test.
.EXAMPLE
PS> Test-Command -Name "winget"
#>
[cmdletbinding()]
param (
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Name of the command to test existence."
)][string[]]$Name
)
PROCESS {
foreach ($n in $Name) {
[bool](Get-Command -Name $n -ErrorAction SilentlyContinue)
}
}
}


function Get-RegistryClasses
{
<#
.SYNOPSIS
Get rhe registry PS drive location for the directory context menu.
.DESCRIPTION
Get rhe registry PS drive location for the directory context menu
if it exists. If the PS drive does not exists then it also sets it
up.
.PARAMETER Global
Whether we want to add the context meny entry for all users.
.PARAMETER Scope
The scope of the registry PS drive's existence.
#>
param (
[Parameter(
Position = 0,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "The scope of the registry PS drive. It is passed
directly to the New-PSDrive cmdlet's Scope argument."
)][string]$Scope = "1",
[Parameter(
Position = 1,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Whether to add to context menu for all users.
It requres administrator rigths
because modifications occur in HKCR."
)][switch]$Global
)
if ($Global){
$ClassDrives = Get-PSDrive -PSProvider "registry" | Where-Object {
$_.Root -eq "HKEY_CLASSES_ROOT"
}
if ($ClassDrives){ # If already exists then use it
return $ClassDrives[0].Name + ":"
} else {
New-PSDrive `
-Name "Classes" `
-PSProvider "registry" `
-Root "HKEY_CLASSES_ROOT" `
-Scope $Scope | Out-Null
}
} else {
$ClassDrives = Get-PSDrive -PSProvider "registry" | Where-Object {
$_.Root -eq "HKEY_CURRENT_USER\SOFTWARE\Classes"
}
if ($ClassDrives){ # If already exists then use it
return $ClassDrives[0].Name + ":"
} else {
New-PSDrive `
-Name "Classes" `
-PSProvider "registry" `
-Root "HKEY_CURRENT_USER\SOFTWARE\Classes" `
-Scope $Scope | Out-Null
}
}
return "Classes:"
}


function Add-ContextMenuDir
{
<#
.SYNOPSIS
Add directory context menu item.
.DESCRIPTION
This command modifies the registry to add a context menu item.
The context menu item will be put to the context menu part that is for
directories, folders and alike.
For example when in the file exploler, one clics at a directory, the
the adde context menu will apper.
.PARAMETER DisplayName
The display name of the application that
we want to put into the context menu.
.PARAMETER ApplicationPath
The path of the executable application.
.PARAMETER ApplicationArgs
Command line arguments for the executable application.
These are jus put after the application path, after a whitespace.
The registry applies here its automatic string formatting.
For example the substring %V will be substituted with the directory
and alike.
.PARAMETER Global
Whether to add to context menu for all users. This requires
administrator rights.
.LINK
https://stackoverflow.com/questions/20449316/how-add-context-menu-item-to-windows-explorer-for-folders
.LINK
https://gist.github.com/flyxyz123/53ac952fe94a14482565f1d96e5704d5
#>
[cmdletbinding()]
param (
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Display name for the context menu option."
)][string]$DisplayName,
[Parameter(
Position = 1,
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Path to the application executable."
)][string]$ApplicationPath,
[Parameter(
Position = 2,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "CLI options to the application executable.
Note that any written %V will be substiuted
to the current working directory."
)][string]$ApplicationArgs="",
[Parameter(
Position = 4,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Whether to add to context menu for all users.
It requres administrator rigths
because modifications occur in HKCR."
)][switch]$Global
)
# https://stackoverflow.com/questions/20449316/how-add-context-menu-item-to-windows-explorer-for-folders
# https://gist.github.com/flyxyz123/53ac952fe94a14482565f1d96e5704d5

if ($Global){
if (Test-Path $registryContextMenuLocation){
Remove-PSDrive HKCR
New-PSDrive @{
PSProvider = "registry"
Root = "HKEY_CLASSES_ROOT"
Name = "HKCR"
}
} else {
New-PSDrive @{
PSProvider = "registry"
Root = "HKEY_CLASSES_ROOT"
Name = "HKCR"
}
}
$psDriveClasses = "HKCR:"
} else {
$psDriveClasses = "HKCU:\SOFTWARE\Classes"
BEGIN {
$RegistryClasses = Get-RegistryClasses -Scope "2" -Global $Global
}
PROCESS {
foreach($n in $DisplayName){
foreach($shell in $ContextMenuDirRegeditEntries){
$RegistryPath = Join-Path -Path (
Join-Path -Path $RegistryClasses -ChildPath $shell
) -ChildPath $n

foreach($shell in $ContextMenuDirRegeditEntries){
$registryPath = Join-Path (
Join-Path $psDriveClasses $shell
) $DisplayName
New-Item -Path $RegistryPath -Force
Set-ItemProperty ` # Setting display text
-Path $RegistryPath `
-Name "(Default)" `
-Value "Open $DisplayName here"
Set-ItemProperty ` # Setting icon
-Path $RegistryPath `
-Name "Icon" `
-Value "$ApplicationPath"

New-Item -Path $registryPath -Force
Set-ItemProperty @{
Path = $registryPath
Name = "(Default)"
Value = "Open $DisplayName here"
}
Set-ItemProperty @{
Path = $registryPath
Name = "Icon"
Value = "$ApplicationPath"
}

$commandPath = Join-Path $registryPath "command"
New-Item -Path $commandPath -Force
Set-ItemProperty @{
Path = $commandPath
Name = "(Default)"
Value = "$ApplicationPath $ApplicationArgs"
$commandPath = Join-Path `
-Path $RegistryPath `
-ChildPath "command"
New-Item -Path $commandPath -Force
Set-ItemProperty ` # Setting command to run
-Path $commandPath `
-Name "(Default)" `
-Value "$ApplicationPath $ApplicationArgs"
}
}
}
}


function Remove-ContextMenuDir
{
<#
.SYNOPSIS
Remove directory context menu item.
.DESCRIPTION
Reverse the effect of the Add-ContextMenuDir command.
The registry entry is identified with the DisplayName parameter.
.PARAMETER DisplayName
The display name of the application that
we want to put into the context menu.
.PARAMETER Global
Whether to remove from context menu for all users. This requires
administrator rights.
.EXAMPLE
Remove-ContextMenuDir -DisplayName "WezTerm"
#>
[cmdletbinding()]
param (
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Display name for the context menu option."
)][string]$DisplayName,
)][string[]]$DisplayName,
[Parameter(
Position = 3,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Whether to add to context menu for all users.
It requres administrator rigths because modifications
occur in HKCR."
)][switch]$Global

)

if ($Global){
if (Test-Path $registryContextMenuLocation){
Remove-PSDrive HKCR
New-PSDrive @{
PSProvider = "registry"
Root = "HKEY_CLASSES_ROOT"
Name = "HKCR"
}
} else {
New-PSDrive @{
PSProvider = "registry"
Root = "HKEY_CLASSES_ROOT"
Name = "HKCR"
BEGIN {
$RegistryClasses = Get-RegistryClasses -Scope "2" -Global $Global
}
PROCESS {
foreach($n in $DisplayName){
foreach($shell in $ContextMenuDirRegeditEntries){
$RegistryPath = Join-Path -Path (
Join-Path -Path $RegistryClasses -ChildPath $shell
) -ChildPath $n
Remove-Item -Path $RegistryPath -Force -Recurse
}
}
$psDriveClasses = "HKCR:"
} else {
$psDriveClasses = "HKCU:\SOFTWARE\Classes"
}

foreach($shell in $ContextMenuDirRegeditEntries){
$registryPath = Join-Path (
Join-Path $psDriveClasses $shell
) $DisplayName
Remove-Item -Path $registryPath -Force -Recurse
}
}
2 changes: 2 additions & 0 deletions home/dot_config/git/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[include]
path = ./delta_page
27 changes: 27 additions & 0 deletions home/dot_config/git/delta_pager
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[core]
pager = delta

[interactive]
diffFilter = delta --color-only --features=interactive

[merge]
conflictstyle = diff3

[diff]
colorMoved = default

[delta]
features = decorations
side-by-side = true

[delta "interactive"]
keep-plus-minus-markers = false

[delta "decorations"]
commit-decoration-style = blue ol
commit-style = raw
file-style = omit
hunk-header-decoration-style = blue box
hunk-header-file-style = red
hunk-header-line-number-style = "#067a00"
hunk-header-style = file line-number syntax
7 changes: 4 additions & 3 deletions home/dot_local/windows_setup/Install-WindowsSetup.ps1.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Install Scoop packages
# NOTE: Scoop is the primary package manager for my environment

irm get.scoop.sh | iex
if (-Not (Get-Command git -ErrorAction SilentlyContinue)){
Invoke-RestMethod get.scoop.sh | Invoke-Expression

if (-Not (Test-Command -Name git)){
scoop install main/git # Git is required to install other buckets
}

Expand All @@ -17,7 +18,7 @@ scoop install @(
{{end -}}
)

if (-Not (Get-Command winget -ErrorAction SilentlyContinue)){
if (-Not (Test-Command -Name winget)){
scoop install main/winget
}

Expand Down

0 comments on commit 8da6b2b

Please sign in to comment.