Dan Willemsen | b82471a | 2018-05-17 16:37:09 -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 | "fmt" |
| 22 | "io" |
| 23 | "os" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // Writer provides an interface to write temporary and permanent messages to |
| 27 | // the terminal. |
| 28 | // |
| 29 | // The terminal is considered to be a dumb terminal if TERM==dumb, or if a |
| 30 | // terminal isn't detected on stdout/stderr (generally because it's a pipe or |
| 31 | // file). Dumb terminals will strip out all ANSI escape sequences, including |
| 32 | // colors. |
| 33 | type Writer interface { |
| 34 | // Print prints the string to the terminal, overwriting any current |
| 35 | // status being displayed. |
| 36 | // |
| 37 | // On a dumb terminal, the status messages will be kept. |
| 38 | Print(str string) |
| 39 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 40 | // Finish ensures that the output ends with a newline (preserving any |
| 41 | // current status line that is current displayed). |
| 42 | // |
| 43 | // This does nothing on dumb terminals. |
| 44 | Finish() |
| 45 | |
| 46 | // Write implements the io.Writer interface. This is primarily so that |
| 47 | // the logger can use this interface to print to stderr without |
| 48 | // breaking the other semantics of this interface. |
| 49 | // |
| 50 | // Try to use any of the other functions if possible. |
| 51 | Write(p []byte) (n int, err error) |
| 52 | |
| 53 | isSmartTerminal() bool |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame^] | 54 | termWidth() (int, bool) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // NewWriter creates a new Writer based on the stdio and the TERM |
| 58 | // environment variable. |
| 59 | func NewWriter(stdio StdioInterface) Writer { |
| 60 | w := &writerImpl{ |
| 61 | stdio: stdio, |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 64 | return w |
| 65 | } |
| 66 | |
| 67 | type writerImpl struct { |
| 68 | stdio StdioInterface |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func (w *writerImpl) Print(str string) { |
Dan Willemsen | f78a734 | 2018-07-12 21:26:10 -0700 | [diff] [blame] | 72 | fmt.Fprint(w.stdio.Stdout(), str) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 73 | if len(str) == 0 || str[len(str)-1] != '\n' { |
Dan Willemsen | f78a734 | 2018-07-12 21:26:10 -0700 | [diff] [blame] | 74 | fmt.Fprint(w.stdio.Stdout(), "\n") |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame^] | 78 | func (w *writerImpl) Finish() {} |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 79 | |
| 80 | func (w *writerImpl) Write(p []byte) (n int, err error) { |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame^] | 81 | return w.stdio.Stdout().Write(p) |
| 82 | } |
| 83 | |
| 84 | func (w *writerImpl) isSmartTerminal() bool { |
| 85 | return isSmartTerminal(w.stdio.Stdout()) |
| 86 | } |
| 87 | |
| 88 | func (w *writerImpl) termWidth() (int, bool) { |
| 89 | return termWidth(w.stdio.Stdout()) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // StdioInterface represents a set of stdin/stdout/stderr Reader/Writers |
| 93 | type StdioInterface interface { |
| 94 | Stdin() io.Reader |
| 95 | Stdout() io.Writer |
| 96 | Stderr() io.Writer |
| 97 | } |
| 98 | |
| 99 | // StdioImpl uses the OS stdin/stdout/stderr to implement StdioInterface |
| 100 | type StdioImpl struct{} |
| 101 | |
| 102 | func (StdioImpl) Stdin() io.Reader { return os.Stdin } |
| 103 | func (StdioImpl) Stdout() io.Writer { return os.Stdout } |
| 104 | func (StdioImpl) Stderr() io.Writer { return os.Stderr } |
| 105 | |
| 106 | var _ StdioInterface = StdioImpl{} |
| 107 | |
| 108 | type customStdio struct { |
| 109 | stdin io.Reader |
| 110 | stdout io.Writer |
| 111 | stderr io.Writer |
| 112 | } |
| 113 | |
| 114 | func NewCustomStdio(stdin io.Reader, stdout, stderr io.Writer) StdioInterface { |
| 115 | return customStdio{stdin, stdout, stderr} |
| 116 | } |
| 117 | |
| 118 | func (c customStdio) Stdin() io.Reader { return c.stdin } |
| 119 | func (c customStdio) Stdout() io.Writer { return c.stdout } |
| 120 | func (c customStdio) Stderr() io.Writer { return c.stderr } |
| 121 | |
| 122 | var _ StdioInterface = customStdio{} |