From 7d18f8a8b2cfdb348f2886b9a5a6699790ed5b58 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Sat, 2 Apr 2022 06:18:51 +0200 Subject: [PATCH 01/23] Work in progress --- .gitmodules | 3 ++ modules/Cli/ArgParser | 1 + modules/Cli/Cli.psm1 | 66 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 160000 modules/Cli/ArgParser create mode 100644 modules/Cli/Cli.psm1 diff --git a/.gitmodules b/.gitmodules index 337f0de..d551f1a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "modules/Git/posh-git"] path = modules/Git/posh-git url = https://github.com/dahlbyk/posh-git.git +[submodule "modules/Cli/ArgParser"] + path = modules/Cli/ArgParser + url = https://github.com/buriedstpatrick/ArgParser diff --git a/modules/Cli/ArgParser b/modules/Cli/ArgParser new file mode 160000 index 0000000..41e57a8 --- /dev/null +++ b/modules/Cli/ArgParser @@ -0,0 +1 @@ +Subproject commit 41e57a82ee3a4a4ec47593b6becb99b4e68842b4 diff --git a/modules/Cli/Cli.psm1 b/modules/Cli/Cli.psm1 new file mode 100644 index 0000000..e433461 --- /dev/null +++ b/modules/Cli/Cli.psm1 @@ -0,0 +1,66 @@ +function Invoke-LoadArgParser { + # Check that module was fetched via Git. If not, update submodules. + if (!(Test-Path (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser"))) { + Push-Location $env:PWSH_REPO + try { + git submodule init + git submodule update + } finally { + Pop-Location + } + } + + # Check that module was build. If not, run build + if (!(Test-Path (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser" "Output" "ArgParser" "ArgParser.psd1"))) { + Push-Location (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser") + try { + .\Install-Requirements.ps1 + .\Start-ModuleBuild.ps1 + } finally { + Pop-Location + } + } + + # Import the module + Import-Module (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser" "Output" "ArgParser") +} + +function Invoke-ProfileCli() { + # Load ArgParser module + Invoke-LoadArgParser + + # Parse arguments into an array, assume arguments space delimited + try { + $arguments = $args.Split(' ') + } catch { + # Unable to parse arguments which probably means nothing was entered beyond the root command 'profile' + # Print a user-friendly message, perhaps the help documentation + Write-Host "Usage: profile " + return + } + + # Hack: Force $arguments to be an array even if it's only a single entry. + # PowerShell treats single entry arrays as strings otherwise. + if (($arguments -is [string])) { + $arguments = @($arguments) + } + + # Print the version of the CLI if requested + if (Get-ArgParserHasSwitch -Name "version" -ShortName "v" -Arguments $arguments) { + Write-Host "Version: 1.0.0.0" + } + + # Print helpful documentation of the CLI if requested, break out of application flow + if (Get-ArgParserHasSwitch -Name "help" -ShortName "h" -Arguments $arguments) { + Write-Host "Usage: profile " + return + } + + $name = Get-ArgParserStringValue -Name "name" -ShortName "n" -Arguments $arguments + if ($name) { + Write-Host "Hello, $name! You have been reported to the authorities, have a great day :D" + } +} + +Set-Alias profile Invoke-ProfileCli +Export-ModuleMember -Function Invoke-ProfileCli -Alias profile From 542d40b45246ce063470e94dcafd2e6a28e74b13 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Sat, 2 Apr 2022 06:59:58 +0200 Subject: [PATCH 02/23] Add Write-Help with actions.json backing file --- modules/Cli/Cli.psm1 | 2 +- modules/Cli/Write-Help.psm1 | 50 +++++++++++++++++++++++ modules/Cli/actions.json | 81 +++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 modules/Cli/Write-Help.psm1 create mode 100644 modules/Cli/actions.json diff --git a/modules/Cli/Cli.psm1 b/modules/Cli/Cli.psm1 index e433461..f345c85 100644 --- a/modules/Cli/Cli.psm1 +++ b/modules/Cli/Cli.psm1 @@ -52,7 +52,7 @@ function Invoke-ProfileCli() { # Print helpful documentation of the CLI if requested, break out of application flow if (Get-ArgParserHasSwitch -Name "help" -ShortName "h" -Arguments $arguments) { - Write-Host "Usage: profile " + Write-Help @args return } diff --git a/modules/Cli/Write-Help.psm1 b/modules/Cli/Write-Help.psm1 new file mode 100644 index 0000000..3f01906 --- /dev/null +++ b/modules/Cli/Write-Help.psm1 @@ -0,0 +1,50 @@ +function Write-Help { + $action = (Get-Content (Join-Path $env:PWSH_REPO "modules" "Cli" "actions.json") | ConvertFrom-Json) + $args | Where-Object { !($_ -like '-*') } | ForEach-Object { + $action = $action."$_" + } + + $helpText = $action.helpText + if(!($null -eq $helpText)) { + Write-Output "Description:" + Write-Output "`t$helpText" + } + + $flags = $action.flags + if(!($null -eq $flags) -and ($flags.Length -gt 0)) { + Write-Output "Flags:" + $flags.PSObject.Properties | ForEach-Object { + $required = $_.Value.required -eq $true ? "[Required]" : "" + + $shortName = $_.Value.shortName ? ", -$($_.Value.shortName)" : $null + Write-Output "`t--$($_.Name)$shortName [$($_.Value.type)] $required" + + if ($_.Value.helpText) { + Write-Output "`t`t$($_.Value.helpText)" + } + + if ($_.Value.default) { + Write-Output "`t`tDefault: $($_.Value.default)" + } + } + } + + $examples = $action.examples + if(!($null -eq $examples) -and ($examples.Length -gt 0)) { + Write-Output "Examples:" + $examples | ForEach-Object { + Write-Output "`t$_" + } + } + + $commands = $action.PSObject.Properties | Where-Object { + !($_.Name -like 'helpText') -and !($_.Name -like 'examples') -and !($_.Name -like 'flags') + } + + if ($commands.Length -gt 0) { + Write-Output "Commands:" + $commands | ForEach-Object { + Write-Output "`t$($_.Name) - $($_.Value.helpText)" + } + } +} \ No newline at end of file diff --git a/modules/Cli/actions.json b/modules/Cli/actions.json new file mode 100644 index 0000000..2523653 --- /dev/null +++ b/modules/Cli/actions.json @@ -0,0 +1,81 @@ +{ + "helpText": "Manage PWSH profile. Use --help for more information about a command scope.", + "config": { + "helpText": "Manage configuration", + "get": { + "helpText": "Display current config" + }, + "find": { + "helpText": "Output the path to the config file" + }, + "edit": { + "helpText": "Edit config with default configured editor (${EDITOR})" + } + }, + "update" : { + "helpText": "Update the PWSH profile to the latest commit on your currently configured branch." + }, + "reload": { + "helpText": "Reload profile" + }, + "alias": { + "helpText": "Manage aliases for this CLI", + "add": { + "helpText": "Add an alias to the profile", + "flags": { + "name": { + "helpText": "Name of the alias", + "type": "string", + "required": true + } + } + }, + "remove": { + "helpText": "Remove a path from the profile", + "flags": { + "name": { + "helpText": "Name of the alias to remove", + "type": "string", + "required": true + } + } + }, + "list": { + "helpText": "List configured profile aliases" + } + }, + "path": { + "helpText": "Manage paths", + "add": { + "helpText": "Add a path to the profile", + "flags": { + "path": { + "helpText": "Path to add", + "type": "string", + "required": true + } + } + }, + "remove": { + "helpText": "Remove a path from the profile", + "flags": { + "path": { + "helpText": "Path to remove", + "type": "string", + "required": true + } + } + }, + "list": { + "helpText": "List profile paths", + "flags": { + "all": { + "helpText": "Print entire path list, not just locally configured in pwsh.yaml", + "shortName": "a", + "type": "switch", + "default": false + } + } + } + } +} From eb2139aec163c1894717c5e8c883842d0d1fdb97 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Sat, 2 Apr 2022 15:42:49 +0200 Subject: [PATCH 03/23] Format-EnvironmentVariables in Write-Help output --- modules/Cli/Cli.psm1 | 2 +- modules/Cli/Write-Help.psm1 | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/Cli/Cli.psm1 b/modules/Cli/Cli.psm1 index f345c85..d81f531 100644 --- a/modules/Cli/Cli.psm1 +++ b/modules/Cli/Cli.psm1 @@ -25,7 +25,7 @@ function Invoke-LoadArgParser { Import-Module (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser" "Output" "ArgParser") } -function Invoke-ProfileCli() { +function Invoke-ProfileCli { # Load ArgParser module Invoke-LoadArgParser diff --git a/modules/Cli/Write-Help.psm1 b/modules/Cli/Write-Help.psm1 index 3f01906..1d51e0d 100644 --- a/modules/Cli/Write-Help.psm1 +++ b/modules/Cli/Write-Help.psm1 @@ -7,24 +7,24 @@ function Write-Help { $helpText = $action.helpText if(!($null -eq $helpText)) { Write-Output "Description:" - Write-Output "`t$helpText" + Write-Output ("`t$helpText" | Format-EnvironmentVariables) } $flags = $action.flags if(!($null -eq $flags) -and ($flags.Length -gt 0)) { Write-Output "Flags:" $flags.PSObject.Properties | ForEach-Object { - $required = $_.Value.required -eq $true ? "[Required]" : "" + $required = $_.Value.required -eq $true ? " [REQUIRED]" : "" $shortName = $_.Value.shortName ? ", -$($_.Value.shortName)" : $null - Write-Output "`t--$($_.Name)$shortName [$($_.Value.type)] $required" + Write-Output "`t--$($_.Name)$shortName [$($_.Value.type)]$required" if ($_.Value.helpText) { - Write-Output "`t`t$($_.Value.helpText)" + Write-Output ("`t`t$($_.Value.helpText)" | Format-EnvironmentVariables) } if ($_.Value.default) { - Write-Output "`t`tDefault: $($_.Value.default)" + Write-Output ("`t`tDefault: $($_.Value.default)" | Format-EnvironmentVariables) } } } @@ -33,7 +33,7 @@ function Write-Help { if(!($null -eq $examples) -and ($examples.Length -gt 0)) { Write-Output "Examples:" $examples | ForEach-Object { - Write-Output "`t$_" + Write-Output ("`t$_" | Format-EnvironmentVariables) } } @@ -44,7 +44,7 @@ function Write-Help { if ($commands.Length -gt 0) { Write-Output "Commands:" $commands | ForEach-Object { - Write-Output "`t$($_.Name) - $($_.Value.helpText)" + Write-Output ("`t$($_.Name) - $($_.Value.helpText)" | Format-EnvironmentVariables) } } } \ No newline at end of file From 64e283c8acafbff16c271166e61306607e33639b Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Mon, 4 Apr 2022 16:27:24 +0200 Subject: [PATCH 04/23] Safer path check for arg parser module --- modules/Cli/Cli.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/Cli/Cli.psm1 b/modules/Cli/Cli.psm1 index d81f531..f9c8666 100644 --- a/modules/Cli/Cli.psm1 +++ b/modules/Cli/Cli.psm1 @@ -1,6 +1,6 @@ function Invoke-LoadArgParser { # Check that module was fetched via Git. If not, update submodules. - if (!(Test-Path (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser"))) { + if (!(Test-Path (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser" "Output" "ArgParser" "ArgParser.psd1"))) { Push-Location $env:PWSH_REPO try { git submodule init From 1a6710e061d0b75aa5713ce527d1794cca92fd37 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Mon, 4 Apr 2022 17:19:36 +0200 Subject: [PATCH 05/23] wip --- modules/Core/Modules.psm1 | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 modules/Core/Modules.psm1 diff --git a/modules/Core/Modules.psm1 b/modules/Core/Modules.psm1 new file mode 100644 index 0000000..1de64b6 --- /dev/null +++ b/modules/Core/Modules.psm1 @@ -0,0 +1,48 @@ +Import-Module (Join-Path $env:PWSH_REPO "modules" "Core" "Config.psm1") + +<# +.SYNOPSIS +Get PWSH profile modules with status + +.DESCRIPTION +Get PWSH profile modules with status + +.EXAMPLE +Get-ProfileModules -Include Enabled,Disabled + +.NOTES + +#> +function Get-ProfileModules { + [cmdletbinding()] + param ( + [Parameter(Mandatory=$false)] + [string[]]$Include + ) + process { + $config = Get-Config + + return $config.modules.Keys | ForEach-Object { + $module = $null + + if ($Include?.Length -lt 1) { + $module = $_ + } + + if ($config.modules["$($_)"]?.disabled -eq $true -and $Include?.Contains("Disabled")) { + $module = $_ + } + + if ($config.modules["$($_)"]?.disabled -eq $false -and $Include?.Contains("Enabled")) { + $module = $_ + } + + if ($null -ne $module) { + return @{ + Name = $_ + Disabled = $config.modules["$($_)"]?.disabled + } + } + } | Where-Object { !( $_ -eq $null )} + } +} From 8e10fa7530fd290a53f5e56951584cfc169a71f3 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Fri, 8 Apr 2022 19:34:06 +0200 Subject: [PATCH 06/23] Add AutoHotkey module with Win+Enter to open Windows Terminal --- modules/AutoHotkey/AutoHotkey.psm1 | 21 +++++++++++++++++++++ modules/AutoHotkey/Keybinds.ahk | 8 ++++++++ pwsh.yaml | 7 +++++++ 3 files changed, 36 insertions(+) create mode 100644 modules/AutoHotkey/AutoHotkey.psm1 create mode 100644 modules/AutoHotkey/Keybinds.ahk diff --git a/modules/AutoHotkey/AutoHotkey.psm1 b/modules/AutoHotkey/AutoHotkey.psm1 new file mode 100644 index 0000000..61f7ac9 --- /dev/null +++ b/modules/AutoHotkey/AutoHotkey.psm1 @@ -0,0 +1,21 @@ +function Invoke-Keybindings($config) { + Invoke-Item (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "Keybinds.ahk") + + $startupDir = (Join-Path $env:APPDATA "Microsoft" "Windows" "Start Menu" "Programs" "Startup") + + if ($config?.loadKeybindsOnStartup -and !(Test-Path (Join-Path $startupDir "Keybinds.ahk"))) { + if (!(Test-IsAdministrator)) { + Write-WarnMessage "Unable to add AutoHotkey keybinds to Startup. Please run your profile as administrator to initially set this up." + return + } + + # Create symbolic link in startup directory + Write-InfoMessage "Adding symbolic link for Keybinds.ahk to startup directory" + + New-Item -ItemType SymbolicLink ` + -Path (Join-Path $startupDir "Keybinds.ahk") ` + -Value (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "Keybinds.ahk") + } +} + +Invoke-Keybindings (Get-Config).modules.AutoHotkey?.config diff --git a/modules/AutoHotkey/Keybinds.ahk b/modules/AutoHotkey/Keybinds.ahk new file mode 100644 index 0000000..bc56485 --- /dev/null +++ b/modules/AutoHotkey/Keybinds.ahk @@ -0,0 +1,8 @@ +#SingleInstance,Force + +; Open Windows Terminal on Windows+Enter +LWin & Enter:: + Run, wt.exe + return + +^x::ExitApp \ No newline at end of file diff --git a/pwsh.yaml b/pwsh.yaml index 7c9cc7a..eb82cec 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -103,6 +103,13 @@ modules: Yarn: disabled: false config: + AutoHotkey: + disabled: true + config: + # Whether AutoHotkey keybinds should be loaded when signing in + # Requires admin-session to initialize + # Default: true + loadKeybindsOnStartup: true # Paths to add to the session paths: From 7abfca2f2508f11d2cadc58bd93f613979110420 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Fri, 8 Apr 2022 19:38:16 +0200 Subject: [PATCH 07/23] Disable all modules by default --- pwsh.yaml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pwsh.yaml b/pwsh.yaml index eb82cec..e5a6a38 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -14,10 +14,10 @@ # "config" => The module will be loaded with the given configuration modules: FuzzyFinder: - disabled: false + disabled: true config: Ssh: - disabled: false + disabled: true config: # List of paths to SSH keys to load on shell init # Default: empty @@ -27,19 +27,19 @@ modules: # - ~/.ssh/id_github keys: Dotnet: - disabled: false + disabled: true config: Git: - disabled: false + disabled: true config: Lf: - disabled: false + disabled: true config: Misc: - disabled: false + disabled: true config: Prompt: - disabled: false + disabled: true config: # The shell prompt to invoke # Options: @@ -75,7 +75,7 @@ modules: # Default: false folderIcons: true Vim: - disabled: false + disabled: true config: # Sets vim (or neovim) as the default $env:EDITOR # This will be moved to a generalized option in the future @@ -84,24 +84,24 @@ modules: # Aliases vim so that it points to nvim instead useNeovim: true PasswordState: - disabled: false + disabled: true config: baseUrl: domain: Postgres: - disabled: false + disabled: true config: Kubectl: - disabled: false + disabled: true config: Helm: - disabled: false + disabled: true config: Npm: - disabled: false + disabled: true config: Yarn: - disabled: false + disabled: true config: AutoHotkey: disabled: true From 6432cd05c54ffbc1d2c52578d8118d0eff80b776 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Fri, 8 Apr 2022 19:55:25 +0200 Subject: [PATCH 08/23] Fix disabled module exclusions --- pwshrc.ps1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pwshrc.ps1 b/pwshrc.ps1 index e601d63..f3f768c 100644 --- a/pwshrc.ps1 +++ b/pwshrc.ps1 @@ -28,9 +28,8 @@ function Invoke-Core { function Invoke-Modules($config) { # Get disabled module names - $disabledModules = $config.modules.PSObject.Properties ` - | Where-Object { $_.Value.disabled } ` - | Select-Object -ExpandProperty Name + $modules = $config.modules + $disabledModules = $modules.Keys | Where-Object { $modules[$_].disabled } # Load enabled feature modules Get-ChildItem (Join-Path $env:PWSH_REPO modules) -Directory ` @@ -65,4 +64,6 @@ Invoke-Modules $config Invoke-Path $config # Start prompt -Invoke-Prompt $config.modules?.Prompt?.config +if (!($config.modules?.Prompt?.disabled)) { + Invoke-Prompt $config.modules?.Prompt?.config +} From dfa4bcb29592e562557f05881b50d90cbf20639f Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Sun, 10 Apr 2022 14:02:12 +0200 Subject: [PATCH 09/23] AutoHotkey: Better install/uninstall scripts. WiP CLI scope parsing. --- modules/AutoHotkey/.gitignore | 1 + modules/AutoHotkey/AutoHotkey.psm1 | 54 ++++++++++++----- .../{Keybinds.ahk => Keybinds.ahk.TEMPLATE} | 6 +- modules/AutoHotkey/readme.md | 9 +++ modules/Cli/Cli.psm1 | 29 ++++++++-- modules/Core/Modules.psm1 | 6 +- modules/Core/Testers.psm1 | 4 +- modules/Ssh/Ssh.psm1 | 2 +- pwsh.yaml | 58 ++++++++++++------- pwshrc.ps1 | 4 +- readme.md | 2 +- 11 files changed, 127 insertions(+), 48 deletions(-) create mode 100644 modules/AutoHotkey/.gitignore rename modules/AutoHotkey/{Keybinds.ahk => Keybinds.ahk.TEMPLATE} (60%) create mode 100644 modules/AutoHotkey/readme.md diff --git a/modules/AutoHotkey/.gitignore b/modules/AutoHotkey/.gitignore new file mode 100644 index 0000000..26e8f28 --- /dev/null +++ b/modules/AutoHotkey/.gitignore @@ -0,0 +1 @@ +*.ahk \ No newline at end of file diff --git a/modules/AutoHotkey/AutoHotkey.psm1 b/modules/AutoHotkey/AutoHotkey.psm1 index 61f7ac9..9a6de13 100644 --- a/modules/AutoHotkey/AutoHotkey.psm1 +++ b/modules/AutoHotkey/AutoHotkey.psm1 @@ -1,21 +1,49 @@ -function Invoke-Keybindings($config) { - Invoke-Item (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "Keybinds.ahk") +function Invoke-AutoHotkeyKeybindings($config) { + Get-ChildItem (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "*.ahk.TEMPLATE") | ForEach-Object { + $destination = $_.FullName.Replace(".TEMPLATE", "") + if (!(Test-Path $destination)) { + Get-Content $_.FullName | Format-EnvironmentVariables | Out-File $destination + } + } + + if ($config?.loadKeybindsOnStartup -and !(Test-Path (Join-Path $startupDir "Keybinds.ahk"))) { + Install-AutoHotkeyKeybindings + } +} +function Install-AutoHotkeyKeybindings { $startupDir = (Join-Path $env:APPDATA "Microsoft" "Windows" "Start Menu" "Programs" "Startup") - if ($config?.loadKeybindsOnStartup -and !(Test-Path (Join-Path $startupDir "Keybinds.ahk"))) { - if (!(Test-IsAdministrator)) { - Write-WarnMessage "Unable to add AutoHotkey keybinds to Startup. Please run your profile as administrator to initially set this up." - return - } + if (!(Test-IsAdministrator)) { + Write-ErrorMessage "Unable to install AutoHotkey keybinds. Please run your profile as administrator to initially set this up." + return + } + + # Create symbolic link in startup directory + Write-InfoMessage "Adding symbolic link for Keybinds.ahk to startup directory" + + New-Item -ItemType SymbolicLink ` + -Path (Join-Path $startupDir "Keybinds.ahk") ` + -Value (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "Keybinds.ahk") +} - # Create symbolic link in startup directory - Write-InfoMessage "Adding symbolic link for Keybinds.ahk to startup directory" +function Uninstall-AutoHotkeyKeybindsings { + if (!(Test-IsAdministrator)) { + Write-ErrorMessage "Unable to uninstall AutoHotkey keybinds. Please run this command in an administrator context." + return + } - New-Item -ItemType SymbolicLink ` - -Path (Join-Path $startupDir "Keybinds.ahk") ` - -Value (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "Keybinds.ahk") + $destination = (Join-Path $startupDir $env:APPDATA "Microsoft" "Windows" "Start Menu" "Programs" "Startup" "Keybinds.ahk") + if (!(Test-Path $destination)) { + Write-ErrorMessage "Unable to uninstall AutoHotkey keybinds. The symbolic link does not exist." + return } + + Remove-Item $destination } -Invoke-Keybindings (Get-Config).modules.AutoHotkey?.config +$config = (Get-Config).modules.AutoHotkey + +if ($config.enabled) { + Invoke-AutoHotkeyKeybindings $config?.config +} diff --git a/modules/AutoHotkey/Keybinds.ahk b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE similarity index 60% rename from modules/AutoHotkey/Keybinds.ahk rename to modules/AutoHotkey/Keybinds.ahk.TEMPLATE index bc56485..afaa7c2 100644 --- a/modules/AutoHotkey/Keybinds.ahk +++ b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE @@ -5,4 +5,8 @@ LWin & Enter:: Run, wt.exe return -^x::ExitApp \ No newline at end of file +; Stop keybinds +LWin & Q::ExitApp + +; Reload keybinds +LWin & R::Reload diff --git a/modules/AutoHotkey/readme.md b/modules/AutoHotkey/readme.md new file mode 100644 index 0000000..50c0c1c --- /dev/null +++ b/modules/AutoHotkey/readme.md @@ -0,0 +1,9 @@ +# AutoHotkey keybinds + +Adds a couple of global keybinds that work outside of the terminal context. AutoHotkey is required. + +| Keybind | Action | +|---------------|------------------------------------------------------------| +| `Super+Enter` | Open terminal instance (Configure terminal in `pwsh.yaml`) | +| `Super+Q` | Stop all keybinds | +| `Super+R` | Re-load all keybinds | diff --git a/modules/Cli/Cli.psm1 b/modules/Cli/Cli.psm1 index f9c8666..d3ecde4 100644 --- a/modules/Cli/Cli.psm1 +++ b/modules/Cli/Cli.psm1 @@ -1,3 +1,13 @@ +function New-ProfileAlias { + Write-Host "hello!" +} + +$commands = @{ + "alias" = @{ + "add" = "New-ProfileAlias" + } +} + function Invoke-LoadArgParser { # Check that module was fetched via Git. If not, update submodules. if (!(Test-Path (Join-Path $env:PWSH_REPO "modules" "Cli" "ArgParser" "Output" "ArgParser" "ArgParser.psd1"))) { @@ -56,10 +66,21 @@ function Invoke-ProfileCli { return } - $name = Get-ArgParserStringValue -Name "name" -ShortName "n" -Arguments $arguments - if ($name) { - Write-Host "Hello, $name! You have been reported to the authorities, have a great day :D" - } + $scope = Get-ArgParserScope -Arguments $arguments + + # $name = Get-ArgParserStringValue -Name "name" -ShortName "n" -Arguments $arguments + # if ($name) { + # Write-Host "Hello, $name! You have been reported to the authorities, have a great day :D" + # } + + $index = 0 + $command = $commands + $scope | ForEach-Object { + $command = $command[$_] + $index++ + } | Select-Object -Last 1 + + $command | Invoke-Expression } Set-Alias profile Invoke-ProfileCli diff --git a/modules/Core/Modules.psm1 b/modules/Core/Modules.psm1 index 1de64b6..11f8733 100644 --- a/modules/Core/Modules.psm1 +++ b/modules/Core/Modules.psm1 @@ -29,18 +29,18 @@ function Get-ProfileModules { $module = $_ } - if ($config.modules["$($_)"]?.disabled -eq $true -and $Include?.Contains("Disabled")) { + if ($config.modules["$($_)"]?.enabled -eq $false -and $Include?.Contains("Disabled")) { $module = $_ } - if ($config.modules["$($_)"]?.disabled -eq $false -and $Include?.Contains("Enabled")) { + if ($config.modules["$($_)"]?.enabled -eq $true -and $Include?.Contains("Enabled")) { $module = $_ } if ($null -ne $module) { return @{ Name = $_ - Disabled = $config.modules["$($_)"]?.disabled + Enabled = $config.modules["$($_)"]?.enabled } } } | Where-Object { !( $_ -eq $null )} diff --git a/modules/Core/Testers.psm1 b/modules/Core/Testers.psm1 index a5451e4..d2eaa73 100644 --- a/modules/Core/Testers.psm1 +++ b/modules/Core/Testers.psm1 @@ -17,5 +17,5 @@ function Test-Command($command) { } function Test-Module($module) { - return !((Get-Config).modules?."$module"?.disabled ?? $true) -} \ No newline at end of file + return (Get-Config).modules?."$module"?.enabled ?? $false +} diff --git a/modules/Ssh/Ssh.psm1 b/modules/Ssh/Ssh.psm1 index a8050e4..33867d4 100644 --- a/modules/Ssh/Ssh.psm1 +++ b/modules/Ssh/Ssh.psm1 @@ -1,5 +1,5 @@ $sshConfig = (Get-Config).modules.Ssh -if ($sshConfig.disabled) { +if (!($sshConfig?.enabled ?? $false)) { return } diff --git a/pwsh.yaml b/pwsh.yaml index e5a6a38..f453f0e 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -14,10 +14,10 @@ # "config" => The module will be loaded with the given configuration modules: FuzzyFinder: - disabled: true + enabled: false config: Ssh: - disabled: true + enabled: false config: # List of paths to SSH keys to load on shell init # Default: empty @@ -27,19 +27,19 @@ modules: # - ~/.ssh/id_github keys: Dotnet: - disabled: true + enabled: false config: Git: - disabled: true + enabled: false config: Lf: - disabled: true + enabled: false config: Misc: - disabled: true + enabled: false config: Prompt: - disabled: true + enabled: false config: # The shell prompt to invoke # Options: @@ -71,46 +71,62 @@ modules: # Whether to import folder icons using the Terminal-Icons module # Options: - # true | false + # false | false # Default: false - folderIcons: true + folderIcons: false Vim: - disabled: true + enabled: false config: # Sets vim (or neovim) as the default $env:EDITOR # This will be moved to a generalized option in the future - setDefaultEditor: true + setDefaultEditor: false # Aliases vim so that it points to nvim instead - useNeovim: true + useNeovim: false PasswordState: - disabled: true + enabled: false config: baseUrl: domain: Postgres: - disabled: true + enabled: false config: Kubectl: - disabled: true + enabled: false config: Helm: - disabled: true + enabled: false config: Npm: - disabled: true + enabled: false config: Yarn: - disabled: true + enabled: false config: AutoHotkey: - disabled: true + enabled: false config: # Whether AutoHotkey keybinds should be loaded when signing in # Requires admin-session to initialize - # Default: true - loadKeybindsOnStartup: true + # Default: false + loadKeybindsOnStartup: false # Paths to add to the session paths: - ${HOME}/tools + +# Terminal emulator settings +terminal: + # Active terminal emulator + # Default: wt.exe + path: wt.exe + + # Any arguments to pass to the terminal emulator + # Example: + # args: | + # --fullscreen + # split-pane + args: | + --fullscreen + split-pane + diff --git a/pwshrc.ps1 b/pwshrc.ps1 index f3f768c..e03c945 100644 --- a/pwshrc.ps1 +++ b/pwshrc.ps1 @@ -29,7 +29,7 @@ function Invoke-Core { function Invoke-Modules($config) { # Get disabled module names $modules = $config.modules - $disabledModules = $modules.Keys | Where-Object { $modules[$_].disabled } + $disabledModules = $modules.Keys | Where-Object { !$modules[$_].enabled } # Load enabled feature modules Get-ChildItem (Join-Path $env:PWSH_REPO modules) -Directory ` @@ -64,6 +64,6 @@ Invoke-Modules $config Invoke-Path $config # Start prompt -if (!($config.modules?.Prompt?.disabled)) { +if ($config.modules?.Prompt?.enabled ?? $false) { Invoke-Prompt $config.modules?.Prompt?.config } diff --git a/readme.md b/readme.md index ea592bd..2759f51 100644 --- a/readme.md +++ b/readme.md @@ -49,7 +49,7 @@ You can very quickly open the config-file using `Ctrl+.` in your PowerShell sess ## Modules -The different modules have certain dependencies, and some of them might not be relevant to you. You can disable them by setting the `modules..disabled` property to true in the config. The only module that can't be disabled (unless you change the script) is the Core module, since the other modules depend on some common functionality like writing readable info messages. +The different modules have certain dependencies, and some of them might not be relevant to you. They are all disabled by default (Except `Core`). You can enabled them by setting the `modules..enabled` property to true in the config. ## Keybindings From 90d00cdc8faf1af2cd29a665c3327a06e334a5f1 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Sun, 10 Apr 2022 14:39:19 +0200 Subject: [PATCH 10/23] Format-ConfigValues support --- modules/AutoHotkey/AutoHotkey.psm1 | 2 +- modules/AutoHotkey/Keybinds.ahk.TEMPLATE | 2 +- modules/Core/Environment.psm1 | 33 ++++++++++++++++++++++++ pwsh.yaml | 9 ++----- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/modules/AutoHotkey/AutoHotkey.psm1 b/modules/AutoHotkey/AutoHotkey.psm1 index 9a6de13..a991131 100644 --- a/modules/AutoHotkey/AutoHotkey.psm1 +++ b/modules/AutoHotkey/AutoHotkey.psm1 @@ -2,7 +2,7 @@ function Invoke-AutoHotkeyKeybindings($config) { Get-ChildItem (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "*.ahk.TEMPLATE") | ForEach-Object { $destination = $_.FullName.Replace(".TEMPLATE", "") if (!(Test-Path $destination)) { - Get-Content $_.FullName | Format-EnvironmentVariables | Out-File $destination + Get-Content $_.FullName | Format-EnvironmentVariables | Format-ConfigValues -Config (Get-Config) | Out-File $destination } } diff --git a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE index afaa7c2..a2c4fbd 100644 --- a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE +++ b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE @@ -2,7 +2,7 @@ ; Open Windows Terminal on Windows+Enter LWin & Enter:: - Run, wt.exe + Run, #{terminal.path} #{terminal.args} return ; Stop keybinds diff --git a/modules/Core/Environment.psm1 b/modules/Core/Environment.psm1 index bf092b0..1352095 100644 --- a/modules/Core/Environment.psm1 +++ b/modules/Core/Environment.psm1 @@ -23,6 +23,39 @@ function Format-EnvironmentVariables { } } +# Replaces config variables with their given values +function Format-ConfigValues { + [cmdletbinding()] + param( + [parameter( + Mandatory = $false, + ValueFromPipeline = $true, + ValueFromPipelineByPropertyName = $true + )] + [string]$Content, + [parameter( + Mandatory = $true + )] + $Config + ) + process { + $Content | Select-String -Pattern '#\{(.*?)\}' -AllMatches | ForEach-Object { + $_.matches | ForEach-Object { + $propertyNames = $_.groups[1].Value.Split(".") + + $value = $Config + $propertyNames | ForEach-Object { + $value = $value[$_] + } + + $Content = $Content.Replace($_.groups[0], $value) + } + } + + return $Content + } +} + # Ensures paths are correctly formatted function Format-Paths { [cmdletbinding()] diff --git a/pwsh.yaml b/pwsh.yaml index f453f0e..6c6e3de 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -123,10 +123,5 @@ terminal: # Any arguments to pass to the terminal emulator # Example: - # args: | - # --fullscreen - # split-pane - args: | - --fullscreen - split-pane - + # args: --fullscreen split-pane + args: --fullscreen split-pane From 1ac35bb33fd01f1117294701edc4c671c23fd049 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Sun, 10 Apr 2022 15:11:08 +0200 Subject: [PATCH 11/23] Dynamic AutoHotkey keybinds --- modules/AutoHotkey/AutoHotkey.psm1 | 27 ++++++++++++++---------- modules/AutoHotkey/Keybinds.ahk.TEMPLATE | 6 +++--- modules/Cli/Cli.psm1 | 4 ++++ pwsh.yaml | 16 ++++++++++---- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/modules/AutoHotkey/AutoHotkey.psm1 b/modules/AutoHotkey/AutoHotkey.psm1 index a991131..a207fa5 100644 --- a/modules/AutoHotkey/AutoHotkey.psm1 +++ b/modules/AutoHotkey/AutoHotkey.psm1 @@ -6,7 +6,7 @@ function Invoke-AutoHotkeyKeybindings($config) { } } - if ($config?.loadKeybindsOnStartup -and !(Test-Path (Join-Path $startupDir "Keybinds.ahk"))) { + if ($config.loadKeybindsOnStartup -and !(Test-Path (Join-Path $env:APPDATA "Microsoft" "Windows" "Start Menu" "Programs" "Startup" "Keybinds.ahk"))) { Install-AutoHotkeyKeybindings } } @@ -15,25 +15,30 @@ function Install-AutoHotkeyKeybindings { $startupDir = (Join-Path $env:APPDATA "Microsoft" "Windows" "Start Menu" "Programs" "Startup") if (!(Test-IsAdministrator)) { - Write-ErrorMessage "Unable to install AutoHotkey keybinds. Please run your profile as administrator to initially set this up." + Write-ErrorMessage "Unable to install AutoHotkey keybinds. Please run your as administrator to install." return } - # Create symbolic link in startup directory - Write-InfoMessage "Adding symbolic link for Keybinds.ahk to startup directory" + # Create symbolic links in startup directory + Get-ChildItem -Exclude *.TEMPLATE (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "*.ahk") | ForEach-Object { + $destination = (Join-Path $startupDir $_.Name) + if (!(Test-Path $destination)) { + Write-InfoMessage "Installing $($_.Name)" - New-Item -ItemType SymbolicLink ` - -Path (Join-Path $startupDir "Keybinds.ahk") ` - -Value (Join-Path $env:PWSH_REPO "modules" "AutoHotkey" "Keybinds.ahk") + New-Item -ItemType SymbolicLink ` + -Path $destination ` + -Value $_.FullName + } + } } -function Uninstall-AutoHotkeyKeybindsings { +function Uninstall-AutoHotkeyKeybindings { if (!(Test-IsAdministrator)) { Write-ErrorMessage "Unable to uninstall AutoHotkey keybinds. Please run this command in an administrator context." return } - $destination = (Join-Path $startupDir $env:APPDATA "Microsoft" "Windows" "Start Menu" "Programs" "Startup" "Keybinds.ahk") + $destination = (Join-Path $env:APPDATA "Microsoft" "Windows" "Start Menu" "Programs" "Startup" "Keybinds.ahk") if (!(Test-Path $destination)) { Write-ErrorMessage "Unable to uninstall AutoHotkey keybinds. The symbolic link does not exist." return @@ -44,6 +49,6 @@ function Uninstall-AutoHotkeyKeybindsings { $config = (Get-Config).modules.AutoHotkey -if ($config.enabled) { - Invoke-AutoHotkeyKeybindings $config?.config +if ($config.enabled -and !($null -eq $config.config)) { + Invoke-AutoHotkeyKeybindings $config.config } diff --git a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE index a2c4fbd..80c5c40 100644 --- a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE +++ b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE @@ -1,12 +1,12 @@ #SingleInstance,Force ; Open Windows Terminal on Windows+Enter -LWin & Enter:: +#{modules.AutoHotkey.config.binds.terminal}:: Run, #{terminal.path} #{terminal.args} return ; Stop keybinds -LWin & Q::ExitApp +#{modules.AutoHotkey.config.binds.stop}::ExitApp ; Reload keybinds -LWin & R::Reload +#{modules.AutoHotkey.config.binds.reload}::Reload diff --git a/modules/Cli/Cli.psm1 b/modules/Cli/Cli.psm1 index d3ecde4..0cc565e 100644 --- a/modules/Cli/Cli.psm1 +++ b/modules/Cli/Cli.psm1 @@ -6,6 +6,10 @@ $commands = @{ "alias" = @{ "add" = "New-ProfileAlias" } + "keybinds" = @{ + "install" = "Install-AutoHotkeyKeybindings" + "uninstall" = "Uninstall-AutoHotkeyKeybindings" + } } function Invoke-LoadArgParser { diff --git a/pwsh.yaml b/pwsh.yaml index 6c6e3de..f78e1cb 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -90,19 +90,15 @@ modules: domain: Postgres: enabled: false - config: Kubectl: enabled: false config: Helm: enabled: false - config: Npm: enabled: false - config: Yarn: enabled: false - config: AutoHotkey: enabled: false config: @@ -111,6 +107,18 @@ modules: # Default: false loadKeybindsOnStartup: false + # Keybindings for various functionality + # Full key list: https://www.autohotkey.com/docs/KeyList.htm + binds: + # Launch configured terminal + terminal: "LWin & Enter" + + # Reload all AutoHotkey keybinds + reload: "LWin & R" + + # Stop all AutoHotkey keybinds + stop: "LWin & Q" + # Paths to add to the session paths: - ${HOME}/tools From 505c434ebe7851c3b0217ce7ffe1c4b1d26201bb Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Mon, 11 Apr 2022 12:51:46 +0200 Subject: [PATCH 12/23] Enforce default Windows OpenSSH agent by default --- modules/Git/Path.psm1 | 4 ++-- modules/Ssh/Ssh.psm1 | 23 +++++++++++++++++++---- pwsh.yaml | 7 ++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/modules/Git/Path.psm1 b/modules/Git/Path.psm1 index 7ccaafd..137901d 100644 --- a/modules/Git/Path.psm1 +++ b/modules/Git/Path.psm1 @@ -1,4 +1,4 @@ # Add Git GNU-like utils to PATH on Windows if ($IsWindows) { - $env:Path += ";$env:ProgramFiles\Git\usr\bin" -} \ No newline at end of file + $env:Path += ";$(Join-Path $env:ProgramFiles "Git" "usr" "bin")" +} diff --git a/modules/Ssh/Ssh.psm1 b/modules/Ssh/Ssh.psm1 index 33867d4..650e1e2 100644 --- a/modules/Ssh/Ssh.psm1 +++ b/modules/Ssh/Ssh.psm1 @@ -1,5 +1,5 @@ $sshConfig = (Get-Config).modules.Ssh -if (!($sshConfig?.enabled ?? $false)) { +if (!($sshConfig.enabled)) { return } @@ -13,10 +13,24 @@ if (!($sshConfig?.enabled ?? $false)) { function Set-GitEnvironmentVars { # Set git SSH agent to whatever is set as the ssh agent in the PATH - $env:GIT_SSH=$((Get-Command -Name ssh).Source) + if ($sshConfig.config.binaries -eq "windows_openssh") { + $binaries = "C:\WINDOWS\System32\OpenSSH" + } + + if ($sshConfig.config.binaries -eq "windows_git") { + $binaries = Convert-Path (Join-Path (Get-Command git).Source ".." ".." "usr" "bin") + } + + $env:PWSH_SSH_BINARIES = $binaries + $env:GIT_SSH = (Join-Path $binaries "ssh.exe") } function Start-OpenSshAgent { + if (!($sshConfig.config.binaries -eq "windows_openssh")) { + Write-WarningMessage "Cannot start non built in ssh-agent for now :(" + return + } + if (!(Get-Process | Where-Object { $_.Name -eq 'ssh-agent'})) { $service = Get-Service ssh-agent @@ -47,9 +61,10 @@ if ($IsWindows) { # For each ssh-agent connection, add the specified public key to the agent. if ($sshConfig.config.keys.Length -gt 0) { $keys = $sshConfig.config.keys | ForEach-Object { Get-Item $_ } - $currentlyAddedSshKeys = $(ssh-add -l)?.Split(" ")?[3]?.Replace("(", "").Replace(")", "").ToLower() ?? @() + $current = "$(Join-Path "$env:PWSH_SSH_BINARIES" "ssh-add") -l" | Invoke-Expression + $currentlyAddedSshKeys = $current.Split(" ")?[3]?.Replace("(", "").Replace(")", "").ToLower() ?? @() $keys | Where-Object { !($_.Name.Replace("id_", "") -in $currentlyAddedSshKeys) } | ForEach-Object { - ssh-add $_.FullName + "$(Join-Path "$env:PWSH_SSH_BINARIES" "ssh-add") $($_.FullName)" | Invoke-Expression } } } diff --git a/pwsh.yaml b/pwsh.yaml index f78e1cb..338523b 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -19,6 +19,11 @@ modules: Ssh: enabled: false config: + # Path to ssh binaries + # Options: + # windows_git | windows_openssh + # Default: windows_openssh + binaries: windows_openssh # List of paths to SSH keys to load on shell init # Default: empty # Example: @@ -132,4 +137,4 @@ terminal: # Any arguments to pass to the terminal emulator # Example: # args: --fullscreen split-pane - args: --fullscreen split-pane + args: From 6c2f2ca118dd3aa4202b862d5671b5ca9b5fb4d8 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Tue, 19 Apr 2022 10:33:07 +0200 Subject: [PATCH 13/23] Open Slack on Super+S --- modules/AutoHotkey/Keybinds.ahk.TEMPLATE | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE index 80c5c40..027567b 100644 --- a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE +++ b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE @@ -5,6 +5,11 @@ Run, #{terminal.path} #{terminal.args} return +; Open Slack on Super+S +LWin & s:: + Run, "${LOCALAPPDATA}\slack\slack.exe" + return + ; Stop keybinds #{modules.AutoHotkey.config.binds.stop}::ExitApp From 2518cd9481c9f640f85f9d09abca0f6fcf6a6903 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Tue, 19 Apr 2022 10:47:07 +0200 Subject: [PATCH 14/23] Edit common configs with Ctrl+E --- modules/Core/Config.psm1 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/modules/Core/Config.psm1 b/modules/Core/Config.psm1 index 27292aa..429fc86 100644 --- a/modules/Core/Config.psm1 +++ b/modules/Core/Config.psm1 @@ -35,9 +35,28 @@ function Edit-UserConfig { "$env:EDITOR $env:PWSH_CONFIG" | Invoke-Expression } +function Edit-Configs { + # Paths to potential config files + $configs = @( + (Convert-Path (Join-Path $env:APPDATA "alacritty" "alacritty.yml")), + (Get-ChildItem (Join-Path $env:LOCALAPPDATA "Packages" "Microsoft.WindowsTerminal_*" "LocalState" "settings.json")).FullName) + + $config = Read-Option "Select config to edit" $configs + + if ($config) { + "$env:EDITOR $config" | Invoke-Expression + } +} + # Keybinds Set-PSReadLineKeyHandler -Chord Ctrl+. -Description "Edit pwsh.yaml config with $env:EDITOR" -ScriptBlock { [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() [Microsoft.PowerShell.PSConsoleReadLine]::Insert('Edit-UserConfig') [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() +} + +Set-PSReadLineKeyHandler -Chord Ctrl+e -Description "Pick a config file to edit with $env:EDITOR" -ScriptBlock { + [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() + [Microsoft.PowerShell.PSConsoleReadLine]::Insert('Edit-Configs') + [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() } \ No newline at end of file From eccb1335b0a141b10849741dd3f9ded45d284254 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Tue, 19 Apr 2022 10:48:36 +0200 Subject: [PATCH 15/23] Only edit configs that actually exist --- modules/Core/Config.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/Core/Config.psm1 b/modules/Core/Config.psm1 index 429fc86..62f8dcd 100644 --- a/modules/Core/Config.psm1 +++ b/modules/Core/Config.psm1 @@ -38,8 +38,9 @@ function Edit-UserConfig { function Edit-Configs { # Paths to potential config files $configs = @( - (Convert-Path (Join-Path $env:APPDATA "alacritty" "alacritty.yml")), + (Join-Path $env:APPDATA "alacritty" "alacritty.yml"), (Get-ChildItem (Join-Path $env:LOCALAPPDATA "Packages" "Microsoft.WindowsTerminal_*" "LocalState" "settings.json")).FullName) + | Where-Object { Test-Path $_ } $config = Read-Option "Select config to edit" $configs From 2c7043e89597385cc6adfc7de6137ee9da94d18b Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Tue, 19 Apr 2022 10:50:09 +0200 Subject: [PATCH 16/23] Add .gitconfig to config edit list --- modules/Core/Config.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/Core/Config.psm1 b/modules/Core/Config.psm1 index 62f8dcd..48a59a0 100644 --- a/modules/Core/Config.psm1 +++ b/modules/Core/Config.psm1 @@ -39,7 +39,8 @@ function Edit-Configs { # Paths to potential config files $configs = @( (Join-Path $env:APPDATA "alacritty" "alacritty.yml"), - (Get-ChildItem (Join-Path $env:LOCALAPPDATA "Packages" "Microsoft.WindowsTerminal_*" "LocalState" "settings.json")).FullName) + (Get-ChildItem (Join-Path $env:LOCALAPPDATA "Packages" "Microsoft.WindowsTerminal_*" "LocalState" "settings.json")).FullName), + (Join-Path $env:HOME ".gitconfig") | Where-Object { Test-Path $_ } $config = Read-Option "Select config to edit" $configs From 6db718f488702db3a73df0dec0937cc5dfb30dc3 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Tue, 19 Apr 2022 11:16:56 +0200 Subject: [PATCH 17/23] Open known text files with $env:EDITOR --- modules/FuzzyFinder/FuzzyFinder.psm1 | 37 ++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/modules/FuzzyFinder/FuzzyFinder.psm1 b/modules/FuzzyFinder/FuzzyFinder.psm1 index fa63191..04b9b80 100644 --- a/modules/FuzzyFinder/FuzzyFinder.psm1 +++ b/modules/FuzzyFinder/FuzzyFinder.psm1 @@ -13,7 +13,14 @@ function Invoke-FuzzyFile { # If selection is file, open it if (Test-Path -Path $selection -PathType Leaf) { - Invoke-Item $selection + # If known text file type, open with configured EDITOR + if ((Test-IsTextFile -FilePath $selection)) { + "$env:EDITOR $selection" | Invoke-Expression + } + # Otherwise, just open it with default app + else { + Invoke-Item $selection + } } # If selection is a directory, navigate and invoke this function again @@ -21,4 +28,30 @@ function Invoke-FuzzyFile { Set-Location $selection Invoke-FuzzyFile } -} \ No newline at end of file +} + +function Test-IsTextFile($FilePath) { + $extension = (Get-ChildItem (Convert-Path $selection)).Extension + + return @( + '.txt', + '.md', + '.yml', + '.yaml', + '.html', + '.js', + '.json', + '.toml', + '.xml', + '.config', + '.css', + '.scss', + '.less', + '.cs', + '.ps1', + 'psm1', + '.sh', + '.gitmodules', + '.gitattributes', + '.gitconfig').Contains($extension) +} From 041d213ad5347de43264d0d040533fa642954c27 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Tue, 19 Apr 2022 16:56:30 +0200 Subject: [PATCH 18/23] Configurable editors per file type --- modules/FuzzyFinder/FuzzyFinder.psm1 | 35 +++++++--------------------- pwsh.yaml | 31 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/modules/FuzzyFinder/FuzzyFinder.psm1 b/modules/FuzzyFinder/FuzzyFinder.psm1 index 04b9b80..f0095f2 100644 --- a/modules/FuzzyFinder/FuzzyFinder.psm1 +++ b/modules/FuzzyFinder/FuzzyFinder.psm1 @@ -13,9 +13,10 @@ function Invoke-FuzzyFile { # If selection is file, open it if (Test-Path -Path $selection -PathType Leaf) { - # If known text file type, open with configured EDITOR - if ((Test-IsTextFile -FilePath $selection)) { - "$env:EDITOR $selection" | Invoke-Expression + # If known text file type, open with configured editor + $editor = Get-Editor -FilePath $selection + if ($editor) { + "$($editor.path) $selection" | Invoke-Expression } # Otherwise, just open it with default app else { @@ -30,28 +31,10 @@ function Invoke-FuzzyFile { } } -function Test-IsTextFile($FilePath) { - $extension = (Get-ChildItem (Convert-Path $selection)).Extension +function Get-Editor($FilePath) { + $extension = (Get-ChildItem (Convert-Path $FilePath)).Extension - return @( - '.txt', - '.md', - '.yml', - '.yaml', - '.html', - '.js', - '.json', - '.toml', - '.xml', - '.config', - '.css', - '.scss', - '.less', - '.cs', - '.ps1', - 'psm1', - '.sh', - '.gitmodules', - '.gitattributes', - '.gitconfig').Contains($extension) + return (Get-Config).editors + | Where-Object { $_.extensions.Contains($extension) } + | Select-Object -First 1 } diff --git a/pwsh.yaml b/pwsh.yaml index 338523b..75aa1ae 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -138,3 +138,34 @@ terminal: # Example: # args: --fullscreen split-pane args: + +# Default editors when opening certain files +editors: + - path: nvim + extensions: + - .txt + - .md + - .yml + - .yaml + - .html + - .htm + - .js + - .ts + - .json + - .toml + - .xml + - .config + - .css + - .scss + - .less + - .ps1 + - .psm1 + - .psd1 + - .sh + - .gitmodules + - .gitattributes + - .editorconfig + - .gitconfig + - path: code + extensions: + - .cs \ No newline at end of file From 0ec99603a815a7fad18940aba60624614398cff7 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Mon, 25 Apr 2022 22:00:55 +0200 Subject: [PATCH 19/23] Wrap terminal path in quotes, remove lfcd keybind --- modules/AutoHotkey/Keybinds.ahk.TEMPLATE | 2 +- modules/Core/Config.psm1 | 2 +- modules/Lf/Keybindings.psm1 | 6 +----- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE index 027567b..0bc423c 100644 --- a/modules/AutoHotkey/Keybinds.ahk.TEMPLATE +++ b/modules/AutoHotkey/Keybinds.ahk.TEMPLATE @@ -2,7 +2,7 @@ ; Open Windows Terminal on Windows+Enter #{modules.AutoHotkey.config.binds.terminal}:: - Run, #{terminal.path} #{terminal.args} + Run, "#{terminal.path}" #{terminal.args} return ; Open Slack on Super+S diff --git a/modules/Core/Config.psm1 b/modules/Core/Config.psm1 index 48a59a0..5e28568 100644 --- a/modules/Core/Config.psm1 +++ b/modules/Core/Config.psm1 @@ -51,7 +51,7 @@ function Edit-Configs { } # Keybinds -Set-PSReadLineKeyHandler -Chord Ctrl+. -Description "Edit pwsh.yaml config with $env:EDITOR" -ScriptBlock { +Set-PSReadLineKeyHandler -Chord "Ctrl+." -Description "Edit pwsh.yaml config with $env:EDITOR" -ScriptBlock { [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() [Microsoft.PowerShell.PSConsoleReadLine]::Insert('Edit-UserConfig') [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() diff --git a/modules/Lf/Keybindings.psm1 b/modules/Lf/Keybindings.psm1 index 3759e8b..8b13789 100644 --- a/modules/Lf/Keybindings.psm1 +++ b/modules/Lf/Keybindings.psm1 @@ -1,5 +1 @@ -Set-PSReadLineKeyHandler -Chord Ctrl+o -Description "LF: Run lf in current dir, navigate to selection on exit" -ScriptBlock { - [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() - [Microsoft.PowerShell.PSConsoleReadLine]::Insert('Invoke-Lfcd') - [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() -} + From 5f1037cb4924848df8ceeb4e588e251ffdb9cc18 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Mon, 23 May 2022 15:25:09 +0200 Subject: [PATCH 20/23] Generate random hash for temporary configs. --- modules/Core/Config.psm1 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/Core/Config.psm1 b/modules/Core/Config.psm1 index 5e28568..597e66a 100644 --- a/modules/Core/Config.psm1 +++ b/modules/Core/Config.psm1 @@ -10,10 +10,13 @@ function Get-Config { $config = $config | Format-EnvironmentVariables | Format-Paths - $config | Where-Object { !($_ -ilike '*#*') -and $_ } > $env:TMP\pwsh.yaml - $config = (ConvertFrom-Yaml -Path $env:TMP\pwsh.yaml) + $hash = Get-Random + $tempConfigPath = (Join-Path $env:TMP "pwsh.$hash.yaml") - Remove-Item $env:TMP\pwsh.yaml + $config | Where-Object { !($_ -ilike '*#*') -and $_ } | Out-File -Path $tempConfigPath + $config = (ConvertFrom-Yaml -Path $tempConfigPath) + + Remove-Item $tempConfigPath return $config } From 95d6ac92e5147caba2d3ec89556b79b6990ae898 Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Wed, 25 May 2022 09:29:38 +0200 Subject: [PATCH 21/23] Add starship.toml to list of editable user configs --- modules/Core/Config.psm1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/Core/Config.psm1 b/modules/Core/Config.psm1 index 597e66a..fbe4780 100644 --- a/modules/Core/Config.psm1 +++ b/modules/Core/Config.psm1 @@ -43,7 +43,8 @@ function Edit-Configs { $configs = @( (Join-Path $env:APPDATA "alacritty" "alacritty.yml"), (Get-ChildItem (Join-Path $env:LOCALAPPDATA "Packages" "Microsoft.WindowsTerminal_*" "LocalState" "settings.json")).FullName), - (Join-Path $env:HOME ".gitconfig") + (Join-Path $env:HOME ".gitconfig"), + (Join-Path $env:HOME ".config" "starship.toml") | Where-Object { Test-Path $_ } $config = Read-Option "Select config to edit" $configs @@ -64,4 +65,4 @@ Set-PSReadLineKeyHandler -Chord Ctrl+e -Description "Pick a config file to edit [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() [Microsoft.PowerShell.PSConsoleReadLine]::Insert('Edit-Configs') [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() -} \ No newline at end of file +} From 8cd660c08207b3e89f20eaa6f1c8fb42c26ec0ac Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Tue, 21 Jun 2022 09:49:43 +0200 Subject: [PATCH 22/23] paths: Add option to exclude paths --- pwsh.yaml | 8 ++++++-- pwshrc.ps1 | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pwsh.yaml b/pwsh.yaml index 75aa1ae..6acad48 100644 --- a/pwsh.yaml +++ b/pwsh.yaml @@ -124,9 +124,13 @@ modules: # Stop all AutoHotkey keybinds stop: "LWin & Q" -# Paths to add to the session +# Configure paths paths: -- ${HOME}/tools + # Paths to append to the PATH environment variable + include: + - ${HOME}/tools + # Exact paths remove from the PATH environment variable + exclude: # Terminal emulator settings terminal: diff --git a/pwshrc.ps1 b/pwshrc.ps1 index e03c945..1d7587e 100644 --- a/pwshrc.ps1 +++ b/pwshrc.ps1 @@ -40,7 +40,8 @@ function Invoke-Modules($config) { function Invoke-Path($config) { $path = $env:Path.Split(";") | Where-Object { $_ } | Sort-Object | Get-Unique - $path += $config.paths + $path += $config.paths.include + $path = $path | Where-Object { !( $config.paths.exclude.Contains($_)) } $env:Path = [string]::Join(";", $path) } From 31fdb1c67d7e9ea102a4063750ecf6b94194a74e Mon Sep 17 00:00:00 2001 From: Patrick Christensen Date: Mon, 27 Jun 2022 14:59:16 +0200 Subject: [PATCH 23/23] Fix Linux issues such as the PATH and TMP environment variables --- modules/Core/Config.psm1 | 5 +++++ pwshrc.ps1 | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/Core/Config.psm1 b/modules/Core/Config.psm1 index fbe4780..c5bc532 100644 --- a/modules/Core/Config.psm1 +++ b/modules/Core/Config.psm1 @@ -11,6 +11,11 @@ function Get-Config { $config = $config | Format-EnvironmentVariables | Format-Paths $hash = Get-Random + $env:TMP = $env:TMP ?? (Join-Path $HOME "tmp") + if (!(Test-Path $env:TMP)) { + New-Item -ItemType Directory -Force -Path $env:TMP + } + $tempConfigPath = (Join-Path $env:TMP "pwsh.$hash.yaml") $config | Where-Object { !($_ -ilike '*#*') -and $_ } | Out-File -Path $tempConfigPath diff --git a/pwshrc.ps1 b/pwshrc.ps1 index 1d7587e..833c724 100644 --- a/pwshrc.ps1 +++ b/pwshrc.ps1 @@ -39,11 +39,11 @@ function Invoke-Modules($config) { } function Invoke-Path($config) { - $path = $env:Path.Split(";") | Where-Object { $_ } | Sort-Object | Get-Unique + $path = $env:PATH.Split(";") | Where-Object { $_ } | Sort-Object | Get-Unique $path += $config.paths.include $path = $path | Where-Object { !( $config.paths.exclude.Contains($_)) } - $env:Path = [string]::Join(";", $path) + $env:PATH = [string]::Join(";", $path) } # Load environment variables