diff --git a/completions/git/guid.txt b/completions/git/guid.txt index b36a223e..0f79921c 100644 --- a/completions/git/guid.txt +++ b/completions/git/guid.txt @@ -1 +1 @@ -7991dd41-c069-4802-ae24-9418a129e5f7 +d728e74d-6a4b-4928-a63f-82e56281a087 diff --git a/completions/git/language/en-US.json b/completions/git/language/en-US.json index 12961ea6..7f21e23d 100644 --- a/completions/git/language/en-US.json +++ b/completions/git/language/en-US.json @@ -2861,18 +2861,6 @@ } }, "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}\n", - "If you disable it, the <@Magenta>max_commit<@Blue> configuration will also be invalid." - ] - }, { "name": "max_commit", "value": 20, @@ -2885,7 +2873,7 @@ "$_value = $PSCompletions.config.comp_config.git.max_commit;", "if($_value -ne $null){ \"Current Value: <@Magenta>$_value<@Blue>`n\" }", "}}", - "The maximum number that can be parsed for a project commit. Default to <@Magenta>20<@Blue>.\n", + "The maximum number that can be parsed for a project commit by the hooks. Default to <@Magenta>20<@Blue>.\n", "If it is <@Magenta>-1<@Blue>, all commits will be parsed, which may affect the loading speed." ] } diff --git a/completions/git/language/zh-CN.json b/completions/git/language/zh-CN.json index 93bfc5e5..dfc4616e 100644 --- a/completions/git/language/zh-CN.json +++ b/completions/git/language/zh-CN.json @@ -2860,18 +2860,6 @@ } }, "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}\n", - "如果禁用它,那么 <@Magenta>max_commit<@Blue> 配置也将无效。" - ] - }, { "name": "max_commit", "value": 20, @@ -2884,7 +2872,7 @@ "$_value = $PSCompletions.config.comp_config.git.max_commit;", "if($_value -ne $null){ \"当前值: <@Magenta>$_value<@Blue>`n\" }", "}}", - "可以为当前项目解析的 commit 的最大数量,默认为 <@Magenta>20<@Blue>。\n", + "通过 hooks 可以为当前项目解析的 commit 的最大数量,默认为 <@Magenta>20<@Blue>。\n", "如果设置为 <@Magenta>-1<@Blue>, 会解析所有 commit,这对补全加载速度有影响。" ] } diff --git a/completions/npm/guid.txt b/completions/npm/guid.txt index 9c6d2207..6b7beb86 100644 --- a/completions/npm/guid.txt +++ b/completions/npm/guid.txt @@ -1 +1 @@ -99fd7d6e-a782-4b65-bd8f-1cdb1aa62578 +41098a08-a1dc-475e-bf76-736150c5f7f1 diff --git a/completions/npm/language/en-US.json b/completions/npm/language/en-US.json index c99b637a..ea2d7aed 100644 --- a/completions/npm/language/en-US.json +++ b/completions/npm/language/en-US.json @@ -338,18 +338,5 @@ "npm - package manager." ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/npm/language/zh-CN.json b/completions/npm/language/zh-CN.json index 6c775e87..8d07c74e 100644 --- a/completions/npm/language/zh-CN.json +++ b/completions/npm/language/zh-CN.json @@ -338,18 +338,5 @@ "npm - 软件包管理器。" ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/pnpm/guid.txt b/completions/pnpm/guid.txt index a8efdb7d..d1b8aff3 100644 --- a/completions/pnpm/guid.txt +++ b/completions/pnpm/guid.txt @@ -1 +1 @@ -5141ba32-8a2b-48d4-bea3-f001928929e1 +aa22a111-6ed6-472f-88ba-65d344325ae9 diff --git a/completions/pnpm/language/en-US.json b/completions/pnpm/language/en-US.json index 83c61e83..ad646679 100644 --- a/completions/pnpm/language/en-US.json +++ b/completions/pnpm/language/en-US.json @@ -359,18 +359,5 @@ "pnpm - Package Manager." ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/pnpm/language/zh-CN.json b/completions/pnpm/language/zh-CN.json index 708f4299..cb727ee8 100644 --- a/completions/pnpm/language/zh-CN.json +++ b/completions/pnpm/language/zh-CN.json @@ -359,18 +359,5 @@ "pnpm - 软件包管理器。" ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/psc/guid.txt b/completions/psc/guid.txt index 00220c13..8d164ae3 100644 --- a/completions/psc/guid.txt +++ b/completions/psc/guid.txt @@ -1 +1 @@ -59eaae10-2515-461d-9140-505c2a9da2d9 +556e9d2e-e157-4bd9-8d97-d81af2905cbd diff --git a/completions/psc/hooks.ps1 b/completions/psc/hooks.ps1 index ae6659c1..8c729b09 100644 --- a/completions/psc/hooks.ps1 +++ b/completions/psc/hooks.ps1 @@ -137,6 +137,13 @@ function handleCompletions([array]$completions) { 2 { $tempList += $PSCompletions.return_completion("language", $PSCompletions.replace_content($PSCompletions.info.completion.language.tip), @('SpaceTab')) $tempList += $PSCompletions.return_completion("enable_tip", $PSCompletions.replace_content($PSCompletions.info.completion.enable_tip.tip), @('SpaceTab')) + + if ($PSCompletions.config.comp_config[$completion].Count) { + if ($PSCompletions.config.comp_config[$completion].keys.Contains('enable_hooks')) { + $tip = $PSCompletions.replace_content($PSCompletions.info.completion.enable_hooks.tip) -replace '<@\w+>', '' + $tempList += $PSCompletions.return_completion('enable_hooks', $tip, $symbol) + } + } foreach ($c in $json.config) { $config_item = $c.name $tip = $PSCompletions.replace_content($c.tip) -replace '<\@\w+>', '' @@ -161,6 +168,15 @@ function handleCompletions([array]$completions) { $tempList += $PSCompletions.return_completion('1', $PSCompletions.replace_content($PSCompletions.info.completion.enable_tip.tip_v1)) $tempList += $PSCompletions.return_completion('0', $PSCompletions.replace_content($PSCompletions.info.completion.enable_tip.tip_v0)) } + 'enable_hooks' { + if ($PSCompletions.config.comp_config[$completion].Count) { + if ($PSCompletions.config.comp_config[$completion].keys.Contains('enable_hooks')) { + foreach ($value in 0..1) { + $tempList += $PSCompletions.return_completion($value, $PSCompletions.replace_content($PSCompletions.info.completion.tip_v)) + } + } + } + } Default { $c = $json.config.Where({ $_.name -eq $filter_input_arr[2] }) foreach ($value in $c.values) { diff --git a/completions/psc/language/en-US.json b/completions/psc/language/en-US.json index ef97fc37..b3769c80 100644 --- a/completions/psc/language/en-US.json +++ b/completions/psc/language/en-US.json @@ -1503,9 +1503,12 @@ "}}\n", "Completion file directory: <@Blue>{{ $completion_dir }}\n", "{{", - " if($json.config){", + " if($json.config -or $config.hooks -ne $null){", "\"<@Blue>---------- The special configuration of $($completion)---------- \";", - " $config_list=$json.config | ForEach-Object {", + "$configList = @();", + "if($config.hooks -ne $null){$configList += @{name='enable_hooks';value=[int]$config.hooks;tip=$PSCompletions.info.completion.enable_hooks.tip};};", + "if($json.config){$configList += $json.config;};", + " $config_list = $configList | ForEach-Object {", " \"`n<@Magenta>$($_.name)<@Blue>:`nDefault Value: $($_.value)`n$($PSCompletions.replace_content($_.tip))\" ", " };", " $config_list -join \"`n\";", @@ -1556,9 +1559,12 @@ "}}\n", "Completion file directory: <@Blue>{{ $completion_dir }}\n", "{{", - "if($json.config){", + " if($json.config -or $config.hooks -ne $null){", "\"<@Blue>---------- The special configuration of $($completion)---------- \";", - " $config_list=$json.config | ForEach-Object {", + "$configList = @();", + "if($config.hooks -ne $null){$configList += @{name='enable_hooks';value=[int]$config.hooks;tip=$PSCompletions.info.completion.enable_hooks.tip};};", + "if($json.config){$configList += $json.config;};", + " $config_list = $configList | ForEach-Object {", " \"`n<@Magenta>$($_.name)<@Blue>:`nDefault Value: $($_.value)`n$($PSCompletions.replace_content($_.tip))\" ", " };", " $config_list -join \"`n\";", @@ -1795,7 +1801,7 @@ "$_value = $PSCompletions.config.comp_config.$completion.enable_hooks;", "if($_value -ne $null){ \"Current Value: <@Magenta>$_value<@Blue>`n\" }", "}}", - "Set whether to enable hooks. Default to <@Magenta>1<@Blue>.\n", + "Set whether to enable hooks.\n", "The hooks will parse or generate some completion data and add them dynamically.\n", "You can disable it by running the command (<@Magenta>psc completion {{ $completion }} enable_hooks 0<@Blue>)." ] @@ -1925,18 +1931,5 @@ ] } } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/psc/language/zh-CN.json b/completions/psc/language/zh-CN.json index a475aa46..e099c057 100644 --- a/completions/psc/language/zh-CN.json +++ b/completions/psc/language/zh-CN.json @@ -1512,9 +1512,12 @@ "}}\n", "补全文件所在目录: <@Blue>{{ $completion_dir }}\n", "{{", - "if($json.config){", + "if($json.config -or $config.hooks -ne $null){", " \"<@Blue>---------- $($completion) 补全特殊配置 ---------- \";", - " $config_list=$json.config | ForEach-Object {", + "$configList = @();", + "if($config.hooks -ne $null){$configList += @{name='enable_hooks';value=[int]$config.hooks;tip=$PSCompletions.info.completion.enable_hooks.tip};};", + "if($json.config){$configList += $json.config;};", + " $config_list = $configList | ForEach-Object {", " \"`n<@Magenta>$($_.name)<@Blue>:`n默认值: $($_.value)`n$($PSCompletions.replace_content($_.tip))\"", " };", " $config_list -join \"`n\";", @@ -1565,9 +1568,12 @@ "}}\n", "补全文件所在目录: <@Blue>{{ $completion_dir }}\n", "{{", - "if($json.config){", + "if($json.config -or $config.hooks -ne $null){", " \"<@Blue>---------- $($completion) 补全特殊配置 ---------- \";", - " $config_list=$json.config | ForEach-Object {", + "$configList = @();", + "if($config.hooks -ne $null){$configList += @{name='enable_hooks';value=[int]$config.hooks;tip=$PSCompletions.info.completion.enable_hooks.tip};};", + "if($json.config){$configList += $json.config;};", + " $config_list = $configList | ForEach-Object {", " \"`n<@Magenta>$($_.name)<@Blue>:`n默认值: $($_.value)`n$($PSCompletions.replace_content($_.tip))\"", " };", " $config_list -join \"`n\";", @@ -1804,7 +1810,7 @@ "$_value = $PSCompletions.config.comp_config.$completion.enable_hooks;", "if($_value -ne $null){ \"当前值: <@Magenta>$_value<@Blue>`n\" }", "}}", - "设置是否启用 hooks,默认为 <@Magenta>1<@Blue>。\n", + "设置是否启用 hooks。\n", "hooks 将解析或生成补全数据,然后动态添加到命令补全中。\n", "你可以通过运行 <@Magenta>psc completion {{ $completion }} enable_hooks 0<@Blue> 来禁用它。" ] @@ -1934,18 +1940,5 @@ ] } } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/scoop/guid.txt b/completions/scoop/guid.txt index 1ef03c0e..be5e0650 100644 --- a/completions/scoop/guid.txt +++ b/completions/scoop/guid.txt @@ -1 +1 @@ -0f821390-b8db-48a3-8545-727484c11633 +32669e0d-e174-442a-a5f5-3f4df843c40d diff --git a/completions/scoop/language/en-US.json b/completions/scoop/language/en-US.json index 07510ca5..1b3f704b 100644 --- a/completions/scoop/language/en-US.json +++ b/completions/scoop/language/en-US.json @@ -838,18 +838,5 @@ ] } } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/scoop/language/zh-CN.json b/completions/scoop/language/zh-CN.json index 78b57e57..c97cd8c1 100644 --- a/completions/scoop/language/zh-CN.json +++ b/completions/scoop/language/zh-CN.json @@ -836,18 +836,5 @@ ] } } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/volta/guid.txt b/completions/volta/guid.txt index 21912e64..9ccd5c65 100644 --- a/completions/volta/guid.txt +++ b/completions/volta/guid.txt @@ -1 +1 @@ -39f6401d-5557-408f-b8f1-bd370cc5b7fe +8c5a3a06-2ebc-42a0-84c4-ca144439302f diff --git a/completions/volta/language/en-US.json b/completions/volta/language/en-US.json index eb080955..952c4138 100644 --- a/completions/volta/language/en-US.json +++ b/completions/volta/language/en-US.json @@ -220,18 +220,5 @@ "volta - Accessible JavaScript Tool Manager." ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/volta/language/zh-CN.json b/completions/volta/language/zh-CN.json index 09d4ef7f..5318f9dd 100644 --- a/completions/volta/language/zh-CN.json +++ b/completions/volta/language/zh-CN.json @@ -220,18 +220,5 @@ "volta - 无障碍 JavaScript 工具管理器。" ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/wsl/guid.txt b/completions/wsl/guid.txt index 1dba44cd..9b3ff626 100644 --- a/completions/wsl/guid.txt +++ b/completions/wsl/guid.txt @@ -1 +1 @@ -7a7da543-2de0-4549-a1fd-336be3f3b5d1 +92545ca7-d936-4265-923a-24d8880aa0e2 diff --git a/completions/wsl/language/en-US.json b/completions/wsl/language/en-US.json index afe2f535..25d07e6b 100644 --- a/completions/wsl/language/en-US.json +++ b/completions/wsl/language/en-US.json @@ -488,18 +488,5 @@ "--unregister": "Unregister distribution -- {{$Distro}}", "--export": "Export distribution -- {{ $Distro }}" } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/wsl/language/zh-CN.json b/completions/wsl/language/zh-CN.json index cf712f68..0053de98 100644 --- a/completions/wsl/language/zh-CN.json +++ b/completions/wsl/language/zh-CN.json @@ -484,18 +484,5 @@ "--unregister": "注销分发 -- {{$Distro}}", "--export": "导出分发 -- {{ $Distro }}" } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/yarn/guid.txt b/completions/yarn/guid.txt index 2e0455d5..60ff1ab2 100644 --- a/completions/yarn/guid.txt +++ b/completions/yarn/guid.txt @@ -1 +1 @@ -dda318e9-d66a-45ed-8cc8-a10644725d28 +e071c616-363a-43bf-ae71-71410a54a946 diff --git a/completions/yarn/language/en-US.json b/completions/yarn/language/en-US.json index c3ee24b9..df2df6ba 100644 --- a/completions/yarn/language/en-US.json +++ b/completions/yarn/language/en-US.json @@ -637,18 +637,5 @@ "yarn - package manager." ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/completions/yarn/language/zh-CN.json b/completions/yarn/language/zh-CN.json index 49aac748..4b53d2f1 100644 --- a/completions/yarn/language/zh-CN.json +++ b/completions/yarn/language/zh-CN.json @@ -637,18 +637,5 @@ "yarn - 软件包管理器。" ] } - }, - "config": [ - { - "name": "enable_hooks", - "value": 1, - "values": [ - 1, - 0 - ], - "tip": [ - "{{ $PSCompletions.info.completion.enable_hooks.tip }}" - ] - } - ] + } } diff --git a/module/CHANGELOG-CN.md b/module/CHANGELOG-CN.md index ecb84c71..fc4d7140 100644 --- a/module/CHANGELOG-CN.md +++ b/module/CHANGELOG-CN.md @@ -3,6 +3,13 @@ 简体中文
+## 5.2.2 (2024/12/18) + +- 优化菜单的显示,让菜单项和命令帮助的显示更合理。 +- 优化 PowerShell 内置命令的帮助提示显示效果。 +- 修复模块更新后可能导致所有配置项被重置的问题。 +- 其他修复与优化。 + ## 5.2.1 (2024/12/13) - 修复了由于解析补全时的错误导致部分补全项缺失的问题。 diff --git a/module/CHANGELOG.json b/module/CHANGELOG.json index 052e52cc..faf0fc61 100644 --- a/module/CHANGELOG.json +++ b/module/CHANGELOG.json @@ -1,4 +1,23 @@ [ + { + "version": "5.2.2", + "info": { + "zh-CN": [ + "更新(2024/12/18)\n", + "- 优化菜单的显示,让菜单项和命令帮助的显示更合理。\n", + "- 优化 PowerShell 内置命令的帮助提示显示效果。\n", + "- 修复模块更新后可能导致所有配置项被重置的问题。\n", + "- 其他修复与优化。\n" + ], + "en-US": [ + "Update(2024/12/18)\n", + "- Optimize the display of menus, making the menu items and command help more reasonable.\n", + "- Optimize the display effect of PowerShell built-in command help prompts.\n", + "- Fix a bug where all configuration items may be reset after module updates.\n", + "- Other fixes and optimizations.\n" + ] + } + }, { "version": "5.2.1", "info": { diff --git a/module/CHANGELOG.md b/module/CHANGELOG.md index 68584885..b61ed152 100644 --- a/module/CHANGELOG.md +++ b/module/CHANGELOG.md @@ -3,6 +3,13 @@ English +## 5.2.2 (2024/12/18) + +- Optimize the display of menus, making the menu items and command help more reasonable. +- Optimize the display effect of PowerShell built-in command help prompts. +- Fix a bug where all configuration items may be reset after module updates. +- Other fixes and optimizations. + ## 5.2.1 (2024/12/13) - Fix an issue where some completion items were missing due to errors during parsing of completions. diff --git a/module/PSCompletions/PSCompletions.psd1 b/module/PSCompletions/PSCompletions.psd1 index f53fcad3..585ab801 100644 --- a/module/PSCompletions/PSCompletions.psd1 +++ b/module/PSCompletions/PSCompletions.psd1 @@ -9,7 +9,7 @@ @{ RootModule = 'PSCompletions.psm1' - ModuleVersion = '5.2.1' + ModuleVersion = '5.2.2' GUID = '00929632-527d-4dab-a5b3-21197faccd05' diff --git a/module/PSCompletions/PSCompletions.psm1 b/module/PSCompletions/PSCompletions.psm1 index 92264b07..1b8fa297 100644 --- a/module/PSCompletions/PSCompletions.psm1 +++ b/module/PSCompletions/PSCompletions.psm1 @@ -513,7 +513,7 @@ Set-Item -Path Function:$($PSCompletions.config.function_name) -Value { if ($PSCompletions.config.comp_config[$_]) { $_keys = @() foreach ($k in $PSCompletions.config.comp_config.$_.keys) { - if ($k -notin $config_list) { + if ($k -notin $config_list -and $k -ne 'enable_hooks') { $_keys += $k } } @@ -883,8 +883,8 @@ Set-Item -Path Function:$($PSCompletions.config.function_name) -Value { param([string]$cmd) $PSCompletions.config.comp_config[$cmd] = @{} $path = "$($PSCompletions.path.completions)/$cmd/config.json" - $json = $PSCompletions.get_raw_content($path) | ConvertFrom-Json - $path = "$($PSCompletions.path.completions)/$cmd/language/$($json.language[0]).json" + $json_config = $PSCompletions.get_raw_content($path) | ConvertFrom-Json + $path = "$($PSCompletions.path.completions)/$cmd/language/$($json_config.language[0]).json" $json = $PSCompletions.ConvertFrom_JsonToHashtable($PSCompletions.get_raw_content($path)) foreach ($item in $json.config) { $PSCompletions.config.comp_config[$cmd].$($item.name) = $item.value @@ -905,6 +905,15 @@ Set-Item -Path Function:$($PSCompletions.config.function_name) -Value { }) } } + if ($json_config.hooks -ne $null) { + $PSCompletions.config.comp_config[$cmd].enable_hooks = [int]$json_config.hooks + $change_list.Add(@{ + cmd = $cmd + item = $item + old_value = $old_comp_config[$cmd].enable_hooks + new_value = [int]$json_config.hooks + }) + } $PSCompletions._need_update_data = $true } $old_comp_config = $PSCompletions.config.comp_config.Clone() diff --git a/module/PSCompletions/core/completion/win.ps1 b/module/PSCompletions/core/completion/win.ps1 index b70a112c..e1b1a7e1 100644 --- a/module/PSCompletions/core/completion/win.ps1 +++ b/module/PSCompletions/core/completion/win.ps1 @@ -52,7 +52,30 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod generate_complet return } - $filter_list = $completion.CompletionMatches + $filter_list = @() + foreach ($item in $completion.CompletionMatches) { + # XXX: 像 Get-*、Set-* 等内置命令的 ToolTip 会在每个示例的开头添加 4 个(空格或换行),末尾添加 2 个(空格或换行) + # 因此,其他命令的 tip 不能在开头添加 4 个(空格或换行),或者末尾添加 2 个(空格或换行) + # 这里通过 \s 去匹配空格或换行 + $tip = @() + if ($item.ToolTip -match "^\s{4,}" -or $item.ToolTip -match ".*\s{2,}$") { + foreach ($i in $item.ToolTip -split "\s{4,}") { + $t = $i.Trim() + if ($t) { + $tip += "$t`n`n" + } + } + } + else { + $tip += $item.ToolTip + } + + $filter_list += @{ + CompletionText = $item.CompletionText + ListItemText = $item.ListItemText + ToolTip = $tip + } + } if ($PSCompletions.config.enable_completions_sort -eq 1 -and (Get-Command $alias -ErrorAction SilentlyContinue)) { $path_order = "$($PSCompletions.path.order)/$root.json" diff --git a/module/PSCompletions/core/init.ps1 b/module/PSCompletions/core/init.ps1 index 55d05a85..93292832 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.2.1' + version = '5.2.2' path = @{ root = $_ completions = Join-Path $_ 'completions' @@ -580,8 +580,9 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod get_language { $content_config = $PSCompletions.get_raw_content($path_config) | ConvertFrom-Json if (!$content_config.language) { - $PSCompletions.download_file("$($PSCompletions.url)/completions/$completion/config.json", $path_config) + $PSCompletions.download_file("completions/$completion/config.json", $path_config, $PSCompletions.urls) $content_config = $PSCompletions.get_raw_content($path_config) | ConvertFrom-Json + $content_config | ConvertTo-Json -Compress -Depth 100 | Out-File $path_config -Encoding utf8 -Force } if ($PSCompletions.config.comp_config[$completion].language) { $config_language = $PSCompletions.config.comp_config.$completion.language @@ -775,59 +776,92 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod download_list { @{ list = @('psc') } | ConvertTo-Json -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force } $current_list = ($PSCompletions.get_raw_content($PSCompletions.path.completions_json) | ConvertFrom-Json).list - try { - $content = (Invoke-WebRequest -Uri "$($PSCompletions.url)/completions.json").Content | ConvertFrom-Json + $isErr = $true + foreach ($url in $PSCompletions.urls) { + try { + $content = (Invoke-WebRequest -Uri "$url/completions.json").Content | ConvertFrom-Json - $remote_list = $content.list + $remote_list = $content.list - $diff = Compare-Object $remote_list $current_list -PassThru - if ($diff) { - if ($PSCompletions.is_update_init) { - $diff = '' + $diff = Compare-Object $remote_list $current_list -PassThru + if ($diff) { + $diff | Out-File $PSCompletions.path.change -Force -Encoding utf8 + $content | ConvertTo-Json -Depth 100 -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force + $PSCompletions.list = $remote_list + } + else { + Clear-Content $PSCompletions.path.change -Force + $PSCompletions.list = $current_list } - $diff | Out-File $PSCompletions.path.change -Force -Encoding utf8 - $content | ConvertTo-Json -Depth 100 -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force - $PSCompletions.list = $remote_list + $isErr = $false + return $remote_list } - else { - Clear-Content $PSCompletions.path.change -Force + catch { $PSCompletions.list = $current_list + $PSCompletions._invalid_url += "`n$url/completions.json" } - return $remote_list } - catch { - $PSCompletions.list = $current_list - $PSCompletions._invalid_url = "$($PSCompletions.url)/completions.json" + if ($isErr) { + $PSCompletions._invalid_url = $PSCompletions._invalid_url | Sort-Object -Unique return $false } } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod download_file { - param([string]$url, [string]$file) - try { - $PSCompletions.wc.DownloadFile($url, $file) - } - catch { - if ($PSCompletions.info) { - $download_info = @{ + param( + [string]$path, # 相对于 $baseUrl 的文件路径 + [string]$file, + [array]$baseUrl + ) + + $isErr = $true + $errList = @() + + for ($i = 0; $i -lt $baseUrl.Count; $i++) { + $item = $baseUrl[$i] + $url = $item + '/' + $path + try { + $PSCompletions.wc.DownloadFile($url, $file) + $isErr = $false + break + } + catch { + $errList += @{ url = $url file = $file + err = $_ } - $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.err.download_file)) } - else { - Write-Host "File ($(Split-Path $url -Leaf)) download failed, please check your network connection or try again later." -ForegroundColor Red - Write-Host "File download Url: $url" -ForegroundColor Red - 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 + } + if ($isErr) { + for ($i = 0; $i -lt $errList.Count; $i++) { + $item = $errList[$i] + if ($PSCompletions.info) { + $download_info = @{ + url = $item.url + file = $item.file + } + $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.err.download_file)) + } + else { + Write-Host "File ($(Split-Path $item.url -Leaf)) download failed, please check your network connection or try again later." -ForegroundColor Red + Write-Host "File download Url: $($item.url)" -ForegroundColor Red + Write-Host "File save path: $($item.file)" -ForegroundColor Red + Write-Host "If you are sure that it is not a network problem, please submit an issue`n" -ForegroundColor Red + } + if ($i -eq $errList.Count - 1) { + throw $item.err + } + else { + Write-Host $item.err -f Red + } + Write-Host '' } - throw $_ } } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { param ( [string]$completion, - [bool]$log = $true, - [bool]$is_update = $true + [bool]$log = $true ) $PSCompletions._has_add_completion = $log -and $true @@ -835,9 +869,9 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { $PSCompletions.completions_data[$completion] = $null $PSCompletions.completions[$completion] = $null - $url = "$($PSCompletions.url)/completions/$completion" + $url = "completions/$completion" - $is_update = (Test-Path "$($PSCompletions.path.completions)/$completion") -and $is_update + $is_update = Test-Path "$($PSCompletions.path.completions)/$completion" $completion_dir = Join-Path $PSCompletions.path.completions $completion $language_dir = Join-Path $completion_dir 'language' @@ -850,7 +884,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { url = "$url/config.json" file = Join-Path $completion_dir 'config.json' } - $PSCompletions.download_file($download_info.url, $download_info.file) + $PSCompletions.download_file($download_info.url, $download_info.file, $PSCompletions.urls) $config = $PSCompletions.get_raw_content("$completion_dir/config.json") | ConvertFrom-Json $config | ConvertTo-Json -Compress -Depth 100 | Out-File $download_info.file -Encoding utf8 -Force @@ -867,7 +901,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { OutFile = Join-Path $language_dir "$_.json" } } - if ($config.hooks) { + if ($config.hooks -ne $null) { $files += @{ Uri = "$url/hooks.ps1" OutFile = Join-Path $completion_dir 'hooks.ps1' @@ -880,14 +914,14 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { file = $file.OutFile } try { - $PSCompletions.wc.DownloadFile($download_info.url, $download_info.file) + $PSCompletions.download_file($download_info.url, $download_info.file, $PSCompletions.urls) if ($download_info.file -match '\.json$') { $PSCompletions.get_raw_content($download_info.file) | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100 | Out-File $download_info.file -Encoding utf8 -Force } } catch { Remove-Item $completion_dir -Force -Recurse -ErrorAction SilentlyContinue - throw + throw $_ } } @@ -960,6 +994,14 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion { } } } + if ($config.hooks -ne $null) { + if (!$PSCompletions.config.comp_config[$completion]) { + $PSCompletions.config.comp_config.$completion = @{} + } + if ($PSCompletions.config.comp_config.$completion.enable_hooks -eq $null) { + $PSCompletions.config.comp_config.$completion.enable_hooks = [int]$config.hooks + } + } } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod new_data { $data = @{ @@ -1012,23 +1054,25 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod init_data { if ($PSCompletions.config.url) { $PSCompletions.url = $PSCompletions.config.url + $PSCompletions.urls = @($PSCompletions.config.url) } else { if ($PSCompletions.language -eq 'zh-CN') { $PSCompletions.url = 'https://gitee.com/abgox/PSCompletions/raw/main' + $PSCompletions.urls = @('https://gitee.com/abgox/PSCompletions/raw/main', 'https://raw.githubusercontent.com/abgox/PSCompletions/main') } else { $PSCompletions.url = 'https://raw.githubusercontent.com/abgox/PSCompletions/main' + $PSCompletions.urls = @('https://raw.githubusercontent.com/abgox/PSCompletions/main', 'https://gitee.com/abgox/PSCompletions/raw/main') } } $PSCompletions.list = ($PSCompletions.get_raw_content($PSCompletions.path.completions_json) | ConvertFrom-Json).list $PSCompletions.update = $PSCompletions.get_content($PSCompletions.path.update) - if ('psc' -notin $PSCompletions.data.list -or $PSCompletions.is_init) { - $PSCompletions.add_completion('psc', $false, $false) + if ('psc' -notin $PSCompletions.data.list) { + $PSCompletions.add_completion('psc', $false) $PSCompletions.data | ConvertTo-Json -Depth 100 -Compress | Out-File $PSCompletions.path.data -Force -Encoding utf8 - $PSCompletions.is_init = $null } if ($PSCompletions.completions.psc.info) { $PSCompletions.info = $PSCompletions.completions.psc.info @@ -1084,7 +1128,6 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_powers } } } - Add-Member -InputObject $PSCompletions -MemberType ScriptMethod argc_completions { param( [array]$completions, # The list of completions. @@ -1131,15 +1174,6 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod argc_completions } if (!(Test-Path $PSCompletions.path.temp)) { - $PSCompletions.data = $PSCompletions.ConvertFrom_JsonToHashtable($PSCompletions.get_raw_content($PSCompletions.path.data)) - $PSCompletions.is_init = $true - if ($PSCompletions.data.config) { - $PSCompletions.is_update_init = $true - } - else { - $PSCompletions.is_first_init = $true - $PSCompletions.data = $null - } $PSCompletions.ensure_dir($PSCompletions.path.temp) $PSCompletions.ensure_dir($PSCompletions.path.order) Add-Member -InputObject $PSCompletions -MemberType ScriptMethod move_old_version { @@ -1204,38 +1238,64 @@ if (!(Test-Path $PSCompletions.path.temp)) { } } else { - if ($PSUICulture -eq 'zh-CN') { - $language = 'zh-CN' - $PSCompletions.url = 'https://gitee.com/abgox/PSCompletions/raw/main' + if (Test-Path $PSCompletions.path.data) { + $data = $PSCompletions.get_raw_content($PSCompletions.path.data) | ConvertFrom-Json + if (!$data.config) { + $PSCompletions.is_first_init = $true + } } else { - $language = 'en-US' - $PSCompletions.url = 'https://raw.githubusercontent.com/abgox/PSCompletions/main' + $PSCompletions.is_first_init = $true } + } + if ($PSUICulture -eq 'zh-CN') { + $language = 'zh-CN' + $urls = @('https://gitee.com/abgox/PSCompletions/raw/main', 'https://raw.githubusercontent.com/abgox/PSCompletions/main') + } + else { + $language = 'en-US' + $urls = @('https://raw.githubusercontent.com/abgox/PSCompletions/main', 'https://gitee.com/abgox/PSCompletions/raw/main') + } - $PSCompletions.ensure_dir("$($PSCompletions.path.completions)/psc/language") + $PSCompletions.ensure_dir("$($PSCompletions.path.completions)/psc") + $PSCompletions.ensure_dir("$($PSCompletions.path.completions)/psc/language") - $file_list = @('language/zh-CN.json', 'language/en-US.json', 'config.json', 'guid.txt', 'hooks.ps1') - foreach ($_ in $file_list) { - $outFile = "$($PSCompletions.path.completions)/psc/$_" - $PSCompletions.download_file("$($PSCompletions.url)/completions/psc/$_", $outFile) - if ($outFile -match '\.json$') { - $PSCompletions.get_raw_content($outFile) | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100 | Out-File $outFile -Encoding utf8 -Force - } + $path_config = "$($PSCompletions.path.completions)/psc/config.json" + + $PSCompletions.download_file("completions/psc/config.json", $path_config, $urls) + + $config = $PSCompletions.get_raw_content($path_config) | ConvertFrom-Json + $config | ConvertTo-Json -Compress -Depth 100 | Out-File $path_config -Encoding utf8 -Force + + $file_list = @('guid.txt') + if ($config.hooks -ne $null) { + $file_list += 'hooks.ps1' + } + foreach ($lang in $config.language) { + $file_list += "language/$lang.json" + } + foreach ($_ in $file_list) { + $outFile = "$($PSCompletions.path.completions)/psc/$_" + $PSCompletions.download_file("completions/psc/$_", $outFile, $urls) + if ($outFile -match '\.json$') { + $PSCompletions.get_raw_content($outFile) | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100 | Out-File $outFile -Encoding utf8 -Force } - $PSCompletions.info = $PSCompletions.ConvertFrom_JsonToHashtable($PSCompletions.get_raw_content("$($PSCompletions.path.completions)/psc/language/$language.json")).info } + $PSCompletions.info = $PSCompletions.ConvertFrom_JsonToHashtable($PSCompletions.get_raw_content("$($PSCompletions.path.completions)/psc/language/$language.json")).info } $PSCompletions.move_old_version() - if ($PSCompletions.is_first_init) { - $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.init_info)) - } + $PSCompletions.is_init = $true } $PSCompletions.init_data() if ($PSCompletions.is_init) { $null = $PSCompletions.download_list() + if ($PSCompletions.is_first_init) { + $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.init_info)) + $PScompletions.list | Out-File $PSCompletions.path.change -Encoding utf8 -Force + $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.update_info)) + } } if (!$PSCompletions.config.enable_menu) { Set-PSReadLineKeyHandler $PSCompletions.config.trigger_key MenuComplete @@ -1257,7 +1317,7 @@ foreach ($_ in $PSCompletions.data.aliasMap.Keys) { 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.temp 'CHANGELOG.json')) + $PSCompletions.download_file("module/CHANGELOG.json", (Join-Path $PSCompletions.path.temp 'CHANGELOG.json'), $PSCompletions.urls) # XXX: 这里是为了避免 CompletionPredictor 模块引起的多次确认 if (!$PSCompletions._write_update_confirm) { @@ -1301,11 +1361,12 @@ else { if (!$PSCompletions._show_update_info) { $PSCompletions._show_update_info = $true if ($PSCompletions.config.enable_completions_update -eq 1) { - if ($PSCompletions.update -or $PSCompletions.get_content($PSCompletions.path.change)) { + if (($PSCompletions.update -or $PSCompletions.get_content($PSCompletions.path.change) -and !$PScompletions.is_init)) { $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.update_info)) } } } } +$PSCompletions.is_init = $null $PSCompletions.start_job() diff --git a/module/PSCompletions/core/menu/win.ps1 b/module/PSCompletions/core/menu/win.ps1 index 653686d9..5d2c4dcf 100644 --- a/module/PSCompletions/core/menu/win.ps1 +++ b/module/PSCompletions/core/menu/win.ps1 @@ -13,19 +13,19 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod handle_list if ($PSCompletions.menu.is_show_tip) { function Get-MultilineTruncatedString { param ([string]$inputString, $Host_UI = $Host.UI) - + $lineWidth = $Host_UI.RawUI.BufferSize.Width - + if ($PSCompletions.config.enable_tip_follow_cursor -eq 1) { $lineWidth -= $Host_UI.RawUI.CursorPosition.X } - + $currentWidth = 0 $outputString = '' $currentLine = '' - + $char_record = @{} - + foreach ($char in $inputString.ToCharArray()) { if ($char_record.ContainsKey($char)) { $charWidth = $char_record[$char] @@ -34,7 +34,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod handle_list $charWidth = $Host_UI.RawUI.NewBufferCellArray($char, $Host_UI.RawUI.BackgroundColor, $Host_UI.RawUI.BackgroundColor).LongLength $char_record[$char] = $charWidth } - + if ($currentWidth + $charWidth -gt $lineWidth) { $outputString += $currentLine + "`n" $currentLine = '' @@ -44,7 +44,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod handle_list $currentWidth += $charWidth } $outputString += $currentLine - + return $outputString } function _replace { @@ -81,7 +81,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod handle_list $return = @() if ($PSCompletions.menu.is_show_tip) { foreach ($result in $results) { - $PSCompletions.menu.tip_height_list += $result.ToolTip.Count + $PSCompletions.menu.tip_max_height = [Math]::Max($PSCompletions.menu.tip_max_height, $result.ToolTip.Count) $PSCompletions.menu.list_max_width = [Math]::Max($PSCompletions.menu.list_max_width, $PSCompletions.menu.get_length($result.ListItemText)) $return += @{ ListItemText = $result.ListItemText @@ -164,7 +164,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod handle_list $tip_arr += (Get-MultilineTruncatedString $v $Host.UI) -split "`n" } } - $PSCompletions.menu.tip_height_list += $tip_arr.Count + $PSCompletions.menu.tip_max_height = [Math]::Max($PSCompletions.menu.tip_max_height, $tip_arr.Count) $PSCompletions.menu.list_max_width = [Math]::Max($PSCompletions.menu.list_max_width, $PSCompletions.menu.get_length($item.ListItemText)) $results += @{ ListItemText = $item.ListItemText @@ -211,6 +211,25 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod parse_list if ($PSCompletions.menu.is_show_above) { if ($PSCompletions.config.list_max_count_when_above -eq -1) { $PSCompletions.menu.ui_size.Height = [Math]::Min($PSCompletions.menu.cursor_to_top, $PSCompletions.menu.ui_size.Height) + + $menu_limit = 4 + # 8: 2个补全项 + 3行提示的高度 + $rest = [Math]::Max(0, $PSCompletions.menu.cursor_to_top - 8) + if ($rest -le 1) { + $menu_limit += $rest + ($PSCompletions.menu.cursor_to_top -gt 4) + } + else { + $r = $rest / 2 + if ($r -gt $PSCompletions.menu.tip_max_height) { + $menu_limit += $rest - $PSCompletions.menu.tip_max_height + } + else { + $menu_limit += $r + } + } + if ($PSCompletions.menu.cursor_to_top -eq 6) { + $menu_limit = 4 + } } else { $PSCompletions.menu.ui_size.Height = [Math]::Min($PSCompletions.menu.cursor_to_top, $PSCompletions.config.list_max_count_when_above + 2) @@ -219,6 +238,29 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod parse_list else { if ($PSCompletions.config.list_max_count_when_below -eq -1) { $PSCompletions.menu.ui_size.Height = [Math]::Min($PSCompletions.menu.cursor_to_bottom, $PSCompletions.menu.ui_size.Height) + + $menu_limit = 4 + # 8: 2个补全项 + 3行提示的高度 + $rest = [Math]::Max(0, $PSCompletions.menu.cursor_to_bottom - 8) + if ($rest -le 1) { + $menu_limit += $rest + ($PSCompletions.menu.cursor_to_bottom -gt 4) + $PSCompletions.menu.tip_max_height = $PSCompletions.menu.cursor_to_bottom - $menu_limit - 1 + if ($PSCompletions.menu.tip_max_height -lt 1) { + $PSCompletions.menu.tip_max_height = 1 + } + } + else { + $r = $rest / 2 + if ($r -gt $PSCompletions.menu.tip_max_height) { + $menu_limit += $rest - $PSCompletions.menu.tip_max_height + } + else { + $menu_limit += $r + } + } + if ($PSCompletions.menu.cursor_to_bottom -eq 6) { + $menu_limit = 4 + } } else { $PSCompletions.menu.ui_size.Height = [Math]::Min($PSCompletions.menu.cursor_to_bottom, $PSCompletions.config.list_max_count_when_below + 2) @@ -226,71 +268,37 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod parse_list } if ($PSCompletions.menu.is_show_tip) { - $max = 1 - foreach ($i in $PSCompletions.menu.tip_height_list) { - $max = [Math]::Max($i, $max) - } - $PSCompletions.menu.tip_max_height = $max - - function handle_menu { - if ($PSCompletions.menu.is_show_above) { - $height = $PSCompletions.menu.cursor_to_top - $PSCompletions.menu.tip_max_height - 3 - if ($height -lt 4) { - $PSCompletions.menu.ui_size.Height = [Math]::Min($PSCompletions.menu.cursor_to_top, $PSCompletions.menu.ui_size.Height) - $PSCompletions.menu.pos.Y = $Host.UI.RawUI.CursorPosition.Y - $PSCompletions.menu.ui_size.Height - $PSCompletions.config.height_from_menu_bottom_to_cursor_when_above - } - else { - $new_ui_height = [Math]::Min($height, $PSCompletions.menu.ui_size.Height) - $PSCompletions.menu.ui_size.Height = [Math]::Min($new_ui_height, $PSCompletions.menu.filter_list.Count + 2) - $PSCompletions.menu.pos.Y = $Host.UI.RawUI.CursorPosition.Y - $PSCompletions.menu.ui_size.Height - $PSCompletions.config.height_from_menu_bottom_to_cursor_when_above - } - $PSCompletions.menu.pos_tip.Y = $PSCompletions.menu.pos.Y - $PSCompletions.menu.tip_max_height - 1 - if ($PSCompletions.menu.pos_tip.Y -lt 0) { - if ($PSCompletions.menu.tip_max_height -gt 1) { - $max = 1 - foreach ($i in $PSCompletions.menu.tip_height_list.Where({ $_ -lt $PSCompletions.menu.tip_max_height })) { - $max = [Math]::Max($i, $max) - } - $PSCompletions.menu.tip_max_height = $max - handle_menu - } - else { - $PSCompletions.menu.is_show_tip = $false - } - } + $menu_limit = [Math]::Min($menu_limit, $PSCompletions.menu.ui_size.Height) + if ($PSCompletions.menu.is_show_above) { + if ($menu_limit -gt 12 -and ($PSCompletions.menu.cursor_to_top - $menu_limit) -lt $PSCompletions.menu.tip_max_height) { + $menu_limit = 12 + $PSCompletions.menu.tip_max_height = $PSCompletions.menu.cursor_to_top - $menu_limit - 1 } else { - $height = $PSCompletions.menu.cursor_to_bottom - $PSCompletions.menu.tip_max_height - 3 - if ($height -lt 4) { - $PSCompletions.menu.ui_size.Height = [Math]::Min($PSCompletions.menu.cursor_to_bottom, $PSCompletions.menu.ui_size.Height) - $PSCompletions.menu.pos.Y = $Host.UI.RawUI.CursorPosition.Y + 1 - } - else { - $new_ui_height = [Math]::Min($height, $PSCompletions.menu.ui_size.Height) - $PSCompletions.menu.ui_size.Height = [Math]::Min($new_ui_height, $PSCompletions.menu.filter_list.Count + 2) - $PSCompletions.menu.pos.Y = $Host.UI.RawUI.CursorPosition.Y + 1 - } - $PSCompletions.menu.pos_tip.Y = $PSCompletions.menu.pos.Y + $PSCompletions.menu.ui_size.Height + 1 - if ($PSCompletions.menu.pos_tip.Y -ge $Host.UI.RawUI.BufferSize.Height) { - $PSCompletions.menu.pos_tip.Y = [Math]::Min($PSCompletions.menu.pos_tip.Y, $Host.UI.RawUI.BufferSize.Height - 1) - if ($PSCompletions.menu.tip_max_height -gt 1) { - $max = 1 - foreach ($i in $PSCompletions.menu.tip_height_list.Where({ $_ -lt $PSCompletions.menu.tip_max_height })) { - $max = [Math]::Max($i, $max) - } - $PSCompletions.menu.tip_max_height = $max - handle_menu - } - else { - $PSCompletions.menu.is_show_tip = $false - } - } - else { - $PSCompletions.menu.tip_max_height = $Host.UI.RawUI.BufferSize.Height - $PSCompletions.menu.pos.Y - $PSCompletions.menu.ui_size.Height - 1 + # tip 可以显示的高度 + $rest = $PSCompletions.menu.cursor_to_top - $menu_limit - 1 + if ($PSCompletions.menu.tip_max_height -gt $rest) { + $PSCompletions.menu.tip_max_height = [Math]::Max(1, $rest) } } + $PSCompletions.menu.ui_size.Height = [Math]::Min($menu_limit, $PSCompletions.menu.ui_size.Height) + $PSCompletions.menu.pos.Y = $Host.UI.RawUI.CursorPosition.Y - $PSCompletions.menu.ui_size.Height - $PSCompletions.config.height_from_menu_bottom_to_cursor_when_above + $PSCompletions.menu.pos_tip.Y = $PSCompletions.menu.pos.Y - $PSCompletions.menu.tip_max_height - 1 + if ($PSCompletions.menu.pos_tip.Y -lt 0) { + $PSCompletions.menu.is_show_tip = $false + } + } + else { + if ($menu_limit -gt 12 -and ($PSCompletions.menu.cursor_to_bottom - $menu_limit) -lt $PSCompletions.menu.tip_max_height) { + $menu_limit = 12 + } + $PSCompletions.menu.ui_size.Height = [Math]::Min($menu_limit, $PSCompletions.menu.ui_size.Height) + $PSCompletions.menu.pos.Y = $Host.UI.RawUI.CursorPosition.Y + 1 + $PSCompletions.menu.pos_tip.Y = $PSCompletions.menu.pos.Y + $PSCompletions.menu.ui_size.Height + 1 + if ($PSCompletions.menu.pos_tip.Y -gt $Host.UI.RawUI.BufferSize.Height - 1) { + $PSCompletions.menu.is_show_tip = $false + } } - handle_menu } else { if ($PSCompletions.menu.is_show_above) { @@ -588,12 +596,12 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod new_tip_buf $tip = $PSCompletions.menu.filter_list[$index].ToolTip if ($PSCompletions.menu.tip_max_height -eq 1) { if ($tip.Count -gt 1) { - $tip = "$($tip[0])..." + $tip = $tip[0] } } else { if ($tip.Count -gt $PSCompletions.menu.tip_max_height) { - $tip = $tip[0..($PSCompletions.menu.tip_max_height - 2)] + "$($tip[$PSCompletions.menu.tip_max_height - 1])..." + $tip = $tip[0..($PSCompletions.menu.tip_max_height - 1)] } } $Host.UI.RawUI.SetBufferContents($pos, $Host.UI.RawUI.NewBufferCellArray($tip, $PSCompletions.config.tip_text, $PSCompletions.config.tip_back)) @@ -801,9 +809,8 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_module $PSCompletions.menu.pos = @{ X = 0; Y = 0 } $PSCompletions.menu.pos_tip = @{ X = 0; Y = 0 } - $PSCompletions.menu.list_max_width = 0 - $PSCompletions.menu.tip_max_height = 0 - $PSCompletions.menu.tip_height_list = @() + $PSCompletions.menu.list_max_width = $PSCompletions.config.list_min_width + $PSCompletions.menu.tip_max_height = 1 $PSCompletions.menu.ui_size = $Host.UI.RawUI.BufferSize $PSCompletions.menu.filter = '' # 过滤的关键词 @@ -830,14 +837,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_module $PSCompletions.menu.cursor_to_bottom = $Host.UI.RawUI.BufferSize.Height - 1 - $Host.UI.RawUI.CursorPosition.Y $PSCompletions.menu.cursor_to_top = $Host.UI.RawUI.CursorPosition.Y - $PSCompletions.config.height_from_menu_bottom_to_cursor_when_above - 1 - if ($PSCompletions.menu.cursor_to_bottom -ge $PSCompletions.menu.cursor_to_top) { - $PSCompletions.menu.is_show_above = $false - $PSCompletions.menu.rest_height = $PSCompletions.menu.cursor_to_bottom - $PSCompletions.menu.cursor_to_top - 1 - } - else { - $PSCompletions.menu.is_show_above = $true - $PSCompletions.menu.rest_height = $PSCompletions.menu.cursor_to_top - $PSCompletions.menu.cursor_to_bottom - 1 - } + $PSCompletions.menu.is_show_above = $PSCompletions.menu.cursor_to_bottom -lt $PSCompletions.menu.cursor_to_top $PSCompletions.menu.parse_list() diff --git a/module/PSCompletions/core/utils/Core.ps1 b/module/PSCompletions/core/utils/Core.ps1 index 9e39a96a..3f99af76 100644 --- a/module/PSCompletions/core/utils/Core.ps1 +++ b/module/PSCompletions/core/utils/Core.ps1 @@ -32,30 +32,51 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { } if ($data -match $pattern) { (replace_content $data) }else { return $data } } + function download_file { + param( + [string]$path, # 相对于 $baseUrl 的文件路径 + [string]$file, + [array]$baseUrl + ) + $isErr = $true + for ($i = 0; $i -lt $baseUrl.Count; $i++) { + $item = $baseUrl[$i] + $url = $item + '/' + $path + try { + $wc.DownloadFile($url, $file) + $isErr = $false + break + } + catch {} + } + if ($isErr) { + throw + } + } function ensure_dir { param([string]$path) if (!(Test-Path $path)) { New-Item -ItemType Directory $path > $null } } function ensure_psc { - $url = "$($PSCompletions.url)/completions/psc" - # XXX: language - # $language = if ($PSCompletions.language -eq 'zh-CN') { 'zh-CN' }else { 'en-US' } - ensure_dir "$($PSCompletions.path.completions)/psc" ensure_dir "$($PSCompletions.path.completions)/psc/language" - $path_list = @( - # "psc/language/$language.json", - "psc/language/zh-CN.json", - "psc/language/en-US.json", - "psc/config.json", - "psc/guid.txt", - "psc/hooks.ps1" - ) - foreach ($path in $path_list) { - $path_file = "$($PSCompletions.path.completions)/$path" + $path_config = "$($PSCompletions.path.completions)/psc/config.json" + download_file "completions/psc/config.json" $path_config $PSCompletions.urls + + $config = get_raw_content $path_config | ConvertFrom-Json + + $file_list = @('guid.txt') + if ($config.hooks -ne $null) { + $file_list += 'hooks.ps1' + } + foreach ($lang in $config.language) { + $file_list += "language/$lang.json" + } + foreach ($path in $file_list) { + $path_file = "$($PSCompletions.path.completions)/psc/$path" if (!(Test-Path $path_file)) { - $wc.DownloadFile("$($PSCompletions.url)/completions/$path", $path_file) + download_file "completions/psc/$path" $path_file $PSCompletions.urls } } } @@ -65,24 +86,27 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { @{ list = @('psc') } | ConvertTo-Json -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force } $current_list = (get_raw_content $PSCompletions.path.completions_json | ConvertFrom-Json).list - try { - $content = (Invoke-WebRequest -Uri "$($PSCompletions.url)/completions.json").Content | ConvertFrom-Json + foreach ($url in $PSCompletions.urls) { + try { + $content = (Invoke-WebRequest -Uri "$url/completions.json").Content | ConvertFrom-Json - $remote_list = $content.list + $remote_list = $content.list - $diff = Compare-Object $remote_list $current_list -PassThru - if ($diff) { - if ($PSCompletions.is_update_init) { - $diff = '' + $diff = Compare-Object $remote_list $current_list -PassThru + if ($diff) { + $diff | Out-File $PSCompletions.path.change -Force -Encoding utf8 + $content | ConvertTo-Json -Depth 100 -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force + $PSCompletions.list = $remote_list } - $diff | Out-File $PSCompletions.path.change -Force -Encoding utf8 - $content | ConvertTo-Json -Depth 100 -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force - } - else { - Clear-Content $PSCompletions.path.change -Force + else { + Clear-Content $PSCompletions.path.change -Force + $PSCompletions.list = $current_list + } + $isErr = $false + return $remote_list } + catch {} } - catch {} } ensure_dir $PSCompletions.path.order @@ -138,10 +162,28 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { foreach ($_ in $PSCompletions.data.list) { $path = "$($PSCompletions.path.completions)/$_/config.json" if (!(Test-Path $path)) { - $wc.DownloadFile("$($PSCompletions.url)/completions/$_/config.json", $path) + try { + download_file "completions/$_/config.json" $path $PSCompletions.urls + } + catch { + continue + } + } + ensure_dir "$($PSCompletions.path.completions)/$_/language" + $json_config = get_raw_content $path | ConvertFrom-Json + foreach ($lang in $json_config.language) { + $path_lang = "$($PSCompletions.path.completions)/$_/language/$lang.json" + if (!(Test-Path $path_lang)) { + download_file "completions/$_/language/$lang.json" $path_lang $PSCompletions.urls + } + } + if ($json_config.hooks -ne $null) { + $path_hooks = "$($PSCompletions.path.completions)/$_/hooks.ps1" + if (!(Test-Path $path_hooks)) { + download_file "completions/$_/hooks.ps1" $path_hooks $PSCompletions.urls + } } - $json = get_raw_content $path | ConvertFrom-Json - $path = "$($PSCompletions.path.completions)/$_/language/$($json.language[0]).json" + $path = "$($PSCompletions.path.completions)/$_/language/$($json_config.language[0]).json" $json = get_raw_content $path | ConvertFrom-Json -AsHashtable $config_list = $PSCompletions.default_completion_item foreach ($item in $config_list) { @@ -164,7 +206,14 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { } } foreach ($r in $data.config.comp_config[$_].Keys.Where({ $_ -notin $config_list })) { - $data.config.comp_config[$_].Remove($r) + if ($r -eq 'enable_hooks') { + if ($json_config.hooks -eq $null) { + $data.config.comp_config[$_].Remove($r) + } + } + else { + $data.config.comp_config[$_].Remove($r) + } } } $_keys = @() @@ -186,7 +235,14 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { # check version try { if ($PSCompletions.config.enable_module_update -eq 1) { - $response = Invoke-WebRequest -Uri "$($PSCompletions.url)/module/version.txt" + foreach ($url in $PSCompletions.urls) { + try { + $response = Invoke-WebRequest -Uri "$url/module/version.txt" + break + } + catch {} + } + $content = $response.Content.Trim() $versions = @($PSCompletions.version, $content) | Sort-Object { [Version] $_ } if ($versions[-1] -ne $PSCompletions.version) { @@ -205,8 +261,19 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { if ($PSCompletions.config.enable_completions_update -eq 1) { $update_list = @() foreach ($_ in (Get-ChildItem $PSCompletions.path.completions -ErrorAction SilentlyContinue).Where({ $_.Name -in $PSCompletions.list })) { + $isErr = $true + foreach ($url in $PSCompletions.urls) { + try { + $response = Invoke-WebRequest -Uri "$url/completions/$($_.Name)/guid.txt" + $isErr = $false + break + } + catch {} + } + if ($isErr) { + continue + } try { - $response = Invoke-WebRequest -Uri "$($PSCompletions.url)/completions/$($_.Name)/guid.txt" $content = $response.Content.Trim() $guid = get_raw_content "$($PSCompletions.path.completions)/$($_.Name)/guid.txt" if ($guid -ne $content -and $content -match "^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$") { @@ -220,6 +287,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { } } -ArgumentList $PSCompletions + $wc = New-Object System.Net.WebClient function get_raw_content { param ([string]$path, [bool]$trim = $true) $res = Get-Content $path -Raw -Encoding utf8 -ErrorAction SilentlyContinue @@ -239,7 +307,32 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { } if ($data -match $pattern) { (replace_content $data) }else { return $data } } - + function download_file { + param( + [string]$path, # 相对于 $baseUrl 的文件路径 + [string]$file, + [array]$baseUrl + ) + $isErr = $true + + for ($i = 0; $i -lt $baseUrl.Count; $i++) { + $item = $baseUrl[$i] + $url = $item + '/' + $path + try { + $wc.DownloadFile($url, $file) + $isErr = $false + break + } + catch {} + } + if ($isErr) { + throw + } + } + function ensure_dir { + param([string]$path) + if (!(Test-Path $path)) { New-Item -ItemType Directory $path > $null } + } function getCompletions { $guid = $PSCompletions.guid $obj = @{} @@ -331,8 +424,6 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { $_completions_data."$($root)_common_options" = $obj.commonOptions.$guid | ForEach-Object { $_.CompletionText } return $obj } - - $wc = New-Object System.Net.WebClient function get_language { param ([string]$completion) @@ -340,12 +431,22 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { $content_config = get_raw_content $path_config | ConvertFrom-Json if (!$content_config.language) { - try { - $wc.DownloadFile("$($PSCompletions.url)/completions/$completion/config.json", $path_config) - } - catch {} + download_file "completions/$completion/config.json" $path_config $PSCompletions.urls $content_config = get_raw_content $path_config | ConvertFrom-Json } + ensure_dir "$($PSCompletions.path.completions)/$completion/language" + foreach ($lang in $content_config.language) { + $path_lang = "$($PSCompletions.path.completions)/$completion/language/$lang.json" + if (!(Test-Path $path_lang)) { + download_file "completions/$completion/language/$lang.json" $path_lang $PSCompletions.urls + } + } + if ($content_config.hooks -ne $null) { + $path_hooks = "$($PSCompletions.path.completions)/$completion/hooks.ps1" + if (!(Test-Path $path_hooks)) { + download_file "completions/$completion/hooks.ps1" $path_hooks $PSCompletions.urls + } + } if ($PSCompletions.config.comp_config[$completion].language) { $config_language = $PSCompletions.config.comp_config.$completion.language } @@ -369,20 +470,17 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { foreach ($_ in $filter) { $root = $_.BaseName if ($root -in $PSCompletions.data.list) { - $language = get_language $root + try { + $language = get_language $root + } + catch { + continue + } $path_language = "$($PSCompletions.path.completions)/$root/language/$language.json" if (Test-Path $path_language) { $_completions.$root = get_raw_content $path_language | ConvertFrom-Json -AsHashtable $_completions_data.$root = getCompletions } - else { - try { - $wc.DownloadFile("$($PSCompletions.url)/completions/$root/language/$language.json", $path_language) - $_completions.$root = get_raw_content $path_language | ConvertFrom-Json -AsHashtable - $_completions_data.$root = getCompletions - } - catch {} - } } } return @{ diff --git a/module/PSCompletions/core/utils/Desktop.ps1 b/module/PSCompletions/core/utils/Desktop.ps1 index 43699140..b1d8d7a7 100644 --- a/module/PSCompletions/core/utils/Desktop.ps1 +++ b/module/PSCompletions/core/utils/Desktop.ps1 @@ -97,30 +97,51 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { } if ($data -match $pattern) { (replace_content $data) }else { return $data } } + function download_file { + param( + [string]$path, # 相对于 $baseUrl 的文件路径 + [string]$file, + [array]$baseUrl + ) + $isErr = $true + for ($i = 0; $i -lt $baseUrl.Count; $i++) { + $item = $baseUrl[$i] + $url = $item + '/' + $path + try { + $wc.DownloadFile($url, $file) + $isErr = $false + break + } + catch {} + } + if ($isErr) { + throw + } + } function ensure_dir { param([string]$path) if (!(Test-Path $path)) { New-Item -ItemType Directory $path > $null } } function ensure_psc { - $url = "$($PSCompletions.url)/completions/psc" - # XXX: language - # $language = if ($PSCompletions.language -eq 'zh-CN') { 'zh-CN' }else { 'en-US' } - ensure_dir "$($PSCompletions.path.completions)/psc" ensure_dir "$($PSCompletions.path.completions)/psc/language" - $path_list = @( - # "psc/language/$language.json", - "psc/language/zh-CN.json", - "psc/language/en-US.json", - "psc/config.json", - "psc/guid.txt", - "psc/hooks.ps1" - ) - foreach ($path in $path_list) { - $path_file = "$($PSCompletions.path.completions)/$path" + $path_config = "$($PSCompletions.path.completions)/psc/config.json" + download_file "completions/psc/config.json" $path_config $PSCompletions.urls + + $config = get_raw_content $path_config | ConvertFrom-Json + + $file_list = @('guid.txt') + if ($config.hooks -ne $null) { + $file_list += 'hooks.ps1' + } + foreach ($lang in $config.language) { + $file_list += "language/$lang.json" + } + foreach ($path in $file_list) { + $path_file = "$($PSCompletions.path.completions)/psc/$path" if (!(Test-Path $path_file)) { - $wc.DownloadFile("$($PSCompletions.url)/completions/$path", $path_file) + download_file "completions/psc/$path" $path_file $PSCompletions.urls } } } @@ -130,24 +151,27 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { @{ list = @('psc') } | ConvertTo-Json -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force } $current_list = (get_raw_content $PSCompletions.path.completions_json | ConvertFrom-Json).list - try { - $content = (Invoke-WebRequest -Uri "$($PSCompletions.url)/completions.json").Content | ConvertFrom-Json + foreach ($url in $PSCompletions.urls) { + try { + $content = (Invoke-WebRequest -Uri "$url/completions.json").Content | ConvertFrom-Json - $remote_list = $content.list + $remote_list = $content.list - $diff = Compare-Object $remote_list $current_list -PassThru - if ($diff) { - if ($PSCompletions.is_update_init) { - $diff = '' + $diff = Compare-Object $remote_list $current_list -PassThru + if ($diff) { + $diff | Out-File $PSCompletions.path.change -Force -Encoding utf8 + $content | ConvertTo-Json -Depth 100 -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force + $PSCompletions.list = $remote_list } - $diff | Out-File $PSCompletions.path.change -Force -Encoding utf8 - $content | ConvertTo-Json -Depth 100 -Compress | Out-File $PSCompletions.path.completions_json -Encoding utf8 -Force - } - else { - Clear-Content $PSCompletions.path.change -Force + else { + Clear-Content $PSCompletions.path.change -Force + $PSCompletions.list = $current_list + } + $isErr = $false + return $remote_list } + catch {} } - catch {} } ensure_dir $PSCompletions.path.order @@ -203,10 +227,28 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { foreach ($_ in $PSCompletions.data.list) { $path = "$($PSCompletions.path.completions)/$_/config.json" if (!(Test-Path $path)) { - $wc.DownloadFile("$($PSCompletions.url)/completions/$_/config.json", $path) + try { + download_file "completions/$_/config.json" $path $PSCompletions.urls + } + catch { + continue + } + } + ensure_dir "$($PSCompletions.path.completions)/$_/language" + $json_config = get_raw_content $path | ConvertFrom-Json + foreach ($lang in $json_config.language) { + $path_lang = "$($PSCompletions.path.completions)/$_/language/$lang.json" + if (!(Test-Path $path_lang)) { + download_file "completions/$_/language/$lang.json" $path_lang $PSCompletions.urls + } } - $json = get_raw_content $path | ConvertFrom-Json - $path = "$($PSCompletions.path.completions)/$_/language/$($json.language[0]).json" + if ($json_config.hooks -ne $null) { + $path_hooks = "$($PSCompletions.path.completions)/$_/hooks.ps1" + if (!(Test-Path $path_hooks)) { + download_file "completions/$_/hooks.ps1" $path_hooks $PSCompletions.urls + } + } + $path = "$($PSCompletions.path.completions)/$_/language/$($json_config.language[0]).json" $json = get_raw_content $path | ConvertFrom_JsonToHashtable $config_list = $PSCompletions.default_completion_item foreach ($item in $config_list) { @@ -229,7 +271,14 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { } } foreach ($r in $data.config.comp_config[$_].Keys.Where({ $_ -notin $config_list })) { - $data.config.comp_config[$_].Remove($r) + if ($r -eq 'enable_hooks') { + if ($json_config.hooks -eq $null) { + $data.config.comp_config[$_].Remove($r) + } + } + else { + $data.config.comp_config[$_].Remove($r) + } } } $_keys = @() @@ -251,7 +300,14 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { # check version try { if ($PSCompletions.config.enable_module_update -eq 1) { - $response = Invoke-WebRequest -Uri "$($PSCompletions.url)/module/version.txt" + foreach ($url in $PSCompletions.urls) { + try { + $response = Invoke-WebRequest -Uri "$url/module/version.txt" + break + } + catch {} + } + $content = $response.Content.Trim() $versions = @($PSCompletions.version, $content) | Sort-Object { [Version] $_ } if ($versions[-1] -ne $PSCompletions.version) { @@ -270,8 +326,19 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { if ($PSCompletions.config.enable_completions_update -eq 1) { $update_list = @() foreach ($_ in (Get-ChildItem $PSCompletions.path.completions -ErrorAction SilentlyContinue).Where({ $_.Name -in $PSCompletions.list })) { + $isErr = $true + foreach ($url in $PSCompletions.urls) { + try { + $response = Invoke-WebRequest -Uri "$url/completions/$($_.Name)/guid.txt" + $isErr = $false + break + } + catch {} + } + if ($isErr) { + continue + } try { - $response = Invoke-WebRequest -Uri "$($PSCompletions.url)/completions/$($_.Name)/guid.txt" $content = $response.Content.Trim() $guid = get_raw_content "$($PSCompletions.path.completions)/$($_.Name)/guid.txt" if ($guid -ne $content -and $content -match "^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$") { @@ -382,12 +449,22 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { $content_config = get_raw_content $path_config | ConvertFrom-Json if (!$content_config.language) { - try { - $wc.DownloadFile("$($PSCompletions.url)/completions/$completion/config.json", $path_config) - } - catch {} + download_file "completions/$completion/config.json" $path_config $PSCompletions.urls $content_config = get_raw_content $path_config | ConvertFrom-Json } + ensure_dir "$($PSCompletions.path.completions)/$completion/language" + foreach ($lang in $content_config.language) { + $path_lang = "$($PSCompletions.path.completions)/$completion/language/$lang.json" + if (!(Test-Path $path_lang)) { + download_file "completions/$completion/language/$lang.json" $path_lang $PSCompletions.urls + } + } + if ($content_config.hooks -ne $null) { + $path_hooks = "$($PSCompletions.path.completions)/$completion/hooks.ps1" + if (!(Test-Path $path_hooks)) { + download_file "completions/$completion/hooks.ps1" $path_hooks $PSCompletions.urls + } + } if ($PSCompletions.config.comp_config[$completion].language) { $config_language = $PSCompletions.config.comp_config.$completion.language } @@ -410,20 +487,17 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod start_job { foreach ($_ in $filter) { $root = $_.BaseName if ($root -in $PSCompletions.data.list) { - $language = get_language $root + try { + $language = get_language $root + } + catch { + continue + } $path_language = "$($PSCompletions.path.completions)/$root/language/$language.json" if (Test-Path $path_language) { $_completions.$root = get_raw_content $path_language | ConvertFrom_JsonToHashtable $_completions_data.$root = getCompletions } - else { - try { - $wc.DownloadFile("$($PSCompletions.url)/completions/$root/language/$language.json", $path_language) - $_completions.$root = get_raw_content $path_language | ConvertFrom_JsonToHashtable - $_completions_data.$root = getCompletions - } - catch {} - } } } return @{ diff --git a/module/version.txt b/module/version.txt index 26d99a28..ce7f2b42 100644 --- a/module/version.txt +++ b/module/version.txt @@ -1 +1 @@ -5.2.1 +5.2.2 diff --git a/schema/en-US/completion-cn.json b/schema/en-US/completion-cn.json index df0bd2f5..e4a788f0 100644 --- a/schema/en-US/completion-cn.json +++ b/schema/en-US/completion-cn.json @@ -25,7 +25,7 @@ "definitions": { "completion_tip": { "$ref": "#/definitions/array_items", - "description": "A description of the configuration item.\nMust contain one of the following Chinese symbols.\n。?!;", + "description": "The completion tooltip.\nMust contain one of the following Chinese symbols.\n。?!;\nThe content of the first item cannot match this regular expression: ^\\s{4,}\n- It means that the first item cannot begin with 4+ spaces or `n.\nThe content of of the last item cannot match this regular expression: \\s{2,}$\n- It means that the last item cannot end with 2+ spaces or `n.", "contains": { "type": "string", "pattern": ".*[。?!;].*" diff --git a/schema/en-US/completion.json b/schema/en-US/completion.json index 23f4e005..d12baf7e 100644 --- a/schema/en-US/completion.json +++ b/schema/en-US/completion.json @@ -24,11 +24,8 @@ ], "definitions": { "completion_tip": { - "$ref": "#/definitions/array_items" - // "description": "A description of the configuration item.\nMust contain one of the following Chinese symbols.\n.?!;", - // "contains": { - // "pattern": ".*[\\.?!;].*" - // } + "$ref": "#/definitions/array_items", + "description": "The completion tooltip.\nThe content of the first item cannot match this regular expression: ^\\s{4,}\n- It means that the first item cannot begin with 4+ spaces or `n.\nThe content of of the last item cannot match this regular expression: \\s{2,}$\n- It means that the last item cannot end with 2+ spaces or `n." }, "string": { "type": "string", diff --git a/schema/zh-CN/completion-cn.json b/schema/zh-CN/completion-cn.json index 9cecce94..0da9d863 100644 --- a/schema/zh-CN/completion-cn.json +++ b/schema/zh-CN/completion-cn.json @@ -25,7 +25,7 @@ "definitions": { "completion_tip": { "$ref": "#/definitions/array_items", - "description": "补全提示信息。\n必须包含以下的其中一个中文符号。\n。?!;", + "description": "补全提示信息。\n必须包含以下的其中一个中文符号。\n。?!;\n第一项的内容不能符合此正则表达式: ^\\s{4,}\n- 即第一项不能以 4+ 个空格或 `n 开头。\n最后一项的内容不能符合此正则表达式: \\s{2,}$\n- 即最后一项不能以 2+ 个空格或 `n 结尾。", "contains": { "pattern": ".*[。?!;].*" } diff --git a/schema/zh-CN/completion.json b/schema/zh-CN/completion.json index cfc08966..aa1bb8d4 100644 --- a/schema/zh-CN/completion.json +++ b/schema/zh-CN/completion.json @@ -24,11 +24,8 @@ ], "definitions": { "completion_tip": { - "$ref": "#/definitions/array_items" - // "description": "补全提示信息。\n必须包含以下的其中一个英文符号。\n.?!;", - // "contains": { - // "pattern": ".*[\\.?!;].*" - // } + "$ref": "#/definitions/array_items", + "description": "补全提示信息。\n第一项的内容不能符合此正则表达式: ^\\s{4,}\n- 即第一项不能以 4+ 个空格或 `n 开头。\n最后一项的内容不能符合此正则表达式: \\s{2,}$\n- 即最后一项不能以 2+ 个空格或 `n 结尾。" }, "string": { "type": "string", diff --git a/scripts/create-completion.ps1 b/scripts/create-completion.ps1 index 10425781..3f2ed619 100644 --- a/scripts/create-completion.ps1 +++ b/scripts/create-completion.ps1 @@ -35,7 +35,7 @@ Copy-Item "$($PSScriptRoot)/template/config.json" "$completion_dir/config.json" Copy-Item "$($PSScriptRoot)/template/language/en-US.json" "$completion_dir/language/en-US.json" -Force Copy-Item "$($PSScriptRoot)/template/language/zh-CN.json" "$completion_dir/language/zh-CN.json" -Force - +Copy-Item "$($PSScriptRoot)/template/hooks.ps1" "$completion_dir/hooks.ps1" -Force $test_dir = Join-Path $PSCompletions.path.completions $completion_name Remove-Item $test_dir -Recurse -Force -ErrorAction SilentlyContinue @@ -45,4 +45,7 @@ $PSCompletions.write_with_color($PSCompletions.replace_content($guide.success)) $PSCompletions.data.list += $completion_name $PSCompletions.data.alias.$completion_name = $completion_name $PSCompletions.data.aliasMap.$completion_name = $completion_name +$PSCompletions.data.config.comp_config.$completion_name = @{ + enable_hooks = 1 +} $PSCompletions.data | ConvertTo-Json -Depth 100 | Out-File $PSCompletions.path.data -Encoding utf8 -Force diff --git a/scripts/template/config.json b/scripts/template/config.json index 58c46461..fe93f333 100644 --- a/scripts/template/config.json +++ b/scripts/template/config.json @@ -2,5 +2,6 @@ "language": [ "en-US", "zh-CN" - ] + ], + "hooks": true } diff --git a/scripts/template/guide/en-US.json b/scripts/template/guide/en-US.json index d9a6a12a..ce5ac390 100644 --- a/scripts/template/guide/en-US.json +++ b/scripts/template/guide/en-US.json @@ -4,6 +4,7 @@ "success": [ "<@Green>Create completion successfully!\n", "The completion directory has been linked to the PSCompletions module for easy testing at write time.\n", + "Hooks have been added automatically, if not needed, please delete the <@Magenta>hooks<@Green> attribute in <@Magenta>config.json<@Green> and delete the <@Magenta>hooks.ps1<@Green> file.\n", "Link Directory: <@Magenta>{{ $test_dir }}<@Green>\n", "<@Yellow>When the work has been completed, you should remove it." ], diff --git a/scripts/template/guide/zh-CN.json b/scripts/template/guide/zh-CN.json index 74137528..9080568b 100644 --- a/scripts/template/guide/zh-CN.json +++ b/scripts/template/guide/zh-CN.json @@ -4,6 +4,7 @@ "success": [ "<@Green>补全目录创建完成!\n", "已将补全目录链接到 PSCompletions 模块中,方便在编写时进行测试\n", + "已自动添加 hooks,如果不需要,请删除 <@Magenta>config.json<@Green> 中的 <@Magenta>hooks<@Green> 属性,并删除 <@Magenta>hooks.ps1<@Green> 文件\n", "链接目录: <@Magenta>{{ $test_dir }}<@Green>\n", "<@Yellow>当补全编写完成后,你应该移除它" ], diff --git a/scripts/template/hooks.ps1 b/scripts/template/hooks.ps1 index 60965c3d..c67b0076 100644 --- a/scripts/template/hooks.ps1 +++ b/scripts/template/hooks.ps1 @@ -1,8 +1,10 @@ function handleCompletions($completions) { $tempList = @() + $filter_input_arr = $PSCompletions.filter_input_arr + # Example - switch ($PSCompletions.filter_input_arr[-1]) { + switch ($filter_input_arr[-1]) { 'add' { $tempList += $PSCompletions.return_completion('abc', "Add a new item.") }