Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions command/hook_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,10 @@ func dropCR(data []byte) []byte {
}

func truncateId(id string, maxLen int) string {
totalLength := len(id)
// Note that the id may contain multibyte characters.
// We need to truncate it to maxLen characters, not maxLen bytes.
rid := []rune(id)
totalLength := len(rid)
if totalLength <= maxLen {
return id
}
Expand All @@ -383,11 +386,11 @@ func truncateId(id string, maxLen int) string {
maxLen = 5
}

dots := "..."
dots := []rune("...")
partLen := maxLen / 2

leftIdx := partLen - 1
leftPart := id[0:leftIdx]
leftPart := rid[0:leftIdx]

rightIdx := totalLength - partLen - 1

Expand All @@ -396,7 +399,7 @@ func truncateId(id string, maxLen int) string {
rightIdx -= overlap
}

rightPart := id[rightIdx:]
rightPart := rid[rightIdx:]

return leftPart + dots + rightPart
return string(leftPart) + string(dots) + string(rightPart)
}
45 changes: 45 additions & 0 deletions command/hook_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,51 @@ func TestTruncateId(t *testing.T) {
Expected: "Hello world",
MaxLen: 12,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あ...さ",
MaxLen: 3,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あ...さ",
MaxLen: 5,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あい...さ",
MaxLen: 6,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あい...こさ",
MaxLen: 7,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あいう...こさ",
MaxLen: 8,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あいう...けこさ",
MaxLen: 9,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あいうえ...けこさ",
MaxLen: 10,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あいうえおかきくけこさ",
MaxLen: 11,
},
{
Input: "あいうえおかきくけこさ",
Expected: "あいうえおかきくけこさ",
MaxLen: 12,
},
}
for i, tc := range testCases {
testName := fmt.Sprintf("%d", i)
Expand Down