From d0fcde8c38659b2bb22a69c07659a57370cd99e8 Mon Sep 17 00:00:00 2001 From: Aiello Date: Wed, 24 Feb 2021 01:07:40 +0800 Subject: [PATCH] Use rune when delete or filter answer (#327) * fix use rune when delete input answer * fix use rune when filter with input * test(input): Add test for delete answer * test(select): Add delete select filter word test * test(multiselect): Add delete filter word test --- input.go | 3 ++- input_test.go | 28 +++++++++++++++++++++++++++ multiselect.go | 3 ++- multiselect_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ select.go | 3 ++- select_test.go | 36 ++++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 3 deletions(-) diff --git a/input.go b/input.go index a544b045..2e9ad3ce 100644 --- a/input.go +++ b/input.go @@ -105,7 +105,8 @@ func (i *Input) OnChange(key rune, config *PromptConfig) (bool, error) { } } else if key == terminal.KeyDelete || key == terminal.KeyBackspace { if i.answer != "" { - i.answer = i.answer[0 : len(i.answer)-1] + runeAnswer := []rune(i.answer) + i.answer = string(runeAnswer[0 : len(runeAnswer)-1]) } } else if key >= terminal.KeySpace { i.answer += string(key) diff --git a/input_test.go b/input_test.go index 52dce046..527af5c7 100644 --- a/input_test.go +++ b/input_test.go @@ -209,6 +209,34 @@ func TestInputPrompt(t *testing.T) { }, "R", }, + { + "Test Input prompt interaction when delete", + &Input{ + Message: "What is your name?", + }, + func(c *expect.Console) { + c.ExpectString("What is your name?") + c.Send("Johnny ") + c.Send(string(terminal.KeyDelete)) + c.SendLine("") + c.ExpectEOF() + }, + "Johnny", + }, + { + "Test Input prompt interaction when delete rune", + &Input{ + Message: "What is your name?", + }, + func(c *expect.Console) { + c.ExpectString("What is your name?") + c.Send("小明") + c.Send(string(terminal.KeyDelete)) + c.SendLine("") + c.ExpectEOF() + }, + "小", + }, { "Test Input prompt interaction when ask for suggestion with empty value", &Input{ diff --git a/multiselect.go b/multiselect.go index 628abd72..3d4f95b9 100644 --- a/multiselect.go +++ b/multiselect.go @@ -113,7 +113,8 @@ func (m *MultiSelect) OnChange(key rune, config *PromptConfig) { m.filter = "" } else if key == terminal.KeyDelete || key == terminal.KeyBackspace { if m.filter != "" { - m.filter = m.filter[0 : len(m.filter)-1] + runeFilter := []rune(m.filter) + m.filter = string(runeFilter[0 : len(runeFilter)-1]) } } else if key >= terminal.KeySpace { m.filter += string(key) diff --git a/multiselect_test.go b/multiselect_test.go index c2fd403b..6f284eee 100644 --- a/multiselect_test.go +++ b/multiselect_test.go @@ -488,6 +488,53 @@ func TestMultiSelectPrompt(t *testing.T) { core.OptionAnswer{Value: "Saturday", Index: 6}, }, }, + { + "delete filter word", + &MultiSelect{ + Message: "What days do you prefer:", + Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}, + }, + func(c *expect.Console) { + c.ExpectString("What days do you prefer: [Use arrows to move, space to select, to all, to none, type to filter]") + // Filter down to 'Sunday' + c.Send("su") + // Delete 'u' + c.Send(string(terminal.KeyDelete)) + // Filter down to 'Saturday' + c.Send("at") + // Select 'Saturday' + c.Send(string(terminal.KeyArrowDown)) + c.Send(" ") + c.SendLine("") + c.ExpectEOF() + }, + []core.OptionAnswer{ + core.OptionAnswer{Value: "Saturday", Index: 6}, + }, + }, + { + "delete filter word in rune", + &MultiSelect{ + Message: "今天中午吃什么?", + Options: []string{"青椒牛肉丝", "小炒肉", "小煎鸡"}, + }, + func(c *expect.Console) { + c.ExpectString("今天中午吃什么? [Use arrows to move, space to select, to all, to none, type to filter]") + // Filter down to 小炒肉. + c.Send("小炒") + // Filter down to 小炒肉 and 小煎鸡. + c.Send(string(terminal.KeyDelete)) + // Filter down to 小煎鸡. + c.Send("煎") + // Select 小煎鸡. + c.Send(" ") + c.SendLine("") + c.ExpectEOF() + }, + []core.OptionAnswer{ + core.OptionAnswer{Value: "小煎鸡", Index: 2}, + }, + }, } for _, test := range tests { diff --git a/select.go b/select.go index bc564aa4..fdce0381 100644 --- a/select.go +++ b/select.go @@ -114,8 +114,9 @@ func (s *Select) OnChange(key rune, config *PromptConfig) bool { } else if key == terminal.KeyDelete || key == terminal.KeyBackspace { // if there is content in the filter to delete if s.filter != "" { + runeFilter := []rune(s.filter) // subtract a line from the current filter - s.filter = s.filter[0 : len(s.filter)-1] + s.filter = string(runeFilter[0 : len(runeFilter)-1]) // we removed the last value in the filter } } else if key >= terminal.KeySpace { diff --git a/select_test.go b/select_test.go index c960e314..4596fb7e 100644 --- a/select_test.go +++ b/select_test.go @@ -327,6 +327,42 @@ func TestSelectPrompt(t *testing.T) { }, core.OptionAnswer{Index: 0, Value: "red"}, }, + { + "delete filter word", + &Select{ + Message: "Choose a color:", + Options: []string{"red", "blue", "black"}, + }, + func(c *expect.Console) { + c.ExpectString("Choose a color:") + // Filter down to blue. + c.Send("blu") + // Filter down to blue and black. + c.Send(string(terminal.KeyDelete)) + // Select black. + c.SendLine(string(terminal.KeyArrowDown)) + c.ExpectEOF() + }, + core.OptionAnswer{Index: 2, Value: "black"}, + }, + { + "delete filter word in rune", + &Select{ + Message: "今天中午吃什么?", + Options: []string{"青椒牛肉丝", "小炒肉", "小煎鸡"}, + }, + func(c *expect.Console) { + c.ExpectString("今天中午吃什么?") + // Filter down to 小炒肉. + c.Send("小炒") + // Filter down to 小炒肉 and 小煎鸡. + c.Send(string(terminal.KeyDelete)) + // Select 小煎鸡. + c.SendLine(string(terminal.KeyArrowDown)) + c.ExpectEOF() + }, + core.OptionAnswer{Index: 2, Value: "小煎鸡"}, + }, } for _, test := range tests {