| Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 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 |  | 
|  | 15 | // Package terminal provides a set of interfaces that can be used to interact | 
|  | 16 | // with the terminal (including falling back when the terminal is detected to | 
|  | 17 | // be a redirect or other dumb terminal) | 
|  | 18 | package terminal | 
|  | 19 |  | 
|  | 20 | import ( | 
|  | 21 | "io" | 
|  | 22 | "os" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | // StdioInterface represents a set of stdin/stdout/stderr Reader/Writers | 
|  | 26 | type StdioInterface interface { | 
|  | 27 | Stdin() io.Reader | 
|  | 28 | Stdout() io.Writer | 
|  | 29 | Stderr() io.Writer | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | // StdioImpl uses the OS stdin/stdout/stderr to implement StdioInterface | 
|  | 33 | type StdioImpl struct{} | 
|  | 34 |  | 
|  | 35 | func (StdioImpl) Stdin() io.Reader  { return os.Stdin } | 
|  | 36 | func (StdioImpl) Stdout() io.Writer { return os.Stdout } | 
|  | 37 | func (StdioImpl) Stderr() io.Writer { return os.Stderr } | 
|  | 38 |  | 
|  | 39 | var _ StdioInterface = StdioImpl{} | 
|  | 40 |  | 
|  | 41 | type customStdio struct { | 
|  | 42 | stdin  io.Reader | 
|  | 43 | stdout io.Writer | 
|  | 44 | stderr io.Writer | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | func NewCustomStdio(stdin io.Reader, stdout, stderr io.Writer) StdioInterface { | 
|  | 48 | return customStdio{stdin, stdout, stderr} | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | func (c customStdio) Stdin() io.Reader  { return c.stdin } | 
|  | 52 | func (c customStdio) Stdout() io.Writer { return c.stdout } | 
|  | 53 | func (c customStdio) Stderr() io.Writer { return c.stderr } | 
|  | 54 |  | 
|  | 55 | var _ StdioInterface = customStdio{} |