diff --git a/module/CHANGELOG-CN.md b/module/CHANGELOG-CN.md index 2722a7a..8d8fa95 100644 --- a/module/CHANGELOG-CN.md +++ b/module/CHANGELOG-CN.md @@ -3,6 +3,11 @@ 简体中文

+## 5.0.5 (2024/9/2) + +- 在 `$PSCompletions` 中添加一个方法 `return_completion` 用于 `hooks.ps1`。 +- 其他的优化和修复。 + ## 5.0.4 (2024/9/1) - 修复 `psc rm *` 命令会重置所有配置项的问题。 diff --git a/module/CHANGELOG.json b/module/CHANGELOG.json index a8b0b55..de0ead8 100644 --- a/module/CHANGELOG.json +++ b/module/CHANGELOG.json @@ -1,4 +1,19 @@ [ + { + "version": "5.0.5", + "info": { + "zh-CN": [ + "修复(2024/9/2)\n", + "- 在 <@Magenta>$PSCompletions<@Blue> 中添加一个方法 <@Magenta>return_completion<@Blue> 用于 hooks.ps1\n", + "- 其他的优化和修复。\n" + ], + "en-US": [ + "Fix(2024/9/2)\n", + "- Add a method <@Magenta>return_completion<@Blue> in <@Magenta>$PSCompletions<@Blue> for hooks.ps1.\n", + "- Other optimizations and fixes.\n" + ] + } + }, { "version": "5.0.4", "info": { diff --git a/module/CHANGELOG.md b/module/CHANGELOG.md index 89c7943..e960329 100644 --- a/module/CHANGELOG.md +++ b/module/CHANGELOG.md @@ -3,6 +3,11 @@ English

+## 5.0.5 (2024/9/2) + +- Add a method `return_completion` in `$PSCompletions` for `hooks.ps1`. +- Other optimizations and fixes. + ## 5.0.4 (2024/9/1) - Fix the issue that the `psc rm *` command will reset all configuration items. diff --git a/module/PSCompletions/PSCompletions.psd1 b/module/PSCompletions/PSCompletions.psd1 index fd94a9a..fb43f89 100644 --- a/module/PSCompletions/PSCompletions.psd1 +++ b/module/PSCompletions/PSCompletions.psd1 @@ -9,7 +9,7 @@ @{ RootModule = 'PSCompletions.psm1' - ModuleVersion = '5.0.4' + ModuleVersion = '5.0.5' GUID = '00929632-527d-4dab-a5b3-21197faccd05' diff --git a/module/PSCompletions/PSCompletions.psm1 b/module/PSCompletions/PSCompletions.psm1 index eef9f21..58eb432 100644 --- a/module/PSCompletions/PSCompletions.psm1 +++ b/module/PSCompletions/PSCompletions.psm1 @@ -422,13 +422,7 @@ handle_done ($arg[2] -match 'http[s]?://' -or $arg[2] -eq '') } 'function_name' { - # Get-Command PSCompletions 会导致触发更新,需要特殊处理 - if ($arg[2] -eq 'PSCompletions' -and $PSCompletions.config.function_name -ne 'PSCompletions') { - handle_done $true - return - } - $is_exist = Get-Command $arg[2] -ErrorAction SilentlyContinue - handle_done ($arg[2] -is [string] -and $arg[2] -ne '' -and !$is_exist) + handle_done ($arg[2] -ne '' -and !(Get-Command $arg[2] -ErrorAction SilentlyContinue)) } } } diff --git a/module/PSCompletions/core/init.ps1 b/module/PSCompletions/core/init.ps1 index f2d6272..664b6d0 100644 --- a/module/PSCompletions/core/init.ps1 +++ b/module/PSCompletions/core/init.ps1 @@ -1,7 +1,7 @@ using namespace System.Management.Automation $_ = Split-Path $PSScriptRoot -Parent New-Variable -Name PSCompletions -Value @{ - version = '5.0.4' + version = '5.0.5' path = @{ root = $_ completions = Join-Path $_ 'completions' @@ -107,8 +107,19 @@ else { . $PSScriptRoot\menu\win.ps1 } +Add-Member -InputObject $PSCompletions -MemberType ScriptMethod return_completion { + param([string]$name, [string]$tip = ' ', [array]$symbols) + $_symbol = foreach ($c in $symbols) { $PSCompletions.config.$c } + $padSymbols = if ($_symbol) { "$($PSCompletions.config.between_item_and_symbol)$($_symbol -join '')" }else { '' } + $cmd_arr = $name -split ' ' + @{ + name = $name + ListItemText = "$($cmd_arr[-1])$($padSymbols)" + CompletionText = $cmd_arr[-1] + ToolTip = $tip + } +} Add-Member -InputObject $PSCompletions -MemberType ScriptMethod get_completion { - # 获取 json 数据 if ($PSCompletions.job.State -eq 'Completed') { $PSCompletions.completions = Receive-Job $PSCompletions.job Remove-Job $PSCompletions.job @@ -568,21 +579,10 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod split_array { } $chunks } -Add-Member -InputObject $PSCompletions -MemberType ScriptMethod ensure_file { - param([string]$path) - if (!(Test-Path $path)) { New-Item -ItemType File $path > $null } -} Add-Member -InputObject $PSCompletions -MemberType ScriptMethod ensure_dir { param([string]$path) if (!(Test-Path $path)) { New-Item -ItemType Directory $path > $null } } -Add-Member -InputObject $PSCompletions -MemberType ScriptMethod join_path { - $res = $args[0] - for ($i = 1; $i -lt $args.Count; $i++) { - $res = Join-Path $res $args[$i] - } - $res -} Add-Member -InputObject $PSCompletions -MemberType ScriptMethod get_language { param ([string]$completion) $path_config = "$($PSCompletions.path.completions)/$completion/config.json" @@ -839,7 +839,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod download_file { Write-Host "File save path: $file" -ForegroundColor Red Write-Host "If you are sure that it is not a network problem, please submit an issue`n" -ForegroundColor Red } - throw + throw $_ } } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { @@ -853,10 +853,11 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { $is_update = (Test-Path "$($PSCompletions.path.completions)/$completion") -and $is_update $completion_dir = Join-Path $PSCompletions.path.completions $completion + $language_dir = Join-Path $completion_dir 'language' $PSCompletions.ensure_dir($PSCompletions.path.completions) $PSCompletions.ensure_dir($completion_dir) - $PSCompletions.ensure_dir((Join-Path $completion_dir 'language')) + $PSCompletions.ensure_dir($language_dir) $download_info = @{ url = "$url/config.json" @@ -875,7 +876,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { foreach ($_ in $config.language) { $files += @{ Uri = "$url/language/$_.json" - OutFile = $PSCompletions.join_path($completion_dir, 'language', "$_.json") + OutFile = Join-Path $language_dir "$_.json" } } if ($config.hooks) { @@ -1081,7 +1082,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_powers if (!(Test-Path (Join-Path $PSCompletions.path.core '.temp'))) { Add-Member -InputObject $PSCompletions -MemberType ScriptMethod move_old_version { - $version = (Get-ChildItem (Split-Path $PSCompletions.path.root -Parent) -ErrorAction SilentlyContinue).Name | Sort-Object { [Version]$_ } -ErrorAction SilentlyContinue + $version = (Get-ChildItem (Split-Path $PSCompletions.path.root -Parent) -ErrorAction SilentlyContinue).Name | Sort-Object { [Version]$_ } -ErrorAction SilentlyContinue | Where-Object { $_ -match '^\d+\.\d.*' } if ($version -is [array]) { $old_version = $version[-2] if ($old_version -match '^\d+\.\d.*' -and $old_version -ge '4') { @@ -1206,7 +1207,20 @@ if (!(Test-Path (Join-Path $PSCompletions.path.core '.temp'))) { } } else { - $PSCompletions.is_first_init = $true + if ($PSUICulture -eq 'zh-CN') { + $language = 'zh-CN' + $PSCompletions.url = 'https://gitee.com/abgox/PSCompletions/raw/main' + } + else { + $language = 'en-US' + $PSCompletions.url = 'https://raw.githubusercontent.com/abgox/PSCompletions/main' + } + + $PSCompletions.ensure_dir("$($PSCompletions.path.completions)/psc/language") + $PSCompletions.download_file("$($PSCompletions.url)/completions/psc/language/$language.json", "$($PSCompletions.path.completions)/psc/language/$language.json") + + $PSCompletions.info = $PSCompletions.ConvertFrom_JsonToHashtable($PSCompletions.get_raw_content("$($PSCompletions.path.completions)/psc/language/$language.json")).info + $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.init_info)) } } $PSCompletions.move_old_version() @@ -1215,29 +1229,23 @@ if (!(Test-Path (Join-Path $PSCompletions.path.core '.temp'))) { $PSCompletions.init_data() -if (Get-Command Set-PSReadLineKeyHandler -ErrorAction SilentlyContinue) { - Set-PSReadLineKeyHandler $PSCompletions.config.trigger_key MenuComplete - $PSCompletions.generate_completion() - $PSCompletions.handle_completion() -} - -if ($PSCompletions.is_first_init) { - $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.init_info)) -} +Set-PSReadLineKeyHandler $PSCompletions.config.trigger_key MenuComplete +$PSCompletions.generate_completion() +$PSCompletions.handle_completion() foreach ($_ in $PSCompletions.data.aliasMap.Keys) { if ($PSCompletions.data.aliasMap.$_ -eq 'psc') { - Set-Alias $_ $PSCompletions.config.function_name -ErrorAction SilentlyContinue + Set-Alias $_ $PSCompletions.config.function_name } else { if ($_ -ne $PSCompletions.data.aliasMap.$_) { - Set-Alias $_ $PSCompletions.data.aliasMap.$_ -ErrorAction SilentlyContinue + Set-Alias $_ $PSCompletions.data.aliasMap.$_ } } } -if ($PSCompletions.config.enable_module_update -match '^\d+\.\d.*') { - $PSCompletions.version_list = $PSCompletions.config.enable_module_update, $PSCompletions.version | Sort-Object { [version] $_ } -Descending +if ($PSCompletions.config.enable_module_update -notin @(0, 1)) { + $PSCompletions.version_list = $PSCompletions.config.enable_module_update, $PSCompletions.version | Sort-Object { [version] $_ } -Descending -ErrorAction SilentlyContinue if ($PSCompletions.version_list[0] -ne $PSCompletions.version) { $PSCompletions.wc.DownloadFile("$($PSCompletions.url)/module/CHANGELOG.json", (Join-Path $PSCompletions.path.core 'CHANGELOG.json')) $null = $PSCompletions.confirm_do($PSCompletions.info.module.update, { diff --git a/module/version.txt b/module/version.txt index 2d6c0bc..ab0fa33 100644 --- a/module/version.txt +++ b/module/version.txt @@ -1 +1 @@ -5.0.4 +5.0.5