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) 18package terminal 19 20import ( 21 "io" 22 "os" 23) 24 25// StdioInterface represents a set of stdin/stdout/stderr Reader/Writers 26type 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 33type StdioImpl struct{} 34 35func (StdioImpl) Stdin() io.Reader { return os.Stdin } 36func (StdioImpl) Stdout() io.Writer { return os.Stdout } 37func (StdioImpl) Stderr() io.Writer { return os.Stderr } 38 39var _ StdioInterface = StdioImpl{} 40 41type customStdio struct { 42 stdin io.Reader 43 stdout io.Writer 44 stderr io.Writer 45} 46 47func NewCustomStdio(stdin io.Reader, stdout, stderr io.Writer) StdioInterface { 48 return customStdio{stdin, stdout, stderr} 49} 50 51func (c customStdio) Stdin() io.Reader { return c.stdin } 52func (c customStdio) Stdout() io.Writer { return c.stdout } 53func (c customStdio) Stderr() io.Writer { return c.stderr } 54 55var _ StdioInterface = customStdio{} 56