Skip to content

Commit

Permalink
Merge pull request #11 from mdgrs-mei/support-non-ascii-character-query
Browse files Browse the repository at this point in the history
Support non ascii character query
  • Loading branch information
mdgrs-mei authored Mar 2, 2024
2 parents 0c5b3f4 + 6c0f8b8 commit f3e2cdd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
57 changes: 21 additions & 36 deletions src/PowerShellRun/Application/SearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Diagnostics;
using System.Text;
using PowerShellRun.Dependency;

internal class SearchBar
{
Expand All @@ -20,21 +21,6 @@ internal class SearchBar
public bool IsCursorUpdated { get; set; } = false;
public string DebugPerfString = "";

public int CursorXInCanvas
{
get
{
return _cursorX + _textBox.X;
}
}
public int CursorYInCanvas
{
get
{
return _textBox.Y;
}
}

public SearchBar(string promptString)
{
var theme = SelectorOptionHolder.GetInstance().Option.Theme;
Expand Down Expand Up @@ -71,6 +57,25 @@ public void SetQuery(string query)
_isQuerySetFromOutside = true;
}

public (int X, int Y) GetCursorPositionInCanvas()
{
int x = _textBox.X;
int y = _textBox.Y;
for (int i = 0; i < _cursorX; ++i)
{
if (i >= _readKeysBuffer.Length)
break;

int displayWidth = Unicode.GetDisplayWidth(_readKeysBuffer[i]);
if (displayWidth <= 0)
continue;

x += displayWidth;
}

return (x, y);
}

public void Update()
{
ReadKeys();
Expand Down Expand Up @@ -102,14 +107,6 @@ public void Update()
}
}

private static Key[] _skipKeys =
{
Key.Enter,
Key.Escape,
Key.UpArrow,
Key.DownArrow,
Key.Tab,
};
private void ReadKeys()
{
IsQueryUpdated = false;
Expand All @@ -123,18 +120,6 @@ private void ReadKeys()
if (key.KeyCombination.Modifier.HasFlag(KeyModifier.Alt))
continue;

bool skip = false;
foreach (var skipKey in _skipKeys)
{
if (skipKey == key.KeyCombination.Key)
{
skip = true;
break;
}
}
if (skip)
continue;

if (key.KeyCombination.Key == Key.LeftArrow)
{
SetCursorX(_cursorX - 1);
Expand Down Expand Up @@ -183,7 +168,7 @@ private void ReadKeys()
continue;
}

if (!Char.IsAscii(key.ConsoleKeyInfo.KeyChar))
if (Unicode.GetDisplayWidth(key.ConsoleKeyInfo.KeyChar) <= 0)
continue;

if (_readKeysBuffer.Length >= Constants.QueryCharacterMaxCount)
Expand Down
3 changes: 2 additions & 1 deletion src/PowerShellRun/Application/Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public static SelectorResult Open(
break;
}

canvas.SetCursorOffset(searchBar.CursorXInCanvas, searchBar.CursorYInCanvas);
var cursorPosInCanvas = searchBar.GetCursorPositionInCanvas();
canvas.SetCursorOffset(cursorPosInCanvas.X, cursorPosInCanvas.Y);
if (searchBar.IsQueryUpdated || resultWindow.IsUpdated)
{
canvas.ClearCells();
Expand Down
6 changes: 6 additions & 0 deletions tests/Public/Invoke-PSRunSelector.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
$match | Should -Be 'abc'
}

It 'should support non-ascii character query' {
$context.Query = ''
$match = 'abc', 'aあ' | Invoke-PSRunSelector -Option $option -Context $context
$match | Should -Be 'aあ'
}

It 'should not throw an exception with multi selection' {
$match = 'a' | Invoke-PSRunSelector -Option $option -MultiSelection
$match | Should -Be 'a'
Expand Down

0 comments on commit f3e2cdd

Please sign in to comment.