stdio.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2018 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package terminal provides a set of interfaces that can be used to interact
  15. // with the terminal (including falling back when the terminal is detected to
  16. // be a redirect or other dumb terminal)
  17. package terminal
  18. import (
  19. "io"
  20. "os"
  21. )
  22. // StdioInterface represents a set of stdin/stdout/stderr Reader/Writers
  23. type StdioInterface interface {
  24. Stdin() io.Reader
  25. Stdout() io.Writer
  26. Stderr() io.Writer
  27. }
  28. // StdioImpl uses the OS stdin/stdout/stderr to implement StdioInterface
  29. type StdioImpl struct{}
  30. func (StdioImpl) Stdin() io.Reader { return os.Stdin }
  31. func (StdioImpl) Stdout() io.Writer { return os.Stdout }
  32. func (StdioImpl) Stderr() io.Writer { return os.Stderr }
  33. var _ StdioInterface = StdioImpl{}
  34. type customStdio struct {
  35. stdin io.Reader
  36. stdout io.Writer
  37. stderr io.Writer
  38. }
  39. func NewCustomStdio(stdin io.Reader, stdout, stderr io.Writer) StdioInterface {
  40. return customStdio{stdin, stdout, stderr}
  41. }
  42. func (c customStdio) Stdin() io.Reader { return c.stdin }
  43. func (c customStdio) Stdout() io.Writer { return c.stdout }
  44. func (c customStdio) Stderr() io.Writer { return c.stderr }
  45. var _ StdioInterface = customStdio{}