Skip to content

Commit

Permalink
Merge pull request #27 from abgox/module
Browse files Browse the repository at this point in the history
feat(module): update version to 4.3.1
  • Loading branch information
abgox authored Aug 18, 2024
2 parents 4feae6a + 6f52019 commit 8e1e277
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 101 deletions.
10 changes: 8 additions & 2 deletions module/CHANGELOG-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<a href="./CHANGELOG-CN.md">简体中文</a>
</p>

## 4.3.1 (2024/8/18)

- 添加一个配置项 `menu_is_loop`, 控制是否循环显示菜单,默认值为 `1`
- 禁用它: `psc menu config menu_is_loop 0`
- 优化旧版本的迁移逻辑

## 4.3.0 (2024/8/15)

- 修复在 `Windows PowerShell` 中的模块更新问题
Expand Down Expand Up @@ -83,15 +89,15 @@
- 设置: `psc menu config menu_trigger_key <key>`
2. `menu_enhance`: 默认值为 `1`, 用于设置是否启用补全菜单增强功能

- 设置: `psc menu config menu_enhance 1`
- 禁用它: `psc menu config menu_enhance 0`
- 开启后,`PSCompletions` 会拦截所有补全,并使用 `PSCompletions` 提供的补全菜单渲染补全
- 比如,`PowerShell` 中的 `Get-*`,`Set-*` 等命令都会使用 `PSCompletions` 提供的补全菜单渲染补全
- 需要注意,此配置项生效的前提是启用了 `menu_enable`
- [关于菜单增强](../README-CN.md#关于菜单增强)

3. `menu_show_tip_when_enhance`: 默认值为 `1`, 设置不是通过 `psc add` 添加的补全,是否显示命令提示信息

- 设置: `psc menu config menu_show_tip_when_enhance 1`
- 禁用它: `psc menu config menu_show_tip_when_enhance 0`
-`menu_enhance` 一起使用

- 解决了多字节文字可能导致菜单出现部分渲染错误的问题
Expand Down
25 changes: 21 additions & 4 deletions module/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"version": "4.3.1",
"info": {
"zh-CN": [
"更新(2024/8/18)\n",
"- 添加一个配置项 <@Magenta>menu_is_loop<@Blue>, 控制是否循环显示菜单,默认值为 <@Magenta>1<@Blue>\n",
" - 禁用它: <@Magenta>psc menu config menu_is_loop 0<@Blue>\n",
"- 优化旧版本的迁移逻辑\n"
],
"en-US": [
"Update(2024/8/18)\n",
"- Add a configuration item <@Magenta>menu_is_loop<@Blue>, controlling whether the menu is looped, with a default value of <@Magenta>1<@Blue>.\n",
" - Disable it: <@Magenta>psc menu config menu_is_loop 0<@Blue>\n",
"- Optimize the migration logic of old versions.\n"
]
}
},
{
"version": "4.3.0",
"info": {
Expand Down Expand Up @@ -195,15 +212,15 @@
" 1. <@Magenta>menu_trigger_key<@Blue>: 默认值为 <@Magenta>Tab<@Blue>, 用于设置补全菜单的触发按键\n",
" - 设置: <@Magenta>psc menu config menu_trigger_key <key><@Blue>\n",
" 2. <@Magenta>menu_enhance<@Blue>: 默认值为 <@Magenta>1<@Blue>, 用于设置是否启用补全菜单增强功能\n",
" - 设置: <@Magenta>psc menu config menu_enhance 1<@Blue>\n",
" - 禁用它: <@Magenta>psc menu config menu_enhance 0<@Blue>\n",
" - 开启后,<@Magenta>PSCompletions<@Blue> 会拦截所有补全,并使用 <@Magenta>PSCompletions<@Blue> 提供的补全菜单渲染补全\n",
" - 比如,<@Magenta>PowerShell<@Blue> 中的 <@Magenta>Get-*<@Blue>,<@Magenta>Set-*<@Blue> 等命令都会使用 <@Magenta>PSCompletions<@Blue> 提供的补全菜单渲染补全\n",
" - 需要注意,此配置项生效的前提是启用了 <@Magenta>menu_enable<@Blue>\n",
" - 关于菜单增强:\n",
" - Github: https://github.com/abgox/PSCompletions/blob/main/README-CN.md#关于菜单增强\n",
" - Gitee: https://gitee.com/abgox/PSCompletions/blob/main/README-CN.md#关于菜单增强\n",
" 3. <@Magenta>menu_show_tip_when_enhance<@Blue>: 默认值为 <@Magenta>1<@Blue>, 设置不是通过 <@Magenta>psc add<@Blue> 添加的补全,是否显示命令提示信息\n",
" - 设置: <@Magenta>psc menu config menu_show_tip_when_enhance 1<@Blue>\n",
" - 禁用它: <@Magenta>psc menu config menu_show_tip_when_enhance 0<@Blue>\n",
" - 和 <@Magenta>menu_enhance<@Blue> 一起使用\n",
" - 解决了多字节文字可能导致菜单出现部分渲染错误的问题\n",
" - 这配合 <@Magenta>menu_enhance<@Blue> 很有用\n",
Expand All @@ -221,15 +238,15 @@
" 1. <@Magenta>menu_trigger_key<@Blue>: Default value is <@Magenta>Tab<@Blue>, which is used to set the trigger key of the completion menu.\n",
" - Setting: <@Magenta>psc menu config menu_trigger_key <key><@Blue>\n",
" 2. <@Magenta>menu_enhance<@Blue>: Default value is <@Magenta>1<@Blue>, which is used to to enable or disable the enhanced completion menu feature.\n",
" - Setting: <@Magenta>psc menu config menu_enhance 1<@Blue>\n",
" - Disable it: <@Magenta>psc menu config menu_enhance 0<@Blue>\n",
" - When enabled, <@Magenta>PSCompletions<@Blue> will intercept all completions and uses the completion menu provided by <@Magenta>PSCompletions<@Blue> to render completions.\n",
" - For example, commands such as <@Magenta>Get-*<@Blue>, <@Magenta>Set-*<@Blue> in <@Magenta>PowerShell<@Blue> will use the completion menu provided by <@Magenta>PSCompletions<@Blue> to render the completion.\n",
" - Note: This setting only takes effect if <@Magenta>menu_enable<@Blue> is also enabled.\n",
" - About menu enhance:\n",
" - Github: https://github.com/abgox/PSCompletions/blob/main/README.md#about-menu-enhance\n",
" - Gitee: https://gitee.com/abgox/PSCompletions/blob/main/README.md#about-menu-enhance\n",
" 3. <@Magenta>menu_show_tip_when_enhance<@Blue>: Default value is <@Magenta>1<@Blue>, which is used to control whether to show command tips for completions that are not added through <@Magenta>psc add<@Blue>.\n",
" - Setting: <@Magenta>psc menu config menu_show_tip_when_enhance 1<@Blue>\n",
" - Disable it: <@Magenta>psc menu config menu_show_tip_when_enhance 0<@Blue>\n",
" - Used together with <@Magenta>menu_enhance<@Blue>.\n",
" - Fix an issue where multi-byte characters(such as Chinese characters) could cause partial rendering errors in the menu.\n",
" - This is useful with <@Magenta>menu_enhance<@Blue>.\n",
Expand Down
10 changes: 8 additions & 2 deletions module/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<a href="./CHANGELOG.md">English</a>
</p>

## 4.3.1 (2024/8/18)

- Add a configuration item `menu_is_loop`, controlling whether the menu is looped, with a default value of `1`.
- Disable it: `psc menu config menu_is_loop 0`
- Optimize the migration logic of old versions.

## 4.3.0 (2024/8/15)

- Fix module update issue in `Windows PowerShell`.
Expand Down Expand Up @@ -80,13 +86,13 @@
1. `menu_trigger_key`: Default value is `Tab`, which is used to set the trigger key of the completion menu.
- Setting: `psc menu config menu_trigger_key <key>`
2. `menu_enhance`: Default value is `1`, which is used to enable or disable the enhanced completion menu feature.
- Setting: `psc menu config menu_enhance 1`
- Disable it: `psc menu config menu_enhance 0`
- When enabled, `PSCompletions` will intercept all completions and uses the completion menu provided by `PSCompletions` to render completions.
- For example, commands such as `Get-*`, `Set-*` in `PowerShell` will use the completion menu provided by `PSCompletions` to render the completion.
- Note: This setting only takes effect if `menu_enable` is also enabled.
- [About menu enhance](../README.md#about-menu-enhance)
3. `menu_show_tip_when_enhance`: Default value is `1`, which is used to control whether to show command tips for completions that are not added through `psc add`.
- Setting: `psc menu config menu_show_tip_when_enhance 1`
- Disable it: `psc menu config menu_show_tip_when_enhance 0`
- Use together with `menu_enhance`.

- Fix an issue where multi-byte characters(such as Chinese characters) could cause partial rendering errors in the menu.
Expand Down
2 changes: 1 addition & 1 deletion module/PSCompletions.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

RootModule = 'PSCompletions.psm1'

ModuleVersion = '4.3.0'
ModuleVersion = '4.3.1'

GUID = '00929632-527d-4dab-a5b3-21197faccd05'

Expand Down
4 changes: 2 additions & 2 deletions module/PSCompletions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# ? 由于此处使用 $PSCompletions.replace_content 会导致其无法使用外部变量,所以重新定义一个函数以供使用
function _replace {
param ($data, $separator = '')
$data = ($data -join $separator)
$data = $data -join $separator
$pattern = '\{\{(.*?(\})*)(?=\}\})\}\}'
$matches = [regex]::Matches($data, $pattern)
foreach ($match in $matches) {
Expand Down Expand Up @@ -692,7 +692,7 @@
return
}
}
{ $_ -in @('menu_enable', 'menu_show_tip', 'menu_list_follow_cursor', 'menu_tip_follow_cursor', 'menu_list_cover_buffer', 'menu_tip_cover_buffer', 'menu_is_prefix_match', 'menu_selection_with_margin', 'enter_when_single', 'menu_completions_sort', 'menu_enhance', 'menu_show_tip_when_enhance') } {
{ $_ -in @('menu_enable', 'menu_show_tip', 'menu_list_follow_cursor', 'menu_tip_follow_cursor', 'menu_list_cover_buffer', 'menu_tip_cover_buffer', 'menu_is_prefix_match', 'menu_is_loop', 'menu_selection_with_margin', 'enter_when_single', 'menu_completions_sort', 'menu_enhance', 'menu_show_tip_when_enhance') } {
if (!$is_num -or $arg[3] -notin @(1, 0)) {
$cmd_list = $null
$sub_cmd = $arg[3]
Expand Down
49 changes: 21 additions & 28 deletions module/core/completion/utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

$common_options = @()
$common_options_with_next = @()
if ($PSCompletions.data.$root.common_options) {
foreach ($_ in $PSCompletions.data.$root.common_options) {
foreach ($a in $_.alias) {
$common_options += $a
if ($_.next) { $common_options_with_next += $a }
}
$common_options += $_.name
if ($_.next) { $common_options_with_next += $_.name }
foreach ($_ in $PSCompletions.data.$root.common_options) {
foreach ($a in $_.alias) {
$common_options += $a
if ($_.next) { $common_options_with_next += $a }
}
$common_options += $_.name
if ($_.next) { $common_options_with_next += $_.name }
}
$PSCompletions.temp_WriteSpaceTab = @()
$PSCompletions.temp_WriteSpaceTab += $common_options_with_next
Expand All @@ -42,19 +40,16 @@
$runspacePool.Open()
$runspaces = @()

$tasks = @()
if ($PSCompletions.data.$root.root) {
$tasks += @{
$tasks = @(
@{
node = $PSCompletions.data.$root.root
isOption = $false
}
}
if ($PSCompletions.data.$root.options) {
$tasks += @{
},
@{
node = $PSCompletions.data.$root.options
isOption = $true
}
}
)
foreach ($task in $tasks) {
$runspace = [powershell]::Create().AddScript({
param($obj, $PSCompletions)
Expand All @@ -66,7 +61,7 @@
}
function _replace {
param ($data, $separator = '')
$data = ($data -join $separator)
$data = $data -join $separator
$pattern = '\{\{(.*?(\})*)(?=\}\})\}\}'
$matches = [regex]::Matches($data, $pattern)
foreach ($match in $matches) {
Expand Down Expand Up @@ -135,8 +130,8 @@
# 判断别名出现的位置
$index = (($pre + $pad + $_.name) -split ' ').Length - 1
# 用这个位置创建一个数组,将所有在这个位置出现的别名全部写入这个数组
if (!($_alias_map[$index])) { $_alias_map[$index] = @() }
$_alias_map[$index] += @{
if (!$_alias_map.$index) { $_alias_map.$index = @() }
$_alias_map.$index += @{
name = $_.name
alias = $a
}
Expand Down Expand Up @@ -172,11 +167,11 @@
$PSCompletions.temp_WriteSpaceTab += $result.temp.WriteSpaceTab
$PSCompletions.temp_WriteSpaceTab_and_SpaceTab += $result.temp.WriteSpaceTab_and_SpaceTab
foreach ($a in $result.alias_map.Keys) {
if ($alias_map[$a]) {
$alias_map[$a] += $result.alias_map[$a]
if ($alias_map.$a) {
$alias_map.$a += $result.alias_map.$a
}
else {
$alias_map[$a] = $result.alias_map[$a]
$alias_map.$a = $result.alias_map.$a
}
}
}
Expand Down Expand Up @@ -232,12 +227,10 @@
# 循环命令的长度,针对每一个位置去 $alias_map 找到对应的数组,然后把数组里的值拿出来比对,如果有匹配的,替换掉原来的命令名
# 用位置的好处是,这样遍历是依赖于命令的长度,而命令长度一般不长
for ($i = 0; $i -lt $filter_input_arr.Count; $i++) {
if ($alias_map[$i]) {
foreach ($obj in $alias_map[$i]) {
if ($obj.alias -eq $filter_input_arr[$i]) {
$alias_input_arr[$i] = $obj.name
break
}
foreach ($obj in $alias_map.$i) {
if ($obj.alias -eq $filter_input_arr[$i]) {
$alias_input_arr[$i] = $obj.name
break
}
}
}
Expand Down
1 change: 1 addition & 0 deletions module/core/config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ $PSCompletions.default.menu_config = @{
menu_list_margin_right = 0
menu_list_min_width = 10
menu_is_prefix_match = 0
menu_is_loop = 1
menu_above_margin_bottom = 0
menu_above_list_max_count = -1
menu_below_list_max_count = -1
Expand Down
54 changes: 41 additions & 13 deletions module/core/init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
New-Variable -Name PSCompletions -Value @{} -Option ReadOnly

# 模块版本
$PSCompletions.version = '4.3.0'
$PSCompletions.version = '4.3.1'
$PSCompletions.path = @{}
$PSCompletions.path.root = Split-Path $PSScriptRoot -Parent
$PSCompletions.path.completions = Join-Path $PSCompletions.path.root 'completions'
Expand Down Expand Up @@ -45,6 +45,7 @@ $PSCompletions.menu = @{
"menu_list_follow_cursor",
"menu_tip_follow_cursor",
"menu_is_prefix_match",
"menu_is_loop",
"enter_when_single",
"menu_selection_with_margin",
"menu_completions_sort"
Expand Down Expand Up @@ -92,14 +93,27 @@ if (!(Test-Path $PSCompletions.path.config) -and !(Test-Path $PSCompletions.path
if ($old_version -match "^\d+\.\d.*" -and $old_version -ge "4") {
$old_version_dir = Join-Path (Split-Path $this.path.root -Parent) $old_version
if (!(Test-Path $this.path.completions)) { New-Item -ItemType Directory $this.path.completions > $null }
foreach ($_ in Get-ChildItem "$($old_version_dir)/completions" -ErrorAction SilentlyContinue) {
Move-Item $_.FullName $this.path.completions -Force -ErrorAction SilentlyContinue
$PSCompletions.old_psc = @{}
foreach ($_ in Get-ChildItem "$($old_version_dir)/completions" -Directory -ErrorAction SilentlyContinue) {
if ($_.Name -eq "psc") {
$PSCompletions.old_psc.path = "$($old_version_dir)/completions/psc"
$PSCompletions.old_psc.alias = (Get-Content "$($old_version_dir)/completions/psc/alias.txt" -ErrorAction SilentlyContinue).Where({ $_ -ne '' })
$old_psc_guid = Get-Content "$($old_version_dir)/completions/psc/guid.txt" -Raw -ErrorAction SilentlyContinue
if ($old_psc_guid) { $PSCompletions.old_psc.guid = $old_psc_guid.Trim() }
}
else {
Move-Item $_.FullName $this.path.completions -Force -ErrorAction SilentlyContinue
}
}
Move-Item "$($old_version_dir)/config.json" $this.path.config -Force -ErrorAction SilentlyContinue
Move-Item "$($old_version_dir)/completions.json" $this.path.completions_json -Force -ErrorAction SilentlyContinue
}
}
}
$PSCompletions.move_old_version()
if (!(Test-Path $PSCompletions.path.config) -and !(Test-Path $PSCompletions.path.completions)) {
$PSCompletions.is_first_init = $true
}
}

. $PSScriptRoot\config.ps1
Expand Down Expand Up @@ -417,11 +431,12 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod download_file {
Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion {
param (
[string]$completion,
[bool]$log = $true
[bool]$log = $true,
[bool]$is_update = $true
)
$url = "$($this.url)/completions/$($completion)"

$is_update = Test-Path "$($this.path.completions)/$($completion)"
$is_update = (Test-Path "$($this.path.completions)/$($completion)") -and $is_update

$completion_dir = Join-Path $this.path.completions $completion

Expand Down Expand Up @@ -574,15 +589,28 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod init_data {
$this.info = ($this.get_raw_content($path_language) | ConvertFrom-Json).info
}
else {
$this.ensure_dir("$($this.path.completions)/psc")
$this.ensure_dir("$($this.path.completions)/psc/language")
$language = $this.get_language('psc')
$path_language = "$($this.path.completions)/psc/language/$($language).json"
if ($PSCompletions.old_psc.guid) {
$psc_guid = (Invoke-WebRequest "$($this.url)/completions/psc/guid.txt").Content.Trim()
}
if ($psc_guid -and $PSCompletions.old_psc.guid -eq $psc_guid) {
Move-Item $PSCompletions.old_psc.path $PSCompletions.path.completions -Force
$language = $this.get_language('psc')
$path_language = "$($this.path.completions)/psc/language/$($language).json"
$this.info = ($this.get_raw_content($path_language) | ConvertFrom-Json).info
}
else {
$this.ensure_dir("$($this.path.completions)/psc")
$this.ensure_dir("$($this.path.completions)/psc/language")
$language = $this.get_language('psc')
$path_language = "$($this.path.completions)/psc/language/$($language).json"

$this.download_file("$($this.url)/completions/psc/language/$($language).json", $path_language)
$this.info = ($this.get_raw_content($path_language) | ConvertFrom-Json).info
$this.add_completion('psc')
$this.is_first_init = $true
$this.download_file("$($this.url)/completions/psc/language/$($language).json", $path_language)
$this.info = ($this.get_raw_content($path_language) | ConvertFrom-Json).info
$this.add_completion('psc', $true, $false)
if ($PSCompletions.old_psc.alias) {
$PSCompletions.old_psc.alias | Out-File "$($PSCompletions.path.completions)/psc/alias.txt" -encoding utf8 -Force
}
}
}

if (!(Test-Path $PSCompletions.path.completions_json) -and !($this.download_list())) {
Expand Down
Loading

0 comments on commit 8e1e277

Please sign in to comment.