diff --git a/module/.version b/module/.version deleted file mode 100644 index 7919852..0000000 --- a/module/.version +++ /dev/null @@ -1 +0,0 @@ -4.0.9 diff --git a/module/CHANGELOG-CN.md b/module/CHANGELOG-CN.md new file mode 100644 index 0000000..b9aa32d --- /dev/null +++ b/module/CHANGELOG-CN.md @@ -0,0 +1,101 @@ +

+ English | + 简体中文 +

+ +## 4.1.0 (2024/8/7) + +- 现在 `Windows PowerShell` 也可以使用模块提供的补全菜单了 + - 不过由于渲染问题,补全菜单的边框样式无法自定义 +- 修复一些其他问题 +- 调整源代码文件目录结构 +- 整理代码 + +## 4.0.9 (2024/7/20) + +- 添加两个命令: 添加/移除所有补全 + - `psc add *` + - `psc rm *` +- 优化 `common_options` 的逻辑处理 +- 修复模块更新的问题 +- 调整源代码文件目录结构 +- 整理代码 + +## 4.0.7 (2024/7/6) + +- 将 `ForEach-Object` 替换为 `foreach` + - `ForEach-Object` 在一些特殊情况下的结果不符合预期 +- 整理代码 + +## 4.0.6 (2024/5/20) + +- 一个默认颜色配置改错了,修复一下 + +## 4.0.5 (2024/5/20) + +- 修复了关于补全特殊配置的一些问题 +- 修复了因为存在缓存导致配置修改后无法立即生效的问题 +- 给 `reset` 命令添加 `completion` 子命令,用于重置(移除)补全的特殊配置 +- 修复了一些其他问题 + +## 4.0.4 (2024/5/20) + +- 修复了当补全列表滚动时,补全提示信息、过滤区域、状态区域闪烁的问题 +- 修复了一些其他的小问题 + +## 4.0.3 (2024/5/18) + +- 修复了一个因为修改终端输出编码导致提示信息显示错误的问题 + - 必须先导入 `PSCompletions` 模块,然后再修改终端输出编码,否则还是会显示错误 +- 将 `menu_tip_cover_buffer` 这个配置的默认值从 `0` 修改为 `1` + - 表示默认情况下,菜单提示信息会覆盖缓冲区内容,主要是当提示信息显示在上方时,会覆盖掉上方所有内容,这会看起来背景更简洁 + - 当然,当补全菜单消失后,覆盖的内容会恢复 + - 你也可以禁用它(`psc menu config menu_tip_cover_buffer 0`) +- 修复了一些其他的小问题 + +## 4.0.2 (2024/5/15) + +- 一个测试环境的配置没有及时删除,修复一下 + +## 4.0.1 (2024/5/15) + +- 一个默认配置的颜色写错了,修复一下 + +## 4.0.0 (2024/5/15) + +- 如果你当前使用的 `PSCompletions` 模块需要**管理员权限**,你应该删除 `PSCompletions` 模块,然后以**用户权限**安装最新版的模块。 + + - 完整的模块安装命令: `Install-Module PSCompletions -Scope CurrentUser` + +- 4.0.0 版本重构了整个模块,解决了许多不合理的地方,所以完全不兼容旧版本的配置和补全 + + 1. 性能优化: + - 提升了模块加载速度 + - 提升了补全响应速度 + 2. 补全菜单: + - 补全菜单完全重写,菜单渲染的稳定性大幅提高,从根本上解决了许多渲染 bug + - 补全菜单新增了许多的配置项,你可以自行通过触发补全以及提示信息查看 + - 比如: `menu_show_tip`, 它可以控制补全提示信息是否显示,如果你对命令足够熟悉,建议禁用补全提示信息 + - 此配置也可以单独给某一个补全设置,参考 `psc completion` 下的命令 + 3. 补全别名: + - 现在补全别名支持多个,你可以随意添加。理论上,你可以添加无数个别名 + - 比如,你可以添加 `.\scoop.ps1` 这样的别名,这在有些时候比较有用 + 4. 补全文件和模块解耦 + - 现在可以只编写 json 文件去新增/更新/翻译一个补全,不需要涉及到代码(如果需要使用到 hooks 除外) + - 同时使用了 json schema 控制 json 文件的类型,这让想要为仓库添加补全的贡献者创建 PR 的难度大幅降低 + - 比如你觉得某一个命令的描述不够完善,你也可以修改它的 json 文件后之后,提交一个 PR + 5. 语言: + - 现在从模块层面,支持任何语言,模块会根据语言配置以及每一个补全的 `config.json` 文件来决定语言 + - 这意味着以后可以添加更多语言的支持,只需要编写对应的 json 文件即可 + 6. 其他: + - 新增了一些按键映射,你现在可以通过许多种按键方式在补全菜单中选择补全项,你可以选择一种适合自己的。 + |选择上一项|选择下一项| + |:-:|:-:| + |`Up`|`Down`| + |`Left`|`Right`| + |`Shift + Tab`|`Tab`| + |`Shift + Space`|`Space`| + |`Ctrl + u`|`Ctrl + d`| + |`Ctrl + p`|`Ctrl + n`| + - 现在符号被放在了补全项的后面,补全提示信息中不再出现符号 + - ... diff --git a/module/CHANGELOG.md b/module/CHANGELOG.md new file mode 100644 index 0000000..87ed550 --- /dev/null +++ b/module/CHANGELOG.md @@ -0,0 +1,101 @@ +

+ 简体中文| + English +

+ +## 4.1.0 (2024/8/7) + +- Now `Windows PowerShell` can use the completion menu provided by `PSCompletions`. + - But, due to rendering problems, the border style of the completion menu cannot be customized. +- Fix some other issues. +- Change the directory structure of the source code file. +- Clean up the code. + +## 4.0.9 (2024/7/20) + +- Add two commands: add/remove all completions. + - `psc add *` + - `psc rm *` +- Optimize the logic processing of `common_options`. +- Fix the problem of module update. +- Change the directory structure of the source code file. +- Clean up the code. + +## 4.0.7 (2024/7/6) + +- Replace `ForEach-Object` with `foreach`. + - `ForEach-Object` has unexpected results in some special cases. +- Clean up some code + +## 4.0.6 (2024/5/20) + +- A default configuration color was mistakenly written, fix it. + +## 4.0.5 (2024/5/20) + +- Fix some issues about the special configurations of completion. +- Fix an issue where configuration changes did not take immediate effect due to caching. +- Add the `completion` subcommand to the `reset` command to reset (remove) the special configuration of completion. +- Fix some other issues. + +## 4.0.4 (2024/5/20) + +- Fix a bug where the completion tip area, filter area and status area flashes when scrolling the completion list. +- Fix some other minor issues. + +## 4.0.3 (2024/5/18) + +- Fix a bug where the completion tip was displayed incorrectly because the terminal output encoding was modified. + - You must import the `PSCompletions` module first, and then modify the terminal output encoding. Otherwise, it will still display incorrectly. +- Change the default value of `menu_tip_cover_buffer` from `0` to `1`. + - This configuration means that the completion tip will cover the buffer content by default, which is mainly when the completion tip is displayed above, it will cover all the buffer content above, which will make the background look cleaner. + - Of course, the buffer content covered by the completion tip will be restored when the completion menu exits. + - You can disable it by running `psc menu config menu_tip_cover_buffer 0`. +- Fix some other minor issues. + +## 4.0.2 (2024/5/15) + +- A test environment configuration was not deleted, fix it. + +## 4.0.1 (2024/5/15) + +- A default configuration color was mistakenly written, fix it. + +## 4.0.0 (2024/5/15) + +- If you are using the `PSCompletions` module with **administrator permission**, you should remove the `PSCompletions` module and install the latest version with **user permission**. + + - Full module installation command: `Install-Module PSCompletions -Scope CurrentUser` + +- This version completely rewrites the module, and solves many inappropriate places, so it's completely incompatible with the old version configuration and completion. + 1. Performance optimization: + - Improve module loading speed. + - Improve completion response speed. + 2. Completion menu: + - Completely rewrite the completion menu, which greatly improves the stability of the menu rendering, and solves many rendering bugs from the root. + - The completion menu now has many configuration items, you can trigger completion by running `psc menu`, then learn about them by completion tip. + - For example: `menu_show_tip`, which can control whether the completion tip is displayed. + - If you are familiar with the command, it's recommended to disable the completion tip. + - This configuration can also be set separately for each completion, refer to `psc completion` for command. + 3. Completion alias: + - Now completion alias supports multiple, you can add as many as you like. Theoretically, you can add an infinite number of aliases. + - For example, you can add `.\scoop.ps1` as an alias, which can be useful in some cases. + 4. Decoupling of completion file and module. + - Add/update/translate completions now doesn't involve module core code. (Except if hooks are needed.) + - Use json schema to control the type of json file, which makes it easier for contributors to create PR for the completion repository. + - For example, if you think the description of a command is not good, you can modify its json file and create a PR. + 5. Language: + - Now the module supports any language, and the module determines the language based on the language configuration and each completion's config.json file. + - It means that more languages can be supported in the future by simply writing the corresponding json file. + 6. Other: + - Added some key mappings, which allows you to select completion items in the completion menu using various key combinations. You can choose one that suits you. + |Select previous item|Select next item| + |:-:|:-:| + |`Up`|`Down`| + |`Left`|`Right`| + |`Shift + Tab`|`Tab`| + |`Shift + Space`|`Space`| + |`Ctrl + u`|`Ctrl + d`| + |`Ctrl + p`|`Ctrl + n`| + - The symbol is now placed behind the completion item, and the symbol is no longer displayed in the completion tip. + - ... diff --git a/module/PSCompletions.psd1 b/module/PSCompletions.psd1 index 0754fdd..34438f8 100644 --- a/module/PSCompletions.psd1 +++ b/module/PSCompletions.psd1 @@ -10,7 +10,7 @@ RootModule = 'PSCompletions.psm1' - ModuleVersion = '4.0.9' + ModuleVersion = '4.1.0' GUID = '00929632-527d-4dab-a5b3-21197faccd05' diff --git a/module/PSCompletions.psm1 b/module/PSCompletions.psm1 index f98857c..428635e 100644 --- a/module/PSCompletions.psm1 +++ b/module/PSCompletions.psm1 @@ -37,7 +37,6 @@ function PSCompletions { } $PSCompletions.show_with_less_table($data, ('Completion', 'Alias', $max_len)) } - function Out-Config { $PSCompletions.config | ConvertTo-Json | Out-File $PSCompletions.path.config -Force -Encoding utf8 } @@ -575,7 +574,7 @@ function PSCompletions { $PSCompletions.config.menu_color_selected_back = 'DarkGray' $PSCompletions.config.menu_color_filter_text = 'Yellow' $PSCompletions.config.menu_color_filter_back = 'Black' - $PSCompletions.config.menu_color_border_text = 'Gray' + $PSCompletions.config.menu_color_border_text = 'DarkGray' $PSCompletions.config.menu_color_border_back = 'Black' $PSCompletions.config.menu_color_status_text = 'Blue' $PSCompletions.config.menu_color_status_back = 'Black' @@ -874,6 +873,8 @@ function PSCompletions { if (!$no_show_msg) { $PSCompletions.write_with_color((_replace $PSCompletions.info.reset.done)) } } function _help { + $json = $PSCompletions.data.psc + $info = $PSCompletions.info $PSCompletions.write_with_color((_replace $PSCompletions.info.description)) } $need_init = $true diff --git a/module/core/init.ps1 b/module/core/init.ps1 index 4215d45..7f95908 100644 --- a/module/core/init.ps1 +++ b/module/core/init.ps1 @@ -3,7 +3,7 @@ using namespace System.Management.Automation New-Variable -Name PSCompletions -Value @{} -Option Constant # 模块版本 -$PSCompletions.version = '4.0.9' +$PSCompletions.version = '4.1.0' $PSCompletions.path = @{} $PSCompletions.path.root = Split-Path $PSScriptRoot -Parent $PSCompletions.path.completions = Join-Path $PSCompletions.path.root 'completions' @@ -86,11 +86,11 @@ if (Get-Command Set-PSReadLineKeyHandler -ErrorAction SilentlyContinue) { } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod ensure_file { - param( [string]$path ) + param([string]$path) if (!(Test-Path $path)) { New-Item -ItemType File $path > $null } } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod ensure_dir { - param( [string]$path ) + param([string]$path) if (!(Test-Path $path)) { New-Item -ItemType Directory $path > $null } } @@ -112,6 +112,8 @@ if (!(Test-Path $PSCompletions.path.config) -and !(Test-Path $PSCompletions.path $PSCompletions.move_old_version() } +. $PSScriptRoot\pwsh\config.ps1 + if ($PSEdition -eq "Core") { if ($IsWindows) { # pwsh (Windows) @@ -119,20 +121,18 @@ if ($PSEdition -eq "Core") { . $PSScriptRoot\pwsh\Win\menu.ps1 } else { - . $PSScriptRoot\pwsh\Win\utils.ps1 - . $PSScriptRoot\pwsh\Unix\menu.ps1 + # WSL/Unix... + . $PSScriptRoot\pwsh\Unix\utils.ps1 } - . $PSScriptRoot\pwsh\config.ps1 } else { - # powershell 5.x + # Windows PowerShell 5.x . $PSScriptRoot\powershell\utils.ps1 - . $PSScriptRoot\powershell\config.ps1 - . $PSScriptRoot\powershell\menu.ps1 + . $PSScriptRoot\pwsh\Win\menu.ps1 } Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod get_length { - param([string]$c) - return $Host.UI.RawUI.NewBufferCellArray($c, $Host.UI.RawUI.BackgroundColor, $Host.UI.RawUI.BackgroundColor).LongLength + param([string]$str) + return $Host.UI.RawUI.NewBufferCellArray($str, $Host.UI.RawUI.BackgroundColor, $Host.UI.RawUI.BackgroundColor).LongLength } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod join_path { $res = $args[0] @@ -179,7 +179,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod get_raw_content } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod replace_content { param ($data, $separator = '') - $data = ($data -join $separator) + $data = $data -join $separator $pattern = '\{\{(.*?(\})*)(?=\}\})\}\}' $matches = [regex]::Matches($data, $pattern) foreach ($match in $matches) { @@ -628,6 +628,77 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod init_data { } } } +Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_powershell_menu { + param($filter_list) + if ($Host.UI.RawUI.BufferSize.Height -lt 5) { + [Microsoft.PowerShell.PSConsoleReadLine]::UndoAll() + [Microsoft.PowerShell.PSConsoleReadLine]::Insert($PSCompletions.info.min_area) + '' + return + } + + $json = $PSCompletions.data.$($PSCompletions.current_cmd) + $info = $json.info + + $max_width = 0 + $tip_max_height = 0 + $filter_list = foreach ($_ in $filter_list) { + $symbol = foreach ($c in $_.symbol) { + $PSCompletions.config."symbol_$($c)" + } + $symbol = $symbol -join '' + $pad = if ($symbol) { "$($PSCompletions.config.menu_between_item_and_symbol)$($symbol)" }else { '' } + $name_with_symbol = "$($_.name[-1])$($pad)" + + $width = $this.get_length($name_with_symbol) + if ($width -gt $max_width) { $max_width = $width } + + if ($_.tip) { + $tip = $PSCompletions.replace_content($_.tip) + $tip_arr = $tip -split "`n" + } + else { + $tip = ' ' + $tip_arr = @() + } + if ($tip_arr.Count -gt $tip_max_height) { $tip_max_height = $tip_arr.Count } + @{ + name = $name_with_symbol + value = $_.name[-1] + width = $width + tip = $tip + } + } + $item_witdh = $max_width + 2 + $ui_max_width = 100 + $ui_width = if ($Host.UI.RawUI.BufferSize.Width -gt $ui_max_width) { $ui_max_width }else { $Host.UI.RawUI.BufferSize.Width } + $max_count = ($Host.UI.RawUI.BufferSize.Height - $tip_max_height) * ([math]::Floor($ui_width) / ($item_witdh)) + + $display_count = 0 + if ($max_count -lt 5 -or !$PSCompletions.config.menu_show_tip) { + $max_count = ($Host.UI.RawUI.BufferSize.Height) * ([math]::Floor($ui_width) / ($item_witdh)) + foreach ($_ in $filter_list) { + if ($max_count -gt $display_count -and $_.name) { + $display_count++ + [CompletionResult]::new($_.value, $_.name, 'ParameterValue', ' ') + } + } + } + else { + foreach ($_ in $filter_list) { + if ($max_count -gt $display_count -and $_.name) { + $display_count++ + [CompletionResult]::new($_.value, $_.name, 'ParameterValue', $_.tip) + } + } + } + + if ($filter_list -is [array] -and $display_count -lt $filter_list.Count) { + [CompletionResult]::new(' ', '...', 'ParameterValue', $PSCompletions.info.comp_hide) + $display_count++ + } + if ($display_count -eq 1 -and !$PSCompletions.config.enter_when_single) { ' ' } +} $PSCompletions.init_data() @@ -655,13 +726,7 @@ if ($PSCompletions.config.module_update -match "^\d+\.\d.*") { $PSCompletions.wc.DownloadFile("$($PSCompletions.url)/module/log.json", (Join-Path $PSCompletions.path.core 'log.json')) $null = $PSCompletions.confirm_do($PSCompletions.info.module.update, { $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.module.updating)) - try { - Update-Module PSCompletions -ErrorAction Stop - $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.module.update_done)) - } - catch { - $PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.module.update_err)) - } + Update-Module PSCompletions -ErrorAction Stop }) } else { diff --git a/module/core/powershell/config.ps1 b/module/core/powershell/config.ps1 deleted file mode 100644 index 2d26f47..0000000 --- a/module/core/powershell/config.ps1 +++ /dev/null @@ -1,111 +0,0 @@ -$PSCompletions.default.env = @{ - # env - language = $PSUICulture - update = 1 - module_update = 1 - github = 'https://github.com/abgox/PSCompletions' - gitee = 'https://gitee.com/abgox/PSCompletions' - url = '' - disable_cache = 0 -} -$PSCompletions.default.symbol = @{ - symbol_SpaceTab = [char]::ConvertFromUtf32([convert]::ToInt32(("U+1F604" -replace 'U\+', '0x'), 16)) - symbol_WriteSpaceTab = [char]::ConvertFromUtf32([convert]::ToInt32(("U+1F60E" -replace 'U\+', '0x'), 16)) - symbol_OptionTab = [char]::ConvertFromUtf32([convert]::ToInt32(("U+1F914" -replace 'U\+', '0x'), 16)) -} -$PSCompletions.default.menu_line = @{ - # menu line - menu_line_horizontal = [string][char]9552 - menu_line_vertical = [string][char]9553 - menu_line_top_left = [string][char]9556 - menu_line_bottom_left = [string][char]9562 - menu_line_top_right = [string][char]9559 - menu_line_bottom_right = [string][char]9565 -} -$PSCompletions.default.menu_color = @{ - # menu color - menu_color_item_text = 'Blue' - menu_color_item_back = 'Black' - menu_color_selected_text = 'white' - menu_color_selected_back = 'DarkGray' - menu_color_filter_text = 'Yellow' - menu_color_filter_back = 'Black' - menu_color_border_text = 'DarkGray' - menu_color_border_back = 'Black' - menu_color_status_text = 'Blue' - menu_color_status_back = 'Black' - menu_color_tip_text = 'Cyan' - menu_color_tip_back = 'Black' -} -$PSCompletions.default.menu_config = @{ - # menu config - enter_when_single = 0 - menu_enable = 1 - menu_show_tip = 1 - menu_completions_sort = 1 - menu_selection_with_margin = 1 - menu_tip_follow_cursor = 0 - menu_tip_cover_buffer = 1 - menu_list_follow_cursor = 1 - menu_list_cover_buffer = 0 - menu_list_margin_left = 0 - menu_list_margin_right = 0 - menu_list_min_width = 10 - menu_is_prefix_match = 0 - menu_above_margin_bottom = 0 - menu_above_list_max_count = -1 - menu_below_list_max_count = -1 - menu_between_item_and_symbol = ' ' - menu_status_symbol = '/' - menu_filter_symbol = '[]' -} -# completion config -$PSCompletions.default.comp_config = @{} - -Add-Member -InputObject $PSCompletions -MemberType ScriptMethod get_config { - if (Test-Path $this.path.config) { - $c = $PSCompletions.ConvertFrom_JsonToHashtable($this.get_raw_content($this.path.config)) - if ($c) { - foreach($_ in @('env', 'symbol', 'menu_line', 'menu_color', 'menu_config')){ - foreach ($config in $this.default.$_.Keys) { - if ($config -notin $c.keys) { - $hasDiff = $true - $c.$config = $this.default.$_.$config - } - } - } - if ($c.comp_config -eq $null) { - $hasDiff = $true - $c.comp_config = @{} - } - if ($hasDiff) { - $c | ConvertTo-Json | Out-File $this.path.config -Encoding utf8 -Force - } - return $c - } - else { - $need_init = $true - } - } - else { - $need_init = $true - } - if ($need_init) { - $c = @{} - foreach($_ in @('env', 'symbol', 'menu_line', 'menu_color', 'menu_config')){ - foreach ($config in $this.default.$_.Keys) { - $c.$config = $this.default.$_.$config - } - } - $c.comp_config = @{} - $c | ConvertTo-Json | Out-File $this.path.config -Encoding utf8 -Force - } - return $c -} - -Add-Member -InputObject $PSCompletions -MemberType ScriptMethod set_config { - param ([string]$k, $v) - $this.config = $this.get_config() - $this.config.$k = $v - $this.config | ConvertTo-Json | Out-File $this.path.config -Encoding utf8 -Force -} diff --git a/module/core/powershell/menu.ps1 b/module/core/powershell/menu.ps1 deleted file mode 100644 index c473959..0000000 --- a/module/core/powershell/menu.ps1 +++ /dev/null @@ -1,69 +0,0 @@ -Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_powershell_menu { - param($filter_list) - if ($Host.UI.RawUI.BufferSize.Height -lt 5) { - [Microsoft.PowerShell.PSConsoleReadLine]::UndoAll() - [Microsoft.PowerShell.PSConsoleReadLine]::Insert($PSCompletions.info.min_area) - '' - return - } - - $json = $PSCompletions.data.$($PSCompletions.current_cmd) - $info = $json.info - - $max_width = 0 - $tip_max_height = 0 - $filter_list = foreach ($_ in $filter_list) { - $symbol = foreach ($c in $_.symbol) { $PSCompletions.config."symbol_$($c)" } - $symbol = $symbol -join '' - $pad = if ($symbol) { "$($PSCompletions.config.menu_between_item_and_symbol)$($symbol)" }else { '' } - $name_with_symbol = "$($_.name[-1])$($pad)" - - $width = $this.get_length($name_with_symbol) - if ($width -gt $max_width) { $max_width = $width } - - if ($_.tip) { - $tip = $PSCompletions.replace_content($_.tip) - $tip_arr = $tip -split "`n" - } - else { - $tip = ' ' - $tip_arr = @() - } - if ($tip_arr.Count -gt $tip_max_height) { $tip_max_height = $tip_arr.Count } - @{ - name = $name_with_symbol - value = $_.name[-1] - width = $width - tip = $tip - } - } - $item_witdh = $max_width + 2 - $ui_max_width = 100 - $ui_width = if ($Host.UI.RawUI.BufferSize.Width -gt $ui_max_width) { $ui_max_width }else { $Host.UI.RawUI.BufferSize.Width } - $max_count = ($Host.UI.RawUI.BufferSize.Height - $tip_max_height) * ([math]::Floor($ui_width) / ($item_witdh)) - - $display_count = 0 - if ($max_count -lt 5 -or !$PSCompletions.config.menu_show_tip) { - $max_count = ($Host.UI.RawUI.BufferSize.Height) * ([math]::Floor($ui_width) / ($item_witdh)) - foreach ($_ in $filter_list) { - if ($max_count -gt $display_count -and $_.name) { - $display_count++ - [CompletionResult]::new($_.value, $_.name, 'ParameterValue', ' ') - } - } - } - else { - foreach ($_ in $filter_list) { - if ($max_count -gt $display_count -and $_.name) { - $display_count++ - [CompletionResult]::new($_.value, $_.name, 'ParameterValue', $_.tip) - } - } - } - - if ($filter_list -is [array] -and $display_count -lt $filter_list.Count) { - [CompletionResult]::new(' ', '...', 'ParameterValue', $PSCompletions.info.comp_hide) - $display_count++ - } - if ($display_count -eq 1 -and !$PSCompletions.config.enter_when_single) { ' ' } -} diff --git a/module/core/powershell/utils.ps1 b/module/core/powershell/utils.ps1 index 7a0a1ab..0dde78a 100644 --- a/module/core/powershell/utils.ps1 +++ b/module/core/powershell/utils.ps1 @@ -428,7 +428,18 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod handle_completio else { $PSCompletions.menu.is_show_tip = $PSCompletions.config.menu_show_tip -eq 1 } - $PSCompletions.menu.show_powershell_menu($filter_list) + if ($PSCompletions.config.menu_enable) { + $PSCompletions.config.menu_line_horizontal = "-" + $PSCompletions.config.menu_line_vertical = "|" + $PSCompletions.config.menu_line_top_left = "+" + $PSCompletions.config.menu_line_bottom_left = "+" + $PSCompletions.config.menu_line_top_right = "+" + $PSCompletions.config.menu_line_bottom_right = "+" + $PSCompletions.menu.show_module_menu($filter_list) + } + else { + $PSCompletions.menu.show_powershell_menu($filter_list) + } } } } diff --git a/module/core/pwsh/Unix/menu.ps1 b/module/core/pwsh/Unix/menu.ps1 deleted file mode 100644 index c473959..0000000 --- a/module/core/pwsh/Unix/menu.ps1 +++ /dev/null @@ -1,69 +0,0 @@ -Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_powershell_menu { - param($filter_list) - if ($Host.UI.RawUI.BufferSize.Height -lt 5) { - [Microsoft.PowerShell.PSConsoleReadLine]::UndoAll() - [Microsoft.PowerShell.PSConsoleReadLine]::Insert($PSCompletions.info.min_area) - '' - return - } - - $json = $PSCompletions.data.$($PSCompletions.current_cmd) - $info = $json.info - - $max_width = 0 - $tip_max_height = 0 - $filter_list = foreach ($_ in $filter_list) { - $symbol = foreach ($c in $_.symbol) { $PSCompletions.config."symbol_$($c)" } - $symbol = $symbol -join '' - $pad = if ($symbol) { "$($PSCompletions.config.menu_between_item_and_symbol)$($symbol)" }else { '' } - $name_with_symbol = "$($_.name[-1])$($pad)" - - $width = $this.get_length($name_with_symbol) - if ($width -gt $max_width) { $max_width = $width } - - if ($_.tip) { - $tip = $PSCompletions.replace_content($_.tip) - $tip_arr = $tip -split "`n" - } - else { - $tip = ' ' - $tip_arr = @() - } - if ($tip_arr.Count -gt $tip_max_height) { $tip_max_height = $tip_arr.Count } - @{ - name = $name_with_symbol - value = $_.name[-1] - width = $width - tip = $tip - } - } - $item_witdh = $max_width + 2 - $ui_max_width = 100 - $ui_width = if ($Host.UI.RawUI.BufferSize.Width -gt $ui_max_width) { $ui_max_width }else { $Host.UI.RawUI.BufferSize.Width } - $max_count = ($Host.UI.RawUI.BufferSize.Height - $tip_max_height) * ([math]::Floor($ui_width) / ($item_witdh)) - - $display_count = 0 - if ($max_count -lt 5 -or !$PSCompletions.config.menu_show_tip) { - $max_count = ($Host.UI.RawUI.BufferSize.Height) * ([math]::Floor($ui_width) / ($item_witdh)) - foreach ($_ in $filter_list) { - if ($max_count -gt $display_count -and $_.name) { - $display_count++ - [CompletionResult]::new($_.value, $_.name, 'ParameterValue', ' ') - } - } - } - else { - foreach ($_ in $filter_list) { - if ($max_count -gt $display_count -and $_.name) { - $display_count++ - [CompletionResult]::new($_.value, $_.name, 'ParameterValue', $_.tip) - } - } - } - - if ($filter_list -is [array] -and $display_count -lt $filter_list.Count) { - [CompletionResult]::new(' ', '...', 'ParameterValue', $PSCompletions.info.comp_hide) - $display_count++ - } - if ($display_count -eq 1 -and !$PSCompletions.config.enter_when_single) { ' ' } -} diff --git a/module/core/pwsh/Unix/utils.ps1 b/module/core/pwsh/Unix/utils.ps1 index e91c85e..e001d1e 100644 --- a/module/core/pwsh/Unix/utils.ps1 +++ b/module/core/pwsh/Unix/utils.ps1 @@ -1,6 +1,6 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod ConvertFrom_JsonToHashtable { param([string]$json) - return ConvertFrom-Json -AsHashtable $json + ConvertFrom-Json -AsHashtable $json } Add-Member -InputObject $PSCompletions -MemberType ScriptMethod handle_completion { @@ -400,4 +400,4 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod handle_completio $PSCompletions.menu.show_powershell_menu($filter_list) } } -} \ No newline at end of file +} diff --git a/module/core/pwsh/Win/menu.ps1 b/module/core/pwsh/Win/menu.ps1 index e5db268..7239afa 100644 --- a/module/core/pwsh/Win/menu.ps1 +++ b/module/core/pwsh/Win/menu.ps1 @@ -187,8 +187,6 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod get_old_buf $this.old_menu_pos = $old_top # 之前的位置 } Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod new_buffer { - param($offset = 0) - if ($PSCompletions.config.menu_list_cover_buffer) { $box = @() foreach ($_ in 0..$this.ui_size.Height) { @@ -207,13 +205,13 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod new_buffer $content_box = @() $line_top = [string]$PSCompletions.config.menu_line_top_left + $PSCompletions.config.menu_line_horizontal * ($this.list_max_width + $PSCompletions.config.menu_list_margin_left + $PSCompletions.config.menu_list_margin_right) + $PSCompletions.config.menu_line_top_right $border_box += $line_top - foreach ($_ in $offset..($this.ui_size.height - 3 + $offset)) { + foreach ($_ in 0..($this.ui_size.height - 3)) { $content = $this.filter_list[$_].name $border_box += [string]$PSCompletions.config.menu_line_vertical + (' ' * ($PSCompletions.config.menu_list_margin_left + $content.Length + $PSCompletions.config.menu_list_margin_right)) + [string]$PSCompletions.config.menu_line_vertical $content_box += (' ' * $PSCompletions.config.menu_list_margin_left) + $content + (' ' * $PSCompletions.config.menu_list_margin_right) } $status = "$(([string]($this.selected_index + 1)).PadLeft($this.filter_list.Count.ToString().Length, ' '))" - $line_bottom = [string]$PSCompletions.config.menu_line_bottom_left + $PSCompletions.config.menu_line_horizontal + ' ' * ($status.Length + 1) + $PSCompletions.config.menu_line_horizontal * ($this.list_max_width + $PSCompletions.config.menu_list_margin_left + $PSCompletions.config.menu_list_margin_right - $status.Length - 2) + $PSCompletions.config.menu_line_bottom_right + $line_bottom = [string]$PSCompletions.config.menu_line_bottom_left + $PSCompletions.config.menu_line_horizontal * 2 + ' ' * ($status.Length + 1) + $PSCompletions.config.menu_line_horizontal * ($this.list_max_width + $PSCompletions.config.menu_list_margin_left + $PSCompletions.config.menu_list_margin_right - $status.Length - 3) + $PSCompletions.config.menu_line_bottom_right $border_box += $line_bottom $buffer = $Host.UI.RawUI.NewBufferCellArray($border_box, $PSCompletions.config.menu_color_border_text, $PSCompletions.config.menu_color_border_back) @@ -284,7 +282,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod new_status_ $this.old_status_pos = $old_top # 之前的位置 $current = "$(([string]($this.selected_index + 1)).PadLeft($this.filter_list.Count.ToString().Length, ' '))" - $buffer_status = $Host.UI.RawUI.NewBufferCellArray(@("$($current) $($PSCompletions.config.menu_status_symbol) $($PSCompletions.menu.filter_list.Count) "), $PSCompletions.config.menu_color_status_text, $PSCompletions.config.menu_color_status_back) + $buffer_status = $Host.UI.RawUI.NewBufferCellArray(@("$($current)$($PSCompletions.config.menu_status_symbol)$($PSCompletions.menu.filter_list.Count)"), $PSCompletions.config.menu_color_status_text, $PSCompletions.config.menu_color_status_back) $pos_status = $Host.UI.RawUI.CursorPosition $pos_status.X = $this.pos.X + 3 @@ -571,7 +569,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_module $this.get_old_buffer() if ($this.is_show_tip) { $this.get_old_tip_buffer() } # 显示菜单 - $this.new_buffer($this.offset) + $this.new_buffer() if ($this.is_show_tip) { $this.new_tip_buffer($this.selected_index) } if ($PSCompletions.config.menu_is_prefix_match) { $this.get_prefix() } $this.new_filter_buffer($this.filter) @@ -671,7 +669,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_module $this.filter_completions($this.origin_filter_list) $this.handle_list() $this.parse_list() - $this.new_buffer($this.offset) + $this.new_buffer() if ($this.is_show_tip) { $this.new_tip_buffer($this.selected_index) } $this.new_filter_buffer() $this.new_status_buffer() @@ -688,7 +686,7 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_module else { $this.reset() $this.parse_list() - $this.new_buffer($this.offset) + $this.new_buffer() if ($this.is_show_tip) { $this.new_tip_buffer($this.selected_index) } $this.new_filter_buffer() $this.new_status_buffer() @@ -714,74 +712,3 @@ Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_module } [console]::OutputEncoding = $lastest_encoding } -Add-Member -InputObject $PSCompletions.menu -MemberType ScriptMethod show_powershell_menu { - param($filter_list) - if ($Host.UI.RawUI.BufferSize.Height -lt 5) { - [Microsoft.PowerShell.PSConsoleReadLine]::UndoAll() - [Microsoft.PowerShell.PSConsoleReadLine]::Insert($PSCompletions.info.min_area) - '' - return - } - - $json = $PSCompletions.data.$($PSCompletions.current_cmd) - $info = $json.info - - $max_width = 0 - $tip_max_height = 0 - $filter_list = foreach ($_ in $filter_list) { - $symbol = foreach ($c in $_.symbol) { - $PSCompletions.config."symbol_$($c)" - } - $symbol = $symbol -join '' - $pad = if ($symbol) { "$($PSCompletions.config.menu_between_item_and_symbol)$($symbol)" }else { '' } - $name_with_symbol = "$($_.name[-1])$($pad)" - - $width = $this.get_length($name_with_symbol) - if ($width -gt $max_width) { $max_width = $width } - - if ($_.tip) { - $tip = $PSCompletions.replace_content($_.tip) - $tip_arr = $tip -split "`n" - } - else { - $tip = ' ' - $tip_arr = @() - } - if ($tip_arr.Count -gt $tip_max_height) { $tip_max_height = $tip_arr.Count } - @{ - name = $name_with_symbol - value = $_.name[-1] - width = $width - tip = $tip - } - } - $item_witdh = $max_width + 2 - $ui_max_width = 100 - $ui_width = if ($Host.UI.RawUI.BufferSize.Width -gt $ui_max_width) { $ui_max_width }else { $Host.UI.RawUI.BufferSize.Width } - $max_count = ($Host.UI.RawUI.BufferSize.Height - $tip_max_height) * ([math]::Floor($ui_width) / ($item_witdh)) - - $display_count = 0 - if ($max_count -lt 5 -or !$PSCompletions.config.menu_show_tip) { - $max_count = ($Host.UI.RawUI.BufferSize.Height) * ([math]::Floor($ui_width) / ($item_witdh)) - foreach ($_ in $filter_list) { - if ($max_count -gt $display_count -and $_.name) { - $display_count++ - [CompletionResult]::new($_.value, $_.name, 'ParameterValue', ' ') - } - } - } - else { - foreach ($_ in $filter_list) { - if ($max_count -gt $display_count -and $_.name) { - $display_count++ - [CompletionResult]::new($_.value, $_.name, 'ParameterValue', $_.tip) - } - } - } - - if ($filter_list -is [array] -and $display_count -lt $filter_list.Count) { - [CompletionResult]::new(' ', '...', 'ParameterValue', $PSCompletions.info.comp_hide) - $display_count++ - } - if ($display_count -eq 1 -and !$PSCompletions.config.enter_when_single) { ' ' } -} diff --git a/module/core/pwsh/Win/utils.ps1 b/module/core/pwsh/Win/utils.ps1 index 0af85f2..967ec70 100644 --- a/module/core/pwsh/Win/utils.ps1 +++ b/module/core/pwsh/Win/utils.ps1 @@ -405,4 +405,4 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod handle_completio } } } -} \ No newline at end of file +} diff --git a/module/core/pwsh/config.ps1 b/module/core/pwsh/config.ps1 index bbd7c9f..804363d 100644 --- a/module/core/pwsh/config.ps1 +++ b/module/core/pwsh/config.ps1 @@ -1,4 +1,4 @@ -$PSCompletions.default.env = @{ +$PSCompletions.default.env = @{ # env language = $PSUICulture update = 1 @@ -66,7 +66,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod get_config { if (Test-Path $this.path.config) { $c = $this.ConvertFrom_JsonToHashtable($this.get_raw_content($this.path.config)) if ($c) { - foreach($_ in @('env', 'symbol', 'menu_line', 'menu_color', 'menu_config')){ + foreach ($_ in @('env', 'symbol', 'menu_line', 'menu_color', 'menu_config')) { foreach ($config in $this.default.$_.Keys) { if ($config -notin $c.keys) { $hasDiff = $true @@ -92,7 +92,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod get_config { } if ($need_init) { $c = @{} - foreach($_ in @('env', 'symbol', 'menu_line', 'menu_color', 'menu_config')){ + foreach ($_ in @('env', 'symbol', 'menu_line', 'menu_color', 'menu_config')) { foreach ($config in $this.default.$_.Keys) { $c.$config = $this.default.$_.$config } diff --git a/module/log.json b/module/log.json index e6860d4..16aef85 100644 --- a/module/log.json +++ b/module/log.json @@ -1,4 +1,22 @@ { + "4.1.0": { + "zh-CN": [ + "更新(2024/8/7)\n", + " - 现在 Windows PowerShell 也可以使用模块自带的补全菜单了\n", + " - 不过由于渲染问题,补全菜单的边框样式无法自定义\n", + " - 修复一些其他问题\n", + " - 调整源代码文件目录结构\n", + " - 整理代码\n" + ], + "en-US": [ + "Update(2024/8/7)\n", + " - Now Windows PowerShell can use the built-in completion menu.\n", + " - But, due to rendering problems, the border style of the completion menu cannot be customized.\n", + " - Fix some other issues.\n", + " - Change the directory structure of the source code file.\n", + " - Clean up the code.\n" + ] + }, "4.0.9": { "zh-CN": [ "修复(2024/7/20)\n", diff --git a/module/version.txt b/module/version.txt index 7919852..ee74734 100644 --- a/module/version.txt +++ b/module/version.txt @@ -1 +1 @@ -4.0.9 +4.1.0