完成一个简易的全局skill、command管理器
This commit is contained in:
39
manager/internal/prompt/prompt.go
Normal file
39
manager/internal/prompt/prompt.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package prompt
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ConfirmWithReader 询问用户确认(y/n),支持自定义输入源
|
||||
// 用于测试时注入 mock 输入
|
||||
func ConfirmWithReader(message string, reader io.Reader) bool {
|
||||
r := bufio.NewReader(reader)
|
||||
|
||||
for {
|
||||
fmt.Printf("%s [y/N]: ", message)
|
||||
response, err := r.ReadString('\n')
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
response = strings.TrimSpace(strings.ToLower(response))
|
||||
|
||||
if response == "y" || response == "yes" {
|
||||
return true
|
||||
} else if response == "n" || response == "no" || response == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Println("请输入 'y' 或 'n'")
|
||||
}
|
||||
}
|
||||
|
||||
// Confirm 询问用户确认(y/n)
|
||||
// 使用标准输入
|
||||
func Confirm(message string) bool {
|
||||
return ConfirmWithReader(message, os.Stdin)
|
||||
}
|
||||
30
manager/internal/prompt/prompt_test.go
Normal file
30
manager/internal/prompt/prompt_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package prompt
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfirmWithReader_Yes(t *testing.T) {
|
||||
tests := []string{"y", "Y", "yes", "YES", "Yes"}
|
||||
|
||||
for _, input := range tests {
|
||||
reader := strings.NewReader(input + "\n")
|
||||
result := ConfirmWithReader("测试?", reader)
|
||||
if !result {
|
||||
t.Errorf("输入 '%s' 应返回 true", input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmWithReader_No(t *testing.T) {
|
||||
tests := []string{"n", "N", "no", "NO", "No", "", "anything"}
|
||||
|
||||
for _, input := range tests {
|
||||
reader := strings.NewReader(input + "\n")
|
||||
result := ConfirmWithReader("测试?", reader)
|
||||
if result {
|
||||
t.Errorf("输入 '%s' 应返回 false", input)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user