console.go 487 B

12345678910111213141516171819202122232425262728293031
  1. package console
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. type Console struct {
  9. Handler func(string) string
  10. }
  11. func NewConsole(handler func(string) string) *Console {
  12. return &Console{
  13. Handler: handler,
  14. }
  15. }
  16. func (c *Console) ListenAndHandle() {
  17. for {
  18. reader := bufio.NewReader(os.Stdin)
  19. text, _ := reader.ReadString('\n')
  20. text = strings.TrimSpace(text)
  21. if len(text) > 0 {
  22. fmt.Println("▌" + c.Handler(text))
  23. } else {
  24. fmt.Println("▌Invalid Command")
  25. }
  26. }
  27. }