refactor!: initialization through templates

This commit is contained in:
iff 2025-04-05 18:31:45 +02:00
parent 4f637c1e1b
commit 885dc081e1
9 changed files with 230 additions and 211 deletions

7
core/templates/init.bash Normal file
View file

@ -0,0 +1,7 @@
alias {{ alias }}='eval $(_PR_LAST_COMMAND="$(fc -ln -1)" _PR_ALIAS="`alias`" _PR_SHELL="bash" "{{ binary_path }}")'
{%- if cnf %}
command_not_found_handle() {
eval $(_PR_LAST_COMMAND="$@" _PR_ALIAS="`alias`" _PR_SHELL="bash" _PR_MODE="cnf" "{{ binary_path }}")
}
{% endif %}

9
core/templates/init.fish Normal file
View file

@ -0,0 +1,9 @@
function {{ alias }} -d "Suggest fixes to the previous command"
eval $(_PR_LAST_COMMAND="$(history | head -n 1)" _PR_ALIAS="$(alias)" _PR_SHELL="fish" "{{ binary_path }}")
end
{%if cnf %}
function fish_command_not_found --on-event fish_command_not_found
eval $(_PR_LAST_COMMAND="$argv" _PR_ALIAS="$(alias)" _PR_SHELL="fish" _PR_MODE="cnf" "{{ binary_path }}")
end
{% endif %}

4
core/templates/init.nu Normal file
View file

@ -0,0 +1,4 @@
def --env {{ alias }} [] {
let dir = (with-env { _PR_LAST_COMMAND: (history | last).command, _PR_ALIAS: (help aliases | select name expansion | each ({ |row| $row.name + "=" + $row.expansion }) | str join (char nl)), _PR_SHELL: nu } { {{ binary_path }} })
cd $dir
}

50
core/templates/init.ps1 Normal file
View file

@ -0,0 +1,50 @@
function {{ alias }} {
try {
# fetch command and error from session history only when not in cnf mode
if ($env:_PR_MODE -ne 'cnf') {
$env:_PR_LAST_COMMAND = (Get-History | Select-Object -Last 1 | ForEach-Object {$_.CommandLine});
if ($PSVersionTable.PSVersion.Major -ge 7) {
$err = Get-Error;
if ($env:_PR_LAST_COMMAND -eq $err.InvocationInfo.Line) {
$env:_PR_ERROR_MSG = $err.Exception.Message
}
}
if ($env:_PR_LAST_COMMAND -eq $err.InvocationInfo.Line) {
$env:_PR_ERROR_MSG = $err.Exception.Message
}
}
$env:_PR_SHELL = 'pwsh';
&'{{ binary_path }}';
}
finally {
# restore mode from cnf
if ($env:_PR_MODE -eq 'cnf') {
$env:_PR_MODE = $env:_PR_PWSH_ORIGIN_MODE;
$env:_PR_PWSH_ORIGIN_MODE = $null;
}
}
}
{%- if cnf %}
# Uncomment this block to enable command not found hook
# It's not useful as we can't retrieve arguments,
# and it seems to be a recursion bug
# $ExecutionContext.InvokeCommand.CommandNotFoundAction =
# {
# param(
# [string]
# $commandName,
# [System.Management.Automation.CommandLookupEventArgs]
# $eventArgs
# )
# # powershell does not support run command with specific environment variables
# # but you must set global variables. so we are memorizing the current mode and the alias function will reset it later.
# $env:_PR_PWSH_ORIGIN_MODE=$env:_PR_MODE;
# $env:_PR_MODE='cnf';
# # powershell may search command with prefix 'get-' or '.\' first when this hook is hit, strip them
# $env:_PR_LAST_COMMAND=$commandName -replace '^get-|\.\\','';
# $eventArgs.Command = (Get-Command {{ alias }});
# $eventArgs.StopSearch = $True;
# }
{% endif %}

7
core/templates/init.zsh Normal file
View file

@ -0,0 +1,7 @@
alias {{ alias }}='eval $(_PR_LAST_COMMAND="$(fc -ln -1)" _PR_ALIAS="`alias`" _PR_SHELL="zsh" "{{ binary_path }}")'
{%- if cnf %}
command_not_found_handler() {
eval $(_PR_LAST_COMMAND="$@" _PR_SHELL="zsh" _PR_ALIAS="`alias`" _PR_MODE="cnf" "{{ binary_path }}")
}
{% endif %}