Skip to content

Commit 664b168

Browse files
SteveL-MSFTdaxian-dbw
authored andcommitted
Fix correct way to get substring based on buffercells when encountering CJK chars (#1100)
1 parent ceb1ca5 commit 664b168

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

PSReadLine/Completion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ private static string GetMenuItem(string item, int columnWidth)
805805
}
806806
else if (spacesNeeded < 0)
807807
{
808-
item = item.Substring(0, columnWidth - 3) + "...";
808+
item = SubstringByCells(item, columnWidth - 3) + "...";
809809
}
810810

811811
return item;

PSReadLine/Render.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private bool RenderErrorPrompt(RenderData renderData, string defaultColor)
371371

372372
if (string.IsNullOrEmpty(promptText) || _initialY < 0)
373373
{
374-
// No need to flip the prompt color if either the error prompt is not defined
374+
// No need to flip the prompt color if either the error prompt is not defined
375375
// or the initial cursor point has already been scrolled off the buffer.
376376
return false;
377377
}
@@ -875,6 +875,25 @@ private static int LengthInBufferCells(char c)
875875
return 1 + (isWide ? 1 : 0);
876876
}
877877

878+
private static string SubstringByCells(string text, int countOfCells)
879+
{
880+
int length = 0;
881+
int index = 0;
882+
883+
foreach (char c in text)
884+
{
885+
if (length >= countOfCells)
886+
{
887+
return text.Substring(0, index);
888+
}
889+
890+
length += LengthInBufferCells(c);
891+
index++;
892+
}
893+
894+
return string.Empty;
895+
}
896+
878897
private string GetTokenColor(Token token)
879898
{
880899
if ((token.TokenFlags & TokenFlags.CommandName) != 0)

0 commit comments

Comments
 (0)